码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 19D(线段树+离散化)

时间:2015-08-14 21:36:26      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

题意:题意很好理解,三种操作: (1)add x y,添加坐标(x,y),(2)del x y,删除坐标(x,y),(3)find x y,找到并输出严格大于(x,y)的坐标里的最小坐标。
题解:因为点有200000个,而点坐标最大到1e9,所以要离散化处理,我们只需要对x离散化,然后每个x对应一个set存y,然后每次修改操作的时候,根据set内的值维护最大y。

#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
const int N = 200100;
struct Point {
    int x, y;
    Point(int a = -1, int b = -1): x(a), y(b) {}
}P[N];
set<int> py[N];
struct Tree {
    int maxp;
}tree[N << 2];
int n, num[N], res;
char str[N][10];

void build(int k, int left, int right) {
    tree[k].maxp = -1;
    if (left != right) {
        int mid = (left + right) / 2;
        build(k * 2, left, mid);
        build(k * 2 + 1, mid + 1, right);
    }
}

void pushup(int k) {
    tree[k].maxp = max(tree[k * 2].maxp, tree[k * 2 + 1].maxp);
}

void modify(int k, int left, int right, int pos) {
    if (left == right) {
        if (py[pos].size())
            tree[k].maxp = *(--py[pos].end());
        else
            tree[k].maxp = -1;
        return;
    }
    int mid = (left + right) / 2;
    if (pos <= mid)
        modify(k * 2, left, mid, pos);
    else
        modify(k * 2 + 1, mid + 1, right, pos);
    pushup(k);
}

void query(int k, int left, int right, int pos, int p) {
    if (right <= pos)
        return;
    if (tree[k].maxp <= p)
        return;
    if (left == right) {
        res = left;
        return;
    }
    int mid = (left + right) / 2;
    query(k * 2, left, mid, pos, p);
    if (res == -1)
        query(k * 2 + 1, mid + 1, right, pos, p);
}

int main() {
    scanf("%d", &n);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%s%d%d", str[i], &P[i].x, &P[i].y);
        if (str[i][0] == ‘a‘)
            num[++cnt] = P[i].x;
    }
    sort(num + 1, num + 1 + cnt);
    cnt = unique(num + 1, num + 1 + cnt) - (num + 1);
    for (int i = 1; i <= cnt; i++)
        py[i].clear();
    if (cnt == 0)
        tree[1].maxp = -1;
    else
        build(1, 1, cnt);
    for (int i = 1; i <= n; i++) {
        int pos = upper_bound(num + 1, num + 1 + cnt, P[i].x) - (num + 1);
        if (str[i][0] == ‘a‘) {
            py[pos].insert(P[i].y);
            modify(1, 1, cnt, pos);
        }
        else if (str[i][0] == ‘r‘) {
            py[pos].erase(P[i].y);
            modify(1, 1, cnt, pos);
        }
        else {
            res = -1;
            if (pos <= cnt)
                query(1, 1, cnt, pos, P[i].y);
            if (res == -1)
                printf("-1\n");
            else
                printf("%d %d\n", num[res], *py[res].upper_bound(P[i].y));
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 19D(线段树+离散化)

标签:

原文地址:http://blog.csdn.net/hyczms/article/details/47666485

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!