题面:
先给定一个长度为n数列,再给定m个操作,现在需要维护五个操作:
1 x:在光标的前面插入一个数字x。
2:删除光标前的最后一个数字,如果光标前没有数字则忽略。
3:左移一格光标,如果光标已经在最左边则忽略。
4:右移一格光标,如果光标已经在最右边则忽略。
5 k:求前k个数的前缀和,保证k<=数列长度。
初始时光标在数列的最后一个数后面
参照问题描述
对于每个询问,输出一个答案,每个答案占一行
2
3 2
5
3
1 6
4
2
5 2
9
数据范围:n,m,k<=100000,x<=int
我的代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=300000+10;
long long st1[maxn],st2[maxn],top1,top2;
long long s1[maxn],s2[maxn],x,n,m,opt;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>x;
top1++;
st1[top1]=x;
s1[top1]=s1[top1-1]+x;
}
cin>>m;
for(int i=1;i<=m;i++){
cin>>opt;
if(opt==1){
cin>>x;
top1++;
st1[top1]=x;
s1[top1]=s1[top1-1]+x;
}
else if(opt==2){
if(top1>0) top1--;
}
else if(opt==3){
if(top1>0){
top2++;
st2[top2]=st1[top1];
s2[top2]=s2[top2-1]+s1[top1];
}
}
else if(opt==4){
if(top2>0){
top1++;
st1[top1]=st2[top2];
s1[top1]=s1[top1-1]+st2[top2];
}
}
else if(opt==5){
int k;
cin>>k;
if(k<=top1) cout<<s1[k]<<endl;
else{
long long t;
t=s1[top1]+s2[top2]+s2[top2+top1-k];
cout<<t;
}
}
}
return 0;
}
思路是维护两个对顶栈 st1 和 st2,以及两个站的前缀和 s1 和 s2。