蒟蒻求调 fhq 10pts
查看原帖
蒟蒻求调 fhq 10pts
716260
SegmentTree_楼主2024/7/1 00:21
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
const int N = 1e6+5;
struct node{
	int ls, rs;
	int pri, siz;
	ll val;
	ll sum;
	int lazy1, lazy;
	ll lmax, rmax, ans;
	void clear(){
		ls = rs = 0;
		pri = rand();
		siz = 1;
		val = sum = lmax = rmax = ans = 0;
		lazy = 0;
		lazy1 = 114514;
	}
}tr[N];
int tsiz = 0, root = 0;
int tot;
stack<int> st;
void update(int x){
	tr[x].siz = tr[tr[x].ls].siz + tr[tr[x].rs].siz + 1;
	tr[x].sum = tr[x].val + tr[tr[x].ls].sum + tr[tr[x].rs].sum;
	tr[x].lmax = max(max(tr[tr[x].ls].lmax, tr[tr[x].ls].sum + tr[x].val + tr[tr[x].rs].lmax), 0LL);
	tr[x].rmax = max(max(tr[tr[x].rs].rmax, tr[tr[x].rs].sum + tr[x].val + tr[tr[x].ls].rmax), 0ll);
	tr[x].ans = max(tr[tr[x].ls].rmax + tr[tr[x].rs].lmax, 0ll) + tr[x].val;
	if (tr[x].ls) tr[x].ans = max(tr[x].ans, tr[tr[x].ls].ans);
	if (tr[x].rs) tr[x].ans = max(tr[x].ans, tr[tr[x].rs].ans);
}
void newnode(int v){
	if (st.size()){
		tsiz = st.top();
		st.pop();
	}
	else tsiz = ++tot;
	tr[tsiz].clear();
	tr[tsiz].siz = 1;
	tr[tsiz].ls = tr[tsiz].rs = 0;
	tr[tsiz].pri = rand();
	tr[tsiz].val = v;
	tr[tsiz].sum = v;
	tr[tsiz].lmax = tr[tsiz].rmax = max(v, 0LL);
	tr[tsiz].ans = v;
	tr[tsiz].lazy = 0;
	tr[tsiz].lazy1 = 114514;
}
void pushdown(int x){
	if (x && tr[x].lazy1 != 114514){
		int v = tr[x].lazy1;
		tr[tr[x].ls].lazy1 = tr[x].lazy1;
		tr[tr[x].rs].lazy1 = tr[x].lazy1;
		tr[tr[x].ls].val = v;
		tr[tr[x].ls].lmax = tr[tr[x].ls].rmax = max(v * tr[tr[x].ls].siz, 0LL);
		tr[tr[x].ls].sum = v * tr[tr[x].ls].siz;
		tr[tr[x].ls].ans = max(v, tr[tr[x].ls].sum);
		tr[tr[x].rs].val = v;
		tr[tr[x].rs].lmax = tr[tr[x].rs].rmax = max(v * tr[tr[x].rs].siz, 0LL);
		tr[tr[x].rs].sum = v * tr[tr[x].rs].siz;
		tr[tr[x].rs].ans = max(v, tr[tr[x].rs].sum);
		tr[x].lazy1 = 114514;
	}
	if (x && tr[x].lazy){
		swap(tr[x].ls, tr[x].rs);
		tr[tr[x].ls].lazy ^= 1;
		tr[tr[x].rs].lazy ^= 1;
		//update(tr[x].ls);
		//update(tr[x].rs);
		tr[x].lazy = 0;
	}
}
void split(int rt, int x, int &l, int &r){
	if (rt == 0){
		l = r = 0;
		return;
	}
	pushdown(rt);
	if (tr[tr[rt].ls].siz < x) l = rt, split(tr[rt].rs, x - tr[tr[rt].ls].siz - 1, tr[rt].rs, r);
	else r = rt, split(tr[rt].ls, x, l, tr[rt].ls);
	update(rt);
}
int merge(int l, int r){
	if (l == 0 || r == 0) return l + r;
	if (tr[l].pri > tr[r].pri){
		pushdown(l);
		tr[l].rs = merge(tr[l].rs, r);
		update(l);
		return l;
	}
	else{
		pushdown(r);
		tr[r].ls = merge(l, tr[r].ls);
		update(r);
		return r;
	}
}
void remove(int x){
	if (!x) return;
	st.push(x);
	remove(tr[x].ls);
	remove(tr[x].rs);
}
void insert(int v){
	newnode(v);
	root = merge(root, tsiz);
}
void print(int x){
	if (!x) return
	pushdown(x);
	print(tr[x].ls);
	cout << tr[x].val << ' ';
	print(tr[x].rs);
}
void modify(int l, int r, ll v){
	int a, b, c;
	split(root, l - 1, a, b);
	split(b, r - l + 1, b, c);
	tr[b].lazy1 = v;
	tr[b].val = v;
	tr[b].lmax = tr[b].rmax = max(v * tr[b].siz, 0LL);
	tr[b].sum = v * tr[b].siz;
	tr[b].ans = max(v, tr[b].sum);
	root = merge(a, merge(b, c));
}
void rev(int l, int r){
	int a, b, c;
	split(root, l - 1, a, b);
	split(b, r - l + 1, b, c);
	tr[b].lazy ^= 1;
	root = merge(a, merge(b, c));
}
int query2(int l, int r){
	int a, b, c;
	split(root, l - 1, a, b);
	split(b, r - l + 1, b, c);
	ll ans = tr[b].ans;
	root = merge(a, merge(b, c));
	return ans;
}
int query1(int l, int r){
	int a, b, c;
	split(root, l - 1, a, b);
	split(b, r - l + 1, b, c);
	ll ans = tr[b].sum;
	root = merge(a, merge(b, c));
	return ans;
}
void del(int l, int r){
	int a, b, c;
	split(root, l - 1, a, b);
	split(b, r - l + 1, b, c);
	remove(b);
	root = merge(a, c);
}
int n, q;
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	srand(time(NULL));
	cin >> n >> q;
	for (int i = 1;i <= n;i++){
		int v;
		cin >> v;
		insert(v);
	}
	while (q--){
		string op;
		cin >> op;
		if (op == "INSERT"){
			int pos, to;
			cin >> pos >> to;
			int a, b;
			split(root, pos, a, b);
			for (int i = 1;i <= to;i++){
				int c;
				cin >> c;
				newnode(c);
				a = merge(a, tsiz);
			}
			root = merge(a, b);
		}
		else if (op == "DELETE"){
			int pos, to;
			cin >> pos >> to;
			del(pos, pos + to - 1);
		}
		else if (op == "MAKE-SAME"){
			int l, r, v;
			cin >> l >> r >> v;
			modify(l, l + r - 1, v);
		}
		else if (op == "REVERSE"){
			int l, r;
			cin >> l >> r;
			rev(l, l + r - 1);
		}
		else if (op == "GET-SUM"){
			int l, r;
			cin >> l >> r;
			cout << query1(l, l + r - 1) << '\n';
		}
		else if (op == "MAX-SUM"){
			cout << tr[root].ans << '\n';
		}
	}
	return 0;
}
2024/7/1 00:21
加载中...