Advertisement
cincout

Untitled

Oct 4th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define less albert
  6. #define great liza
  7.  
  8. const int N = 1e5 + 150;
  9. int L;
  10.  
  11. int n, q;
  12. int a[N], cnt[N], ans[N], val[N];
  13. int kek[N];
  14. set <int> have;
  15.  
  16. int get_ans()
  17. {
  18. return *have.begin();
  19. }
  20.  
  21. void rem(int v)
  22. {
  23. if (v == 0) return;
  24. kek[v]--;
  25. if (kek[v] == 0)
  26. {
  27. have.insert(v);
  28. }
  29. }
  30.  
  31. void add(int v)
  32. {
  33. if (v == 0) return;
  34. kek[v]++;
  35. if (have.count(v))
  36. {
  37. have.erase(v);
  38. }
  39. }
  40.  
  41. void great(int elem)
  42. {
  43. rem(cnt[elem]);
  44. cnt[elem]++;
  45. add(cnt[elem]);
  46. }
  47.  
  48. void less(int elem)
  49. {
  50. rem(cnt[elem]);
  51. cnt[elem]--;
  52. add(cnt[elem]);
  53. }
  54.  
  55. struct query
  56. {
  57. int l, r, tx, num;
  58. query(int _l, int _r, int _tx, int i)
  59. {
  60. l = _l;
  61. r = _r;
  62. tx = _tx;
  63. num = i;
  64. }
  65. };
  66.  
  67. struct change
  68. {
  69. int pos, value, undo;
  70. change(int a, int b)
  71. {
  72. pos = a;
  73. value = b;
  74. }
  75. };
  76.  
  77. bool cmp(query &a, query &b)
  78. {
  79. int v1 = a.tx / L, v2 = b.tx / L;
  80. if (v1 != v2) return v1 < v2;
  81. v1 = a.l / L, v2 = b.l / L;
  82. if (v1 != v2) return v1 < v2;
  83. return a.r < b.r;
  84. }
  85.  
  86. int main()
  87. {
  88. ios::sync_with_stdio(0);
  89. cin.tie(0);
  90. cin >> n >> q;
  91. for (int i = 1; i <= n + 1; ++i) have.insert(i);
  92. for (int i = 0; i < n; ++i)
  93. {
  94. cin >> a[i];
  95. }
  96. int was = 0;
  97. vector <query> s;
  98. vector <change> c;
  99. int has = 0;
  100. for (int i = 0; i < q; ++i)
  101. {
  102. int t, l, r;
  103. cin >> t >> l >> r;
  104. if (t == 1)
  105. {
  106. --l, --r;
  107. s.push_back(query(l, r, has, i));
  108. }
  109. else
  110. {
  111. has++;
  112. --l;
  113. c.push_back(change(l, r));
  114. }
  115. }
  116. L = pow((long long)(n * n), 1.0 / 3.0);
  117. sort(s.begin(), s.end(), cmp);
  118.  
  119. for (int i = 0; i < n; ++i) val[i] = a[i];
  120. for (auto &i : c)
  121. {
  122. i.undo = val[i.pos];
  123. val[i.pos] = i.value;
  124. }
  125.  
  126. int l = s[0].l;
  127. int r = s[0].r;
  128. int curT = s[0].tx - 1;
  129.  
  130. for (int i = l; i <= r; ++i)
  131. {
  132. great(a[i]);
  133. }
  134.  
  135. for (int i = 0; i <= curT; ++i)
  136. {
  137. int ps = c[i].pos;
  138. int vl = c[i].value;
  139. less(a[ps]);
  140. great(vl);
  141. a[ps] = vl;
  142. }
  143.  
  144. ans[s[0].num] = get_ans();
  145.  
  146. for (int i = 1; i < s.size(); ++i)
  147. {
  148. int curL = s[i].l;
  149. int curR = s[i].r;
  150. int T = s[i].tx - 1;
  151. while (r < curR)
  152. {
  153. great(a[++r]);
  154. }
  155. while (r > curR)
  156. {
  157. less(a[r--]);
  158. }
  159. while (l < curL)
  160. {
  161. less(a[l++]);
  162. }
  163. while (l > curL)
  164. {
  165. great(a[--l]);
  166. }
  167.  
  168. while (curT < T)
  169. {
  170. curT++;
  171. int ps = c[curT].pos;
  172. int vl = c[curT].value;
  173. if (l <= ps && ps <= r)
  174. {
  175. less(a[ps]);
  176. great(vl);
  177. }
  178. a[ps] = vl;
  179. }
  180.  
  181. while (curT > T)
  182. {
  183. int ps = c[curT].pos;
  184. int vl = c[curT].value;
  185. int un = c[curT].undo;
  186. if (l <= ps && ps <= r)
  187. {
  188. less(a[ps]);
  189. great(un);
  190. }
  191. a[ps] = un;
  192. curT--;
  193. }
  194. ans[s[i].num] = get_ans();
  195.  
  196. }
  197.  
  198. for (int i = 0; i < q; ++i)
  199. {
  200. if (ans[i] == 0) continue;
  201. cout << ans[i] << "\n";
  202. }
  203.  
  204. return 0;
  205. }
  206.  
  207. /*
  208. * int overflow, array bounds
  209. * special cases (n=1?), set tle
  210. * do smth instead of nothing and stay organized
  211. * double troubles
  212. * same points in geoma
  213. * in game theory find win cases
  214. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement