ural1028

http://hi.baidu.com/raulliubo/blog/item/fd52a338aecc22c6d4622579.html

 

1028. Stars

Time Limit: 0.25 second
Memory Limit: 16 MB

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 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
output
5
            1 1
            5 1
            7 1
            3 3
            5 5
1
            2
            1
            1
            0

Problem Author: Pavel Zaletsky
Problem Source: Ural Collegiate Programming Contest '99

练习数据结构的好题。

OI恢复,小试牛刀,学习高级数据结构ing

先来个线段树的。

====================================强大的分割线===================================

my ugly code:

//using st
var
    left, right, lch, rch, key : array[0 .. 80000] of longint;
    x, y, level : array[0 .. 15000] of longint;
    n, i, tot, maxx : longint;

procedure buildtree(now : longint);
begin
    if left[now] = right[now] then exit;
    inc(tot);
    lch[now] := tot;
    left[tot] := left[now];
    right[tot] := (left[now] + right[now]) shr 1;
    buildtree(tot);
    inc(tot);
    rch[now] := tot;
    left[tot] := right[lch[now]] + 1;
    right[tot] := right[now];
    buildtree(tot);
end;

procedure insert(value : longint);
var
    j : longint;
begin
    j := 1;
    repeat
        inc(key[j]);
        if value <= right[lch[j]] then j := lch[j]
        else j := rch[j];
    until j = 0;
end;

function find(now, l, r : longint) : longint;
var
    ans : longint;
begin
    if (right[now] < l) or (left[now] > r) then exit(0);
    if (right[now] <= r) and (left[now] >= l) then exit(key[now]);
    ans := find(lch[now], l, r) + find(rch[now], l, r);
    exit(ans);
end;

begin
    readln(n);
    for i := 1 to n do begin
        readln(x[i], y[i]);
        if x[i] > maxx then maxx := x[i];
    end;
    tot := 1;
    left[tot] := 0; right[tot] := maxx;
    buildtree(tot);
    for i := 1 to n do begin
        inc(level[find(1, 0, x[i])]);
        insert(x[i]);
    end;
    for i := 0 to n-1 do writeln(level[i]);
end.


 

posted @ 2009-01-04 12:30  jesonpeng  阅读(241)  评论(0编辑  收藏  举报