poj 2352 Stars 树状数组

题目链接:

http://poj.org/problem?id=2352

题意:

给你一系列点(按y递增,x递增排列),level[X] 指的是 在当前点的左下方 包括自己 的点数为X 的这些点的个数。

题解:

树状数组
y递增,不管y,只要维护x就行了
读入一个点,找到比他小的点的个数,也就是他的等级,然后将这个等级的点的个数加一即可

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long ll;
 6 #define MS(a) memset(a,0,sizeof(a))
 7 #define MP make_pair
 8 #define PB push_back
 9 const int INF = 0x3f3f3f3f;
10 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
11 inline ll read(){
12     ll x=0,f=1;char ch=getchar();
13     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
14     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
15     return x*f;
16 }
17 //////////////////////////////////////////////////////////////////////////
18 const int maxn = 32000+10;
19 
20 int bit[maxn],level[maxn];
21 
22 void add(int x,int v){
23     while(x < maxn){
24         bit[x] += v;
25         x += x&-x;
26     }
27 }
28 
29 int sum(int x){
30     int res = 0;
31     while(x > 0){
32         res += bit[x];
33         x -= x&-x;
34     }
35     return res;
36 }
37 
38 int main(){
39     int n=read();
40     MS(level); int x,y;
41     for(int i=0; i<n; i++){
42         x=read(),y=read(); x++;
43         add(x,1);
44         level[sum(x)]++;
45     }
46 
47     for(int i=1; i<=n; i++)
48         cout << level[i] << endl;
49 
50     return 0;
51 }
原文地址:https://www.cnblogs.com/yxg123123/p/6827570.html