【HDOJ】4325 Flowers

树状数组+离散化的题目,一直在思考为什么结果不一样,后来才发现花开了就是开了不会再谢了。

 1 /* 4325 */
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 #define MAXN 100005
10 
11 typedef struct {
12     int s, t;
13 } node_t;
14 
15 node_t nodes[MAXN];
16 int qi[MAXN];
17 int buf[MAXN*4];
18 int sum[MAXN];
19 
20 inline int lowbit(int x) {
21     return -x&x;
22 }
23 
24 void update(int i, int v, int n) {
25     while (i <= n) {
26         sum[i] += v;
27         i += lowbit(i);
28     }
29 }
30 
31 int query(int i) {
32     int ret = 0;
33     
34     while (i) {
35         ret += sum[i];
36         i -= lowbit(i);
37     }
38     
39     return ret;
40 }
41 
42 int main() {
43     int t, n, m, l;
44     int i, j, k, tmp;
45     int a, b, c;
46     
47     #ifndef ONLINE_JUDGE
48         freopen("data.in", "r", stdin);
49     #endif
50     
51     scanf("%d", &t);
52     for (int tt=1; tt<=t; ++tt) {
53         scanf("%d %d", &n, &m);
54         l = 0;
55         for (i=0; i<n; ++i) {
56             scanf("%d %d", &nodes[i].s, &nodes[i].t);
57             buf[l++] = nodes[i].s;
58             buf[l++] = nodes[i].t;
59         }
60         for (i=0; i<m; ++i) {
61             scanf("%d", &qi[i]);
62             buf[l++] = qi[i];
63         }
64         
65         // discretization
66         buf[l++] = 0;
67         sort(buf, buf+l);
68         c = unique(buf, buf+l) - buf;
69         
70         memset(sum, 0, sizeof(sum));
71         for (i=0; i<n; ++i) {
72             a = lower_bound(buf, buf+c, nodes[i].s) - buf + 1;
73             b = lower_bound(buf, buf+c, nodes[i].t) - buf + 1;
74             update(a, 1, c);
75             update(b+1, -1, c);
76         }
77         
78         printf("Case #%d:\n", tt);
79         for (i=0; i<m; ++i) {
80             j = lower_bound(buf, buf+c, qi[i]) - buf + 1;
81             k = query(j);
82             printf("%d\n", k);
83         }
84     }
85     
86     return 0;
87 }

 

posted on 2015-01-05 21:52  Bombe  阅读(122)  评论(0编辑  收藏  举报

导航