首页 > 代码库 > POJ2352-Stars(SegmentTree || BinaryIndexTree)
POJ2352-Stars(SegmentTree || BinaryIndexTree)
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.
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.
Means:
给你好多星星,问你这些Level的星星有多少个,也就是说,每个星星Level是他左下角的(包括正下,正左)
Solve:
BIT || ST,因为BIT从1开始,所以算的时候每个x要先+1,然后就是算比当前x小的星星的level数就行了.PS:BIT跑的比ST快.jpg
Code(BinaryIndexTree):
1 #include <cstdio> 2 #include <cstring> 3 #define lowbit(x) x&(-x) 4 #define Clear(data , v) memset(data , v , sizeof(data)) 5 #define maxn 32010 6 int n; 7 int tree[maxn] = {0}; 8 int level[maxn] = {0}; 9 void Add(int pos , int data)10 {11 while(pos < maxn)12 {13 tree[pos] += data;14 pos += lowbit(pos);15 }16 }17 int Query(int x)18 {19 int sum = 0;20 while(x > 0)21 {22 sum += tree[x];23 x -= lowbit(x);24 }25 return sum;26 }27 int main()28 {29 while(~scanf("%d" , &n))30 {31 Clear(tree , 0) , Clear(level , 0);32 int x , y;33 for(int i = 0 ; i < n ; ++i) scanf("%d%d" , &x , &y) , ++x , ++level[Query(x)] , Add(x , 1);34 for(int i = 0 ; i < n ; ++i) printf("%d\n" , level[i]);35 }36 }
Code(SegmentTree):
1 #include <cstdio> 2 #include <cstring> 3 #define MAXN 32010 4 #define CLR(data) memset(data , 0 , sizeof(data)) 5 int n; 6 int tree[4 * MAXN] = {0}; 7 int level[MAXN] = {0}; 8 void Update(int id , int l , int r , int x , int y) 9 {10 if(l == r)11 {12 tree[id] += y;13 return;14 }15 int mid = (l + r) >> 1;16 if(x <= mid) Update(id << 1 , l , mid , x , y);17 else Update(id << 1 | 1 , mid + 1 , r , x , y);18 tree[id] = tree[id << 1] + tree[id << 1 | 1];19 }20 int Query(int id , int l , int r , int x , int y)21 {22 if(x <= l && y >= r)23 {24 return tree[id];25 }26 int mid = (l + r) >> 1;27 if(y <= mid) Query(id * 2 , l , mid , x , y);28 else if (x > mid) Query(id << 1 | 1 , mid + 1 , r , x , y);29 else return Query(id << 1 , l , mid , x , mid) + Query(id << 1 | 1 , mid + 1 , r , mid + 1 , y);30 }31 int main()32 {33 while(~scanf("%d" , &n))34 {35 CLR(tree) , CLR(level);36 int x , y;37 for(int i = 0 ; i < n ; ++i)38 {39 scanf("%d%d" , &x , &y);40 ++x;41 ++level[Query(1 , 1 , MAXN , 1 , x)];42 Update(1 , 1 , MAXN , x , 1);43 }44 for(int i = 0 ; i < n ; ++i)45 printf("%d\n" , level[i]);46 }47 }
POJ2352-Stars(SegmentTree || BinaryIndexTree)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。