跳到正文

剪贴板

剪贴板 ea5o1yfg

原文
剪贴板信息

剪贴板信息

收录
更新

splay P2042维护数列

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define FL(a, b, c) for(int a = (b), a##end = (c); a <= a##end; a++)
#define FR(a, b, c) for(int a = (b), a##end = (c); a >= a##end; a--)
#define lowbit(x) ((x) & -(x))
constexpr int N = 5e6 + 10;
#define INF 1000000
int a[N];
struct Splay{
	struct node{
        int size, val, ch[2], fa;
        int cov, rev, sum, lm, rm, m;
    }d[N];
	#define ls(x) d[x].ch[0]
	#define rs(x) d[x].ch[1]
	#define fa(x) d[x].fa
	int root, tot = 0, stk[N], top;
	int newnode(int val){
		int w = top ? stk[top--] : ++tot;
        d[w].rev = ls(w) = rs(w) = fa(w) = 0;
        d[w].val = d[w].sum = d[w].m = val;
        d[w].lm = d[w].rm = max(0, val);
        d[w].cov = INF, d[w].size = 1;
        return w;
	}
    void change(int x, int c){
        if(!x)return;
        if(c == INF)swap(ls(x), rs(x)), swap(d[x].lm, d[x].rm), d[x].rev ^= 1;
        else d[x].m = max((d[x].val = d[x].cov = c), (d[x].sum = c * d[x].size)), d[x].lm = d[x].rm = max((d[x].rev = 0), (d[x].sum));
    }
	void pushdown(int x){
		if(d[x].cov != INF){
            change(ls(x), d[x].cov), change(rs(x), d[x].cov);
            d[x].cov = INF, d[x].rev = 0;
        }
        if(d[x].rev)change(ls(x), INF), change(rs(x), INF), d[x].rev = 0;
	}
	void pushup(int u){
        node &z = d[u], &ls = d[ls(u)], &rs = d[rs(u)];
        z.sum = z.val + ls.sum + rs.sum, z.size = 1 + ls.size + rs.size;
        z.lm = max(ls.lm, ls.sum + z.val + rs.lm);
        z.rm = max(rs.rm, rs.sum + z.val + ls.rm);
        z.m = max(ls.rm + rs.lm, 0) + z.val;
        if(rs(u))z.m = max(z.m, rs.m);
        if(ls(u))z.m = max(z.m, ls.m);
    }
	void delnode(int x){if(x)stk[++top] = x, delnode(ls(x)), delnode(rs(x));}
	bool get(int x){return x == rs(fa(x));}
	void rotate(int x){
		int y = fa(x), z = fa(y), c = get(x);
		if(d[x].ch[!c])fa(d[x].ch[!c]) = y;
		d[y].ch[c] = d[x].ch[!c], d[x].ch[!c] = y, fa(y) = x, fa(x) = z;
		if(z) d[z].ch[y == d[z].ch[1]] = x;
		pushup(y), pushup(x);
	}
	void splay(int x, int p){
		for(int f = fa(x); f = fa(x), (f ^ p); rotate(x))
			if(fa(f) ^ p)rotate(get(f) == get(x) ? f : x);
		if(!p)root = x;
	}
	int find(int k){
		int now = root, siz;
        k++;
		while(now){
			pushdown(now);
			if((siz = d[ls(now)].size + 1) > k) now = ls(now);
			else if(!(k -= siz)) break;
			else now = rs(now);
        }
		return now;
	}
	void print(int now){
		pushdown(now);
		if(ls(now)) print(ls(now));
		if(d[now].val != INF) cout << d[now].val << " ";
		if(rs(now)) print(rs(now));
	}
	int build(int l, int r){
        if(l > r)return 0;
		if(l == r)return newnode(a[l]);
        int mid = l + r >> 1, z = newnode(a[mid]);
        ls(z) = build(l, mid - 1), rs(z) = build(mid + 1, r);
        fa(ls(z)) = z, fa(rs(z)) = z;
        return pushup(z), z;
	}
    void split(int l, int r){
        splay(l = find(l - 1), 0), splay(find(r + 1), root);
    }
	void insert(){
        int pos, tot;
        cin >> pos >> tot;
        FL(i, 1, tot)cin >> a[i];
        int z = build(1, tot);
        splay(find(pos), 0);
        int x = rs(root);
        fa(z) = root, rs(root) = z;
        while(rs(z))z = rs(z);
        fa(x) = z, rs(z) = x;
        for(int i = x; i; i = fa(i))pushup(i);
    }
    void del(){
        int pos, tot;
        cin >> pos >> tot;
        split(pos, pos + tot - 1);
        fa(ls(rs(root))) = 0, delnode(ls(rs(root))), ls(rs(root)) = 0;
        pushup(rs(root)), pushup(root);
    }
    void cover(){
        int pos, tot, c;
        cin >> pos >> tot >> c;
        split(pos, pos + tot - 1);
        change(ls(rs(root)), c);
        pushup(rs(root)), pushup(root);
    }
    void reverse(){
        int pos, tot;
        cin >> pos >> tot;
        split(pos, pos + tot - 1);
        change(ls(rs(root)), INF);
        pushup(rs(root)), pushup(root);
    }
    void sum(){
        int pos, tot;
        cin >> pos >> tot;
        split(pos, pos + tot - 1);
        cout << d[ls(rs(root))].sum << endl;
    }
    void max_sum(){
        cout << d[root].m << endl;
    }
}T;
int32_t main(){
   // freopen("1.txt", "w", stdout);
	cin.tie(0)->sync_with_stdio(0);
	int n, m;
	cin >> n >> m;
    a[0] = a[n + 1] = -1e4;
    FL(i, 1, n)cin >> a[i];
	T.root = T.build(0, n + 1);
	while(m--){
        string s;
        cin >> s;
        if(s == "INSERT")T.insert();
        else if(s[0] == 'D')T.del();
        else if(s == "MAKE-SAME")T.cover();
        else if(s == "REVERSE")T.reverse();
        else if(s == "GET-SUM")T.sum();
        else T.max_sum();
    }
	return 0;
}