Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct node {
- int key, prior, minv, maxv, ans, cnt;
- node *l, *r;
- };
- typedef node* pnode;
- int q, a, b;
- char c;
- int cnt(pnode t) {
- return t ? t->cnt : 0;
- }
- int choose(int A, int B) {
- if (A == -1) return B;
- if (B == -1) return A;
- return min(A, B);
- }
- void updnd(pnode t) {
- if (!t) return;
- t->cnt = 1 + cnt(t->l) + cnt(t->r);
- if (t->l && t->r) {
- t->ans = choose(t->l->ans, t->r->ans);
- int V = min(t->r->minv - t->key, t->key - t->l->maxv);
- t->ans = choose(V, t->ans);
- t->maxv = max(t->key, max(t->r->maxv, t->l->maxv));
- t->minv = min(t->key, min(t->r->minv, t->l->minv));
- } else if (!t->l && !t->r) {
- t->ans = -1, t->minv = t->maxv = t->key;
- } else {
- pnode oquepresta = t->l ? t->l : t->r;
- int V = min(abs(oquepresta->minv - t->key), abs(oquepresta->maxv - t->key));
- t->ans = choose(V, choose(t->ans, oquepresta->ans));
- t->maxv = max(t->key, oquepresta->maxv);
- t->minv = min(t->key, oquepresta->minv);
- }
- }
- pnode newnode(int v=0) {
- pnode t = (pnode)malloc(sizeof(node));
- t->l = t->r = nullptr;
- t->prior = rand();
- t->key = v;
- t->minv = t->maxv = v;
- t->ans = -1;
- t->cnt = 1;
- return t;
- }
- // <=k -> u
- void split(pnode t, pnode &u, pnode &v, int k) {
- if (!t) return void(u = v = nullptr);
- if (t->key <= k) split(t->r, t->r, v, k), u=t;
- else split(t->l, u, t->l, k), v=t;
- updnd(t);
- }
- void merge(pnode &t, pnode u, pnode v) {
- if (!u || !v) t = u ? u : v;
- else if (u->prior > v->prior) merge(u->r, u->r, v), t=u;
- else merge(v->l, u, v->l), t=v;
- updnd(t);
- }
- void insert(pnode &t, pnode it) {
- if (!t) return void(t = it);
- if (t->prior > it->prior)
- insert(t->key > it->key ? t->l : t->r, it);
- else
- split(t, it->l, it->r, it->key), t=it;
- updnd(t);
- }
- bool exists(pnode &t, pnode it) {
- if (!t) return false;
- if (t->key == it->key) return true;
- else if (t->key > it->key) return exists(t->r, it);
- else return exists(t->l, it);
- }
- void del(pnode &t, pnode it) {
- pnode p1, p2, p3;
- split(t, p1, p2, it->key-1);
- split(p2, p2, p3, it->key);
- merge(t, p1, p3);
- }
- int getval(pnode t, int ind, int ac =0) {
- if (!t) return -1;
- int cur_pos = ac + cnt(t->l);
- if (cur_pos == ind) return t->key;
- else if (cur_pos > ind) return getval(t->l, ind, ac);
- else return getval(t->r, ind, cur_pos+1);
- }
- int query(pnode t, int a, int b, int ac=0) {
- int i1 = getval(t, a), i2 = getval(t, b);
- cerr << i1 << " " << i2 << "\n";
- pnode p1, p2, p3;
- split(t, p1, p2, i1-1);
- split(p2, p2, p3, i2);
- int answer = p2->ans;
- merge(p2, p2, p3);
- merge(t, p1, p2);
- return answer;
- }
- int main() {
- cin >> q;
- pnode T = newnode(-10);
- for (int i = 1; i <= q; i++) {
- cin >> c;
- if (c == 'I') {
- cin >> a;
- pnode nn = newnode(a);
- if (!exists(T, nn)) insert(T, nn);
- } else if (c == 'D') {
- cin >> a;
- pnode nn = newnode(a);
- if (!exists(T, nn)) del(T, nn);
- } else if (c == 'N') {
- cin >> a >> b;
- if (b == a) cout << "-1\n";
- else cout << query(T, a+1, b+1) << "\n";
- } else {
- cin >> a >> b;
- if (b == a) cout << "-1\n";
- else cout << getval(T, b+1) - getval(T, a+1) << "\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement