제출 #1266842

#제출 시각아이디문제언어결과실행 시간메모리
1266842dostsHomework (CEOI22_homework)C++20
53 / 100
1093 ms14312 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)};
}

string s;

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

void solve() {
    string tree;
    cin >> tree;
    s = tree;
    Node p = f(0,tree.size()-1);
    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...