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
Sample Input
add 1 1
add 3 4
find 0 0
remove 1 1
find 0 0
add 1 1
find 0 0
Sample Output
3 4
1 1
HINT
题意
在一个直角坐标系内,每次有三种操作
1.插入一个点(x,y)保证(x,y)未插入过
2.删除一个点(x,y)保证(x,y)插入过
3.询问点(x,y)右上角所有点最靠左边,然后最靠下面的点的位置
题解:
我们可以用set降维,就是每个叶子几点有一个set存y值,然后我们可以记录一个max,存最大的y值,这样对于每次询问(x,y),问题转化成求x点右边第一个max比y大的位置,
然后再用lower_bound求set中刚好大于它的y值。感觉好像也不是很难。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define N 500050 4 int n,cnt,kth[N]; 5 struct Query{int l,r;char id;}que[N]; 6 struct Tree{int l,r,max;set<int>sx;}tr[N<<2]; 7 template<typename T>void read(T&x) 8 { 9 int k=0;char c=getchar(); 10 x=0; 11 while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar(); 12 if (c==EOF)exit(0); 13 while(isdigit(c))x=x*10+c-'0',c=getchar(); 14 x=k?-x:x; 15 } 16 17 void read_char(char &c) 18 {while(!isalpha(c=getchar()));}; 19 void push_up(int x) 20 { 21 tr[x].max=max(tr[x<<1].max,tr[x<<1|1].max); 22 } 23 void bt(int x,int l,int r) 24 { 25 //tr[x]=Tree{l,r,0}; 26 tr[x].max=0; 27 tr[x].l=l; tr[x].r=r; 28 if (l==r)return; 29 int mid=(l+r)>>1; 30 bt(x<<1,l,mid); 31 bt(x<<1|1,mid+1,r); 32 } 33 void add(int x,int p,int tt) 34 { 35 if (tr[x].l==tr[x].r) 36 { 37 tr[x].sx.insert(tt); 38 tr[x].max=max(tr[x].max,tt); 39 return; 40 } 41 int mid=(tr[x].l+tr[x].r)>>1; 42 if (p<=mid)add(x<<1,p,tt); 43 if (mid<p)add(x<<1|1,p,tt); 44 push_up(x); 45 } 46 void del(int x,int p,int tt) 47 { 48 if (tr[x].l==tr[x].r) 49 { 50 tr[x].sx.erase(tt); 51 tr[x].max=tr[x].sx.empty()?0:*(--tr[x].sx.end()); 52 return; 53 } 54 int mid=(tr[x].l+tr[x].r)>>1; 55 if (p<=mid)del(x<<1,p,tt); 56 if (mid<p)del(x<<1|1,p,tt); 57 push_up(x); 58 } 59 pair<int,int> query(int x,int p,int tt) 60 { 61 if (tr[x].max<tt)return make_pair(-1,-1); 62 if (tr[x].l==tr[x].r) 63 return make_pair(tr[x].l,*(tr[x].sx.lower_bound(tt))); 64 int mid=(tr[x].l+tr[x].r)>>1; 65 if (p>mid)return query(x<<1|1,p,tt); 66 else 67 { 68 pair<int,int> tp=query(x<<1,p,tt); 69 return tp.first==-1?query(x<<1|1,p,tt):tp; 70 } 71 } 72 int main() 73 { 74 #ifndef ONLINE_JUDGE 75 freopen("aa.in","r",stdin); 76 #endif 77 read(n); 78 for(int i=1;i<=n;i++) 79 { 80 read_char(que[i].id); 81 read(que[i].l); 82 read(que[i].r); 83 kth[++cnt]=que[i].l; 84 kth[++cnt]=que[i].r; 85 } 86 sort(kth+1,kth+cnt+1); 87 cnt=unique(kth+1,kth+cnt+1)-kth-1; 88 bt(1,1,cnt); 89 for(int i=1;i<=n;i++) 90 { 91 int p=lower_bound(kth+1,kth+cnt+1,que[i].l)-kth; 92 int tt=lower_bound(kth+1,kth+cnt+1,que[i].r)-kth; 93 if (que[i].id=='a')add(1,p,tt); 94 if (que[i].id=='r')del(1,p,tt); 95 if (que[i].id=='f') 96 { 97 pair<int,int> ans=query(1,p+1,tt+1); 98 if (ans.first==-1)printf("-1\n"); 99 else 100 printf("%d %d\n",kth[ans.first],kth[ans.second]); 101 } 102 } 103 }