题目大意:
在坐标上有n个星星,如果某个星星坐标为(x, y), 它的左下位置为:(x0,y0),x0<=x 且y0<=y。如果左下位置有a个星星,就表示这个星星属于level x
按照y递增,如果y相同则x递增的顺序给出n个星星,求出所有level水平的数量。
分析与总结:
因为输入是按照按照y递增,如果y相同则x递增的顺序给出的, 所以,对于第i颗星星,它的level就是之前出现过的星星中,横坐标x小于等于i星横坐标的那些星星的总数量(前面的y一定比后面的y小)。
所以,需要找到一种数据结构来记录所有星星的x值,方便的求出所有值为0~x的星星总数量。
树状数组和线段树都是很适合处理这种问题。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <set>
#include <list>
#include <queue>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
const int N = 100005;
const double eps = 1e-10;
int c[32100],level[32100],n;
void update(int x,int num)
{
while(x <= 32100)
{
c[x] += num;
x += x&(-x);
}
}
int getsum(int x)
{
int s = 0;
while(x)
{
s += c[x];
x -= x&(-x);
}
return s;
}
int main()
{
while(~scanf("%d",&n))
{
int x,y;
memset(level, 0, sizeof(level));
memset(c, 0, sizeof(c));
for(int i=0; i<n; ++i)
{
scanf("%d%d",&x,&y);
++x;
level[getsum(x)]++;
update(x,1);
}
for(int i=0; i<n; ++i)
printf("%d\n",level[i]);
}
return 0;
}