90ptsWA#3 求调
查看原帖
90ptsWA#3 求调
355377
xiaozhuo楼主2023/9/8 10:28
#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
#define tagnone 1e5
int n, m, cnt, root, a[500010], rub[4000010], top;
struct Tree
{
	int size, sum, ml, mr, m, val, fa, son[2], tag, rev;
}tr[500010];
int rubbish()
{
	if(top == 0) return ++cnt;
	return rub[top --]; 
}
int New(int val, int fa)
{
	int node = rubbish();
	tr[node].size = 1;
	tr[node].m = tr[node].val = tr[node].sum = val;
	tr[node].ml = tr[node].mr = max(0, val);
	tr[node].rev = 0;
	tr[node].tag = tagnone;
	tr[node].son[0] = tr[node].son[1] = 0;
	tr[node].fa = fa;
	return node; 
}
void change_val(int now, int val)
{
	if(!now) return;
	tr[now].sum = val * tr[now].size;
	tr[now].m = max(tr[now].val, tr[now].sum);
	tr[now].ml = tr[now].mr = max(0, tr[now].sum);
	tr[now].tag = tr[now].val = val;
}
void change_rev(int now)
{
	if(!now) return;
	swap(tr[now].son[0], tr[now].son[1]);
	swap(tr[now].ml, tr[now].mr);
	tr[now].rev ^= 1;
}
void pushup(int now)
{
	tr[now].size = tr[tr[now].son[0]].size + tr[tr[now].son[1]].size + 1;
	tr[now].sum = tr[tr[now].son[0]].sum + tr[tr[now].son[1]].sum + tr[now].val;
	tr[now].ml = max(tr[tr[now].son[0]].ml, tr[tr[now].son[0]].sum + tr[tr[now].son[1]].ml + tr[now].val);
	tr[now].mr = max(tr[tr[now].son[1]].mr, tr[tr[now].son[1]].sum + tr[tr[now].son[0]].mr + tr[now].val);
	tr[now].m = max(max(tr[tr[now].son[0]].m, tr[tr[now].son[1]].m), tr[tr[now].son[0]].mr + tr[tr[now].son[1]].ml + tr[now].val);
}
void pushdown(int now)
{
	if(!now) return;
	if(tr[now].tag != tagnone)
	{
		int val = tr[now].tag;
		tr[now].tag = tagnone;
		tr[now].rev = 0;
		change_val(tr[now].son[0], val);
		change_val(tr[now].son[1], val);
	}
	if(tr[now].rev)
	{
		change_rev(tr[now].son[0]);
		change_rev(tr[now].son[1]);
		tr[now].rev ^= 1;
	}
}
void build(int l, int r, int &t, int fa)
{
	if(l > r) return;
	int mid = (l + r) >> 1;
	t = New(a[mid], fa);
	build(l, mid - 1, tr[t].son[0], t);
	build(mid + 1, r, tr[t].son[1], t);
	if(l == r) return;
	pushup(t);
}
void init()
{
	root = New(-INF, 0);
	tr[root].son[1] = New(INF, root);
	build(1, n, tr[2].son[0], 2);
	pushup(2), pushup(1);
}
void rotate(int x)
{
	int y = tr[x].fa, z = tr[y].fa;
	int k = x == tr[y].son[0];
	tr[y].son[k ^ 1] = tr[x].son[k];
	tr[tr[x].son[k]].fa = y;
	tr[x].fa = z;
	if(z) tr[z].son[y == tr[z].son[1]] = x;
	tr[x].son[k] = y;
	tr[y].fa = x;
	pushup(y), pushup(x);
}
void splay(int x, int goal)
{
	while(tr[x].fa != goal)
	{
		int y = tr[x].fa, z = tr[y].fa;
		if(z != goal) (tr[y].son[0] == x) ^ (tr[z].son[0] == y) ? rotate(x) : rotate(y);
		rotate(x);
	}
	if(!goal) root = x;
}
int findk(int rk)
{
	int now = root;
	while(1)
	{
		pushdown(now);
		if(!now) return 0;
		if(tr[tr[now].son[0]].size >= rk) now = tr[now].son[0];
		else
		{
			rk -= (tr[tr[now].son[0]].size + 1);
			if(rk <= 0) return now;
			now = tr[now].son[1];
		}
	}
}
int split(int k, int len)
{
	int x = findk(k - 1), y = findk(k + len);
	splay(x, 0), splay(y, x);
	return tr[y].son[0];
}
void reverse(int now)
{
	if(!now) return;
	reverse(tr[now].son[0]);
	reverse(tr[now].son[1]);
	tr[now].fa = tr[now].son[0] = tr[now].son[1] = tr[now].size = 0;
	rub[++top] = now;
}
void insert(int k, int len)
{
	for(int i = 1;i <= len;i ++) cin >> a[i];
	int x = findk(k + 1);
	int y = findk(k + 2);
	splay(x, 0);
	splay(y, x);
	build(1, len, tr[y].son[0], y);
	pushup(y), pushup(x);
}
void del(int k, int len)
{
	int now = split(k + 1, len);
	int y = tr[now].fa;
	tr[y].son[0] = 0;
	reverse(now);
	pushup(y), pushup(tr[y].fa);
}
void update(int k, int len)
{
	int val;
	cin >> val;
	int now = split(k + 1, len);
	change_val(now, val);
	int y = tr[now].fa, x = tr[y].fa;
	pushup(y), pushup(x);
}
void rev(int k, int len)
{
	int now = split(k + 1, len);
	change_rev(now);
	int y = tr[now].fa, x = tr[y].fa;
	pushup(y), pushup(x);
}
void query(int k, int len)
{
	int now = split(k + 1, len);
	cout << tr[now].sum << endl;
}
void ma()
{
	splay(1, 0), splay(2, 1);
	cout << tr[tr[2].son[0]].m << endl;
}
void print(int now)
{
	if(tr[now].son[0]) print(tr[now].son[0]);
	cout << tr[now].val << " ";
	if(tr[now].son[1]) print(tr[now].son[1]);
}
int main()
{
	cin >> n >> m;
	for(int i = 1;i <= n;i ++) cin >> a[i];
	init();
//	insert(2, 2);
//	print(root);
//	cout << endl;
//	del(2, 3);
//	print(root);
//	cout << endl;
//	insert(3, 1);
//	print(root);
	tr[0].m = -INF;
	a[0] = a[n + 1] = -INF;
	while(m --)
	{
		string s;
		int k, len;
		cin >> s;
		if(s != "MAX-SUM") cin >> k >> len;
		else ma();
		if(s == "GET-SUM" && len == 0) 
		{
			cout << 0 << endl;
			continue;
		}
		if(s == "INSERT") insert(k, len);
		if(s == "DELETE") del(k, len);
		if(s == "MAKE-SAME") update(k, len);
		if(s == "REVERSE") rev(k, len);
		if(s == "GET-SUM") query(k, len);
	}
	return 0;
}
2023/9/8 10:28
加载中...