HDU1541 - Stars

2016/11/13

树状数组统计的裸题。

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring>
#define CLR(x) memset((x), 0, sizeof(x))
#define lowbit(x) ((x) & (-x))
using namespace std;
const int MAX_N = 15000 + 1000;
const int MAX_M = MAX_N << 1;
struct BIT {
    int c[MAX_M], cnt[MAX_N];
    void mkTree() { CLR(c), CLR(cnt); }
    void add(int x) { for(;x<=MAX_M;x+=lowbit(x)) ++c[x]; }
    int sum(int x) { int s=0;for(;x>0;x-=lowbit(x)) s+=c[x]; return s; }
} bt;
int main() {
    int N, x, y;
    while(cin >> N) {
        bt.mkTree();
        for(int i=0;i<N;i++) {
            cin >> x >> y;
            bt.cnt[bt.sum(x+1)]++;
            bt.add(x+1);
        }
        for(int i=0;i<N;i++) cout << bt.cnt[i] << endl;
    }
}