CF 19D 线段树改点求点
Description
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.
Sample Input
7 add 1 1 add 3 4 find 0 0 remove 1 1 find 0 0 add 1 1 find 0 0
1 1 3 4 1 1
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
7 7 -1 5 5
题意:给一个坐标系,有三种操作,add是加一个点,remove是删除一个点,find是找一个位于这个点严格右上方的点,使x尽可能的小,假如最小x上的点有多个,那么优先选取y最小的点,如果不存在这么一个点,输出-1.
解题思路:首先对x进行离散化,按x建树,节点保存这个x上的y最大值,如果这个x上没有一个点,设最大值为-1.然后进行线段树单点操作。
代码:
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<set> using namespace std; const int maxn=220222; #define L(x) 2*x #define R(x) 2*x+1 struct node { int l,r,mx; int mid(){return (l+r)>>1;} }tree[5*maxn]; struct ask { int x,y; int op; }pp[maxn]; set<int> s[maxn]; set<int>::iterator it; int a[maxn]; void pushup(int p) { tree[p].mx=max(tree[L(p)].mx,tree[R(p)].mx); } void build(int p,int l,int r) { tree[p].l=l; tree[p].r=r; tree[p].mx=-1; if(l==r)return; int m=tree[p].mid(); build(L(p),l,m); build(R(p),m+1,r); pushup(p); } void update(int p,int pos) { if(tree[p].l==tree[p].r) { tree[p].mx=(s[pos].empty())?-1:(*(--s[pos].end())); return; } int m=tree[p].mid(); if(pos<=m)update(L(p),pos); else update(R(p),pos); pushup(p); } int query(int p,int l,int r,int y) { if(l>r) return -1; if(tree[p].mx<=y) return -1; if(tree[p].l==tree[p].r) return tree[p].l; int m=tree[p].mid(); if(r<=m) return query(L(p),l,r,y); else if(l>m) return query(R(p),l,r,y); else { int t=query(L(p),l,m,y); if(t>=0) return t; return query(R(p),m+1,r,y); } } int main() { int i,j,k,m,n,p,q; char str[10]; while(~scanf("%d",&n)) { int indexx=0; for(i=1;i<=n;i++) { scanf("%s%d%d",str,&pp[i].x,&pp[i].y); a[indexx++]=pp[i].x; if(strcmp(str,"add")==0)pp[i].op=1; else if(strcmp(str,"remove")==0)pp[i].op=2; else pp[i].op=3; //cout<<pp[i].op<<endl; } sort(a,a+indexx); indexx=unique(a,a+indexx)-a; //for(i=0;i<indexx;i++)cout<<a[i]<<" ";cout<<endl; //cout<<indexx<<endl; for(i=0;i<=indexx;i++)s[i].clear(); build(1,0,indexx-1); for(i=1;i<=n;i++) { j=lower_bound(a,a+indexx,pp[i].x)-a; // cout<<j<<endl; // cout<<"j="<<j<<" "<<pp[i].x<<endl; if(pp[i].op==1)s[j].insert(pp[i].y),update(1,j); else if(pp[i].op==2) { s[j].erase(s[j].find(pp[i].y)); update(1,j); } else { k=query(1,j+1,indexx-1,pp[i].y); //cout<<"k="<<k<<" "<<a[k]<<endl; // cout<<*(s[j].begin())<<endl; if(k==-1)puts("-1"); else { it=s[k].upper_bound(pp[i].y); if(it==s[k].end())puts("-1"); else printf("%d %d\n",a[k],*it); } } } // j=lower_bound(a,a+indexx,10)-a; // for(it=s[j].begin();it!=s[j].end();it++)cout<<(*it)<<" ";cout<<endl; } return 0; } /* 10 add 5 7 add 2 1 add 8 8 add 5 10 add 2 5 find 7 5 find 8 3 find 2 2 find 5 4 find 2 6 */

浙公网安备 33010602011771号