codeforces 19D D. Points 树套树
D. Points
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/19/problem/DDescription
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
add 1 1
add 3 4
find 0 0
remove 1 1
find 0 0
add 1 1
find 0 0
Sample Output
1 1
3 4
1 1
3 4
1 1
HINT
题意
二维插入删除,找右上角的点
题解:
线段树套set
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <set> #include <map> #include <vector> #include <algorithm> using namespace std; #define LL(x) (x<<1) #define RR(x) (x<<1|1) const int N=2e5+5; vector<int> sx; map<int,int> imap; struct OP { char str[10]; int x,y; void input() { scanf("%s%d%d",str,&x,&y); sx.push_back(x); } }op[N]; struct Segtree { int imax[N*4]; set<int> valu[N]; void clear() { memset(imax,-1,sizeof(imax)); for(int i=0;i<N;i++) valu[i].clear(); } void add(int st,int ed,int ind,int x,int y) { imax[ind]=max(imax[ind],y); if(st==ed) valu[x].insert(y);//注意这里是x else { int mid=st+(ed-st)/2; if(x<=mid) add(st,mid,LL(ind),x,y); else add(mid+1,ed,RR(ind),x,y); } } void remove(int st,int ed,int ind,int x,int y) { if(st==ed) { valu[x].erase(y); imax[ind]=valu[x].empty()?-1:*(--valu[x].end()); } else { int mid=st+(ed-st)/2; if(x<=mid) remove(st,mid,LL(ind),x,y); else remove(mid+1,ed,RR(ind),x,y); imax[ind]=max(imax[LL(ind)],imax[RR(ind)]); } } pair<int,int> find(int st,int ed,int ind,int x,int y) { if(imax[ind]<y||ed<x) return make_pair(-1,-1); if(st==ed) return make_pair(st,*(valu[st].lower_bound(y))); else { int mid=st+(ed-st)/2; pair<int,int> tmp=find(st,mid,LL(ind),x,y); if(tmp.first!=-1) return tmp; return find(mid+1,ed,RR(ind),x,y); } } }seg; int main() { int n; while(scanf("%d",&n)!=EOF) { sx.clear();imap.clear();seg.clear(); for(int i=0;i<n;i++) op[i].input(); sort(sx.begin(),sx.end()); sx.erase(unique(sx.begin(),sx.end()),sx.end()); int len=(int)sx.size()-1; for(int i=0;i<=len;i++) imap[sx[i]]=i; for(int i=0;i<n;i++) { char ch=op[i].str[0]; int x=op[i].x,y=op[i].y; if(ch=='a') seg.add(0,len,1,imap[x],y); else if(ch=='r') seg.remove(0,len,1,imap[x],y); else { pair<int,int> res=seg.find(0,len,1,imap[x]+1,y+1); if(res.first==-1) puts("-1"); else printf("%d %d\n",sx[res.first],res.second); } } } return 0; }