题目地址


注意点:

  • 最小前缀和仅需要维护光标前的数值,因此sum[i](前缀和)及f[i](最小前缀和)可直接依托top_1(代表光标前数值的栈)存在.

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=2e6,INF=2e9;
int stck_1[MAXN],top_1=0;//光标前 
int f[MAXN];//最大前缀和 
int id=0;
int sum[MAXN];//前缀和 
int stck_2[MAXN],top_2=0;//光标后 
int main(){
	memset(f,0xcf,sizeof(f));
	int q;
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		char opt;
		cin>>opt;
		if(opt=='I'){//光标前插入数值 
			int x;
			scanf("%d",&x);
			stck_1[++top_1]=x;
			sum[top_1]=sum[top_1-1]+x;
			f[top_1]=max(f[top_1-1],sum[top_1]);
		}else if(opt=='Q'){//查询最大前缀和 
			int x;
			scanf("%d",&x);
			printf("%d\n",f[x]);
		}else if(opt=='L'){//光标左移 
			if(!top_1)continue;
			stck_2[++top_2]=stck_1[top_1--];
		}else if(opt=='R'){//光标右移 
			if(!top_2)continue;
			stck_1[++top_1]=stck_2[top_2--];
			sum[top_1]=sum[top_1-1]+stck_1[top_1];
			f[top_1]=max(f[top_1-1],sum[top_1]);
		}else if(opt=='D'){//删除光标前一个元素 
			if(!top_1)continue;
			top_1--;
		}
	}
	return 0;
}