编辑器求调

学术版

cdxxx04 @ 2024-11-29 14:16:04

代码:

#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize("Ofast")

using namespace std;

struct stack_{
    int STACK[1000005],TOP;
    bool empty(){return TOP==0;}
    int top(){return STACK[TOP];}
    int size(){return TOP;}
    int get(int x){return STACK[x];}
    void pop(){STACK[TOP--]=0;if(TOP<0)TOP=0;}
    void print(){for(int i=1;i<=TOP;i++)printf("%lld ",STACK[i]);cout<<endl;}
    void push(int x){STACK[++TOP]=x;}
};//手写栈
stack_ fa,fm,fq,sa;
int Q;

inline void insert(int x){
    fa.push(x);
    fm.push(max(fq.top(), fq.top() + x));
    fq.push(fq.top() + x);
}
inline void first_pop(){
    fa.pop();  
    fm.pop();
    fq.pop();
}
inline void second_pop(){
    sa.pop();
}
inline void first_to_second(){
    sa.push(fa.top());
}
inline void second_to_first(){
    insert(sa.top());
}

signed main()
{
    ios::sync_with_stdio(false);    cin.tie(nullptr);

    cin >> Q;
    while (Q -- ){
        char ch;    cin >> ch;

        if (ch == 'I'){
            int x;  cin >> x;
            insert(x);
        }else   if (ch == 'D'){
            if (fa.empty()) continue;
            first_pop();
        }else   if (ch == 'L'){
            if (fa.empty()) continue;
            first_to_second();
            first_pop();
        }else   if (ch == 'R'){
            if (sa.empty()) continue;
            second_to_first();
            second_pop();
        }else{
            int k;  cin >> k;
            cout << fm.get(k) << '\n';
        }
    }
    return 0;
}

输入:

50
I 654
L
I -256
I -838
Q 2
I 937
I -34
I -892
I 969
R
I -444
I -310
Q 2
L
I 421
Q 8
I 95
R
Q 4
R
Q 6
I -568
I -977
L
Q 11
Q 4
Q 7
L
L
D
I 948
Q 7
Q 4
Q 5
Q 3
L
D
I -736
I 162
D
R
I -256
I -847
I 795
I -475
Q 7
I 246
I 804
R
Q 1

正确输出:

-256
-256
540
-157
-114
612
-157
540
540
-157
-157
-157
540
-256

错误输出:

-256
-256
540
-157
-114
612
-157
540
540
-157
-191
-157
540
0

|