Http://codeforces.com/problemset/problem/19/D
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) was located in The Bottom-left corner, Ox axis is directed right, Oy axis was 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 the 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 the 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 giv es its coordinates to Pete.
Bob managed to answer the requests, when they were, or, if their amount grew up to 2 105 Bob failed to cope. Now he needs a program that would 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, find x y-the request to find the bottom-left point. All the coordinates in the input file is non-negative and don ' t exceed 9.
Output
For each request of type ' find x y output in a separate line ' the answer to It-coordinates of the bottommost Amon G The leftmost marked points, lying strictly above and to the right of point (x,? Y). If There is no points strictly above and to the right of a point (x,? Y), Output -1.
Sample Input
Input
7add 1 1add 3 4find 0 0remove 1 1find 0 0add 1 1find 0 0
Output
1 13 41 1
Input
13add 5 5add 5 6add 5 7add 6 5add 6 6add 6 7add 7 5add 7 6add 7 7find 6 6remove 7 7find 6 6find 4 4
Output
7 7-15 5
/**CF 19D Segment Tree +stl topic: In the first quadrant of a planar Cartesian coordinate system, a series of points are deleted, added, and queried. The query is returned (x, y) has the top left most bottom point of the coordinates, if there is no return-1 problem-solving ideas: There is a single point of use line tree Update and single-point query, for all x-axis of the same point into a set, to determine the x directly after the Upper_bound is the corresponding point of the ordinate */# Include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <set >using namespace Std;const int maxn=200005;struct note{int mark,x,y;} e[maxn];int N,num[maxn];char A[10];set <in t>s[maxn];struct node{int l,r; int max_y;} Tree[4*maxn];void push_up (int root) {Tree[root].max_y=max (tree[root<<1].max_y,tree[root<<1|1].max_y);} void build (int l,int r,int root) {tree[root].l=l; Tree[root].r=r; Tree[root].max_y=-1; if (l==r) return; int mid= (L+R) >>1; Build (l,mid,root<<1); Build (mid+1,r,root<<1|1);} void Update (int x,int root) {if (X==TREE[ROOT].L&&X==TREE[ROOT].R) {if (S[x].size ()) tree[r oot].max_y=* (--s[x].end ()); else Tree[root].max_y=-1; Return } int mid= (TREE[ROOT].L+TREE[ROOT].R) >>1; if (x<=mid) update (X,ROOT<<1); else update (X,ROOT<<1|1); Push_up (root);} int query (int x,int y,int root) {if (tree[root].r<=x) return-1; if (tree[root].max_y<=y) return-1; if (TREE[ROOT].L==TREE[ROOT].R) return TREE[ROOT].L; int T=query (x,y,root<<1); if (t==-1) t=query (x,y,root<<1|1); return t;} int main () {while (~SCANF ("%d", &n)} {for (int i=1; i<=n; i++) {scanf ("%s%d%d", A,& ; e[i].x,&e[i].y); if (a[0]== ' a ') e[i].mark=1; else if (a[0]== ' R ') e[i].mark=2; else e[i].mark=3; S[i].clear (); num[i]=e[i].x; } sort (num+1,num+1+n); int m = Unique (num+1, num+1+n)-(num+1);///go to Heavy, return address build (1,m,1) of the next iterator to the last valid point; for (int i=1; i<=n; i++) {int X=upper_bound (num+1,num+1+m,e[i].x)-(num+1); int y=e[i].y; if (e[i].mark==1) {S[x].insert (y); Update (x,1); } else if (e[i].mark==2) {s[x].erase (y); Update (x,1); } else {int ans=query (x,y,1); if (Ans==-1) puts ("-1"); else {printf ("%d%d\n", Num[ans],*s[ans].upper_bound (y)); }}}} return 0;}
cf19d segment Tree +stl various applications