[ural1028]STARS

 

#include<cstdio>
#include<cstdlib>
#define maxn 32001
int n;
int x,y;
int c[maxn];
int ans[maxn];
int lowbit(int x)
{
    return (x & (x ^ (x-1)));
}
void add(int k)
{
     while (k<=maxn)
     {
           c[k]++;
           k+=lowbit(k);
     }
}
int sum(int k)
{
    int tot=0;
    while (k>0)
    {
          tot+=c[k];
          k-=lowbit(k);
    }
    return tot;
}
int main()
{
//    freopen("ural1028.in","r",stdin);
//    freopen("ural1028.out","w",stdout);
    scanf("%d\n",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d %d\n",&x,&y);
        ans[sum(x+1)]++;
        add(x+1);
    }
    for (int i=0;i<n;i++) printf("%d\n",ans[i]);
//    system("pause");
    return 0;
}

 

加操作

 

void add(int k,int x)
{
    while (k<=n)
    {
        c[k]+=x;
        k=k+(k & -k);
    }
}

 

减操作

 

void sub(int k,int x)
{
    while (k<=n)
    {
        c[k]-=x;
        k=k+(k & -k);
    }
}

 

求和操作

 

void sum(int k)
{
    int tot=0;
    while (k>0)
    {
        tot+=c[k];
        k-=(k & -k);
    }
}