Advertisement
GerONSo

D open 53

Jan 7th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 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 _offline
  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;
  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 / k != b.t / k) {
  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. if(pos >= curl && pos <= curr) {
  127. delLeft(pos);
  128. }
  129. addpos[pos]++;
  130. a[pos] = val;
  131. if(pos >= curl && pos <= curr) {
  132. addLeft(pos);
  133. }
  134. }
  135.  
  136. void delT(int num) {
  137. int pos = crqs[num].pos;
  138. if(pos >= curl && pos <= curr) {
  139. ccc[a[pos]]--;
  140. if(ccc[a[pos]] == 0 && a[pos] < curpos) {
  141. curpos = a[pos];
  142. }
  143. }
  144. addpos[pos]--;
  145. a[pos] = add[pos][addpos[pos]];
  146. if(pos >= curl && pos <= curr) {
  147. addLeft(pos);
  148. }
  149. }
  150.  
  151. int answer() {
  152. return curpos;
  153. }
  154.  
  155. signed main() {
  156. seriy();
  157. int n, q;
  158. cin >> n >> q;
  159. a.resize(n);
  160. add.resize(n);
  161. addpos.resize(n, 0);
  162. for(int i = 0; i < n; i++) {
  163. cin >> a[i];
  164. add[i].pb(a[i]);
  165. }
  166. vi l(q), r(q);
  167. int tt = 0;
  168. vector<rrq> rqs;
  169.  
  170. for(int i = 0; i < q; i++) {
  171. char rq;
  172. cin >> rq;
  173. cin >> l[i] >> r[i];
  174. l[i]--;
  175. if(rq == '!') {
  176. crqs.pb({tt + 1, l[i], r[i]});
  177. add[l[i]].pb(r[i]);
  178. // cerr << l[i] << " " << r[i] << '\n';
  179. tt++;
  180. }
  181. else {
  182. r[i]--;
  183. rqs.pb({tt, l[i], r[i], i});
  184. }
  185. }
  186. int num = 0;
  187. k = pow(n, 2. / 3.);
  188. sort(all(rqs), cmp);
  189. vi ans(q, -1);
  190. for(int i = 0; i < rqs.size(); i++) {
  191.  
  192. while(curt < rqs[i].t) {
  193. addT(curt);
  194. curt++;
  195. }
  196. while(curl > rqs[i].l) {
  197. curl--;
  198. addLeft(curl);
  199. }
  200. while(curr < rqs[i].r) {
  201. curr++;
  202. addRight(curr);
  203. }
  204.  
  205. while(curl < rqs[i].l) {
  206. delLeft(curl);
  207. curl++;
  208. }
  209. while(curr > rqs[i].r) {
  210. delRight(curr);
  211. curr--;
  212. }
  213. while(curt > rqs[i].t) {
  214. curt--;
  215. delT(curt);
  216. }
  217. // for(auto j : a) {
  218. // cerr << j << " ";
  219. // }
  220. // cerr << '\n';
  221. // cerr << rqs[i].t << " " << rqs[i].l << " " << rqs[i].r << " " << answer() << '\n';
  222. ans[rqs[i].id] = answer();
  223. }
  224. for(auto i : ans) {
  225. if(i != -1) cout << i << '\n';
  226. }
  227. return 0;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement