提交记录
//* https://www.luogu.com.cn/problem/P2186
#include <iostream>
#include <stack>
#define F first
#define S second
#define C(str) if(c[i].F==str)
#define NOERR(t) if((t)){puts("ERROR"); return;} else
#define MT(t) ((t<0 ? ~(t-1) : t)>1000000000)
#define EMP (s.empty())
#define LT (s.size()<2)
#define TWO ll tmp1=s.top();s.pop();ll tmp2=s.top();s.pop()
using namespace std;
typedef long long ll;
ll n,ai,l;
pair<string,ll> c[2005];
void fun(ll num){
NOERR(MT(num)){
stack<ll> s;
s.push(num);
for(ll i=0; i<l; i++){
C("NUM") NOERR(MT(c[i].S)) s.push(c[i].S);
else C("POP") NOERR(EMP) s.pop();
else C("INV"){
NOERR(EMP){
ll tmp=s.top();
s.pop();
s.push(~(tmp-1));
}
}else C("DUP"){
NOERR(EMP) s.push(s.top());
}else C("SWP"){
NOERR(LT){
TWO;
s.push(tmp1),s.push(tmp2);
}
}else C("ADD"){
NOERR(LT){
TWO;
NOERR(MT(tmp1+tmp2)) s.push(tmp1+tmp2);
}
}else C("SUB"){
NOERR(LT){
TWO;
NOERR(MT(tmp1-tmp2)) s.push(tmp1-tmp2);
}
}else C("MUL"){
NOERR(LT){
TWO;
NOERR(MT(tmp1*tmp2)) s.push(tmp1*tmp2);
}
}else C("DIV"){
NOERR(LT){
TWO;
NOERR(tmp2==0 || MT(tmp1/tmp2)) s.push(tmp1/tmp2);
}
}else C("MOD"){
NOERR(LT){
TWO;
NOERR(MT(tmp1%tmp2)) s.push(tmp1%tmp2);
}
}else C("END") break;
}
NOERR(s.size()!=1) cout << s.top() << endl;
}
}
void inp(){
string t;
for(l=0; cin >> c[l].F; l++){
if(c[l].F=="END") break;
if(c[l].F=="NUM") cin >> c[l].S;
}
}
void work(){
cin >> n;
while(n--){
cin >> ai;
fun(ai);
}
}
int main(){
inp();
work();
return 0;
}