Advertisement
GerONSo

D open 51

Jan 7th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. /*
  2. ┓┏┓┏┓┃
  3. ┛┗┛┗┛┃
  4. ┓┏┓┏┓┃
  5. ┛┗┛┗┛┃
  6. ┓┏┓┏┓┃\○/
  7. ┛┗┛┗┛┃ / /
  8. ┓┏┓┏┓┃ノ
  9. ┛┗┛┗┛┃
  10. ┓┏┓┏┓┃
  11. ┛┗┛┗┛┃
  12. ┓┏┓┏┓┃
  13. ┛┗┛┗┛┃
  14. ┓┏┓┏┓┃
  15. ┛┗┛┗┛┃
  16. ┓┏┓┏┓┃┓
  17. ┛┗┛┗┛┃┃
  18. MIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSICMIPTCLASSIC
  19. */
  20.  
  21. // #define pragma
  22.  
  23. #ifdef pragma
  24. #pragma GCC optimize("Ofast")
  25. #pragma GCC optimize("no-stack-protector")
  26. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  27. #pragma GCC optimize("unroll-loops")
  28. #pragma GCC diagnostic ignored "-Wunused-result"
  29. #endif // pragma
  30.  
  31. #include<bits/stdc++.h>
  32. #include <ext/pb_ds/assoc_container.hpp>
  33. #include <ext/pb_ds/tree_policy.hpp>
  34.  
  35. #define ll long long
  36. #define all(x) begin(x), end(x)
  37. #define pb push_back
  38. #define x first
  39. #define y second
  40. #define int long long
  41. #define zero(two) memset(two, 0, sizeof(two))
  42.  
  43. using namespace std;
  44. using namespace __gnu_pbds;
  45.  
  46.  
  47. typedef vector<int> vi;
  48. typedef vector<bool> vb;
  49. typedef pair<int, int> pii;
  50. typedef long double ld;
  51. typedef vector<vi> matrix;
  52. template<typename T>
  53. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  54.  
  55. const ld PI = atan2(0, -1);
  56.  
  57. void seriy() {
  58. ios::sync_with_stdio(0);
  59. cin.tie(0);
  60. cout.tie(0);
  61. cout << fixed << setprecision(10);
  62. #if 1
  63. freopen("input", "r", stdin);
  64. freopen("output", "w", stdout);
  65. #endif
  66. }
  67.  
  68. struct rrq {
  69. int t, l, r, id;
  70. };
  71.  
  72. struct crq {
  73. int t, pos, val;
  74. };
  75.  
  76. const int INF = 1e9 + 7;
  77. const int MAXN = 1e6 + 100;
  78.  
  79. int k, kt;
  80. vi addpos;
  81. matrix add;
  82. vi a, ccc(MAXN);
  83. int curpos = 0;
  84. vector<crq> crqs;
  85. int curl = 1, curr = 0, curt = 0;
  86.  
  87. bool cmp(rrq a, rrq b) {
  88. if(a.t / kt != b.t / kt) {
  89. return a.t < b.t;
  90. }
  91. else if(a.l / k != b.l / k) {
  92. return a.l < b.l;
  93. }
  94. return a.r < b.r;
  95. }
  96.  
  97. void addLeft(int pos) {
  98. pos = a[pos];
  99. ccc[pos]++;
  100. // if(pos == curpos) {
  101. // while(ccc[curpos]) {
  102. // curpos++;
  103. // }
  104. // }
  105. }
  106.  
  107. void addRight(int pos) {
  108. addLeft(pos);
  109. }
  110.  
  111. void delLeft(int pos) {
  112. pos = a[pos];
  113. ccc[pos]--;
  114. // if(ccc[pos] == 0 && pos < curpos) {
  115. // curpos = pos;
  116. // }
  117. }
  118.  
  119. void delRight(int pos) {
  120. delLeft(pos);
  121. }
  122.  
  123. void addT(int num) {
  124. int pos = crqs[num].pos;
  125. int val = crqs[num].val;
  126. int pv = a[pos];
  127. addpos[pos]++;
  128. a[pos] = val;
  129. if(pos >= curl && pos <= curr) {
  130. addLeft(pos);
  131. }
  132. a[pos] = pv;
  133. if(pos >= curl && pos <= curr) {
  134. delLeft(pos);
  135. }
  136. a[pos] = val;
  137. }
  138.  
  139. void delT(int num) {
  140. int pos = crqs[num].pos;
  141. int pv = a[pos];
  142. addpos[pos]--;
  143. a[pos] = add[pos][addpos[pos]];
  144. if(pos >= curl && pos <= curr) {
  145. addLeft(pos);
  146. }
  147. a[pos] = pv;
  148. if(pos >= curl && pos <= curr) {
  149. delLeft(pos);
  150. }
  151. a[pos] = add[pos][addpos[pos]];
  152. }
  153.  
  154. int answer() {
  155. for(int i = 0; ; i++) {
  156. if(!ccc[i]) {
  157. return i;
  158. }
  159. }
  160. }
  161.  
  162. signed main() {
  163. seriy();
  164. int n, q;
  165. cin >> n >> q;
  166. a.resize(n);
  167. add.resize(n);
  168. addpos.resize(n, 0);
  169. for(int i = 0; i < n; i++) {
  170. cin >> a[i];
  171. add[i].pb(a[i]);
  172. }
  173. vi l(q), r(q);
  174. int tt = 0;
  175. vector<rrq> rqs;
  176. for(int i = 0; i < q; i++) {
  177. char rq;
  178. cin >> rq;
  179. cin >> l[i] >> r[i];
  180. l[i]--;
  181. if(rq == '!') {
  182. crqs.pb({tt + 1, l[i], r[i]});
  183. add[l[i]].pb(r[i]);
  184. // cerr << l[i] << " " << r[i] << '\n';
  185. tt++;
  186. }
  187. else {
  188. r[i]--;
  189. rqs.pb({tt, l[i], r[i], i});
  190. }
  191. }
  192. int num = 0;
  193. k = pow(n, 2. / 3.);
  194. kt = pow(tt, 2. / 3.) + 1;
  195. sort(all(rqs), cmp);
  196. vi ans(q, -1);
  197. for(int i = 0; i < rqs.size(); i++) {
  198.  
  199.  
  200. while(curl > rqs[i].l) {
  201. curl--;
  202. addLeft(curl);
  203. }
  204. while(curr < rqs[i].r) {
  205. curr++;
  206. addRight(curr);
  207. }
  208. while(curl < rqs[i].l) {
  209. delLeft(curl);
  210. curl++;
  211. }
  212. while(curr > rqs[i].r) {
  213. delRight(curr);
  214. curr--;
  215. }
  216. while(curt < rqs[i].t) {
  217. addT(curt);
  218. curt++;
  219. }
  220. while(curt > rqs[i].t) {
  221. curt--;
  222. delT(curt);
  223. }
  224. // for(auto j : a) {
  225. // cerr << j << " ";
  226. // }
  227. // cerr << '\n';
  228. // cerr << rqs[i].t << " " << rqs[i].l << " " << rqs[i].r << " " << answer() << '\n';
  229. ans[rqs[i].id] = answer();
  230. }
  231. for(auto i : ans) {
  232. if(i != -1) cout << i << '\n';
  233. }
  234. return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement