[POJ3067]Japan

题目链接:http://poj.org/problem?id=3067

线段树和树状数组都可以做。

线段树:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <stack>
10 #include <list>
11 #include <vector>
12
13 using namespace std;
14
15 const int maxn = 3000010;
16
17 typedef long long LL;
18 typedef struct Node {
19     int x;
20     int y;
21 };
22
23 Node node[maxn];
24 int sum[maxn];
25 int num[maxn];
26
27 inline bool cmp(Node a, Node b) {
28     if(a.y != b.y) {
29         return a.y > b.y;
30     }
31     return a.x < b.x;
32 }
33
34 void update(int left, int right, int rt, int x) {
35     if(left == right) {
36         sum[rt]++;
37         num[left]++;
38         return ;
39     }
40     int mid = (left + right) >> 1;
41     if(x <= mid) {
42         update(left, mid, rt<<1, x);
43     }
44     else {
45         update(mid+1, right, rt<<1|1, x);
46     }
47     sum[rt] = sum[rt<<1] + sum[rt<<1|1];
48 }
49
50 int query(int left, int right, int L, int R, int rt) {
51     if(L <= left && R >= right) {
52         return sum[rt];
53     }
54     int mid = (left + right) >> 1;
55     int ans = 0;
56     if(L <= mid) {
57         ans += query(left, mid, L, R, rt<<1);
58     }
59     if(R > mid) {
60         ans += query(mid+1, right, L, R, rt<<1|1);
61     }
62     return ans;
63 }
64
65 int n, m, k;
66 int res;
67 LL omega;
68
69 int main() {
70     // freopen("in", "r", stdin);
71     int kase = 1;
72     int T;
73     scanf("%d", &T);
74     while(T--) {
75         memset(sum, 0, sizeof(sum));
76         memset(num, 0, sizeof(num));
77         res = 0;
78         omega = 0;
79         scanf("%d %d %d", &n, &m, &k);
80         for(int i = 1; i <= k; i++) {
81             scanf("%d %d", &node[i].x, &node[i].y);
82         }
83         sort(node+1, node+k+1, cmp);
84         for(int i = 1; i <= k; i++) {
85             int tmp = node[i].y;
86             if(i > 1 && node[i].y == node[i-1].y) {
87                 res++;
88             }
89             else {
90                 res = 0;
91             }
92             omega += query(1, n, 1, node[i].x, 1) - num[node[i].x] - res;
93             update(1, n, 1, node[i].x);
94         }
95         printf("Test case %d: %I64d\n", kase++, omega);
96     }
97 }

树状数组:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <stack>
10 #include <list>
11 #include <vector>
12
13 using namespace std;
14
15 const int maxn = 1000010;
16 typedef long long LL;
17 typedef struct Node {
18     LL x;
19     LL y;
20 };
21
22 Node node[maxn];
23 LL d[maxn<<1];
24 LL n, m, k;
25 LL ans;
26
27 inline bool cmp(Node a, Node b) {
28     if(a.x != b.x) {
29         return a.x > b.x;
30     }
31     return a.y > b.y;
32 }
33
34 //求某点管辖范围
35 LL lowbit(LL x) { //求x末尾最低位1的位置(末尾0的个数+1)
36     // return x & (x ^ (x - 1));
37     return x & (-x);
38 }
39
40 //区间更新树状数组(i到x)
41 void update(LL i, LL x, LL num) {
42     while(i <= x) {
43         d[i] += num;
44         i += lowbit(i);
45     }
46 }
47
48 //获取前x项和
49 LL getsum(LL x) {
50     LL sum = 0;
51     while(x > 0) {
52         sum += d[x];
53         x -= lowbit(x);
54     }
55     return sum;
56 }
57
58 int main() {
59     // freopen("in", "r", stdin);
60     LL kase = 1;
61     LL T;
62     scanf("%I64d", &T);
63     while(T--) {
64         memset(d, 0, sizeof(d));
65         scanf("%I64d %I64d %I64d", &n, &m, &k);
66         for(LL i = 1; i <= k; i++) {
67             scanf("%I64d %I64d", &node[i].x, &node[i].y);
68         }
69         sort(node+1, node+k+1, cmp);
70         ans = 0;
71         for(int i = 1; i <= k; i++) {
72             ans += getsum(node[i].y-1);
73             update(node[i].y, m, 1);
74         }
75         printf("Test case %I64d: %I64d\n", kase++, ans);
76     }
77 }

时间: 2024-10-06 21:06:21

[POJ3067]Japan的相关文章

poj3067 Japan(树状数组)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=3067 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities

poj-3067 Japan(树状数组)

Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. C

POJ3067 Japan 树状数组的应用

这题以前做过,用的线段树,现在用树状数组做一次, 题意:给你n个城市在日本左边,m个城市在日本右边,然后k条路,问你这k条路有几个交点,注意城市的序号其实就是一维坐标所在位置,所以就是两条平行的数轴,上面有点,而且之间有连线,问你有多少交点 一开始不好想把,这种题目也就排排序来试试看了,先对要修建的公路进行排序,然后再看这样是否可以更加方便的求出交点的数论,取路的左边点为先决条件从小到大排列,若相等则按照右边点来升序排列,然后以左边点为序号 和value值为1进行插入树状数组,先求出当前已经插入

POJ3067:Japan(树状数组求逆序对)

Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. C

POJ3067 Japan【树状数组】【逆序数】

题目链接: http://poj.org/problem?id=3067 题目大意: 有两排的城市,一排N个城市,编号为1~N,一排M个城市,编号为1~M.这两排城市之间有K条路. 路都是直线连接,问:这些路,有多少道路是相交的,并且焦点不是城市所在的点,求出交点个数. 思路: 树状数组的思想.参考网上的图,先将所有边(u,v)按u升序排列,如果u相同,则按v升序排列.可 以看出来,路(u1,v1)和路(u2,v2)如果有交点的话,u1 > u2 并且 v1 < v2,或者 u1 < u

树状数组总结——转

转自:夏天的风 http://blog.csdn.net/shahdza/article/details/6314818#comments 又做了几道树状数组的题,决定放一块儿总结一下:恩,总结一下.. (ps:大牛可以直接跳过...) 这得从一张图说起: 树状数组中用的d[],每个点都有一定的管辖范围: 如d[1]=a[1]; d[2]=a[1]+a[2]; d[3]=a[3]; d[4]=a[1]+a[2]+a[3]+a[4]; 等等: 这样的结构关键是为了,对一个数组内部动态的删除,增加,

POJ3067:Japan(线段树)

Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. C

(POJ 3067) Japan (慢慢熟悉的树状数组)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29295   Accepted: 7902 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas

POJ——T 3067 Japan

http://poj.org/problem?id=3067 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29474   Accepted: 7950 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with