Advertisement
Kevin_Zhang

Untitled

Jun 6th, 2022
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #ifdef KEV
  9. #define DE(args...) fprintf(stderr, args), fflush(stderr)
  10. #else
  11. #define DE(...) 0
  12. #endif
  13. typedef long long ll;
  14.  
  15. #define MAX_N 200010
  16. int N, Q;
  17.  
  18. ll a[MAX_N];
  19. //char fastchar() {
  20. //    static char buf[1 << 20];
  21. //    static char *p = buf, *end = buf;
  22. //    if (p == end && (end = (p = buf) + fread(buf, 1, 1 << 20, stdin)) == buf) return EOF;
  23. //    return *p++;
  24. //}
  25. //
  26. //int readint() {
  27. //    char c;
  28. //    while (!isdigit(c = fastchar()));
  29. //  int x = c - '0';
  30. //    while (isdigit(c = fastchar()))
  31. //        x = x * 10 + c - '0';
  32. //    return x;
  33. //}
  34. //
  35. //
  36. typedef struct _node {
  37.     int sz, val;
  38.     ll sum;
  39.     uint32_t pri;
  40.     bool rev;
  41.     struct _node *l, *r;
  42. } node;
  43. uint32_t my_rand() {
  44.     static uint32_t x = 21389;
  45.     return x *= 0xdefaced, ++x;
  46. }
  47. int SZ(node* s) { return s == NULL ? 0 : s->sz; }
  48. ll SUM(node* s) { return s == NULL ? 0 : s->sum; }
  49.  
  50. node* gen_node(int val) {
  51.     static node room[MAX_N];
  52.     static node* ptr = room;
  53.     node* ret = ptr++;
  54.     ret->sz = 1;
  55.     ret->val = ret->sum = val;
  56.     ret->l = ret->r = NULL;
  57.     ret->pri = my_rand();
  58.     ret->rev = false;
  59.     return ret;
  60. }
  61.  
  62. node* root = NULL;
  63.  
  64. void update(node* now) {
  65.     if (now == NULL) return;
  66.     now->sz = 1 + SZ(now->l) + SZ(now->r);
  67.     now->sum = now->val + SUM(now->l) + SUM(now->r);
  68. }
  69. void go_rev(node* now) {
  70.     if (now == NULL) return;
  71.     now->rev ^= 1;
  72.     // swap(now->l, now->r);
  73.     {
  74.         node* tmp = now->l;
  75.         now->l = now->r;
  76.         now->r = tmp;
  77.     }
  78. }
  79. void push(node* now) {
  80.     if (now == NULL) return;
  81.     if (now->rev) {
  82.         now->rev = false;
  83.         go_rev(now->l), go_rev(now->r);
  84.     }
  85. }
  86.  
  87.  
  88. node* merge(node *a, node *b) {
  89.     if (!a) return b;
  90.     if (!b) return a;
  91.     if (a->pri > b->pri) {
  92.         push(a);
  93.         a->r = merge(a->r, b);
  94.         update(a);
  95.     } else {
  96.         push(b);
  97.         b->l = merge(a, b->l);
  98.         update(b);
  99.     }
  100. }
  101. void split(node *now, int sz, node **a, node **b) {
  102.     if (!now) {
  103.         (*a) = (*b) = NULL;
  104.         return;
  105.     }
  106.     push(now);
  107.     if (SZ(now->l) + 1 <= sz) {
  108.         (*a) = now;
  109.         split(now->r, sz - 1 - SZ(now->l), &((*a)->r), b);
  110.         update(*a);
  111.     } else {
  112.         (*b) = now;
  113.         split(now->l, sz, a, &((*b)->l));
  114.         update(*b);
  115.     }
  116. }
  117.  
  118. // 1 based
  119. // put at 0 based p index
  120. void add_pk(int p, int k) {
  121.     node *a, *b;
  122.     split(root, p, &a, &b);
  123.     root = merge( merge(a, gen_node(k)), b );
  124. //  for (int i = N++;i > p;--i)
  125. //      a[i+1] = a[i];
  126. //  a[p+1] = k;
  127. }
  128. void rm_p(int p) {
  129.     node *a, *b, *c, *d;
  130.     split(root, p-1, &a, &b);
  131.     split(b, 1, &c, &d);
  132.     root = merge(a, d);
  133. //  for (int i = p;i < N;++i)
  134. //      a[i] = a[i+1];
  135. //  --N;
  136. }
  137. void rev(int l, int r) {
  138.     node *a, *b, *c, *d;
  139.     int len = r - l + 1;
  140.     split(root, l-1, &a, &b);
  141.     split(b, len, &c, &d);
  142.     go_rev(c);
  143.     root = merge( merge(a, c), d );
  144. }
  145. void swap_block(int l1, int r1, int l2, int r2) {
  146.     if (l1 > l2) {
  147.         l1 ^= l2 ^= l1 ^= l2;
  148.         r1 ^= r2 ^= r1 ^= r2;
  149.     }
  150.     int s1 = l1-1, s2 = r1-l1+1, s3 = l2-r1-1, s4 = r2-l2+1;
  151.     // a : s1, c : s2, e : s3, g : s4, h : n - before
  152.     // swap(s2, s4)
  153.     // a g e c h
  154.     node *a, *b, *c, *d, *e, *f, *g, *h;
  155.     split(root, s1, &a, &b);
  156.     split(b, s2, &c, &d);
  157.     split(d, s3, &e, &f);
  158.     split(f, s4, &g, &h);
  159.  
  160.     root = merge(a, merge(g, merge(e, merge(c, h))));
  161. //  return;
  162. //  int j = l1;
  163. //  for (int i = l2;i <= r2;++i)
  164. //      tmp[j++] = a[i];
  165. //  for (int i = r1+1;i < l2;++i)
  166. //      tmp[j++] = a[i];
  167. //  for (int i = l1;i <= r1;++i)
  168. //      tmp[j++] = a[i];
  169. //  for (int i = l1;i <= r2;++i)
  170. //      a[i] = tmp[i];
  171. }
  172. void chmin_lrk(int l, int r, int k) {
  173.     exit(1);
  174.     return;
  175.     for (int i = l;i <= r;++i)
  176.         if (a[i] > k) a[i] = k;
  177. }
  178. ll get_sum(int l, int r) {
  179.     node *a, *b, *c, *d;
  180.     int len = r - l + 1;
  181.     split(root, l-1, &a, &b);
  182.     split(b, len, &c, &d);
  183.     ll res = SUM(c);
  184.     root = merge( merge(a, c), d );
  185.     return res;
  186. //  ll s = 0;
  187. //  for (int i = l;i <= r;++i)
  188. //      s += a[i];
  189. //  return s;
  190. }
  191. int32_t main() {
  192.     scanf("%d %d", &N, &Q);
  193.     //N = readint(), Q = readint();
  194.  
  195.     for (int i = 1;i <= N;++i)
  196.         scanf("%lld", a + i);
  197.         //a[i] = readint();
  198.  
  199.  
  200.     for (int i = 1;i <= N;++i)
  201.         add_pk(i, a[i]);
  202.  
  203.     for (int t, p, l, r, x, y, k, i = 0;i < Q;++i) {
  204.  
  205. //      for (int i = 1;i <= SZ(root);++i)
  206. //          DE("%lld ", get_sum(i, i));
  207. //      DE("\n");
  208.  
  209.         //t = readint();
  210.         scanf("%d", &t);
  211.         if (t == 1) {
  212.             //p = readint(), k = readint();
  213.             scanf("%d %d", &p, &k);
  214.             add_pk(p, k);
  215.         }
  216.         if (t == 2) {
  217.             //p = readint();
  218.             scanf("%d", &p);
  219.             rm_p(p);
  220.         }
  221.         if (t == 3) {
  222.             //l = readint(), r = readint();
  223.             scanf("%d %d", &l, &r);
  224.             rev(l, r);
  225.         }
  226.         if (t == 4) {
  227.             //l = readint(), r = readint(), x = readint(), y = readint();
  228.             scanf("%d %d %d %d", &l, &r, &x, &y);
  229.             swap_block(l, r, x, y);
  230.         }
  231.         if (t == 5) {
  232.             //l = readint(), r = readint(), k = readint();
  233.             scanf("%d %d %d", &l, &r, &k);
  234.             chmin_lrk(l, r, k);
  235.         }
  236.         if (t == 6) {
  237.             //l = readint(), r = readint();
  238.             scanf("%d %d", &l, &r);
  239.             printf("%lld\n", get_sum(l, r));
  240.         }
  241.     }
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement