poj 2352 Stars 树状数组

树状数组,果断1A啦,心情舒畅啊,哈哈

先依照y轴从小到大排序,然后一次增加并统计每一个点得x坐标,这道题数据不严谨啊,在y轴同样得情况下,应该以x轴从大到小排序,可我都没排x轴就过了

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<memory.h>
using namespace std;
int Max;
struct node{
    int x,y;
}a[15005];
int tree[32005];
bool cmp(node a,node b){
    return a.y<b.y;
}
int lowbit(int x){
    return x&(-x);
}
void add(int i,int val){
    while(i<=Max){
        tree[i]+=val;
        i+=lowbit(i);
    }
}
int sum(int i){
    int r=0;
    while(i>=1){
        r+=tree[i];
        i-=lowbit(i);
    }
    return r;
}
int main(){
 //   freopen("in.txt","r",stdin);
    int n;
    Max=0;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].x++;
            a[i].y++;
            if(a[i].x>Max) Max=a[i].x;
        }
        sort(a,a+n,cmp);
        memset(tree,0,sizeof(tree));
        int ans[15005];
        memset(ans,0,sizeof(ans));
        for(int i=0;i<n;i++){
            add(a[i].x,1);
            ans[sum(a[i].x)]++;
        }
        for(int i=1;i<=n;i++)
            printf("%d\n",ans[i]);
    }
}

你可能感兴趣的:(树状数组)