刚学栈的萌新qwq
先给定一个长度为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;
int st1[maxn],st2[maxn],top1,top2;
int s1[maxn],s2[maxn],n,m,opt,x,k;
signed main()
{
cin>>n;
for(int i=1; i<=n; i++) {
cin>>x;
top1++;
st1[top1]=x;
s1[top1]=s1[top1-1]+x;
}
cin>>m;
while(m--) {
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]+st1[top1];
}
}
else if(opt==4) {
if(top2>0) {
top1++;
st1[top1]=st2[top2];
s1[top1]=s1[top1-1]+st2[top2];
top2--;
}
}
else if(opt==5) {
cin>>k;
if(k<=top1) {
cout<<s1[k]<<endl;
}
else {
int t;
t=s1[top1]+s2[top2]-s2[top2+top1-k];
cout<<t;
}
}
}
return 0;
}
/*
头文件:#include<stack>
定义:stack<int> st;
取栈顶元素:st.top(); 有返回值
入栈(压栈):st.push(m);
出栈(弹栈):st.pop();删除栈顶元素
判断栈是否为空:st.empty();
栈中元素有效个数:st.size();
*/