POJ 2352 Stars

来源:互联网 发布:js调用浏览器打印 编辑:程序博客网 时间:2024/05/25 05:37
Stars
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23046 Accepted: 10049

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

51 15 17 13 35 5

Sample Output

12110
 
刚学会线段树,还不怎么熟练,看了一下网上的代码,觉得线段树好神奇!

题目大意: 有n个星星, 现在分别给出它们的坐标(按y递增的顺序给出),每个星星有一个等级(该星星的等级是x坐标和y坐标都不大于该星的星星数),先要求出每个等级的星星有多少个

思路:用线段树或树状数组, 由于是按y递增的坐标给出的, 新加入一个点只需判断区间【1,x】多少个点, 并更新后面包含他区间的点的个数 。

我对树状数组并不是很了解。

AC代码(树状数组):

#include<stdio.h>#include<string.h>int n=0,c[32002];int lowbit(int x){return x&(-x);}void add(int x){while(x<=32002){c[x]++;x+=lowbit(x);}}int sum(int x){int s=0;while(x>0){s+=c[x];x-=lowbit(x);}return s;}int main(){int i=0,x=0,y=0,level[15002];while(scanf("%d",&n)!=EOF){memset(level,0,sizeof(level));memset(c,0,sizeof(level));for(i=0;i<n;i++){scanf("%d%d",&x,&y);x++;level[sum(x)]++;add(x);}for(i=0;i<n;i++)printf("%d\n",level[i]);}return 0;}


 

AC代码(线段树):

#include <stdio.h> #include <string.h> #define max 32002 struct {     int sum, l, r; }tree[4*max]; void build_tree(int l, int r, int p) {     int m = (l+r)/2;     tree[p].sum = 0;     if(l == r)     {         tree[p].l = tree[p].r = l;         return ;     }     tree[p].l = l; tree[p].r = r;     build_tree(l, m, p*2);     build_tree(m+1, r, p*2+1); } void add(int l, int r, int p) {     int m = (tree[p].l+tree[p].r)/2;     if(tree[p].l == l && tree[p].r == r)     {         tree[p].sum++;         return ;     }     if(l>m)          add(l, r, 2*p+1);     else if(r<=m)          add(l, r, 2*p);     else     {          add(l, m, 2*p);          add(m+1, r, 2*p+1);     }     tree[p].sum = tree[p*2].sum+tree[p*2+1].sum; } int query(int l, int r, int p) {     int m = (tree[p].l+tree[p].r)/2;     if(tree[p].l == l && tree[p].r == r)         return tree[p].sum;     if(l>m)         return query(l, r, 2*p+1);     else if(r<=m)         return query(l, r, 2*p);     else         return query(l, m, 2*p)+query(m+1, r, 2*p+1);  } int main() {     int i = 0, n = 0, x= 0, y = 0, lev[15002];     while(scanf("%d", &n) !=  EOF)     {         memset(lev, 0, sizeof(lev));         build_tree(0, max, 1);         for(i = 0; i<n; i++)         {             scanf("%d %d", &x, &y);             lev[query(0, x, 1)]++;             add(x, x, 1);         }         for(i = 0; i<n; i++)             printf("%d\n", lev[i]);     }     return 0; }