Stars poj 2352 treap

来源:互联网 发布:达芬奇 知乎 编辑:程序博客网 时间:2024/06/05 09:52

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.

Hint


This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

Solution


因为y是有序的,那么按照输入顺序每次查询比x小的x有多少个,然后插入就行了。照理说这里线段树就可以但是我打了treap

所谓treap,就是一颗二叉搜索树同时满足堆的性质。我们知道二叉搜索树任意节点都大于左子树中的节点,小于右子树中的节点,这样二叉搜索树的中序遍历就是有序的了。为了保证这样一棵树的树高尽可能平衡,我们给每个点赋值一个随机的权prio代表优先级,并按照优先级做旋转操作使得满足堆的性质,这样期望的树高就是logn的了。查询就记录一个size,递归的做就行

此题无删除操作。

Code


#include <stdio.h>#include <string.h>#include <time.h>#include <stdlib.h>#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)#define N 20005struct treeNode {int key, prio, size, son[2];} t[N];int ans[N];int cnt = 1, root = 0;int read() {    char ch = getchar(); int x = 0;    for (; ch < '0' || ch > '9'; ch = getchar());    for (; ch <= '9' && ch >= '0'; x = x * 10 + ch - '0', ch = getchar());    return x;}void push_up(int x) {    t[x].size = t[t[x].son[0]].size + t[t[x].son[1]].size + 1;}void rotate(int p, int &x) {    int y = t[x].son[!p];    t[x].son[!p] = t[y].son[p];    t[y].son[p] = x;    push_up(x);    push_up(y);    x = y;}void insert(int key, int &x) {    if (x == 0) {        t[x = cnt ++] = (treeNode) {key, rand(), 1, {0, 0}};        return ;    } else {        t[x].size += 1;        int p = key <= t[x].key;        insert(key, t[x].son[!p]);        if (t[x].prio > t[t[x].son[!p]].prio) {            rotate(p, x);        }    }}int query(int key, int x) {    if (x == 0) {        return 0;    } else if (t[x].key <= key) {        return t[t[x].son[0]].size + 1 + query(key, t[x].son[1]);    } else {        return query(key, t[x].son[0]);    }}int main(void) {    int n = read();    rep(i, 1, n) {        int x = read();        int y = read();        ans[query(x, root)] += 1;        insert(x, root);    }    rep(i, 0, n - 1) {        printf("%d\n", ans[i]);    }    return 0;}
原创粉丝点击