Sicily 1802. Atomic Nucleus Investigation

来源:互联网 发布:淘宝宣传软文 编辑:程序博客网 时间:2024/05/22 14:15

1802. Atomic Nucleus Investigation

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Recently the physicists have been investigating a large family of atomic nucleuses, named “X family”. They have discovered that these atomic nucleuses in X family are quite similar to each other, and can only be distinguished by their weights. And the smaller the weight difference between two kinds of nucleuses is, the more possibly one kind tend to transform to the other.

To research X family in depth, the physicists have devised a series of experiments.

As the most important foundation of these experiments is to control the generation of X family nucleuses, and figure out the instant minimum weight difference among them, the physicists feel like devising a special device which can carry out different kinds of instructions and accomplish the task above. Here are 3 kinds of instructions in desire:

- generate M

Suppose the weights of nucleuses are numbers starting from 1, and M is a positive integer, then the device should produce a nucleus weighted M. If there already exists a nucleus with the same weight, then this instruction should not be taken into account.

remove M

Suppose M is a positive integer, then the device should remove the existing nucleus weighted M. If no such nucleus exists, the instruction should also be ignored. Please note that a nucleus can exist all the same unless it’s removed by the device.

query

The device should output the instant minimum weight difference among those existing nucleuses. For example, if there exists 3 nucleuses weighted 1, 7, 10 respectively, then the minimum weight difference is 10-7=3. If there is less than 2 nucleuses with different weight, the query result should be “-1” (without quotes).

Now, your task is to write a program to simulate this special device.

Input

Input may contain multiple test cases. The first line is a positive integer t, the number of test cases below. For each test case, the input is an instruction sequence. The first line is a single integer n, (1 ≤ ≤ 100000), denoting the number of instructions in this case. And n instructions follow. Each of them is one of the three kinds of instruction as described above, where M is a positive integer no more than 100000.

Output

For each test case, each query instruction should correspond to one line of output, containing only one positive integer, which is the corresponding query result. Please output a blank line between test cases.

Sample Input

112generate 1remove 2generate 1querygenerate 7querygenerate 10querygenerate 5queryremove 7query

Sample Output

-1632

4

// Problem#: 1802// Submission#: 3591809// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <algorithm>using namespace std;const int N = 131072;const int INF = 1000000000;struct Node {    int min, max, min_diff;}node[N << 1];void init() {    for (int i = 1; i < (N << 1); i++) {        node[i].min = INF;        node[i].max = -INF;        node[i].min_diff = INF;    }}inline int query() {    return node[1].min_diff < N ? node[1].min_diff : -1;}inline int get_left_child(int p) {    return (p << 1);}inline int get_right_child(int p) {    return (p << 1) + 1;}inline int get_parent(int c) {    return (c >> 1);}inline int get_node(int m) {    return m + N - 1;}void update(int p) {    int left_child = get_left_child(p);    int right_child= get_right_child(p);    node[p].min = min(node[left_child].min, node[right_child].min);    node[p].max = max(node[left_child].max, node[right_child].max);    node[p].min_diff = min(node[left_child].min_diff, node[right_child].min_diff);    node[p].min_diff = min(node[p].min_diff, node[right_child].min - node[left_child].max);}void add(int m) {    int cur = get_node(m);    if (node[cur].min == INF) {        node[cur].min = m;        node[cur].max = m;        do {            cur = get_parent(cur);            update(cur);        } while (cur > 1);    }}void remove(int m) {    int cur = get_node(m);    if (node[cur].min < INF) {        node[cur].min = INF;        node[cur].max = -INF;        do {            cur = get_parent(cur);            update(cur);        } while (cur > 1);    }}int main() {    int casen;    scanf("%d", &casen);    while (casen--) {        init();        int n;        scanf("%d", &n);        for (int i = 0; i < n; i++) {            char cmd[10];            scanf("%s", cmd);            if (cmd[0] == 'q') printf("%d\n", query());            else {                int m;                scanf("%d", &m);                if (cmd[0] == 'g') add(m);                else remove(m);            }        }        if (casen > 0) puts("");    }    return 0;}                                 


0 0