Codeforces19D Points 树套树
Primo Pan

Codeforces 19D Points

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

题目大意

在平面直角坐标系上进行一堆骚操作。Add表示添加(x,y) Remove删除(x,y) Find(x,y)表示查询(x,y)右边的最左边的上面的最下面那个点……

题解

建一棵线段树,每个叶子节点维护一棵平衡树,平衡树里记录横坐标为x的纵坐标(横坐标较大我们可以先离散化),线段树维护横坐标在这个区间里的纵坐标的最大值,所以查询的时候只要这个线段树结点维护的key小于我们要找到y这个区间里就没有解,在key值符合条件的情况下找横坐标最左边的一个点,找到这个横坐标再在平衡树里找恰好比y大的那个点即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
#define maxn (int)2e5+10
#define lson o<<1
#define rson o<<1|1
struct Opt{
char s[10];
int x,y;
}opt[maxn];
struct SGT{
int l,r,key;
}T[maxn<<2];
#define ll long long
int n,a[maxn],b[maxn],c[maxn];
set<int> S[maxn];
set<int>::iterator it;
inline void build(int o,int l,int r)
{
if (l>r) return ;
T[o].l=l;T[o].r=r;T[o].key=-1;
if (l==r)return ;
int mid=(l+r)>>1;
build(lson,l,mid);build(rson,mid+1,r);
T[o].key=max(T[lson].key,T[rson].key);
}
inline void update(int o,int x)
{
int mid=(T[o].l+T[o].r)>>1;
if (T[o].l==T[o].r && T[o].l==x)
{
if (S[T[o].l].size())
{
it=(--S[T[o].l].end());
T[o].key=*it;
}
else T[o].key=-1;
return ;
}
if (mid>=x) update(lson,x);
else update(rson,x);
T[o].key=max(T[lson].key,T[rson].key);
}
inline int query(int o,int x,int y)
{
if (T[o].r<=x) return -1;
if (T[o].key<=y) return -1;
if (T[o].l==T[o].r) return T[o].r;
int t=query(lson,x,y);
if (t==-1) return query(rson,x,y);
return t;
}
int cnt=0,ct2;
int main()
{
cin>>n;
for (int i=1;i<=n;i++)
{
scanf("%s%d%d",opt[i].s,&opt[i].x,&opt[i].y);
a[i]=opt[i].x;
}
sort(a+1,a+1+n);
int t=unique(a+1,a+1+n)-a-1;
for (int i=1;i<=n;i++) b[i]=lower_bound(a+1,a+1+t,opt[i].x)-a;
build(1,1,maxn);
for (int i=1;i<=n;i++)
{
if (opt[i].s[0]=='a')
{
S[b[i]].insert(opt[i].y);
update(1,b[i]);
}else
if (opt[i].s[0]=='r')
{
S[b[i]].erase(opt[i].y);
update(1,b[i]);
}
else{
int ans=query(1,b[i],opt[i].y);
if (ans==-1) cout<<-1<<endl;
else{
printf("%d %d\n",a[ans],*S[ans].upper_bound(opt[i].y));
}
}
}
}