POJ - 2352 Stars (树状数组)

题意:给你星星的坐标(Y递增,若Y相等则X递增),每个星星有个等级,规定它的等级是它左下方的星星个数,输出所有星星的等级

思路:树状数组

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 32010;

int n,lev[MAXN],c[MAXN];

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

int sum(int x){
    int res = 0;
    while (x > 0)
        res += c[x],x -= x&-x;
    return res;
}

int main(){
    int x,y;
    while (scanf("%d",&n) != EOF){
        memset(lev,0,sizeof(lev));
        memset(c,0,sizeof(c));
        for (int i = 0; i < n; i++){
            scanf("%d%d",&x,&y);
            x++;
            lev[sum(x)]++;
            add(x,1);
        }
        for (int i = 0; i < n; i++)
            printf("%d\n",lev[i]);
    }
    return 0;
}


你可能感兴趣的:(POJ - 2352 Stars (树状数组))