Points

https://codeforces.com/problemset/problem/19/D

问题描述

Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coordinate system on it as follows: point (0, 0) is located in the bottom-left corner, Ox axis is directed right, Oy axis is directed up. Pete gives Bob requests of three types:

add x y — on the sheet of paper Bob marks a point with coordinates (x, y). For each request of this type it’s guaranteed that point (x, y) is not yet marked on Bob’s sheet at the time of the request.
remove x y — on the sheet of paper Bob erases the previously marked point with coordinates (x, y). For each request of this type it’s guaranteed that point (x, y) is already marked on Bob’s sheet at the time of the request.
find x y — on the sheet of paper Bob finds all the marked points, lying strictly above and strictly to the right of point (x, y). Among these points Bob chooses the leftmost one, if it is not unique, he chooses the bottommost one, and gives its coordinates to Pete.
Bob managed to answer the requests, when they were 10, 100 or 1000, but when their amount grew up to 2·105, Bob failed to cope. Now he needs a program that will answer all Pete’s requests. Help Bob, please!

Input

The first input line contains number n (1 ≤ n ≤ 2·105) — amount of requests. Then there follow n lines — descriptions of the requests. add x y describes the request to add a point, remove x y — the request to erase a point, find x y — the request to find the bottom-left point. All the coordinates in the input file are non-negative and don’t exceed 109.

Output

For each request of type find x y output in a separate line the answer to it — coordinates of the bottommost among the leftmost marked points, lying strictly above and to the right of point (x, y). If there are no points strictly above and to the right of point (x, y), output -1.

问题分析

问题大意是 给定一些点和n个操作:
add(x,y) : 添加一个新点
remove(x,y) : 删除一个点
find(x,y) : 查找一个点(x’,y’),满足x’ > x and y’ > y.并且满足x’ 和 y’尽量小。

本题主要的思路,是用一个set数组保存每一个 x 对应的 y 的集合,然后用线段树来维护 x 区间内最大的y。然后利用二分法来求出满足条件的x。求出x时,就可以用set中的upper_bound求出满足条件的y。

AC代码


/*
author:Manson
date:8.18.2018
theme: 线段树 
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+5;

struct edge{
    int x;
    int y;
    int mark;
}e[N];

struct node{
    int left;
    int right;
    //存储y的最大值 
    int y;
}a[4*N];

int n,m;
//存储 x 的值 
int num[N];
//y 的值 
set<int>se[N]; 

void push_up(int root){
    a[root].y = max(a[root*2].y,a[root*2+1].y);
}

void build(int root,int l,int r){
    a[root].left = l;
    a[root].right = r;
    a[root].y = -1;
    if(a[root].left == a[root].right){
        return;
    }
    int mid = (l+r)/2;
    //建立左子树 
    build(root*2,l,mid);
    //建立右子树
    build(root*2+1,mid+1,r); 
}

void update(int root,int x){
    if(a[root].left == a[root].right){
        if(se[x].size()){
            a[root].y = *(--se[x].end());
        }
        else{
            a[root].y = -1;
        }
        return;
    }
    int mid = (a[root].left+a[root].right)/2;
    //更新左子树 
    if(x <= mid){
        update(root*2,x);
    }
    //更新右子树 
    else{
        update(root*2+1,x);
    }
    push_up(root);
}

int query(int root,int x,int y){
    if(a[root].right <= x){
        return -1;
    }
    if(a[root].y <= y){
        return -1;
    }
    if(a[root].left == a[root].right){
        return a[root].left;
    }
    int t = query(root*2,x,y);
    if(t == -1){
        t = query(root*2+1,x,y);
    }
    return t;
}

void solve(){
    sort(num,num+m);
    m = unique(num,num+m)-(num);
    build(1,0,m);

    for(int i = 0;i < n;i++){
        int x = upper_bound(num,num+m,e[i].x)-num;
        int y = e[i].y;
        //add
        if(e[i].mark == 1){
            se[x].insert(y);
            update(1,x);
        }
        //remove
        else if(e[i].mark == 2){
            se[x].erase(y);
            update(1,x);
        }
        //find
        else{
            int ans = query(1,x,y);
            if(ans == -1){
                cout<<"-1"<<endl;
            }
            else{
                cout<<num[ans-1]<<" "<<*se[ans].upper_bound(y)<<endl;;
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    m = 0;
    cin>>n;
    char s[10];
    for(int i = 0;i < n;i++){
        cin>>s>>e[i].x>>e[i].y;
        if(s[0] == 'a'){
            e[i].mark = 1;
        }
        else if(s[0] == 'r'){
            e[i].mark = 2;
        }
        else{
            e[i].mark = 3;
        }
        se[m].clear();
        num[m++] = e[i].x;
    }
    solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值