Advertisement
GerONSo

D open 65

Jan 7th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 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;
  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. delLeft(pos);
  140. }
  141. addpos[pos]--;
  142. a[pos] = add[pos][addpos[pos]];
  143. if(pos >= curl && pos <= curr) {
  144. addLeft(pos);
  145. }
  146. }
  147.  
  148. int answer() {
  149. return curpos;
  150. }
  151.  
  152. signed main() {
  153. seriy();
  154. int n, q;
  155. cin >> n >> q;
  156. a.resize(n);
  157. add.resize(n);
  158. addpos.resize(n, 0);
  159. int mxx = 0;
  160. for(int i = 0; i < n; i++) {
  161. cin >> a[i];
  162. mxx = max(mxx, a[i]);
  163. add[i].pb(a[i]);
  164. }
  165. vi l(q), r(q);
  166. int tt = 0;
  167. vector<rrq> rqs;
  168. vector<char> rq;
  169. for(int i = 0; i < q; i++) {
  170. char rqq;
  171. cin >> rqq;
  172. cin >> l[i] >> r[i];
  173.  
  174. rq.pb(rqq);
  175. if(rqq == '!') {
  176. crqs.pb({tt + 1, l[i] - 1, r[i]});
  177.  
  178. add[l[i] - 1].pb(r[i]);
  179. // cerr << l[i] << " " << r[i] << '\n';
  180. tt++;
  181. }
  182. else {
  183. l[i]--;
  184. r[i]--;
  185. rqs.pb({tt, l[i], r[i], i});
  186. }
  187. }
  188. if(mxx <= 10) {
  189. int num = 0;
  190. vector<set<int>> vs(11);
  191. for(int i = 0; i < n; i++) {
  192. vs[a[i]].insert(i);
  193. }
  194. while(q--) {
  195. if(rq[num] == '?') {
  196. int ans = 11;
  197. for(int i = 0; i < 11; i++) {
  198. // cerr << (vs[i].lower_bound(l[num]) == vs[i].end()) << '\n';
  199. if(vs[i].lower_bound(l[num]) == vs[i].end() || (*vs[i].lower_bound(l[num])) > r[num]) {
  200. ans = i;
  201. break;
  202. }
  203. }
  204. // cerr << '\n';
  205. cout << ans << '\n';
  206. }
  207. else {
  208. l[num]--;
  209. int ans = 0;
  210. for(int i = 0; i < 11; i++) {
  211. if(vs[i].lower_bound(l[num]) != vs[i].end() && (*vs[i].lower_bound(l[num])) == l[num]) {
  212. ans = i;
  213. break;
  214. }
  215. }
  216. vs[ans].erase(l[num]);
  217. vs[r[num]].insert(l[num]);
  218. }
  219. num++;
  220. }
  221. }
  222. else {
  223. int num = 0;
  224. k = pow(n, 2. / 3.);
  225. sort(all(rqs), cmp);
  226. vi ans(q, -1);
  227. for(int i = 0; i < rqs.size(); i++) {
  228.  
  229. while(curt < rqs[i].t) {
  230. addT(curt);
  231. curt++;
  232. }
  233. while(curl > rqs[i].l) {
  234. curl--;
  235. addLeft(curl);
  236. }
  237. while(curr < rqs[i].r) {
  238. curr++;
  239. addRight(curr);
  240. }
  241.  
  242. while(curl < rqs[i].l) {
  243. delLeft(curl);
  244. curl++;
  245. }
  246. while(curr > rqs[i].r) {
  247. delRight(curr);
  248. curr--;
  249. }
  250. while(curt > rqs[i].t) {
  251. curt--;
  252. delT(curt);
  253. }
  254. // for(auto j : a) {
  255. // cerr << j << " ";
  256. // }
  257. // cerr << '\n';
  258. // cerr << rqs[i].t << " " << rqs[i].l << " " << rqs[i].r << " " << answer() << '\n';
  259. ans[rqs[i].id] = answer();
  260. }
  261. for(auto i : ans) {
  262. if(i != -1) cout << i << '\n';
  263. }
  264. }
  265. return 0;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement