Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define SET(x, a) memset(x, a, sizeof x )
- #define pll pair<ll,ll>
- #define INF 1001
- #define pii pair<int, int>
- // const double pi = acos(-1);
- #define MAXN 2850003
- struct splaytree{
- struct node{
- node * ch[2];
- node * f;
- ll sz, maxsum, mls, mrs, val, sum;
- bool same, rev;
- int lsize () {return ch[0]->sz;}
- int rsize () {return ch[1]->sz;}
- } * root, * null, *lb, *rb, SS[MAXN];
- int SC;
- node * newnode(int value, node * father){
- node * e = SS+ ++SC;
- e->f = father;
- e->maxsum = e->mls = e->mrs = e->val = e->sum = value;
- e->sz = 1;
- e->same = e->rev = false;
- e->ch[1] = e->ch[0] = null;
- return e;
- }
- void init(){
- SC = 0;
- null = newnode(-INF, 0);
- null->sz = 0;
- lb = root = newnode(-INF, null);
- rb = root->ch[1] = newnode(-INF, root);
- null->sum = lb->sum = rb->sum = 0;
- update(root);
- }
- void update(node * x){
- if(x == null) return;
- x->sz = x->ch[0]->sz + x->ch[1]->sz + 1;
- x->sum = x->val;
- if(x->ch[0]->val == -INF) x->sum += x->ch[0]->sum;
- if(x->ch[1]->val == -INF) x->sum += x->ch[1]->sum;
- // x->sum = x->ch[0]->sum + x->ch[1]->sum + x->val;
- x->mrs = x->val + x->ch[1]->sum;
- x->mrs = max(x->mrs, max(x->ch[1]->sum + x->val + x->ch[0]->mrs, x->ch[1]->mrs));
- x->mls = x->val + x->ch[0]->sum;
- x->mls = max(x->mls, max(x->ch[0]->sum + x->val + x->ch[1]->mls, x->ch[0]->mls));
- x->maxsum = x->val;
- x->maxsum = max(x->maxsum, max(x->ch[0]->maxsum, max(x->ch[1]->maxsum, x->ch[1]->mls + x->ch[0]->mrs + x->val)));
- x->maxsum = max(x->maxsum, max(x->mls + x->val, x->mrs + x->val));
- cout << x->lsize() << ' ' << x->rsize() << ' ' << x->sum << endl;
- }
- void pushdown(node * x){
- if(x == null) return;
- if(x->rev){
- x->rev = 0;
- node * tmp = x->ch[0];
- x->ch[0] = x->ch[1];
- x->ch[1] = tmp;
- x->ch[0]->rev ^= 1;
- x->ch[1]->rev ^= 1;
- swap(x->mls, x->mrs);
- }
- if(x->same){
- x->same=false;
- x->ch[0]->same=x->ch[1]->same=true;
- x->ch[0]->val=x->ch[1]->val=x->val;
- x->sum = x->maxsum = x->mls = x->mrs = x->val * x->sz;
- if(x->val < 0)
- x->maxsum = x->mls = x->mrs = x->val;
- }
- }
- void rotate(node *x,int o){
- node *y=x->f;
- pushdown(y->ch[!o]);
- pushdown(x->ch[0]);
- pushdown(x->ch[1]);
- y->ch[o] = x->ch[!o];
- y->ch[o]->f=y;
- x->f = y->f;
- if(y->f->ch[0]==y)
- y->f->ch[0]=x;
- else
- y->f->ch[1]=x;
- y->f=x;
- x->ch[!o]=y;
- update(y);
- update(x);
- if (root==y) root=x;
- }
- void Delete(int pos, int tot){
- select(pos, null);
- select(pos + tot + 1, root);
- root->ch[1]->ch[0] = null;
- splay(root->ch[1], null);
- }
- void splay(node * x, node * y){
- pushdown(x);
- while(x->f != y){
- if(x->f->f == y){
- if(x->f->ch[0] == x) rotate(x, 0);
- else rotate(x, 1);
- }else if(x->f->f->ch[0] == x->f){
- if(x->f->ch[0] == x) rotate(x->f, 0), rotate(x, 0);
- else rotate(x,1), rotate(x, 0);
- }else{
- if(x->f->ch[1] == x) rotate(x->f, 1), rotate(x, 1);
- else rotate(x, 0), rotate(x, 1);
- }
- }
- }
- void select(int k, node * f){
- node * x = root;
- pushdown(x);
- // cout << x->lsize() << ' ' << x->rsize() << endl;
- // cout << x->ch[0]->val << ' ' << x->ch[1]->val << endl;
- for(;k != (x->lsize() + 1);){
- if(k <= x->lsize()) x = x->ch[0];
- else{
- k -= x->lsize() + 1;
- x = x->ch[1];
- }
- pushdown(x);
- }
- splay(x, f);
- }
- void insert(int pos, int tot, int *C){
- node *t, *z;
- z = t = newnode(C[1], null);
- for(int i = 2; i <= tot; ++i)
- z = z->ch[1] = newnode(C[i], z);
- select(pos + 1, null);
- select(pos + 2, root);
- // cout << "hi" << endl;
- root->ch[1]->ch[0] = t;
- t->f = root->ch[1];
- // cout << "hi" << endl;
- splay(z, null);
- }
- void MakeSame(int pos,int tot,int value){
- select(pos,null);
- select(pos+tot+1,root);
- root->ch[1]->ch[0]->same=true;
- root->ch[1]->ch[0]->val=value;
- splay(root->ch[1]->ch[0],null);
- }
- void Reverse(int pos,int tot){
- select(pos,null);
- select(pos+tot+1,root);
- root->ch[1]->ch[0]->rev=!root->ch[1]->ch[0]->rev;
- splay(root->ch[1]->ch[0],null);
- }
- int GetSum(int pos,int tot){
- select(pos,null);
- select(pos+tot+1,root);
- pushdown(root->ch[1]->ch[0]);
- return root->ch[1]->ch[0]->sum;
- }
- int MaxSum(){
- pushdown(root);
- update(root);
- cout << root->maxsum << ' ' << root->mls << ' ' << root->mrs << endl;
- cout << root->lsize() << ' ' << root->rsize() << endl;
- return root->maxsum;
- }
- }Splay;
- int C[500001];
- int main(){
- ios::sync_with_stdio(false);
- cin.tie(0);
- int n, m; cin >> n >> m;
- for(int i = 1; i <= n; ++i)
- cin >> C[i];
- Splay.init();
- Splay.insert(0, n, C);
- while(m--){
- string cmd; cin >> cmd;
- if(cmd[0] == 'I'){
- int len, pos; cin >> pos >> len;
- for(int i = 1; i <= len;++i)
- cin >> C[i];
- Splay.insert(pos, len, C);
- }else if(cmd[0] == 'D'){
- int pos, tot; cin >> pos >> tot;
- Splay.Delete(pos, tot);
- }else if(cmd == "MAKE-SAME"){
- int pos, tot,val; cin >> pos >> tot >> val;
- Splay.MakeSame(pos, tot, val);
- }else if(cmd[0] == 'R'){
- int pos, tot; cin >> pos >> tot;
- Splay.Reverse(pos, tot);
- }else if(cmd[0] == 'G'){
- int pos, tot; cin >> pos >> tot;
- cout << Splay.GetSum(pos, tot) << endl;
- }else{
- cout << Splay.MaxSum() << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement