POJ2352题解

二维当成一维想,POJ2352 Star

题目描述

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.
5jhXXu.png
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.

输入输出格式

输入格式

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.

输出格式

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.

INPUT & OUTPUT’s examples

Input’s eg #1

1
2
3
4
5
6
5
1 1
5 1
7 1
3 3
5 5

Output’s eg #1

1
2
3
4
5
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.

Limits

Time Limit: 1000MS Memory Limit: 65536Kib

分析

树状数组的一道模板题。

我们来看一下题面中的这句话:

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.

是不是很像树状数组中的前缀和?没错,就是的。

那么我们就可以用一个一维的树状数组维护了,对于每一次输入的坐标,我们把它的横坐标插入树状数组,(因为我们只关心一个恒星的左面有多少个星星)。

那么答案就显而易见了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Headers */
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
using namespace std;
namespace FastIO{
const int BUFSIZE=1<<20;
char ibuf[BUFSIZE],*is=ibuf,*it=ibuf;
char obuf[BUFSIZE],*os=obuf,*ot=obuf+BUFSIZE;
inline char getch(){
if(is==it)
it=(is=ibuf)+fread(ibuf,1,BUFSIZE,stdin);
return (is==it)?EOF:*is++;
}
inline int getint(){
int res=0,neg=0,ch=getch();
while(!(isdigit(ch)||ch=='-')&&ch!=EOF)
ch=getch();
if(ch=='-'){
neg=1;ch=getch();
}
while(isdigit(ch)){
res=(res<<3)+(res<<1)+(ch-'0');
ch=getch();
}
return neg?-res:res;
}
inline void flush(){
fwrite(obuf,1,os-obuf,stdout);
os=obuf;
}
inline void putch(char ch){
*os++=ch;
if(os==ot) flush();
}
inline void putint(int res){
static char q[10];
if(res==0) putch('0');
else if(res<0){
putch('-');
res=-res;
}
int top=0;
while(res){
q[top++]=res%10+'0';
res/=10;
}
while(top--) putch(q[top]);
}
}
using namespace FastIO;
/* definitions */
const int MAXN = 3e4+10;
const int MAXM = 15010;
int Tree[MAXN];
int Level[MAXN];
int n;
/* functions */
int lowbit(int x){
return x&(-x);
}
void Insert(int x){
while(x<=MAXN){
++Tree[x];
x+=lowbit(x);
}
}
int Query(int x){
int ans=0;
while(x){
ans+=Tree[x];
x-=lowbit(x);
}
return ans;
}
void work(){
memset(Level,0,sizeof(Level));
memset(Tree,0,sizeof(Tree));
for(int i=0;i<n;++i){
int x,y;
scanf("%d%d",&x,&y);
Insert(++x);
++Level[Query(x)];
}
for(int i=1;i<=n;++i)printf("%d\n",Level[i]);
}
int main(){
while(scanf("%d", &n) != EOF)work();
return 0;
}

THE END