Stars
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 39832   Accepted: 17283

Description

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.
<center> </center>
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.

Source


之前这题用堆树做过,就是模板题啊,将x坐标插入树中,用随机产生的pri进行维护,递归寻找不大于某key值的个数。详见这篇:

poj2352stars【treap树水题】

然而这题当然也可以用树状数组和线段树搞,树状数组的话就是《算法设计与实现》 的原题

线段树的话就是单点更新区间求和,每次插入新数据都把他以及他的父节点的sum++,向下成段查询(0,x)的区间和。举例说明他的原理:如果查询范围是0~maxn,第一次就return 了,由于插入的时候在根节点到他自己这个叶子节点的路径上都++了,那么每次插入新值根节点都会++,返回的自然是所有个数;再比如说我查询的是最左边的两个点,他们的LCA那个点的sum值是他俩出现的次数

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int tot[32005],n,x,y;
struct node
{
    int sum,l,r;
}num[82005];
void build(int rt,int l,int r)
{
    num[rt].l=l;num[rt].r=r;
    num[rt].sum=0;
    if(l==r)return;
    int mid=(l+r)/2;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
}
void update(int rt,int x)
{
    num[rt].sum++;
    if(num[rt].l==num[rt].r&&num[rt].l==x)
        return;
    int mid=(num[rt].l+num[rt].r)/2;
    if(x<=mid)update(rt<<1,x);
    else update(rt<<1|1,x);
}
int query(int rt,int l,int r)
{
    int sum=0;
    if(num[rt].l==l&&num[rt].r==r)
        return num[rt].sum;
    int mid=(num[rt].l+num[rt].r)/2;
    if(r<=mid) sum=query(rt<<1,l,r);
    else if(l>mid) sum=query(rt<<1|1,l,r);
    else
        sum=query(rt<<1,l,mid)+query(rt<<1|1,mid+1,r);
    return sum;
}
int main()
{
    freopen("cin.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        build(1,0,32010);
        memset(tot,0,sizeof(tot));
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            x++;
            tot[query(1,0,x)]++;
            update(1,x);
        }
        for(int i=0;i<n;i++)
            printf("%d\n",tot[i]);
    }
    return 0;
}