Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Code made by Bayemirov Beket
- #include <algorithm>
- //iostream is so mainstream
- #include <iostream>
- #include <assert.h>
- #include <stdlib.h>
- #include <cstring>
- #include <cstdlib>
- #include <stdio.h>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <cmath>
- #include <map>
- inline int Rand() { return (rand() << 16) | rand(); }
- #define y1 google
- #define ms(a,x) memset(a,x,sizeof a)
- #define sqr(x) x * x
- #define sz(x) (int)(x.size());
- #define INF 2147483647
- #define MOD 1000000007
- #define N 1005
- #define pf push_front
- #define pb push_back
- #define mp make_pair
- #define INF 2147483647
- #define F first
- #define S second
- #define fname ""
- using namespace std;
- typedef long long ll;
- struct node {
- ll l, r, key, y, cnt;
- bool rev;
- ll sum;
- };
- ll root, A, B, len = 0, M, l, r, p, new_val;
- node t[1000005];
- void push (ll v) {
- if (v && t[v].rev) {
- t[v].rev = false;
- swap (t[v].l, t[v].r);
- if (t[v].l)
- t[t[v].l].rev ^= true;
- if (t[v].r)
- t[t[v].r].rev ^= true;
- }
- }
- void update(ll v) {
- t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt + 1;
- t[v].sum = t[t[v].l].sum + t[t[v].r].sum + t[v].key;
- }
- void split (ll v, ll &A, ll &B, ll x, ll now = 0) {
- if (!v) {
- A = B = 0;
- return;
- }
- push(v);
- ll cur = now + t[t[v].l].cnt + 1;
- if (x < cur) {
- split (t[v].l, A, t[v].l, x, now);
- B = v;
- }
- else {
- split (t[v].r, t[v].r, B, x, now + t[t[v].l].cnt + 1);
- A = v;
- }
- update(v);
- }
- ll _merge (ll A, ll B) {
- if (!A || !B)
- return A + B;
- push(A);
- push(B);
- if (t[A].y > t[B].y) {
- t[A].r = _merge(t[A].r, B);
- update(A);
- return A;
- }
- else {
- t[B].l = _merge(A, t[B].l);
- update(B);
- return B;
- }
- }
- void add (ll v) {
- t[++len].key = v;
- t[len].y = Rand();
- t[len].cnt = 1;
- t[len].l = t[len].r = 0;
- t[len].sum = v;
- t[len].rev = false;
- root = _merge (root, len);
- }
- int main () {
- #ifndef ONLINE_JUDGE
- freopen(fname".in", "r", stdin);
- freopen(fname".out", "w", stdout);
- #endif
- ll n;
- scanf ("%I64d", &n);
- for (ll i = 1; i <= n; i++) {
- ll x;
- scanf ("%I64d", &x);
- add (x);
- }
- ll m;
- scanf ("%I64d", &m);
- char ch;
- while (m--) {
- scanf ("\n%c", &ch);
- if (ch == 'R') {
- M = 0;
- scanf ("%I64d%I64d", &l, &r);
- split (root, A, B, r);
- split (A, A, M, l - 1);
- t[M].rev ^= true;
- A = _merge (A, M);
- root = _merge (A, B);
- }
- else if (ch == 'Q') {
- M = 0;
- scanf ("%I64d%I64d", &l, &r);
- A = B = 0;
- split (root, A, B, r);
- split (A, A, M, l - 1);
- printf ("%I64d\n", t[M].sum);
- A = _merge (A, M);
- root = _merge (A, B);
- }
- else {
- A = B = M = 0;
- scanf ("%I64d%I64d", &p, &new_val);
- split (root, A, B, p);
- split (A, A, M, p - 1);
- t[M].key = new_val;
- t[M].sum = new_val;
- A = _merge(A, M);
- root = _merge(A, B);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement