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.

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.

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 }

时间: 2024-08-09 02:01:32

POJ2352-Stars(SegmentTree || BinaryIndexTree)的相关文章

POJ2352 Stars 【树状数组】or【线段树】

Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31172   Accepted: 13595 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 st

POJ2352 Stars 树状数组

POJ2352 非常裸的树状数组的题. 注意数组下标不能从0开始 因为lowbit(0)==0 所以 所有横坐标统一加1 数组要开的够大 就酱 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<cmath> #include<vector> using namespace std; const int maxn=15000,

poj--2352 Stars(树状数组)

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. As

HDOJ1166-敌兵布阵(SegmentTree || BinaryIndexTree)

Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视.中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人

POJ2352 Stars (静态二叉检索树)

https://vjudge.net/problem/POJ-2352 分析: 由于是按照y坐标的升序,y坐标向等的按x的升序的顺序给出星星.那么某个星星的等级数就是在他前面x坐标小于等于他的x坐标的星星的个数. 暴力的时间复杂度为n^2,超时 所以我们要记录前面所有x坐标出现的次数.然后要求出[0,xi]出现的和. 这就是静态二叉检索树 的标准问题了. #include <iostream> #include <cstdio> #include <cstring> #

POJ2352 Stars

B - Stars Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2352 Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordi

POJ 2481 树状数组 区间覆盖(POJ2352 Stars 的变形题)(线段化点)

0)学会将题目情景转化为自己熟悉的结构或模型. 题目大意: 每个奶牛有自己的一个区间,求每个奶牛的区间所覆盖的子区间个数(注意,真子集,相等的不算),按照输入的顺序输出. 转化: 要学会将题目情景转化为自己熟悉的模型或结构上.把每个区间的左端x值作为点的x坐标,右端x值作为点的y坐标,就可以把所有区间转化为一个二维坐标图上的点集,而此时每个点左上方的点(同Stars那道题目一样不包括自身)的个数,就是每个区间所覆盖的子区间的个数(对应题目要求,这里或许可以再变形). 同POJ2481 Stars

[POJ2352] Stars(树状数组)

传送门 先按照下标x排序,然后依次把y加入树状数组,边加入边统计即可. 注意下标re从零开始,需+1s ——代码 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <string> 5 # include <cmath> 6 # include <vector> 7 # include <map> 8 # includ

树状数组总结——转

转自:夏天的风 http://blog.csdn.net/shahdza/article/details/6314818#comments 又做了几道树状数组的题,决定放一块儿总结一下:恩,总结一下.. (ps:大牛可以直接跳过...) 这得从一张图说起: 树状数组中用的d[],每个点都有一定的管辖范围: 如d[1]=a[1]; d[2]=a[1]+a[2]; d[3]=a[3]; d[4]=a[1]+a[2]+a[3]+a[4]; 等等: 这样的结构关键是为了,对一个数组内部动态的删除,增加,