Advertisement
GerONSo

Untitled

Jan 15th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 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 0
  63. freopen("input", "r", stdin);
  64. freopen("output", "w", stdout);
  65. #endif
  66. }
  67.  
  68. /** Fast input-output */
  69.  
  70. template <class T = int> inline T readInt();
  71. inline double readDouble();
  72. inline int readUInt();
  73. inline int readChar(); // first non-blank character
  74. inline void readWord( char *s );
  75. inline bool readLine( char *s ); // do not save '\n'
  76. inline bool isEof();
  77. inline int getChar();
  78. inline int peekChar();
  79. inline bool seekEof();
  80. inline void skipBlanks();
  81.  
  82. template <class T> inline void writeInt( T x, char end = 0, int len = -1 );
  83. inline void writeChar( int x );
  84. inline void writeWord( const char *s );
  85. inline void writeDouble( double x, int len = 0 );
  86. inline void flush();
  87.  
  88. static struct buffer_flusher_t {
  89. ~buffer_flusher_t() {
  90. flush();
  91. }
  92. } buffer_flusher;
  93.  
  94. /** Read */
  95.  
  96. static const int buf_size = 4096;
  97.  
  98. static unsigned char buf[buf_size];
  99. static int buf_len = 0, buf_pos = 0;
  100.  
  101. inline bool isEof() {
  102. if (buf_pos == buf_len) {
  103. buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
  104. if (buf_pos == buf_len)
  105. return 1;
  106. }
  107. return 0;
  108. }
  109.  
  110.  
  111. inline int getChar() {
  112. return isEof() ? -1 : buf[buf_pos++];
  113. }
  114.  
  115. inline int peekChar() {
  116. return isEof() ? -1 : buf[buf_pos];
  117. }
  118.  
  119. inline bool seekEof() {
  120. int c;
  121. while ((c = peekChar()) != -1 && c <= 32)
  122. buf_pos++;
  123. return c == -1;
  124. }
  125.  
  126. inline void skipBlanks() {
  127. while (!isEof() && buf[buf_pos] <= 32U)
  128. buf_pos++;
  129. }
  130.  
  131. inline int readChar() {
  132. int c = getChar();
  133. while (c != -1 && c <= 32)
  134. c = getChar();
  135. return c;
  136. }
  137.  
  138. inline int readUInt() {
  139. int c = readChar(), x = 0;
  140. while ('0' <= c && c <= '9')
  141. x = x * 10 + c - '0', c = getChar();
  142. return x;
  143. }
  144.  
  145. template <class T>
  146. inline T readInt() {
  147. int s = 1, c = readChar();
  148. T x = 0;
  149. if (c == '-')
  150. s = -1, c = getChar();
  151. else if (c == '+')
  152. c = getChar();
  153. while ('0' <= c && c <= '9')
  154. x = x * 10 + c - '0', c = getChar();
  155. return s == 1 ? x : -x;
  156. }
  157.  
  158. inline double readDouble() {
  159. int s = 1, c = readChar();
  160. double x = 0;
  161. if (c == '-')
  162. s = -1, c = getChar();
  163. while ('0' <= c && c <= '9')
  164. x = x * 10 + c - '0', c = getChar();
  165. if (c == '.') {
  166. c = getChar();
  167. double coef = 1;
  168. while ('0' <= c && c <= '9')
  169. x += (c - '0') * (coef *= 1e-1), c = getChar();
  170. }
  171. return s == 1 ? x : -x;
  172. }
  173.  
  174. inline void readWord( char *s ) {
  175. int c = readChar();
  176. while (c > 32)
  177. *s++ = c, c = getChar();
  178. *s = 0;
  179. }
  180.  
  181. inline bool readLine( char *s ) {
  182. int c = getChar();
  183. while (c != '\n' && c != -1)
  184. *s++ = c, c = getChar();
  185. *s = 0;
  186. return c != -1;
  187. }
  188.  
  189. /** Write */
  190.  
  191. static int write_buf_pos = 0;
  192. static char write_buf[buf_size];
  193.  
  194. inline void writeChar( int x ) {
  195. if (write_buf_pos == buf_size)
  196. fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
  197. write_buf[write_buf_pos++] = x;
  198. }
  199.  
  200. inline void flush() {
  201. if (write_buf_pos)
  202. fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
  203. }
  204.  
  205. template <class T>
  206. inline void writeInt( T x, char end, int output_len ) {
  207. if (x < 0)
  208. writeChar('-'), x = -x;
  209.  
  210. char s[24];
  211. int n = 0;
  212. while (x || !n)
  213. s[n++] = '0' + x % 10, x /= 10;
  214. while (n < output_len)
  215. s[n++] = '0';
  216. while (n--)
  217. writeChar(s[n]);
  218. if (end)
  219. writeChar(end);
  220. }
  221.  
  222. inline void writeWord( const char *s ) {
  223. while (*s)
  224. writeChar(*s++);
  225. }
  226.  
  227. inline void writeDouble( double x, int output_len ) {
  228. if (x < 0)
  229. writeChar('-'), x = -x;
  230. int t = (int)x;
  231. writeInt(t), x -= t;
  232. writeChar('.');
  233. for (int i = output_len - 1; i > 0; i--) {
  234. x *= 10;
  235. t = std::min(9ll, (int)x);
  236. writeChar('0' + t), x -= t;
  237. }
  238. x *= 10;
  239. t = std::min(9ll, (int)(x + 0.5));
  240. writeChar('0' + t);
  241. }
  242.  
  243. struct rrq {
  244. int t, l, r, id;
  245. };
  246.  
  247. struct crq {
  248. int t, pos, val;
  249. };
  250.  
  251. const int INF = 1e9 + 7;
  252. const int MAXN = 1e6 + 100;
  253.  
  254. int k, kt;
  255. vi addpos;
  256. matrix add;
  257. vi a, ccc(MAXN);
  258. int curpos = 0;
  259. vector<crq> crqs;
  260. int curl = 1, curr = 0, curt = 0;
  261.  
  262. bool cmp(rrq a, rrq b) {
  263. if(a.t / kt != b.t / kt) {
  264. return a.t < b.t;
  265. }
  266. else if(a.l / k != b.l / k) {
  267. return a.l < b.l;
  268. }
  269. return a.r < b.r;
  270. }
  271.  
  272. void addLeft(int pos) {
  273. pos = a[pos];
  274. ccc[pos]++;
  275. if(pos == curpos) {
  276. while(ccc[curpos]) {
  277. curpos++;
  278. }
  279. }
  280. }
  281.  
  282. void addRight(int pos) {
  283. addLeft(pos);
  284. }
  285.  
  286. void delLeft(int pos) {
  287. pos = a[pos];
  288. ccc[pos]--;
  289. if(ccc[pos] == 0 && pos < curpos) {
  290. curpos = pos;
  291. }
  292. }
  293.  
  294. void delRight(int pos) {
  295. delLeft(pos);
  296. }
  297.  
  298. void addT(int num) {
  299. int pos = crqs[num].pos;
  300. int val = crqs[num].val;
  301. if(pos >= curl && pos <= curr) {
  302. delLeft(pos);
  303. }
  304. addpos[pos]++;
  305. a[pos] = val;
  306. if(pos >= curl && pos <= curr) {
  307. addLeft(pos);
  308. }
  309. }
  310.  
  311. void delT(int num) {
  312. int pos = crqs[num].pos;
  313. if(pos >= curl && pos <= curr) {
  314. ccc[a[pos]]--;
  315. if(ccc[a[pos]] == 0 && a[pos] < curpos) {
  316. curpos = a[pos];
  317. }
  318. }
  319. addpos[pos]--;
  320. a[pos] = add[pos][addpos[pos]];
  321. if(pos >= curl && pos <= curr) {
  322. addLeft(pos);
  323. }
  324. }
  325.  
  326. int answer() {
  327. return curpos;
  328. }
  329.  
  330. signed main() {
  331. seriy();
  332. int n, q;
  333. n = readInt();
  334. q = readInt();
  335. a.resize(n);
  336. add.resize(n);
  337. addpos.resize(n, 0);
  338. for(int i = 0; i < n; i++) {
  339. a[i] = readInt();
  340. add[i].pb(a[i]);
  341. }
  342. vi l(q), r(q);
  343. int tt = 0;
  344. vector<rrq> rqs;
  345.  
  346. for(int i = 0; i < q; i++) {
  347. char rq;
  348. rq = readChar();
  349. l[i] = readInt();
  350. r[i] = readInt();
  351. l[i]--;
  352. if(rq == '!') {
  353. crqs.pb({tt + 1, l[i], r[i]});
  354. add[l[i]].pb(r[i]);
  355. // cerr << l[i] << " " << r[i] << '\n';
  356. tt++;
  357. }
  358. else {
  359. r[i]--;
  360. rqs.pb({tt, l[i], r[i], i});
  361. }
  362. }
  363. int num = 0;
  364. k = pow(n, 2. / 3.);
  365. kt = pow(tt, 2. / 3.) + 1;
  366. sort(all(rqs), cmp);
  367. vi ans(q, -1);
  368. for(int i = 0; i < rqs.size(); i++) {
  369. while(curl > rqs[i].l) {
  370. curl--;
  371. addLeft(curl);
  372. }
  373. while(curr < rqs[i].r) {
  374. curr++;
  375. addRight(curr);
  376. }
  377.  
  378. while(curl < rqs[i].l) {
  379. delLeft(curl);
  380. curl++;
  381. }
  382. while(curr > rqs[i].r) {
  383. delRight(curr);
  384. curr--;
  385. }
  386. while(curt < rqs[i].t) {
  387. addT(curt);
  388. curt++;
  389. }
  390. while(curt > rqs[i].t) {
  391. curt--;
  392. delT(curt);
  393. }
  394. // for(auto j : a) {
  395. // cerr << j << " ";
  396. // }
  397. // cerr << '\n';
  398. // cerr << rqs[i].t << " " << rqs[i].l << " " << rqs[i].r << " " << answer() << '\n';
  399. ans[rqs[i].id] = answer();
  400. }
  401. for(auto i : ans) {
  402. if(i != -1) {
  403. writeInt(i);
  404. writeChar('\n');
  405. }
  406. }
  407. return 0;
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement