WA12求调
查看原帖
WA12求调
575363
revolutionary_oier楼主2024/9/27 16:14
#include<bits/stdc++.h>
#define int long long 
using namespace std;

const int maxn=5e5+10;
const int inf=1e18;
int T,n,m;
int a[maxn],pre[maxn],op[maxn],s[maxn],f[maxn];
struct node{
	int l,r;
	int mx;
}t[3][maxn<<2];
inline void ipt(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		pre[i]=pre[i-1]+a[i];
		s[i]=pre[i];
		f[i]=-inf;
	}
	f[0]=0,s[n+1]=0;
} 
inline void upd(int o,int i){t[o][i].mx=max(t[o][i<<1].mx,t[o][i<<1|1].mx);}
inline void build(int o,int i,int l,int r){
	t[o][i].l=l,t[o][i].r=r,t[o][i].mx=-inf;
	if(l==r)return ;
	int mid=l+r>>1;
	build(o,i<<1,l,mid);
	build(o,i<<1|1,mid+1,r);
//	upd(o,i);
}
inline void init(){
	sort(s+1,s+n+2);
	m=unique(s+1,s+n+2)-s-1;
//	printf("m = %lld\n",m);
	for(int i=1;i<=n+1;i++){
		op[i]=lower_bound(s+1,s+m+1,pre[i])-s;
//		printf("%lld ",op[i]);
	}
	build(0,1,1,m);
	build(1,1,1,m);
	build(2,1,1,m);
//	printf("\n");
}
inline void change(int o,int i,int l,int r,int x){
	if(t[o][i].l>r||t[o][i].r<l)return ;
	if(l<=t[o][i].l&&t[o][i].r<=r){
		t[o][i].mx=max(t[o][i].mx,x);
		return ;
	}
	change(o,i<<1,l,r,x);
	change(o,i<<1|1,l,r,x);
	upd(o,i);
}
inline int query(int o,int i,int l,int r){
	if(t[o][i].l>r||t[o][i].r<l)return -inf;
	if(l<=t[o][i].l&&t[o][i].r<=r)return t[o][i].mx;
	int ret=max(query(o,i<<1,l,r),query(o,i<<1|1,l,r));
	return ret;
}
inline void solve(){
	if(pre[1]>0)f[1]=1;
	else if(pre[1]==0)f[1]=0;
	else f[1]=-1;
	change(0,1,op[n+1],op[n+1],0);
	change(1,1,op[n+1],op[n+1],0);
	change(2,1,op[n+1],op[n+1],0);
	change(0,1,op[1],op[1],f[1]-1);
	change(1,1,op[1],op[1],f[1]);
	change(2,1,op[1],op[1],f[1]+1);
	for(int i=2;i<=n;i++){
		f[i]=max(f[i],query(0,1,1,op[i]-1)+i);
		f[i]=max(f[i],query(1,1,op[i],op[i]));
//		printf("f[2] = %lld\n",f[2]);
//		if(i==2)printf("f[2] = %lld\n",f[2]);
		f[i]=max(f[i],query(2,1,op[i]+1,m)-i);
//		if(i==2)printf("= %lld\n",query(2,1,op[i]+1,m)-i);
		change(0,1,op[i],op[i],f[i]-i);
		change(1,1,op[i],op[i],f[i]);
		change(2,1,op[i],op[i],f[i]+i);
	}
//	printf("%lld %lld %lld ",f[1],f[2],f[3]);
	printf("%lld\n",f[n]);
}
signed main(){
	scanf("%lld",&T);
	while(T--){
		ipt();
		init();
		solve();
	}
	
	return 0;
} 
2024/9/27 16:14
加载中...