1. 程式人生 > >codeforces 19D D. Points 樹套樹

codeforces 19D D. Points 樹套樹

math div lower space truct axis memory clas 大於

D. Points

Time Limit: 1 Sec Memory Limit: 256 MB

題目連接

http://codeforces.com/contest/19/problem/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) 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


Sample Output

1 1

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 }
View Code

codeforces 19D D. Points 樹套樹