Problem
Stars
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46461 Accepted: 20040 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 5Sample Output
1 2 1 1 0Hint
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
天文学家经常观察星象图。星象图中用平面上的点来表示一颗星星,每一颗星星都有一个笛卡尔坐标。设定星星的等级为其左下角星星的总数。
天文学家们想知道星星等级的分布情况。
比如上图,5号星星的等级为3(其左下角有编号为1、2、4的星星共三颗)。2号星星和4号星星的等级为1。在上图中只有一颗星星等级为0,两颗星星等级为1,一颗星星等级为2,一颗星星等级为3。
给定一个星象图,请你写一个程序计算各个等级的星星数目。
Input
输入的第一行包含星星的总数N (1<=N<=15000)。接下来N行,描述星星的坐标(X,Y)(X和Y用空格分开,0<=X,Y<=32000)。星象图 中的每个点处最多只有一颗星星。所有星星按Y坐标升序排列。Y坐标相等的星星按X坐标升序排列。
Output
输出包含N行,每行一个整数。第一行包含等级0的星星数目,第二行包含等级1的星星数目,依此类推,最后一行包含等级为N-1的星星数目。
Solution
题目一看就是要求和啦
先明确一下左下角这个概念,求左下角那么肯定不包括自己啦!
那就是e[1..x[i],1..y[i]] 的星星个数-1 //1是自己 ,e[x,y]表示在(x,y)的点
可以先查询再修改就不用-1啦
即sum{e[1..x[i],1..y[i]}
然后因为是按照y升序读入的那么读入第i个时,前i-1个的y肯定<=y[i]
所以我们可以这样想
sum{e[1..x[i],1..y[i]]} = sum{e[1..x[i]]} //右边式子的e表示第i列的个数
于是直接树状数组搞啦
但是要注意——坐标从0开始
可是lowbit(0)=0树状数组的坐标里没0 啊!
于是我可以采用以下两种方法
(1)把这个e数组往右平移1个单位使他从下标1开始
1 program test; 2 const 3 maxn=32001;//********* 4 var 5 e,num:array[1..32001] of longint; 6 i,n,x,y:longint; 7 8 function lowbit(n:longint):longint; 9 begin 10 exit(-n and n); 11 end; 12 13 procedure add(ii:longint); 14 begin 15 while ii<=maxn do 16 begin 17 inc(e[ii]); 18 ii:=ii+lowbit(ii); 19 end; 20 end; 21 22 function query(ii:longint):longint; 23 begin 24 query:=0; 25 while ii>0 do 26 begin 27 inc(query,e[ii]); 28 ii:=ii-lowbit(ii); 29 end; 30 end; 31 32 begin 33 readln(n); 34 for i:= 1 to n do 35 begin 36 readln(x,y); x:=x+1;//********* 37 inc(num[query(x)]); 38 add(x); 39 end; 40 for i:= 0 to n-1 do writeln(num[i]); 41 end.
(2)在lowbit上修改使得可以求出正确的lowbit值
1 program test; 2 const 3 maxn=32000;//********* 4 var 5 e,num:array[0..32001] of longint; 6 i,n,x,y:longint; 7 8 function lowbit(n:longint):longint; 9 begin 10 n:=n+1;//********* 11 exit(-n and n); 12 end; 13 14 procedure add(ii:longint); 15 begin 16 while ii<=maxn do 17 begin 18 inc(e[ii]); 19 ii:=ii+lowbit(ii); 20 end; 21 end; 22 23 function query(ii:longint):longint; 24 begin 25 query:=0; 26 while ii>-1 do //********* 27 begin 28 inc(query,e[ii]); 29 ii:=ii-lowbit(ii); 30 end; 31 end; 32 33 begin 34 readln(n); 35 for i:= 1 to n do 36 begin 37 readln(x,y); //x:=x+1; 38 inc(num[query(x)]); 39 add(x); 40 end; 41 for i:= 0 to n-1 do writeln(num[i]); 42 end.