Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. #include<cstdio>
  2. #include<utility>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<cstdlib>
  6. #include<cassert>
  7. #include<ctime>
  8. using namespace std;
  9.  
  10.  
  11. FILE *fin = freopen("zeap.in", "r", stdin);
  12. FILE *fout = freopen("zeap.out", "w", stdout);
  13.  
  14. const int INF = 1000000001;
  15. char S[20];
  16.  
  17. struct Node{
  18. int val;
  19. int MIN;
  20. int MAX;
  21. int minDIFF;
  22. long long priority;
  23. Node* left;
  24. Node* right;
  25. };
  26. Node* emptyNode = new Node;
  27.  
  28. void recompute(Node* root)
  29. {
  30. if (root == emptyNode) return;
  31.  
  32. root -> minDIFF = INF;
  33. root -> MIN = root -> MAX = root -> val;
  34.  
  35. if (root -> left != emptyNode)
  36. {
  37. root -> minDIFF = min(min(root -> val - root -> left -> MAX, root -> left -> minDIFF), root -> minDIFF);
  38. root -> MIN = min(root -> MIN, root -> left -> MIN);
  39. root -> MAX = max(root -> left -> MAX, root -> MAX);
  40. }
  41. if (root -> right != emptyNode)
  42. {
  43. root -> minDIFF = min(min(root -> right -> minDIFF, root -> right -> MIN - root -> val), root -> minDIFF);
  44. root -> MIN = min(root -> right -> MIN, root -> MIN) ;
  45. root -> MAX = max(root -> right -> MAX, root -> MAX);
  46. }
  47. }
  48.  
  49. pair < Node*, Node* > split(Node* node, int val)
  50. {
  51. pair < Node*, Node* > p;
  52. if (node == emptyNode)
  53. {
  54. p.first = emptyNode;
  55. p.second = emptyNode;
  56. }
  57. else
  58. if (val < node -> val)
  59. {
  60. pair < Node*, Node* > q = split(node -> left, val);
  61. p.first = q.first;
  62. node -> left = q.second;
  63. p.second = node;
  64. recompute(node);
  65. }
  66. else
  67. {
  68. pair < Node*, Node* > q = split(node -> right, val);
  69. p.second = q.second;
  70. node -> right = q.first;
  71. p.first = node;
  72. recompute(node);
  73. }
  74.  
  75. return p;
  76. }
  77.  
  78. bool Search(Node* node, int val)
  79. {
  80. if (node == emptyNode)
  81. return 0;
  82. if (node -> val == val)
  83. return 1;
  84. if (node -> val < val)
  85. return Search(node -> right, val);
  86. return Search(node -> left, val);
  87. }
  88.  
  89. Node* join(Node* first, Node* second)
  90. {
  91. if (first == emptyNode)
  92. return second;
  93. else if (second == emptyNode)
  94. return first;
  95. else if (first -> priority > second -> priority)
  96. {
  97. first -> right = join(first -> right, second);
  98. recompute(first);
  99. return first;
  100. }
  101. else
  102. {
  103. second -> left = join(first, second -> left);
  104. recompute(second);
  105. return second;
  106. }
  107. }
  108.  
  109. Node* Insert(Node* node, int val)
  110. {
  111. Node* newNode = new Node;
  112. newNode -> val = newNode -> MIN = newNode -> MAX = val;
  113. newNode -> minDIFF = INF;
  114. newNode -> priority = ((long long) rand() << 45)
  115. ^ ((long long) rand() << 30)
  116. ^ ((long long) rand() << 15)
  117. ^ ((long long) rand() << 0);
  118. newNode -> priority &= 0x7fffffffffffffff;
  119. newNode -> left = emptyNode;
  120. newNode -> right = emptyNode;
  121.  
  122. pair < Node*, Node* > p = split(node, val);
  123. return join(join(p.first, newNode), p.second);
  124. }
  125.  
  126. Node* Delete(Node* node, int val)
  127. {
  128. pair < Node*, Node* > p = split(node, val);
  129. pair < Node*, Node* > q = split(p.first, val - 1);
  130.  
  131. return join(q.first, p.second);
  132. }
  133.  
  134. int getNumber()
  135. {
  136. int ind = 2, number = 0;
  137. while (S[ind] >= '0' && S[ind] <= '9')
  138. number = number * 10 + S[ind] - '0', ind++;
  139. return number;
  140. }
  141.  
  142. void print(Node* root, int depth = 0) {
  143. if (root != emptyNode) {
  144. for(int i = 0; i < depth; i++)
  145. printf(" ");
  146. printf("%d: %lld\n", root->val, root->priority);
  147. print(root->left, depth + 1);
  148. print(root->right, depth + 1);
  149. }
  150. if (depth == 0)
  151. printf("\n");
  152. }
  153.  
  154. int main()
  155. {
  156. emptyNode -> val = emptyNode -> MAX = -INF;
  157. emptyNode -> MIN = emptyNode -> minDIFF = INF;
  158. emptyNode -> priority = -1;
  159. emptyNode -> left = emptyNode;
  160. emptyNode -> right = emptyNode;
  161.  
  162. Node* root = emptyNode;
  163.  
  164. while(gets(S))
  165. {
  166. if (S[0] != 'M')
  167. {
  168. int number = getNumber();
  169. if (S[0] == 'I')
  170. {
  171. if (!Search(root, number))
  172. root = Insert(root, number);
  173. }
  174. if (S[0] == 'S')
  175. {
  176. if (Search(root, number))
  177. root = Delete(root, number);
  178. else
  179. printf("-1\n");
  180. }
  181. if (S[0] == 'C')
  182. printf("%d\n", Search(root, number));
  183. }
  184. else
  185. {
  186. if (S[1] == 'I')
  187. {
  188. int diff = root -> minDIFF;
  189. if (diff == INF)
  190. printf("-1\n");
  191. else
  192. printf("%d\n", diff);
  193. }
  194. else
  195. {
  196. int diff = root -> MAX - root -> MIN;
  197. if (diff < 1)
  198. printf("-1\n");
  199. else
  200. printf("%d\n", diff);
  201. }
  202. }
  203. }
  204.  
  205. //print(root);
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement