Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6. int key, prior, minv, maxv, ans, cnt;
  7. node *l, *r;
  8. };
  9.  
  10. typedef node* pnode;
  11.  
  12. int q, a, b;
  13. char c;
  14.  
  15. int cnt(pnode t) {
  16. return t ? t->cnt : 0;
  17. }
  18.  
  19. int choose(int A, int B) {
  20. if (A == -1) return B;
  21. if (B == -1) return A;
  22. return min(A, B);
  23. }
  24.  
  25. void updnd(pnode t) {
  26. if (!t) return;
  27. t->cnt = 1 + cnt(t->l) + cnt(t->r);
  28. if (t->l && t->r) {
  29. t->ans = choose(t->l->ans, t->r->ans);
  30. int V = min(t->r->minv - t->key, t->key - t->l->maxv);
  31. t->ans = choose(V, t->ans);
  32. t->maxv = max(t->key, max(t->r->maxv, t->l->maxv));
  33. t->minv = min(t->key, min(t->r->minv, t->l->minv));
  34. } else if (!t->l && !t->r) {
  35. t->ans = -1, t->minv = t->maxv = t->key;
  36. } else {
  37. pnode oquepresta = t->l ? t->l : t->r;
  38. int V = min(abs(oquepresta->minv - t->key), abs(oquepresta->maxv - t->key));
  39. t->ans = choose(V, choose(t->ans, oquepresta->ans));
  40. t->maxv = max(t->key, oquepresta->maxv);
  41. t->minv = min(t->key, oquepresta->minv);
  42. }
  43. }
  44.  
  45. pnode newnode(int v=0) {
  46. pnode t = (pnode)malloc(sizeof(node));
  47. t->l = t->r = nullptr;
  48. t->prior = rand();
  49. t->key = v;
  50. t->minv = t->maxv = v;
  51. t->ans = -1;
  52. t->cnt = 1;
  53. return t;
  54. }
  55. // <=k -> u
  56. void split(pnode t, pnode &u, pnode &v, int k) {
  57. if (!t) return void(u = v = nullptr);
  58. if (t->key <= k) split(t->r, t->r, v, k), u=t;
  59. else split(t->l, u, t->l, k), v=t;
  60. updnd(t);
  61. }
  62.  
  63. void merge(pnode &t, pnode u, pnode v) {
  64. if (!u || !v) t = u ? u : v;
  65. else if (u->prior > v->prior) merge(u->r, u->r, v), t=u;
  66. else merge(v->l, u, v->l), t=v;
  67. updnd(t);
  68. }
  69.  
  70. void insert(pnode &t, pnode it) {
  71. if (!t) return void(t = it);
  72. if (t->prior > it->prior)
  73. insert(t->key > it->key ? t->l : t->r, it);
  74. else
  75. split(t, it->l, it->r, it->key), t=it;
  76. updnd(t);
  77. }
  78.  
  79. bool exists(pnode &t, pnode it) {
  80. if (!t) return false;
  81. if (t->key == it->key) return true;
  82. else if (t->key > it->key) return exists(t->r, it);
  83. else return exists(t->l, it);
  84. }
  85.  
  86. void del(pnode &t, pnode it) {
  87. pnode p1, p2, p3;
  88. split(t, p1, p2, it->key-1);
  89. split(p2, p2, p3, it->key);
  90. merge(t, p1, p3);
  91. }
  92.  
  93. int getval(pnode t, int ind, int ac =0) {
  94. if (!t) return -1;
  95. int cur_pos = ac + cnt(t->l);
  96. if (cur_pos == ind) return t->key;
  97. else if (cur_pos > ind) return getval(t->l, ind, ac);
  98. else return getval(t->r, ind, cur_pos+1);
  99. }
  100.  
  101. int query(pnode t, int a, int b, int ac=0) {
  102. int i1 = getval(t, a), i2 = getval(t, b);
  103. cerr << i1 << " " << i2 << "\n";
  104. pnode p1, p2, p3;
  105. split(t, p1, p2, i1-1);
  106. split(p2, p2, p3, i2);
  107. int answer = p2->ans;
  108. merge(p2, p2, p3);
  109. merge(t, p1, p2);
  110. return answer;
  111. }
  112.  
  113. int main() {
  114. cin >> q;
  115. pnode T = newnode(-10);
  116. for (int i = 1; i <= q; i++) {
  117. cin >> c;
  118. if (c == 'I') {
  119. cin >> a;
  120. pnode nn = newnode(a);
  121. if (!exists(T, nn)) insert(T, nn);
  122. } else if (c == 'D') {
  123. cin >> a;
  124. pnode nn = newnode(a);
  125. if (!exists(T, nn)) del(T, nn);
  126. } else if (c == 'N') {
  127. cin >> a >> b;
  128. if (b == a) cout << "-1\n";
  129. else cout << query(T, a+1, b+1) << "\n";
  130. } else {
  131. cin >> a >> b;
  132. if (b == a) cout << "-1\n";
  133. else cout << getval(T, b+1) - getval(T, a+1) << "\n";
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement