POJ 2352 线段树(单点更新)

来源:互联网 发布:c语言排序算法最快 编辑:程序博客网 时间:2024/05/22 10:50

题目

Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 39440 Accepted: 17129
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.

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排序,x相同按y增序排列。然后我们在新序列里将y看作一个点插入线段树。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <set>#include <cmath>#include <map>#include <queue>#include <sstream>#include <vector>#define m0(a) memset(a,0,sizeof(a))#define mm(a) memset(a,0x3f,sizeof(a))#define m_1(a) memset(a,-1,sizeof(a))#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)#define FFR freopen("data.in","r",stdin)#define FFW freopen("data.out","w",stdout)using namespace std;struct Po{    int x,y;};Po G[15000+100];int ans[15000+100];int T[32000*4+100];int NN;bool cmp(Po a,Po b){    if(a.x<b.x)        return true;    else if(a.x==b.x)        if(a.y<b.y)            return true;    return false;}void init(int n){    NN = 1;    while(NN<n) NN<<=1;    NN--;    m0(T);    m0(ans);}int aP(int t){    int sum = 0;    T[t+=NN]++;    sum+=T[t];    if(t&1) sum+=T[t-1];    for(t>>=1;t;t>>=1){        if(t&1) sum+=T[t-1];        T[t] = T[t<<1]+T[t<<1|1];    }    return sum-1;}int main(){    ios_base::sync_with_stdio(false); cin.tie(0);    int n;    cin>>n;    int i;    int Mn = -1;    f(i,1,n){        cin>>G[i].x>>G[i].y;        if(Mn<G[i].y) Mn = G[i].y;    }    sort(G+1,G+1+n,cmp);    init(Mn);    f(i,1,n)        ans[aP(G[i].y+1)]++;    f(i,0,n-1)        cout<<ans[i]<<endl;    return 0;}
0 0
原创粉丝点击