Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef unsigned int uint;
  8.  
  9. const int MAXN = (int)1e6 + 7;
  10. const int INF = (int)1e9 + 7;
  11. const ll LINF = (ll)4e18 + 7LL;
  12. const double eps = 1e-8;
  13.  
  14. #define TASK "rmq"
  15.  
  16. struct Heap;
  17.  
  18. const int C = 25;
  19.  
  20. struct Tree {
  21.  
  22. vector<Tree*> g;
  23. ll val;
  24. int index;
  25. int rank;
  26. Heap* h;
  27. Tree* pr;
  28.  
  29. Tree(ll x, int Ind) {
  30. pr = 0;
  31. val = x;
  32. index = Ind;
  33. rank = 0;
  34. h = 0;
  35. }
  36. };
  37.  
  38. Tree* merge(Tree* a, Tree* b) {
  39. if (!a)
  40. return b;
  41. if (!b)
  42. return a;
  43. if (a->val < b->val || (a->val == b->val && a->index < b->index)) {
  44. b->pr = a;
  45. a->g.push_back(b);
  46. ++(a->rank);
  47. return a;
  48. } else {
  49. a->pr = b;
  50. b->g.push_back(a);
  51. ++(b->rank);
  52. return b;
  53. }
  54. }
  55.  
  56. struct Heap {
  57.  
  58. Tree* a[C];
  59. int Ind;
  60.  
  61. Heap() {
  62. for (int i = 0; i < C; ++i)
  63. a[i] = 0;
  64. }
  65.  
  66. Heap(Tree *t) {
  67. for (int i = 0; i < C; ++i)
  68. a[i] = 0;
  69. a[t->rank] = t;
  70. }
  71.  
  72. ll getMin() {
  73. ll mn = LINF;
  74. for (int i = 0; i < C; ++i)
  75. if (a[i])
  76. mn = min(mn, a[i]->val);
  77. return mn;
  78. }
  79. };
  80.  
  81. void print(Tree *t) {
  82. if (!t) {
  83. cout << "empty1 ";
  84. return;
  85. }
  86. cout << t->val << " ";
  87. for (auto to : t->g) {
  88. print(to);
  89. }
  90. }
  91.  
  92. void print(Heap *h) {
  93. if (!h) {
  94. cout << "empty" << endl;
  95. return;
  96. }
  97. for (int i = 0; i < C; ++i) {
  98. print(h->a[i]);
  99. cout << endl;
  100. }
  101. }
  102.  
  103. Heap* merge(Heap* a, Heap* b) {
  104. Tree* last = 0;
  105. for (int i = 0; i < C; ++i) {
  106. Tree* t = merge(a->a[i], b->a[i]);
  107. if (!t) {
  108. a->a[i] = last;
  109. last = 0;
  110. } else if (t->rank == i) {
  111. a->a[i] = t;
  112. if (last) {
  113. last = merge(a->a[i], last);
  114. a->a[i] = 0;
  115. }
  116. } else {
  117. a->a[i] = last;
  118. last = t;
  119. }
  120. if (a->a[i])
  121. a->a[i]->h = a;
  122. }
  123. return a;
  124. }
  125.  
  126. Heap* a[MAXN];
  127. Tree* ind[MAXN];
  128.  
  129. void change(int i, ll x) {
  130. Tree *t = ind[i];
  131. if (x < t->val) {
  132. t->val = x;
  133. //cout << ind[i] << endl;
  134. while (t->pr && (t->val < t->pr->val || (t->val == t->pr->val && t->index < t->pr->index))) {
  135. int j = t->pr->index;
  136. swap(t->val, t->pr->val);
  137. swap(ind[i], ind[j]);
  138. swap(t->index, t->pr->index);
  139. t = t->pr;
  140. //cout << ind[i] << endl;
  141. }
  142. } else {
  143. t->val = x;
  144. while (t->g.size()) {
  145. ll mn = LINF;
  146. int ind1 = -1;
  147. for (int j = 0; j < (int)t->g.size(); ++j) {
  148. if (t->g[j]->val < mn || (t->g[j]->val == mn && t->g[j]->index < ind1)) {
  149. ind1 = j;
  150. mn = t->g[j]->val;
  151. }
  152. }
  153. Tree* to = t->g[ind1];
  154. if (to->val > x || (to->val == x && to->index > i))
  155. break;
  156. int j = to->index;
  157. swap(t->val, to->val);
  158. swap(ind[i], ind[j]);
  159. swap(t->index, to->index);
  160. t = to;
  161. }
  162. }
  163. }
  164.  
  165. void pop(Tree* t, Heap* &h) {
  166. Heap* h1 = new Heap();
  167. for (auto ch : t->g) {
  168. ch->pr = 0;
  169. h1 = merge(h1, new Heap(ch));
  170. }
  171. ind[t->index] = 0;
  172. h->a[t->rank] = 0;
  173. h = merge(h, h1);
  174. }
  175.  
  176. void extractMin(Heap *h) {
  177. Tree* need = 0;
  178. ll mn = LINF;
  179. int j = INF;
  180. for (int i = 0; i < C; ++i) {
  181. Tree* t = h->a[i];
  182. if (!t)
  183. continue;
  184. if (t->val < mn || (t->val == mn && t->index < j)) {
  185. need = t;
  186. mn = t->val;
  187. j = i;
  188. }
  189. }
  190. pop(need, h);
  191. }
  192.  
  193. void del(int i) {
  194. change(i, -LINF);
  195. Tree* t = ind[i];
  196. pop(t, t->h);
  197. }
  198.  
  199.  
  200. int main() {
  201. ios_base::sync_with_stdio(0);
  202. //freopen(TASK".in", "r", stdin); freopen(TASK".out", "w", stdout);
  203. //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  204. int n, m;
  205. int cnt = 0;
  206. cin >> n >> m;
  207. for (int i = 0; i < n; ++i) {
  208. a[i] = new Heap();
  209. a[i]->Ind = i;
  210. }
  211. for (int q = 0; q < m; ++q) {
  212. int type;
  213. cin >> type;
  214. //cout << "*" << endl;
  215. if (type == 0) {
  216. ll x, to;
  217. cin >> to >> x;
  218. --to;
  219. Tree* t = new Tree(x, ++cnt);
  220. Heap* h = new Heap(t);
  221. ind[cnt] = t;
  222. //print(a[to]);
  223. a[to] = merge(a[to], h);
  224. }
  225. if (type == 1) {
  226. int x, y;
  227. cin >> x >> y;
  228. //print(a[x - 1]);
  229. //print(a[y - 1]);
  230. merge(a[y - 1], a[x - 1]);
  231. a[x - 1] = new Heap();
  232. a[x - 1]->Ind = x - 1;
  233. }
  234. if (type == 2) {
  235. int i;
  236. cin >> i;
  237. if (ind[i] == 0)
  238. continue;
  239. del(i);
  240. }
  241. if (type == 3) {
  242. int i, x;
  243. cin >> i >> x;
  244. change(i, x);
  245. }
  246. if (type == 4) {
  247. int x;
  248. cin >> x;
  249. cout << a[x - 1]->getMin() << "\n";
  250. }
  251. if (type == 5) {
  252. int i;
  253. cin >> i;
  254. //cout << a[0]->a[1]->index << endl;
  255. extractMin(a[i - 1]);
  256. //cout << a[0]->a[0]->index << endl;
  257. }
  258. }
  259.  
  260. return 9/11;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement