参考讲解http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees
提供sum和add操作每次操作复杂度o(logn)
#include<iostream>
#include<stdio.h>
#include<string.h>
#define MAX 32002
using namespace std;
int a[MAX];
int c[MAX];
int n;
int leve[MAX];
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<MAX)
{
c[x]+=d;
x+=lowbit(x);
}
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
memset(leve,0,sizeof(leve));
for(i=0;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x++;
leve[sum(x)]++;
add(x,1);
}
for(i=0;i<n;i++)
printf("%d\n",leve[i]);
}
}