poj 2352 stars_线段树基础

Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16362 Accepted: 7064

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. 
poj 2352 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.

Source

注:这道题目用树状数组是非常方便的,直接进行统计,这里用线段树主要是加强对线段树的理解。这里可以不用lazy思想而直接进行统计。
   
     
1 type
2 ji =^ rec;
3 rec = record
4 sum:longint;
5 l,r:longint;
6 lson,rson:ji;
7 end;
8
9 var
10 a:ji;
11 ans:array[ 0 .. 50005 ] of longint;
12 i,j,k,n:longint;
13 procedure build(var a:ji; l,r:longint);
14 var
15 mid:longint;
16 begin
17 new (a);
18 a ^ .sum: = 0 ;
19 a ^ .l: = l;
20 a ^ .r: = r;
21 if l = r then exit;
22 mid: = (l + r) >> 1 ;
23 build(a ^ .lson,l,mid);
24 build(a ^ .rson,mid + 1 ,r);
25 end;
26
27 function sum(x:longint; a:ji):longint;
28 var
29 mid,k:longint;
30 begin
31 k: = 0 ;
32 if ( 1 <= a ^ .l)and(x >= a ^ .r) then
33 begin
34 inc(k,a ^ .sum);
35 exit(k);
36 end;
37 mid: = (a ^ .l + a ^ .r) >> 1 ;
38 if 1 <= mid then inc(k,sum(x,a ^ .lson));
39 if x > mid then inc(k,sum(x,a ^ .rson));
40 exit(k);
41 end;
42
43 procedure insert(x:longint; a:ji);
44 var
45 mid:longint;
46 begin
47 if (x = a ^ .l)and(x = a ^ .r) then
48 begin
49 inc(a ^ .sum);
50 exit;
51 end;
52 mid: = (a ^ .l + a ^ .r) >> 1 ;
53 if x <= mid then insert(x,a ^ .lson);
54 if x > mid then insert(x,a ^ .rson);
55 a ^ .sum: = a ^ .lson ^ .sum + a ^ .rson ^ .sum;
56 end;
57
58 begin
59 readln(n);
60 build(a, 1 , 32001 );
61 for i: = 1 to n do
62 begin
63 readln(j,k);
64 inc(j);
65 inc(ans[sum(j,a)]);
66 insert(j,a);
67 end;
68 for i: = 0 to n - 1 do writeln(ans[i]);
69 end.

你可能感兴趣的:(tar)