標籤:poj
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
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
Source
Ural Collegiate Programming Contest 1999
題目大意:
在座標上有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 <iostream>#include <stdio.h>#include <string.h>#include <string>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <list>#include <algorithm>#include <climits>using namespace std;#define lson 2*i#define rson 2*i+1#define LS l,mid,lson#define RS mid+1,r,rson#define UP(i,x,y) for(i=x;i<=y;i++)#define DOWN(i,x,y) for(i=x;i>=y;i--)#define MEM(a,x) memset(a,x,sizeof(a))#define W(a) while(a)#define gcd(a,b) __gcd(a,b)#define LL long long#define N 32005#define INF 0x3f3f3f3f#define EXP 1e-8#define lowbit(x) (x&-x)const int mod = 1e9+7;int a[N];int n,c[N],ans[N],x,y;int sum(int x){ int ret=0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret;}void add(int x,int d){ while(x<=N) { c[x]+=d; x+=lowbit(x); }}int main(){ int i,j,k; while(~scanf("%d",&n)) { MEM(c,0); MEM(ans,0); for(i = 1; i<=n; i++) { scanf("%d%d",&x,&y); x++; ans[sum(x)]++; add(x,1); } for(i = 0; i<n; i++) printf("%d\n",ans[i]); } return 0;}
POJ2352:Stars(樹狀數組)