A - 并肩行过山与水 POJ - 2352 Stars 树状数组

A - 并肩行过山与水

 POJ - 2352 

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. 


For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3. 

You are to write a program that will count the amounts of the stars of each level on a given map.

Input

The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate. 

Output

The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.

Sample Input

5
1 1
5 1
7 1
3 3
5 5

Sample Output

1
2
1
1
0

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

 题意:对于点(x,y)又k个点(xi,yi)(xi<=x&&yi<=y)则(x,y)为k级,输出0~N-1级的点的个数

思路:树状数组存小于等于x的数的个数。由于输入的点是按y升序再x升序的,故而每输入一个点坐标直接查询之前输入的点的横坐标小于等于当前x的点的个数即为当前点的级别。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <malloc.h>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define clc(a,b,n) for(int i=0;i<=n;i++)a[i]=b
#define clc2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b
#define fora(i,a,b) for(int i=a;i<b;i++)
#define fors(i,a,b) for(int i=a;i>b;i--)
#define fora2(i,a,b) for(int i=a;i<=b;i++)
#define fors2(i,a,b) for(int i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f
#define BASE 131
#define lowbit(x) x&(-x)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
using namespace std;
const int maxn=32000+11;
int tre[maxn];
int ans[maxn];
void update(int i,int val,int n)//n是树的大小
{
    for(;i<=n;i+=lowbit(i))
        tre[i]+=val;
}
int sum(int i)
{
    int ret=0;
    for(;i>0;i-=lowbit(i))
        ret+=tre[i];
    return ret;
}
int main()
{
    int N;
    while(~scanf("%d",&N))
    {
        memset(tre,0,sizeof(tre));
        memset(ans,0,sizeof(ans));
        fora2(i,1,N)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            x++;
            int tem=sum(x);//当前比x小OR等于的点的个数(纵坐标也一定小于等于当前的y)
            ans[tem]++;
            update(x,1,maxn-1);
        }

        fora(i,0,N)printf("%d\n",ans[i]);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liyang__abc/article/details/81810081
今日推荐