# include # include #include # include #include #include #include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include # include #include #include #include #include #include #include //#include using namespace std; using LL = long long; using ULL = unsigned long long; long long MOD = 1000000000 + 7;//998244353; constexpr long long INF = numeric_limits::max() / 2; const double PI = acos(-1); #define fir first #define sec second #define thi third #define debug(x) cerr<<#x<<": "< Pll; typedef pair> Ppll; typedef pair>> Pbll; typedef pair>> Pvll; typedef pair Vec2; struct Tll { LL first, second, third; }; struct Fll { LL first, second, third, fourd; }; typedef pair Ptll; #define rep(i,rept) for(LL i=0;i=0;i--) void YN(bool f) { if (f) cout << "YES" << endl; else cout << "NO" << endl; } void yn(bool f) { if (f) cout << "Yes" << endl; else cout << "No" << endl; } struct Edge { LL to, cost; }; struct edge { LL from, to, cost; }; vector>g; vectoredges; vectorv; mapma; setst; LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans, a[210000], b[210000], dp[2100000]; string str, ss; bool f; char c; int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } }; LL plu(LL a,LL b) { return a + b; } LL minu(LL a, LL b) { return a-b; } LL multi(LL a, LL b) { return a * b; } LL _xor(LL a, LL b) { return a ^ b; } LL _and(LL a, LL b) { return a & b; } LL _or(LL a, LL b) { return a | b; } LL how_to(string s) { LL ret = 0; for (int i = 0; i < s.size(); i++) { ret *= 10; ret += (s[i]-'0'); } return ret; } templatestruct operate { int p; // 1:(+,-) 2:(*,/) char c; // + - * ^ etc... functionf; // +:(a,b)=a+b operate() {}; operate(int _p, char _c, function_f) { p = _p, c = _c, f = _f; }; }; templatestruct Parser { // results int root; // vals[root] is the answer vector vals; // value of each node vector left, right; // the index of left-node, right-node function how_to_vals; // evaluation methods vector> operators; // all operatorss are single character int priority_max = 2; // generate nodes int newnode(string op, int lp, int rp, T val = T()) { bool flag = 0; left.push_back(lp); right.push_back(rp); for (int i = 0; i < operators.size(); i++) if (op.size() == 1 && op[0] == operators[i].c) vals.push_back(operators[i].f(vals[lp], vals[rp])), flag = 1; if (flag == 0) vals.push_back(val); return (int)vals.size() - 1; } void eval_init(functionfunc) { how_to_vals = func; } void operate_init(operate op) { operators.push_back(op); } // main solver T solve(const string &S) { int p = 0; for (int i = 0; i < operators.size(); i++) priority_max = max(operators[i].p, priority_max); root = expr(S, p); return vals[root]; } // parser int expr(const string &S, int &p, int pri=1) { int lp; if (pri == priority_max) lp = value(S, p); else lp = expr(S, p, pri + 1); while (p < (int)S.size()) { bool flag = 0; for (int i = 0; i < operators.size(); i++) { if (S[p] == operators[i].c&&pri == operators[i].p)flag = 1; } if (!flag)break; string op; op.push_back(S[p]); ++p; if (priority_max == pri) { int rp = value(S, p); lp = newnode(op, lp, rp); } else { int rp = expr(S, p, pri + 1); lp = newnode(op, lp, rp); } } return lp; } int value(const string &S, int &p) { if (S[p] == '(') { ++p; // skip '(' int lp = expr(S, p); ++p; // skip ')' return lp; } else { /* each process */ string c; while (p < S.size()) { bool flag = 0; for (int i = 0; i < operators.size(); i++) if (S[p] == operators[i].c)flag = 1; if (S[p] == ')')flag = 1; if (S[p] == '(')flag = 1; if (flag)break; c.push_back(S[p]); ++p; }; return newnode(c, -1, -1, how_to_vals(c)); } } }; int main() { Parser prs; prs.operate_init((operate(1, '+', plu))); prs.operate_init((operate(1, '-', minu))); prs.operate_init((operate(2, '*', multi))); prs.operate_init((operate(3, '^', _xor))); prs.operate_init((operate(3, '&',_and))); prs.operate_init((operate(3, '|', _or))); prs.eval_init(how_to); cin >> str; prs.solve(str); cout << prs.vals[prs.root] << endl; return 0; }