Live2d Test Env

CodeForces19D:Points(线段树+set(动态查找每个点右上方的点))

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.

Example

Input
7
add 1 1
add 3 4
find 0 0
remove 1 1
find 0 0
add 1 1
find 0 0
Output
1 1
3 4
1 1
Input
13
add 5 5
add 5 6
add 5 7
add 6 5
add 6 6
add 6 7
add 7 5
add 7 6
add 7 7
find 6 6
remove 7 7
find 6 6
find 4 4
Output
7 7
-1
5 5

题意:给定二维平面,有Q个操作。对于每个操作,输入Opt,X,Y。

          如果Opt==“add”,则加(X,Y)。

          如果Opt==“remove”,则删去(X,Y)

          否则,查找(X,Y)右上方最靠左的点,若果有多个,取Y最小,输出坐标。

思路:离散化,对X轴建立线段树,树节点保存子树的最大Y坐标值Max。利用线段树查找的思想相当于是分治吧,即,在查找(X,Y)右上方点时,只要找到X的右边部分的线段树里第一个Max>Y,得到X’,然后set二分得到Y‘就ok。

为了统一,我新习惯把单点也看成区间。

#include<set>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=200010;
int x[maxn],y[maxn],opt[maxn],a[maxn];
char chr[10]; set<int>s[maxn];
void read(int &res){
    char c=getchar(); for(;c>'9'||c<'0';c=getchar());
    for(res=0;c>='0'&&c<='9';c=getchar()) res=(res<<3)+(res<<1)+c-'0';
}
int Max[maxn<<2],ans,ht;
struct segment_tree
{
    void add(int Now,int L,int R,int l,int r,int Y)
    {
        if(l<=L&&r>=R){
            s[l].insert(Y);
            Max[Now]=*(s[l].rbegin());
            return ;
        }
        int Mid=(L+R)>>1;
        if(l<=Mid) add(Now<<1,L,Mid,l,r,Y);
        if(r>Mid) add(Now<<1|1,Mid+1,R,l,r,Y);
        Max[Now]=max(Max[Now<<1],Max[Now<<1|1]);
    }
    void del(int Now,int L,int R,int l,int r,int Y)
    {
        if(l<=L&&r>=R){
            s[l].erase(Y);
            if(s[l].empty()) Max[Now]=-1;
            else Max[Now]=*(s[l].rbegin());
            return ;
        }
        int Mid=(L+R)>>1;
        if(l<=Mid) del(Now<<1,L,Mid,l,r,Y);
        if(r>Mid) del(Now<<1|1,Mid+1,R,l,r,Y);
        Max[Now]=max(Max[Now<<1],Max[Now<<1|1]);
    }
    void find(int Now,int L,int R,int l,int r,int Y)
    {
        if(Max[Now]<=Y) return ;
        if(l<=L&&r>=R){    
            if(L==R){
                ans=L; ht=*(s[L].upper_bound(Y)); 
                return ;
            }
            int Mid=(L+R)>>1;
            find(Now<<1,L,Mid,l,r,Y);
            if(ans==-1) find(Now<<1|1,Mid+1,R,l,r,Y);
            return ;
        }
        int Mid=(L+R)>>1;
        if(l<=Mid) find(Now<<1,L,Mid,l,r,Y);
        if(ans==-1&&r>Mid) find(Now<<1|1,Mid+1,R,l,r,Y);
    }
}Tree;
int main()
{
    int N,M;
    read(N);
    for(int i=1;i<=N;i++){
        scanf("%s",chr); read(x[i]); read(y[i]);
        if(chr[0]=='r') opt[i]=-1;
        if(chr[0]=='a') opt[i]=1;
        if(chr[0]=='f') opt[i]=0;
        a[i]=x[i];
    }
    sort(a+1,a+N+1);
    M=unique(a+1,a+N+1)-(a+1);
    for(int i=1;i<=N;i++){
        int X=lower_bound(a+1,a+M+1,x[i])-a;
        if(opt[i]==1) Tree.add(1,1,M,X,X,y[i]);
        else if(opt[i]==-1) Tree.del(1,1,M,X,X,y[i]);
        else{
            ans=ht=-1;
            Tree.find(1,1,M,X+1,M,y[i]);
            if(ans==-1) printf("-1\n");
            else printf("%d %d\n",a[ans],ht);
        }
    }
    return 0;
}

 

posted @ 2018-03-12 20:59  nimphy  阅读(268)  评论(0编辑  收藏  举报