POJ2352 Stars (静态二叉检索树)

https://vjudge.net/problem/POJ-2352

分析:
由于是按照y坐标的升序,y坐标向等的按x的升序的顺序给出星星。那么某个星星的等级数就是在他前面x坐标小于等于他的x坐标的星星的个数。
暴力的时间复杂度为n^2,超时
所以我们要记录前面所有x坐标出现的次数。然后要求出[0,xi]出现的和。
这就是静态二叉检索树 的标准问题了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxx=32010;
const int maxn=15010;
int c[maxx];
int level[maxn];

int sum(int x)
{
    int res=0;
    while(x)
    {
        res+=c[x];
        x-=(x&-x);
    }
    return res;
}
void add(int x,int d)
{
    while(x<=maxx)
    {
        c[x]+=d;
        x+=(x&(-x));
    }
}
int main()
{
    int x,y,n;
    scanf("%d",&n);
    memset(c,0,sizeof(c));
    memset(level,0,sizeof(level));
    for(int i=0;i<n;i++)
    {
        scanf("%d %d",&x,&y);
        level[sum(x+1)]++;
        add(x+1,1);
    }
    for(int i=0;i<n;i++)
        printf("%d
",level[i]);
    return 0;
}
原文地址:https://www.cnblogs.com/YingZhixin/p/7531588.html