0 分 splay 求调 qwq
查看原帖
0 分 splay 求调 qwq
341373
Autofreeze楼主2021/6/13 13:58
#include<bits/stdc++.h>
#define N 501001
#define MAX 2001
#define re register
#define inf 1e18
using namespace std; 
typedef long long ll;
typedef double db;
inline void read(re ll &ret)
{
	ret=0;re char c=getchar();re bool pd=false;
	while(!isdigit(c)){pd|=c=='-';c=getchar();}
	while(isdigit(c)){ret=(ret<<1)+(ret<<3)+(c&15);c=getchar();}
	ret=pd?-ret:ret;
	return;
}
ll n,m,a[N],id[N],root,cnt;
queue<ll>q;
struct node
{
	ll son[2],fa,siz,sum,maxn,lmax,rmax,val;
	bool tag,rev;
}spy[N];
char s[N];
#define ls(x) spy[x].son[0]
#define rs(x) spy[x].son[1]
#define fa(x) spy[x].fa
inline void pushup(re ll pos)
{
	if(!ls(pos)&&!rs(pos))
	{
		spy[pos].siz=1,spy[pos].sum=spy[pos].val,spy[pos].maxn=spy[pos].val;
		spy[pos].lmax=spy[pos].rmax=spy[pos].val*(spy[pos].val>=0);
	}
	else if(!ls(pos))
	{
		spy[pos].siz=spy[rs(pos)].siz+1;
		spy[pos].sum=spy[rs(pos)].sum+spy[pos].val;
		spy[pos].maxn=max(spy[rs(pos)].maxn,spy[pos].val+spy[rs(pos)].lmax);
		spy[pos].lmax=spy[pos].val+spy[rs(pos)].lmax;
		spy[pos].rmax=max(spy[rs(pos)].rmax,spy[rs(pos)].sum+spy[pos].val);
	}
	else if(!rs(pos))
	{
		spy[pos].siz=spy[ls(pos)].siz+1;
		spy[pos].sum=spy[ls(pos)].sum+spy[pos].val;
		spy[pos].maxn=max(spy[ls(pos)].maxn,spy[ls(pos)].rmax+spy[pos].val);
		spy[pos].lmax=max(spy[ls(pos)].lmax,spy[ls(pos)].sum+spy[pos].val);
		spy[pos].rmax=spy[pos].val+spy[ls(pos)].rmax;
	}
	else
	{
		spy[pos].siz=spy[ls(pos)].siz+spy[rs(pos)].siz+1;
		spy[pos].sum=spy[ls(pos)].sum+spy[rs(pos)].sum+spy[pos].val;
		spy[pos].maxn=max(spy[ls(pos)].maxn,max(spy[rs(pos)].maxn,spy[ls(pos)].rmax+spy[rs(pos)].lmax+spy[pos].val));
		spy[pos].lmax=max(spy[ls(pos)].lmax,spy[ls(pos)].sum+spy[rs(pos)].lmax+spy[pos].val);
		spy[pos].rmax=max(spy[rs(pos)].rmax,spy[rs(pos)].sum+spy[ls(pos)].rmax+spy[pos].val);
	}
	
	return;
}
inline void add(re ll pos,re ll num)
{
	spy[pos].val=num;
	spy[pos].sum=spy[pos].siz*num;
	if(num>=0)
		spy[pos].maxn=spy[pos].lmax=spy[pos].rmax=num*spy[pos].siz;
	else
		spy[pos].maxn=num,spy[pos].lmax=spy[pos].rmax=0;
	return;
}
inline void reverse(re ll pos)
{
	spy[pos].rev^=1;
	swap(ls(pos),rs(pos));
	swap(spy[pos].lmax,spy[pos].rmax);
	return;
}
inline void pushdown(re ll pos)
{
	if(spy[pos].tag)
	{
		spy[pos].tag=spy[pos].rev=0;
		if(ls(pos))
			add(ls(pos),spy[pos].val);
		if(rs(pos))
			add(rs(pos),spy[pos].val);
	}
	if(spy[pos].rev)
	{
		spy[pos].rev=0;
		if(ls(pos))
			reverse(ls(pos));
		if(rs(pos))
			reverse(rs(pos));
	}
	return;
}
inline bool whison(re ll x)
{
	return rs(fa(x))==x;
}
inline void connect(re ll p,re ll q,re bool whi)
{
	fa(p)=q;
	if(q)
		spy[q].son[whi]=p,pushup(q);
	return;
}
inline void rotate(re ll x)
{
	re ll f=fa(x),g=fa(f);
	re bool f1=whison(x),f2=whison(f);
	connect(spy[x].son[!f1],f,f1);
	connect(f,x,!f1);
	connect(x,g,f2);
	pushup(f);
	pushup(x);
	return;
}
inline void pushall(re ll x)
{
	if(x^root)
		pushall(fa(x));
	pushdown(x);
	return;
}
inline void splay(re ll x,re ll f)
{
	pushall(x);
	while(fa(x)^f)
	{
		re ll fat=fa(x);
		if(fa(fat)^f)
		{
			if(whison(x)==whison(fat))
				rotate(fat);
			else
				rotate(x);
		}
		rotate(x);
	}
	if(!f)
		root=x;
	return;
}
inline void build(re ll l,re ll r,re ll fat)
{
	re ll mid=l+r>>1;
	re ll now=id[mid],pre=id[fat];
	spy[now].val=a[mid];
	if(l==r)
	{
		spy[now].sum=spy[now].maxn=a[l];
		spy[now].siz=1;
		if(a[l]>=0)
			spy[now].lmax=spy[now].rmax=a[l];
		else
			spy[now].lmax=spy[now].rmax=0;
	}
	if(l<mid)
		build(l,mid-1,mid);
	if(mid<r)
		build(mid+1,r,mid);
	fa(now)=pre;
	pushup(now);
	if(pre)
		spy[pre].son[mid>=fat]=now;
	return;
}
inline ll find(re ll pos,re ll k)
{
	pushdown(pos);
	if(ls(pos)&&spy[ls(pos)].siz>=k)
		return find(ls(pos),k);
	else
	{
		if(ls(pos))
			k-=spy[ls(pos)].siz;
		k--;
		if(!k)
			return pos;
		return find(rs(pos),k);
	}
}
inline ll split(re ll l,re ll r)
{
	re ll num1=find(root,l),num2=find(root,r+2);
	splay(num1,0);
	splay(num2,num1);
	return ls(num2);
}
inline void insert(re ll pos,re ll tot)
{
	for(re int i=1;i<=tot;i++)
		read(a[i]);
	for(re int i=1;i<=tot;i++)
	{
		if(!q.empty())
		{
			id[i]=q.front();
			q.pop();	
		}
		else
			id[i]=++cnt;
	}
	build(1,tot,0);
	re ll num1=find(root,pos+1),num2=find(root,pos+2);
	splay(num1,0);
	splay(num2,num1);
	connect((tot+1)>>1,num2,0);
	pushup(num2);
	pushup(num1);
	return;
}
inline void destroy(re ll pos)
{
	if(ls(pos))
		destroy(ls(pos));
	if(rs(pos))
		destroy(rs(pos));
	q.push(pos);
	ls(pos)=rs(pos)=fa(pos)=spy[pos].siz=spy[pos].sum=spy[pos].maxn=spy[pos].lmax=spy[pos].rmax=spy[pos].val=spy[pos].tag=spy[pos].rev=0;
	return;
}
inline void erase(re ll pos,re ll tot)
{
	re ll now=split(pos,pos+tot-1),tmp=fa(now);
	destroy(now);
	ls(tmp)=0;
	pushup(tmp);
	pushup(fa(tmp));
	return;
}
inline void upgrade(re ll l,re ll r,re ll c)
{
	re ll now=split(l,r);
	add(now,c);
	return;
}
inline void dfs(re ll ver)
{
	if(ls(ver))
		dfs(ls(ver));
	printf("%lld ",spy[ver].val);
	if(rs(ver))
		dfs(rs(ver));
}
signed main()
{
	read(n);
	read(m);
	for(re int i=1;i<=n+2;i++)
		id[i]=i;
	a[1]=a[n+2]=-inf;
	for(re int i=2;i<=n+1;i++)
		read(a[i]);
	root=(n+3)>>1;
	cnt=n+2;
	build(1,n+2,0);
//	dfs(root); 
	for(re int i=1;i<=m;i++)
	{
		scanf("%s",s+1);
		if(s[1]=='I')
		{
			re ll pos,tot;
			read(pos);
			read(tot);
			insert(pos,tot);
		}
		else if(s[1]=='D')
		{
			re ll pos,tot;
			read(pos);
			read(tot);
			erase(pos,tot);
		}
		else if(s[1]=='M')
		{
			if(s[3]=='K')
			{
				re ll pos,tot,c;
				read(pos);
				read(tot);
				read(c);
				upgrade(pos,pos+tot-1,c);
			}
			else
				printf("%lld\n",spy[root].maxn);
		}
		else if(s[1]=='R')
		{
			re ll pos,tot;
			read(pos);
			read(tot);
			reverse(split(pos,pos+tot-1)); 
		}
		else if(s[1]=='G')
		{
			re ll pos,tot;
			read(pos);
			read(tot);
			printf("%lld\n",spy[split(pos,pos+tot-1)].sum);
		}
	}
	exit(0);
}
2021/6/13 13:58
加载中...