CodeForces 19D Points(线段树单点修改+离散化)

题意:

给了三种操作
1. add(x,y)将这个点加入二维坐标系
2. remove(x,y)将这个点从二维坐标系移除。
3. find(x,y)就是找到在(x,y)右上方的第一个点,先满足最左边第一个点,如果最左有多个点,那么满足最下面第一个点。

思路:

我们可以建立n个set以x为横坐标,那么我们这个题就转化为找一个最小的x是否存在满足条件,那么x一旦被找到,那么纵坐标就自然而然的找到了,当然更新操作就是对maxy的维护,然后查询操作就是找出一个最小的x。

解析:

还有因为n非常大,所以要采用离散化的方法。

my code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#define ls o<<1
#define rs o<<1|1
#define lson ls, L, M
#define rson rs, M+1, R
using namespace std;
const int N = (int)2e5 + 10;
struct Oper {
    int x, y;
    char cmd[10];
} oper[N];

int n, tot, arr[N];
set<int> X[N];
set<int>::iterator it;

void init() {
    tot = 0;
    for(int i = 0; i < N; i++) {
        X[i].clear();
    }
}

void discrete() {
    sort(arr, arr+tot);
    tot = unique(arr, arr+tot) - arr;
}

int maxy[N<<2];

void build(int o, int L, int R) {
    maxy[o] = -1;
    if(L == R) return ;
    int M = (L + R)/2;
    build(lson);
    build(rson);
}

inline void pushUp(int o) {
    maxy[o] = max(maxy[ls], maxy[rs]);
}

void modify(int o, int L, int R, int pos) {
    if(L == R) {
        if(X[pos].size() == 0) {
            maxy[o] = -1;
        }else {
            maxy[o] = *(--X[pos].end());
        }
        return ;
    }
    int M = (L + R)/2;
    if(pos <= M) modify(lson, pos);
    else modify(rson, pos);
    pushUp(o);
}

int query(int o, int L, int R, int ql, int qr, int limit) {
    if(maxy[o] <= limit || ql > qr) return -1;
    if(L == R) return L;
    int M = (L + R)/2;
    if(qr <= M) return query(lson, ql, qr, limit);
    else if(ql > M) return query(rson, ql, qr, limit);
    else {
        int ret = query(lson, ql, qr, limit);
        if(ret != -1) return ret;
        return query(rson, ql, qr, limit);
    }
}

int main() {
    while(scanf("%d", &n) != EOF) {
        init();
        for(int i = 0; i < n; i++) {
            scanf("%s%d%d", oper[i].cmd, &oper[i].x, &oper[i].y);
            arr[tot++] = oper[i].x;
        }
        discrete();
        build(1, 0, tot);

        int x, y, val;
        for(int i = 0; i < n; i++) {
            x = lower_bound(arr, arr+tot, oper[i].x) - arr;
            if(oper[i].cmd[0] == 'a') {
                X[x].insert(oper[i].y);
                modify(1, 0, tot, x);
            }else if(oper[i].cmd[0] == 'r') {
                it = X[x].find(oper[i].y);
                X[x].erase(it);
                modify(1, 0, tot, x);
            }else {
                int ret = query(1, 0, tot, x + 1, tot, oper[i].y);
                if(ret == -1) {
                    puts("-1");
                }else {
                    it = X[ret].upper_bound(oper[i].y);
                    if(it == X[ret].end()) {
                        puts("-1");
                    }else {
                        printf("%d %d\n", arr[ret], *it);
                    }
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值