# hdu 3627 Giant For 离散化+线段树 在一个二维平面内任意的添加一个点和删除一个点，然后来寻找一个横纵坐标都比已经添加的点大的点，首先选择横坐标最小的，其次是纵坐标最小的

Problem Description
It is known to us all that YY and LMY are mathematics lovers. They like to find and solve interesting mathematic problems together. Now LMY designs a game for matrix. There is a large matrix whose rows and columns are both not more than 1000000000. And there are three operations for the matrix:
1) add: Mark an element in the matrix. It is guaranteed that the element has not been marked before.
2) remove: Delete an element’s mark. It is guaranteed that the element has been marked before.
3) find: For a given element’s row and column, return a marked element’s row and column, where the marked element’s row and column are larger than the given element’s row and column respectively. If there are multiple solutions, return the element whose row is the smallest; and if there are still multiple solutions, return the element whose column is the smallest. If there is no solution, return -1.
LMY lets YY develop a program to solve the problem. Could you also develop a program to solve the problem?

Input
The input consists of multiple test cases. For each test case, the first line contains only one integer n. n ≤ 200000. Each of the next n lines describes an operation. There is a blank line between two consecutive test cases.
End of input is indicated by a line containing a zero.

Output
Start each test case with "Case #:" on a single line, where # is the case number starting from 1. For each “find” operation, output the result. The format is showed as sample output. There is a blank line between two consecutive test cases.

Sample Input
5 add 48 1 add 25 69 add 88 52 remove 25 69 add 23 89 10 add 47 23 find 66 83 find 27 73 add 84 97 find 10 58 remove 47 23 add 41 89 remove 41 89 find 65 68 add 25 41 0

Sample Output
Case 1: Case 2: -1 -1 84 97 84 97

//

#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
using namespace std;
#define MAXN 200000

int sx[MAXN], tot;

set<int> sy[MAXN];

struct Node {
int _a, _b;
Node *_left, *_right;
int _max_y;
} nodes[MAXN * 2], *ptr;

void build_tree(int a, int b) {
Node* root = ++ptr;
root->_a = a; root->_b = b; root->_max_y = -1;
if (root->_a == root->_b) {
root->_left = root->_right = 0;
return;
}
root->_left = ptr + 1;
build_tree(a, (a + b) / 2);
root->_right = ptr + 1;
build_tree((a + b) / 2 + 1, b);
}

int lx, ly;
char lo;

void modify(Node* root) {
if (root->_a == root->_b) {
if (lo == 'a') {
sy[root->_a].insert(ly);
root->_max_y = *sy[root->_a].rbegin();
} else {
sy[root->_a].erase(ly);
if (sy[root->_a].size() == 0)
root->_max_y = -1;
else
root->_max_y = *sy[root->_a].rbegin();
}
return;
}
int mid = (root->_a + root->_b) / 2;
if (lx <= sx[mid])
modify(root->_left);
else
modify(root->_right);
root->_max_y = max(root->_left->_max_y, root->_right->_max_y);
}

int la, lb;

int query(Node* root) {
if (la <= root->_a && root->_b <= lb)
return root->_max_y;
int mid = (root->_a + root->_b) / 2, res = -1;
if (la <= mid)
res = query(root->_left);
if (mid < lb)
res = max(res, query(root->_right));
return res;
}

int get_x_index(int x, int y) {
int pos = upper_bound(sx, sx + tot, x) - sx, r = tot - 1, mid, l = pos - 1;
if (pos == tot)
return -1;
la = pos, lb = tot - 1;
if (query(nodes + 1) <= y)
return -1;
while (l + 1 != r) {
mid = (l + r) / 2;
la = pos; lb = mid;
if (query(nodes + 1) > y)
r = mid;
else
l = mid;
}
return r;
}

struct QNode {
int _x, _y;
char _opt[10];
} q[MAXN];

int main() {
int n, cas = 0;
while (scanf("%d", &n) != EOF) {
if (n == 0) break;
tot = 0;
for (int i = 0; i < n; ++i) {
scanf("%s%d%d", q[i]._opt, &q[i]._x, &q[i]._y);
if (q[i]._opt[0] == 'a')
sx[tot++] = q[i]._x;
}
sort(sx, sx + tot);
tot = unique(sx, sx + tot) - sx;
ptr = nodes;
build_tree(0, tot - 1);

if (cas)
putchar('\n');
printf("Case %d:\n", ++cas);
int idx;
for (int i = 0; i < n; ++i)
switch (q[i]._opt[0]) {
case 'a': case 'r':
lx = q[i]._x;
ly = q[i]._y;
lo = q[i]._opt[0];
modify(nodes + 1);
break;
case 'f':
idx = get_x_index(q[i]._x, q[i]._y);
if (idx == -1)
puts("-1");
else
printf("%d %d\n", sx[idx], *sy[idx].upper_bound(q[i]._y));
}

for (int i = 0; i < tot; ++i)
sy[i].clear();
}
return 0;
}

#### POJ 3241 Object Clustering 二维平面曼哈顿距离最小生成树

2015-05-14 18:32:11

#### 某个点到其他点的曼哈顿距离之和最小（HDU4311）

2014-10-31 20:36:43

#### 数轴上一个点到其他点距离之和最小问题

2016-03-24 10:29:45

#### 2017.2.25【初中部 提高组】模拟赛B组 最短路径(path) 题解

2017-03-01 19:44:49

#### C++第11周项目1、2 - 点类派生出直线类

2013-05-10 14:58:12

#### 平面上有2N+1个点,如何画一个圆,使得N个点在圆内,N个点在圆外,一个点在圆上

2015-03-23 14:22:20

#### (C++)类与对象2-1 Point类的定义

2016-09-21 15:01:18

#### HDU 3548 Enumerate the Triangles（找周长最小的三角形）

2012-07-15 09:24:30

#### 数字之魅：寻找二维平面上的最近的点对

2016-07-12 16:42:34

#### 定义点类--以点类为基类--派生出直线类

2013-05-21 09:17:04