Codeforces Beta Round #19D(Points)线段树

时间:2021-02-09 14:29:54
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
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
 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <set>
#include <map>
#include <vector>
#include <queue>
#define llt long long int
#define ml(x, y) ((x + y) >> 1)
#define mr(x, y) (((x + y) >> 1) + 1)
#define pl(p) (p << 1)
#define pr(p) ((p << 1) | 1)
#define N 200005
using namespace std;
map<int, int> mp;//离散化使用
vector<int> tx;//额外记录x
int segtree[N << ];//维护该区间的最大值,查询用
set<int> ty[N];//存对应x的所有y值
struct node
{
int x, y;
char op[];
}point[N];
void build(int l, int r, int p)//初始化线段树
{
segtree[p] = ;
if (l < r)
{
build(l, ml(l, r), pl(p));
build(mr(l, r), r, pr(p));
}
else
ty[l].clear();
}
void update(int l, int r, int p, int x, int y, int tag)
{
if (l == r)
{
if (tag)//add操作
{
ty[l].insert(y);
if (segtree[p] < y)
segtree[p] = y;
}
else
{
ty[l].erase(y);
segtree[p] = ty[l].empty() ? : *(--ty[l].end());
}
return;
}
if (ml(l, r) >= x)
update(l, ml(l, r), pl(p), x, y, tag);
else
update(mr(l, r), r, pr(p), x, y, tag);
segtree[p] = max(segtree[pl(p)], segtree[pr(p)]);
}
pair<int, int> query(int l, int r, int p, int x, int y)
{
if (r < x || segtree[p] < y)//未找到
return make_pair(-, -);
if (r == l)//找到了最合适的
return make_pair(r, *ty[r].lower_bound(y));
pair<int, int> ans = query(l, ml(l, r), pl(p), x, y);//先往左找
if (ans.first != -)//找到了
return ans;
return query(mr(l, r), r, pr(p), x, y);//未找到,右边找
}
int main()
{
int n, i, size;
while (~scanf("%d", &n))
{
tx.clear();
mp.clear();
for (i = ; i < n; i++)
{
scanf("%s%d%d", point[i].op, &point[i].x, &point[i].y);
tx.push_back(point[i].x);
}
sort(tx.begin(), tx.end());//排序
tx.erase(unique(tx.begin(), tx.end()), tx.end());//除去相同的元素
size = tx.size() - ;
build(, size, );
for (i = ; i <= size; i++)//进行离散化
mp[tx[i]] = i;
for (i = ; i < n; i++)
{
if (point[i].op[] == 'f')
{
pair<int, int> ans = query(, size, , mp[point[i].x] + , point[i].y + );
if (ans.first >= )
printf("%d %d\n", tx[ans.first], ans.second);
else
puts("-1");
continue;
}
update(, size, , mp[point[i].x], point[i].y, point[i].op[] == 'a'? : );
}
}
return ;
}