维护数列(splay)求调
  • 板块学术版
  • 楼主xcyyyyyy
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/1/27 20:50
  • 上次更新2023/10/24 02:54:38
查看原帖
维护数列(splay)求调
691447
xcyyyyyy楼主2023/1/27 20:50
#include<bits/stdc++.h>
#define KL 500000
using namespace std;
struct Node{
	int lmax,maxn,rmax,sum;
	Node operator + (const Node &that)const{
		Node res;
		res.lmax=max(lmax,sum+that.lmax);
		res.maxn=max(max(maxn,that.maxn),rmax+that.lmax);
		res.rmax=max(that.rmax,that.sum+rmax);
		res.sum=sum+that.sum;
		return res;
	}
};

int n,m,root,tot;
Node Maxn[KL];
stack<int> sta;
int arr[KL];
int ch[KL][2],dad[KL],lazy[KL],tag[KL],key[KL],size[KL];
int get(int p){
	return ch[dad[p]][1]==p;
}
int Newnode(int num,int fa){
	int p;
	if(sta.size())p=sta.top(),sta.pop();
	else p=++tot;
	size[p]=1;
	dad[p]=fa;
	tag[p]=0;lazy[p]=0x3f3f3f3f;
	Maxn[p].lmax=Maxn[p].maxn=Maxn[p].rmax=Maxn[p].sum=key[p]=num;
	return p;
}
void push_lazy(int p,int c){
	lazy[p]=key[p]=c;
	Maxn[p].sum=c*size[p];
	Maxn[p].lmax=Maxn[p].rmax=Maxn[p].maxn=max(c*size[p],c);
}
void push_tag(int p){
	tag[p]^=1;
	swap(ch[p][0],ch[p][1]);
	swap(Maxn[ch[p][0]].lmax,Maxn[ch[p][0]].rmax);
	swap(Maxn[ch[p][1]].lmax,Maxn[ch[p][1]].rmax);
}
void push_down(int rt){
	if(lazy[rt]!=0x3f3f3f3f){
		if(ch[rt][0])push_lazy(ch[rt][0],lazy[rt]);
		if(ch[rt][1])push_lazy(ch[rt][1],lazy[rt]);
		lazy[rt]=0x3f3f3f3f;
	}
	if(tag[rt]){
		if(ch[rt][0])push_tag(ch[rt][0]);
		if(ch[rt][1])push_tag(ch[rt][1]);
		tag[rt]=0;
	}
}
void push_up(int p){
	size[p]=size[ch[p][0]]+size[ch[p][1]]+1;
	Node th;
	th.lmax=th.maxn=th.rmax=th.sum=key[p];
	if(ch[p][0]&&ch[p][1])Maxn[p]=(Maxn[ch[p][0]]+th)+Maxn[ch[p][1]];
	else if(ch[p][0])Maxn[p]=Maxn[ch[p][0]]+th;
	else if(ch[p][1])Maxn[p]=th+Maxn[ch[p][1]];
	else Maxn[p]=th;
}
void Debug_tree(int p){
	push_down(p);
	if(ch[p][0])Debug_tree(ch[p][0]);
	if(ch[p][1])Debug_tree(ch[p][1]);
}
void Debug_rotate(){
	Newnode(1,0);
	Newnode(2,1);ch[1][0]=2;
	Newnode(3,1);ch[1][1]=3;
	Newnode(4,2);ch[2][0]=4;
	Newnode(5,2);ch[2][1]=5;
	printf("%d",ch[1][0]);
}
void rotate(int q){
	int p=dad[q];
	int op=(ch[p][1]==q);
	dad[q]=dad[p];	if(dad[p])ch[dad[p]][ch[dad[p]][1]==p]=q;
	ch[p][op]=ch[q][op^1];	if(ch[q][op^1])dad[ch[q][op^1]]=p;
	dad[p]=q;	ch[q][op^1]=p;
	push_up(p);
	push_up(q);
}
void Splay(int p,int objective){
	for(;dad[p]!=objective;rotate(p))
		if(dad[dad[p]]!=objective)
			rotate((p==ch[dad[p]][1])==(dad[p]==ch[dad[dad[p]]][1])?dad[p]:p);
	if(!objective)root=p;
}
void Kth(int p,int objective,int k){
	while(1){
		push_down(p);
		if(size[ch[p][0]]>=k)p=ch[p][0];
		else if(size[ch[p][0]]+1==k){
			Splay(p,objective);
			break;
		}
		else{
			k-=size[ch[p][0]]+1;
			p=ch[p][1];
		}
	}
}
void recycle(int p){
	sta.push(p);
	if(ch[p][0])recycle(ch[p][0]);
	if(ch[p][1])recycle(ch[p][1]);
}
int build(int l,int r,int fa,int *arr){
	if(l>r)return 0;
	int m=(l+r)>>1,p=Newnode(arr[m],fa);
	ch[p][0]=build(l,m-1,p,arr);
	ch[p][1]=build(m+1,r,p,arr);
	push_up(p);
	return p;
}
void Insert(){
	int pos,len;
	scanf("%d%d",&pos,&len);
	for(int i=1;i<=len;i++)scanf("%d",&arr[i]);
	getchar();
	int l=pos+1,r=pos+2;
	Kth(root,0,l);
	Kth(root,root,r);
	ch[ch[root][1]][0]=build(1,len,ch[root][1],arr);
	push_up(ch[root][1]);
	push_up(root);
}
void Delete(){
	int pos,len;
	scanf("%d%d",&pos,&len);
	getchar();
	int l=pos,r=pos+len+1;
	Kth(root,0,l);
	Kth(root,root,r);
	recycle(ch[ch[root][1]][0]);
	ch[ch[root][1]][0]=0;
	push_up(ch[root][1]);
	push_up(root);
}
void Make_same(){
	int pos,len,c;
	scanf("%d%d%d",&pos,&len,&c);
	getchar();
	int l=pos,r=pos+len+1;
	Kth(root,0,l);
	Kth(root,root,r);
	push_lazy(ch[ch[root][1]][0],c);
	push_up(ch[root][1]);
	push_up(root);
}
void Reverse(){
	int pos,len;
	scanf("%d%d",&pos,&len);
	getchar();
	int l=pos,r=pos+len+1;
	Kth(root,0,l);
	Kth(root,root,r);
	push_tag(ch[ch[root][1]][0]);
	push_up(ch[root][1]);
	push_up(root);
}
void Get_sum(){
	int pos,len;
	scanf("%d%d",&pos,&len);
	getchar();
	int l=pos,r=pos+len+1;
	Kth(root,0,l);
	Kth(root,root,r);
	printf("%d\n",Maxn[ch[ch[root][1]][0]].sum);
}
void Max_sum(){
	Kth(root,0,1);
	Kth(root,root,size[root]);
	printf("%d\n",Maxn[ch[ch[root][1]][0]].maxn);
}
int main(){        
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&arr[i]);
	root=Newnode(114514,0);
	ch[root][1]=Newnode(1919810,root);
	ch[ch[root][1]][0]=build(1,n,ch[root][1],arr);
	push_up(ch[root][1]);
	push_up(root);
	getchar();
	while(m--){
		string jsy;
		cin>>jsy;
		if(jsy=="INSERT")Insert();
		else if(jsy=="DELETE")Delete();
		else if(jsy=="MAKE-SAME")Make_same();
		else if(jsy=="REVERSE")Reverse();
		else if(jsy=="GET-SUM")Get_sum();
		else Max_sum();
		Debug_tree(root);
	}
} 
2023/1/27 20:50
加载中...