Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <assert.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdbool.h>
- #ifdef KEV
- #define DE(args...) fprintf(stderr, args), fflush(stderr)
- #else
- #define DE(...) 0
- #endif
- typedef long long ll;
- #define MAX_N 200010
- int N, Q;
- ll a[MAX_N];
- //char fastchar() {
- // static char buf[1 << 20];
- // static char *p = buf, *end = buf;
- // if (p == end && (end = (p = buf) + fread(buf, 1, 1 << 20, stdin)) == buf) return EOF;
- // return *p++;
- //}
- //
- //int readint() {
- // char c;
- // while (!isdigit(c = fastchar()));
- // int x = c - '0';
- // while (isdigit(c = fastchar()))
- // x = x * 10 + c - '0';
- // return x;
- //}
- //
- //
- typedef struct _node {
- int sz, val;
- ll sum;
- uint32_t pri;
- bool rev;
- struct _node *l, *r;
- } node;
- uint32_t my_rand() {
- static uint32_t x = 21389;
- return x *= 0xdefaced, ++x;
- }
- int SZ(node* s) { return s == NULL ? 0 : s->sz; }
- ll SUM(node* s) { return s == NULL ? 0 : s->sum; }
- node* gen_node(int val) {
- static node room[MAX_N];
- static node* ptr = room;
- node* ret = ptr++;
- ret->sz = 1;
- ret->val = ret->sum = val;
- ret->l = ret->r = NULL;
- ret->pri = my_rand();
- ret->rev = false;
- return ret;
- }
- node* root = NULL;
- void update(node* now) {
- if (now == NULL) return;
- now->sz = 1 + SZ(now->l) + SZ(now->r);
- now->sum = now->val + SUM(now->l) + SUM(now->r);
- }
- void go_rev(node* now) {
- if (now == NULL) return;
- now->rev ^= 1;
- // swap(now->l, now->r);
- {
- node* tmp = now->l;
- now->l = now->r;
- now->r = tmp;
- }
- }
- void push(node* now) {
- if (now == NULL) return;
- if (now->rev) {
- now->rev = false;
- go_rev(now->l), go_rev(now->r);
- }
- }
- node* merge(node *a, node *b) {
- if (!a) return b;
- if (!b) return a;
- if (a->pri > b->pri) {
- push(a);
- a->r = merge(a->r, b);
- update(a);
- } else {
- push(b);
- b->l = merge(a, b->l);
- update(b);
- }
- }
- void split(node *now, int sz, node **a, node **b) {
- if (!now) {
- (*a) = (*b) = NULL;
- return;
- }
- push(now);
- if (SZ(now->l) + 1 <= sz) {
- (*a) = now;
- split(now->r, sz - 1 - SZ(now->l), &((*a)->r), b);
- update(*a);
- } else {
- (*b) = now;
- split(now->l, sz, a, &((*b)->l));
- update(*b);
- }
- }
- // 1 based
- // put at 0 based p index
- void add_pk(int p, int k) {
- node *a, *b;
- split(root, p, &a, &b);
- root = merge( merge(a, gen_node(k)), b );
- // for (int i = N++;i > p;--i)
- // a[i+1] = a[i];
- // a[p+1] = k;
- }
- void rm_p(int p) {
- node *a, *b, *c, *d;
- split(root, p-1, &a, &b);
- split(b, 1, &c, &d);
- root = merge(a, d);
- // for (int i = p;i < N;++i)
- // a[i] = a[i+1];
- // --N;
- }
- void rev(int l, int r) {
- node *a, *b, *c, *d;
- int len = r - l + 1;
- split(root, l-1, &a, &b);
- split(b, len, &c, &d);
- go_rev(c);
- root = merge( merge(a, c), d );
- }
- void swap_block(int l1, int r1, int l2, int r2) {
- if (l1 > l2) {
- l1 ^= l2 ^= l1 ^= l2;
- r1 ^= r2 ^= r1 ^= r2;
- }
- int s1 = l1-1, s2 = r1-l1+1, s3 = l2-r1-1, s4 = r2-l2+1;
- // a : s1, c : s2, e : s3, g : s4, h : n - before
- // swap(s2, s4)
- // a g e c h
- node *a, *b, *c, *d, *e, *f, *g, *h;
- split(root, s1, &a, &b);
- split(b, s2, &c, &d);
- split(d, s3, &e, &f);
- split(f, s4, &g, &h);
- root = merge(a, merge(g, merge(e, merge(c, h))));
- // return;
- // int j = l1;
- // for (int i = l2;i <= r2;++i)
- // tmp[j++] = a[i];
- // for (int i = r1+1;i < l2;++i)
- // tmp[j++] = a[i];
- // for (int i = l1;i <= r1;++i)
- // tmp[j++] = a[i];
- // for (int i = l1;i <= r2;++i)
- // a[i] = tmp[i];
- }
- void chmin_lrk(int l, int r, int k) {
- exit(1);
- return;
- for (int i = l;i <= r;++i)
- if (a[i] > k) a[i] = k;
- }
- ll get_sum(int l, int r) {
- node *a, *b, *c, *d;
- int len = r - l + 1;
- split(root, l-1, &a, &b);
- split(b, len, &c, &d);
- ll res = SUM(c);
- root = merge( merge(a, c), d );
- return res;
- // ll s = 0;
- // for (int i = l;i <= r;++i)
- // s += a[i];
- // return s;
- }
- int32_t main() {
- scanf("%d %d", &N, &Q);
- //N = readint(), Q = readint();
- for (int i = 1;i <= N;++i)
- scanf("%lld", a + i);
- //a[i] = readint();
- for (int i = 1;i <= N;++i)
- add_pk(i, a[i]);
- for (int t, p, l, r, x, y, k, i = 0;i < Q;++i) {
- // for (int i = 1;i <= SZ(root);++i)
- // DE("%lld ", get_sum(i, i));
- // DE("\n");
- //t = readint();
- scanf("%d", &t);
- if (t == 1) {
- //p = readint(), k = readint();
- scanf("%d %d", &p, &k);
- add_pk(p, k);
- }
- if (t == 2) {
- //p = readint();
- scanf("%d", &p);
- rm_p(p);
- }
- if (t == 3) {
- //l = readint(), r = readint();
- scanf("%d %d", &l, &r);
- rev(l, r);
- }
- if (t == 4) {
- //l = readint(), r = readint(), x = readint(), y = readint();
- scanf("%d %d %d %d", &l, &r, &x, &y);
- swap_block(l, r, x, y);
- }
- if (t == 5) {
- //l = readint(), r = readint(), k = readint();
- scanf("%d %d %d", &l, &r, &k);
- chmin_lrk(l, r, k);
- }
- if (t == 6) {
- //l = readint(), r = readint();
- scanf("%d %d", &l, &r);
- printf("%lld\n", get_sum(l, r));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement