原题戳我

坡机裸提

题目描述:
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 5
Sample Output

1
2
1
1
0

英语叽里呱啦一大堆, 但是很容易理解。(看样例猜题目)

输入会给你一个n, 表示星星的个数。
接下来,
n行 {
对于每个整数i ∈ [1, n], 每行有两个数(分别代表第 i 个星的横坐标 x[i]y[i])。
}。

要求你输出等级j(j ∈ [0, n – 1] ∩ Z) 的星的个数。

这里要解释的是星的等级, 第 i 颗星表示为

$$
\sum^{n}_{j = 1}=1(x_j<=x_i \&\& y_j<=y_i \&\&i !=j)
$$

(sigma 在诸位 dalao 的教下终于打好了)
即所有横坐标和纵坐标都小于这颗星的星的个数

比如说对于这种情况(也就是样例):
[![样例](http://poj.org/images/2352_1.jpg " 样例")](http://poj.org/images/2352_1.jpg " 样例")

0 级星集 {1}
1 级星集 {2, 4}
2 级星集 {3}
3 级星集 {5}
4 级星集 ?

解法一 树状数组:
如果 (英语够好)(看样例猜)观察仔细, 可以发现输入是按纵坐标 y 排好序的。
即后输入坐标的星的纵坐标一定会大于之前的, 所以每输入一颗星(的坐标),统计之前输入的横坐标 x 星作为当前星的等级。
很自然的就想到用树状树组来维护。
(每输入一颗星, 统计横坐标小于该星的数量, 并将这个星的横坐标的位置加 1)
而且注意到横坐标范围很小,所以只需要在线处理就好了。

//树状数组
#include <cstdio>
#include <algorithm>
using namespace std;
int n, x, y;
int c[32009], lv[32009];
int lowbit(int x) {
    return x & (-x);
}
void add(int x) {
    for ( ; x <= 32009; x += lowbit(x))
        c[x]++;
}

int sum(int x) {
    int z = 0;
    for ( ; x > 0; x -= lowbit(x)) {
        z += c[x];
    }
    return z;
}

int main() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d%d", &x, &y);
        int a = sum(x+1);
        lv[a]++;
        add(x + 1);
    }
    for (int i = 0; i < n;i++)
        printf("%d\n", lv[i]);
    return 0;
}

解法二 线段树(滑稽)

但是这道题其实是出自 Treap 练习题中,所以

真 解法二 treap:
不过…………
本蒟蒻还不会用 treap,
所以…………
会了再填坑吧(大概会填)
(我果然菜的抠脚 QAQ)

两年后

KAD:我来填坑了!

关于 treap 的做法,本质上是一样的,但如果题中坐标值较大,便可以省去离散化,较方便。

直接上代码 (丑),稍后会加注释。

这道题应为随机数用了时间种子被卡了好久。(好在 kix6 大佬及时悬崖勒马,我果然是个彩笔)

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
using namespace std;

int siz[400004], w[400004], s[4000004][2], tot, pos[400004], fa[400004], root;
int ans[400004];

//int st[40004] = {0, 2, 2, 5, 1};

void up(int i) {
    siz[i] = siz[s[i][0]] + siz[s[i][1]] + 1;

    return ;
}

void spin(int i, bool lr) {
    if (root == i) root = s[i][lr];
    bool lr2 = (s[fa[i]][1] == i);

    if (fa[i]) {
        s[fa[i]][lr2] = s[i][lr];
    }
    fa[s[i][lr]] = fa[i];

    int tmp = s[s[i][lr]][!lr];
    s[s[i][lr]][!lr] = i;
    fa[i] = s[i][lr];

    if (tmp) {
        s[i][lr] = tmp;
        fa[tmp] = i;
        up(i);
        up(fa[i]);
        return ;
    }
    s[i][lr] = 0;
    up(i);
    up(fa[i]);

    return ;
}
//需要注意一下相同坐标的插入位置
void ins(int x, int &i) {
    if (!i) {
        i = ++tot;
        siz[i] = 1;
        w[i] = x;
        pos[i] = rand() % 30000;
        return ;
    }
    siz[i]++;
    if (x >= w[i]) {
        ins(x, s[i][1]);
        fa[s[i][1]] = i;
        if (pos[s[i][1]] < pos[i]) {
            spin(i, 1);
        }

    } else if (x < w[i]) {
        ins(x, s[i][0]);
        fa[s[i][0]] = i;
        if (pos[s[i][0]] < pos[i]) {
            spin(i, 0);
        }
    }
    return ;
}

int find(int x, int i) {
    if (siz[i] == 0) {
        return 0;
    }
    if (x >= w[i]) {
        return siz[s[i][0]] + 1 + find(x, s[i][1]); 
    }
    if (x < w[i]) {
        return find(x, s[i][0]);
    }
}

int main() {
    srand(1111111);
    //就是这里!用时间种子必 RE。
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        ins(x, root);//插入坐标
        int lvl = find(x, root);//查询排名
        ans[lvl]++;
    } 
    for (int i = 1; i <= n; ++i) {
        printf("%d\n", ans[i]);
    }
    return 0;
}

……其实也没什么好加注释的,就介样吧。(ε=ε=ε=┏(゜^゜;)┛逃)

分类: 文章

6 条评论

Sys_Con · 2018年1月10日 6:54 下午

[某个并没有什么用的 LaTeX 教程]http://dralpha.altervista.org/zh/tech/lnotes.pdf

    Kinandra · 2018年1月10日 10:31 下午

    并上不去啊

    膜 Master 神威

boshi · 2018年1月9日 8:18 下午

写得高深不过您可以把公式用 LATEX 写

回复 Sys_Con 取消回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注