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;
}


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章