P2201 数列编辑器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<algorithm>
using namespace std;
#define re register
const int MAXn = 1e6;

template <class T>
inline void read(T &a) {
register char c;while (c = getchar(), (c < '0' || c > '9') && c != '-');register bool f = c == '-';register T x = f ? 0 : c - '0';while (c = getchar(), c >= '0' && c <= '9') {x = (x << 3) + (x << 1) + (c ^ 48);}a = f ? -x : x;
}

struct Ele {
int val, sum, maxsum;
Ele(){}
Ele(int val_):val(val_){}
};
struct Stack {
int top;
Ele stk[MAXn + 10];
inline void Push(int x) {
stk[++top] = Ele(x);
}
inline int Pop() {
return stk[top--].val;
}
}fro, beh;
int n;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
char opt;
for (re int i = 1, x; i <= n; ++i) {
cin >> opt;
switch (opt) {
case 'I':
cin >> x;
fro.Push(x);
fro.stk[fro.top].sum = fro.stk[fro.top - 1].sum + fro.stk[fro.top].val;
fro.stk[fro.top].maxsum = (fro.top == 1) ? fro.stk[fro.top].sum :
max(fro.stk[fro.top - 1].maxsum, fro.stk[fro.top].sum);
break;
case 'D':
fro.Pop();
break;
case 'L':
beh.Push(fro.Pop());
break;
case 'R':
fro.Push(beh.Pop());
fro.stk[fro.top].sum = fro.stk[fro.top - 1].sum + fro.stk[fro.top].val;
fro.stk[fro.top].maxsum = (fro.top == 1) ? fro.stk[fro.top].sum :
max(fro.stk[fro.top - 1].maxsum, fro.stk[fro.top].sum);
break;
case 'Q':
cin >> x;
cout << fro.stk[x].maxsum << '\n';
}
}
}