这两个写法有啥区别啊(
查看原帖
这两个写法有啥区别啊(
380579
BMTXLRC楼主2021/9/28 21:10

WA:

int new_node(int x){
	a[++cnt].sum=a[cnt].x=a[cnt].max=x;
	a[cnt].maxl=a[cnt].maxr=max(0,x);
	a[cnt].size=1,a[cnt].pri=rand();
	return cnt;
}

AC:

int new_node(int x){
	a[++cnt].sum=x;
	a[cnt].x=a[cnt].max=x;
	a[cnt].maxl=a[cnt].maxr=max(0,x);
	a[cnt].size=1,a[cnt].pri=rand();
	return cnt;
}

调了我好久(/kk

完整代码:

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
struct FHQ_treap{
	int l,r,pri,sum;
	int x,size;
	int maxl,maxr,max;
}a[N];
int n,m,cnt,root;
int new_node(int x){
	a[++cnt].sum=x;
	a[cnt].x=a[cnt].max=x;
	a[cnt].maxl=a[cnt].maxr=max(0,x);
	a[cnt].size=1,a[cnt].pri=rand();
	return cnt;
}//correct
void pushup(int now){
	if(!now) return;
	a[now].size=a[a[now].l].size+a[a[now].r].size+1;
	a[now].sum=a[a[now].l].sum+a[a[now].r].sum+a[now].x;
	a[now].maxl=max(max(a[a[now].l].maxl,a[a[now].l].sum+a[now].x+a[a[now].r].maxl),0);
	a[now].maxr=max(max(a[a[now].r].maxr,a[a[now].r].sum+a[now].x+a[a[now].l].maxr),0);
	a[now].max=max(a[a[now].l].maxr+a[a[now].r].maxl,0)+a[now].x;
	if(a[now].l) a[now].max=max(a[now].max,a[a[now].l].max);
	if(a[now].r) a[now].max=max(a[now].max,a[a[now].r].max);
}
int merge(int x,int y){
	if(!x||!y) return x+y;
	if(a[x].pri<a[y].pri){
		a[x].r=merge(a[x].r,y);
		pushup(x);
		return x;
	}else{
		a[y].l=merge(x,a[y].l);
		pushup(y);
		return y;
	}
}//correct
void split(int now,int k,int &x,int &y){
	if(!now){
		x=y=0;
		return;
	}
	if(a[a[now].l].size<k){
		x=now;
		split(a[now].r,k-a[a[now].l].size-1,a[now].r,y);
	}else{
		y=now;
		split(a[now].l,k,x,a[now].l);
	}
	pushup(now);
}//correct
void dfs(int now){
	if(a[now].l) dfs(a[now].l);
	printf("%d ",a[now].x);
	if(a[now].r) dfs(a[now].r);
}
int main(){
	scanf("%d",&n);
	for(register int i=1,x;i<=n;i++) scanf("%d",&x),root=merge(root,new_node(x));
	scanf("%d",&m);
	for(register int i=1;i<=m;i++){
		char op=getchar();
		while(op>'Z'||op<'A') op=getchar();
		int pos,x,y,z,k,l,r;
		if(op=='I'){
			scanf("%d %d",&pos,&k);
			split(root,pos-1,x,y);
			root=merge(merge(x,new_node(k)),y);
		}
		if(op=='D'){
			scanf("%d",&pos);
			split(root,pos-1,x,y);
			split(y,1,y,z);
			root=merge(x,z);
		}
		if(op=='R'){
			scanf("%d %d",&pos,&k);
			split(root,pos-1,x,y);
			split(y,1,y,z);
			a[y].x=k;
			pushup(y);
			root=merge(merge(x,y),z);
		}
		if(op=='Q'){
			scanf("%d %d",&l,&r);
			split(root,l-1,x,y);
			split(y,r-l+1,y,z);
			pushup(y);
			printf("%d\n",a[y].max);
			root=merge(merge(x,y),z);
		}
		//dfs(root);cout<<endl;
	}
}
2021/9/28 21:10
加载中...