被题目坑爹好狠 是1到j(1<=j<=k)的最大和。。。还以为是k之前的最大字段和。。。
操作不用我说了把,两个对顶堆分别维护,单次操作复杂度O(1)
#include<iostream> #include<algorithm> using namespace std; const int N=1e6+5,inf=2147483647; int s1[N],s2[N],m1[N],m2[N]; int main(){ // freopen("a.txt","r",stdin); int Q; m2[0]=-inf; while(scanf("%d",&Q)!=EOF){ int t1=0,t2=0; for(register int i=1;i<=Q;++i){ char opt; while((opt=getchar())&&(opt<'A'||opt>'Z')); if(opt=='I'){ int x; scanf("%d",&x); s1[++t1]=x; m1[t1]=m1[t1-1]+x; m2[t1]=max(m2[t1-1],m1[t1]); } else if(opt=='D'&&t1!=0){ --t1; } else if(opt=='L'&&t1!=0){ s2[++t2]=s1[t1];--t1; } else if(opt=='R'&&t2!=0){ s1[++t1]=s2[t2]; m1[t1]=m1[t1-1]+s2[t2]; m2[t1]=max(m2[t1-1],m1[t1]); --t2; } else if(opt=='Q'){ int x; scanf("%d",&x); printf("%d ",m2[x]); } } } return 0; }