栈 L,R 维护光标之前(之后)的序列
那么 I x L.push(x);
D L.pop();
L R.push(L.pop());
R L.push(R.pop());
然后用 s[]数组维护前缀和,ans[] 数组维护前缀和的最大值
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1000010;
vector<int>L,R;
int s[maxn];
int ans[maxn];
int main()
{
int T;
while(~scanf("%d",&T))
{
L.clear();
R.clear();
char op[2];
int tot=0;
s[0]=0;
while(T--)
{
scanf("%s",op);
if(*op=='I')
{
int x;
scanf("%d",&x);
L.push_back(x);
s[tot+1]=s[tot]+x;
tot++;
if(tot==1) ans[tot]=s[tot];
else ans[tot]=max(s[tot],ans[tot-1]);
}
else if(*op=='D')
{
if(L.size()==0) continue;
L.pop_back();
tot--;
}
else if(*op=='L')
{
if(L.empty()) continue;
R.push_back(L.back());
L.pop_back();
tot--;
}
else if(*op=='R')
{
if(R.empty()) continue;
L.push_back(R.back());
s[tot+1]=s[tot]+R.back();
R.pop_back();
tot++;
ans[tot]=max(s[tot],ans[tot-1]);
}
else if(*op=='Q')
{
int k;
scanf("%d",&k);
printf("%d\n",ans[k]);
}
}
}
}