제출 #1266841

#제출 시각아이디문제언어결과실행 시간메모리
1266841dostsHomework (CEOI22_homework)C++20
53 / 100
781 ms589824 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#define int long long
#define pii pair<int,int>
#define vi vector<int>
#define ff first
#define ss second
#define sp << " " <<
#define all(x) x.begin(),x.end()
#define big(x) ((int)(x.size()))
using namespace std;
const int MOD = 1e9+7, LIM = 1e6+1, inf = 2e9;

const int N = 1e5+1;

struct Node {
    int n,l,r;
};

Node mn(Node p1,Node p2) {
    return {p1.n+p2.n,min(p1.l,p2.l),p1.r+p2.r-1};
}
Node mx(Node p1,Node p2) {
    return {p1.n+p2.n,p1.l+p2.l,max(p1.n+p2.r,p2.n+p1.r)};
}

Node f(string s) {
    if (s == "?") return {1,1,1};
    int ptr = 4;
    int bal = 0;
    for (;ptr<s.size();ptr++) {
        if (s[ptr] == '(') bal++;
        if (s[ptr] == ')') bal--;
        if (!bal && s[ptr] == ',') break;
    }
    Node e1 = f(string(s.begin()+4,s.begin()+ptr));
    Node e2 = f(string(s.begin()+ptr+1,prev(s.end())));
    if (s.substr(0,3) == "max") return mx(e1,e2);
    return mn(e1,e2);
}

void solve() {
    string tree;
    cin >> tree;
    Node p = f(tree);
    cout << p.r-p.l+1 << '\n';
} 

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    #ifdef Dodi
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    int t = 1;
    //cin >> t;
    while (t --> 0) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...