首页 > 代码库 > [POJ2352] Stars(树状数组)

[POJ2352] Stars(树状数组)

传送门

 

先按照下标x排序,然后依次把y加入树状数组,边加入边统计即可。

注意下标re从零开始,需+1s

——代码

技术分享
 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <string> 5 # include <cmath> 6 # include <vector> 7 # include <map> 8 # include <queue> 9 # include <cstdlib>10 # include <algorithm>11 # define MAXN 3200212 using namespace std;13 14 inline int get_num() {15     int k = 0, f = 1;16     char c = getchar();17     for(; !isdigit(c); c = getchar()) if(c == -) f = -1;18     for(; isdigit(c); c = getchar()) k = k * 10 + c - 0;19     return k * f;20 }21 22 int n;23 int c[MAXN], ans[MAXN];24 struct node25 {26     int a, b;27 }p[MAXN];28 29 inline bool cmp(node x, node y)30 {31     return x.a < y.a || (x.a == y.a && x.b < y.b);32 }33 34 inline int lowbit(int x)35 {36     return x & -x;37 }38 39 inline int query(int x)40 {41     int ans = 0;42     for(; x; x -= lowbit(x)) ans += c[x];43     return ans;44 }45 46 inline void add(int x)47 {48     for(; x <= 32001; x += lowbit(x)) c[x] += 1;49 }50 51 int main()52 {53     int i, t;54     n = get_num();55     for(i = 1; i <= n; i++)56     {57         p[i].a = get_num() + 1;58         p[i].b = get_num() + 1;59     }60     sort(p + 1, p + n + 1, cmp);61     for(i = 1; i <= n; i++)62     {63         t = query(p[i].b);64         add(p[i].b);65         ans[t]++;66     }67     for(i = 0; i < n; i++) printf("%d\n", ans[i]);68     return 0;69 }
View Code

 

[POJ2352] Stars(树状数组)