poj 2352

来源:互联网 发布:杭州编程培训和盈 编辑:程序博客网 时间:2024/06/05 05:44
题意:按纵坐标升序给定N颗星星的坐标(x[i],y[i]),计算出每个等级的星星有多少,对于等级是这样定义的:对于第i颗星星,计算出1~i-1 颗星星中横坐标和纵坐标都比第i颗星星小的数量ans,那么ans就是此颗星星的等级。因为y是升序,所以就以 x 坐标为根节点,记录个数,注意横坐标不能为0 因为,x += lowbit(x) 将一直未0 死循环,#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 32100;int f[maxn], c[maxn];int lowbit(int x){    return x&-x;}void add(int x, int d){    while(x < maxn)    {        c[x] += d;        x += lowbit(x);    }}int sum(int x){    int res = 0;    while(x > 0)    {        res += c[x];        x -= lowbit(x);    }    return res;}int main(){    int n;    while(scanf("%d", &n) != EOF)    {        memset(f, 0, sizeof(f));        memset(c, 0, sizeof(c));        for(int i = 0; i < n; i++)        {            int x, y;            scanf("%d%d", &x, &y);            x++;            f[sum(x)]++;            add(x, 1);        }        for(int i = 0; i < n; i++)            printf("%d\n", f[i]);    }}