题目链接:http://poj.org/problem?id=2352
树状数组裸题,求的是前k个数中X≤Xk且Y≤Yk的数对个数恰好为i的k有多少个
贴代码
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=32005;
int c[32005],f[15005],n;
int lowbit(int x){
return x & -x;
}
void add(int x){
while (x<=maxn){
c[x]+=1;
x+=lowbit(x);
}
}
int query(int x){
int s=0;
while (x>=1){
s+=c[x];
x-=lowbit(x);
}
return s;
}
int main(){
// freopen("stars.in","r",stdin);
// freopen("stars.out","w",stdout);
scanf("%d",&n);
memset(c,sizeof(c),0);
memset(f,sizeof(f),0);
for (int i=1;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
x++;
add(x);
int tt=query(x);
f[tt]+=1;
}
for (int i=1;i<=n;i++) printf("%d\n",f[i]);
return 0;
}
【写的有漏洞的,欢迎路过大神吐槽】
2016/11/13 22:47:41
Ending.