Submission #830160

#TimeUsernameProblemLanguageResultExecution timeMemory
830160themm1Homework (CEOI22_homework)C++17
100 / 100
177 ms147456 KiB
#include <bits/stdc++.h>
using namespace std;
// ordered set whith s.order_of_key(x) method, which returns rank of element x in set s
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
*/

// pair printing
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "(" << par.first << "; " << par.second << ")"; return out;}
// set printing
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for(const auto &x:cont) out << x << ", "; out << "}"; return out; }
// map printing
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for(const auto &x:cont) out << x << ", "; out << "}"; return out; }
// unordered_set printing
template <class T>
ostream& operator<<(ostream& out, const unordered_set<T> &cont) {out << "{";for(const auto &x:cont) out << x << ", ";out << "}";return out;}
// unordered_map printing
template <class T, class U>
ostream& operator<<(ostream& out, const unordered_map<T, U> &cont) {out << "{";for(const auto &x:cont) out << x << ", ";out << "}";return out;}
// vector printing
template<class T>
ostream& operator<<(ostream& out, const vector<T> &cont){ out << "[";  for (const auto &x:cont) out << x << ", ";  out << "]"; return out;}

#define print(x) cout << (x) << endl;
#define dmp(x) cerr << #x << " = " << x << endl
#define dmpn(x) cerr << #x << " = " << x << "; "
#define dmpl(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << x << endl

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define pb push_back
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define contains(s,x) ((s).find(x) != (s).end())
const int MOD = 998244353;

int N, M;
vector<int> parents;
vector<vector<int>> children;
vector<bool> is_min;

pii dfs(int v) {
        if (children[v].empty()) return { 1, M };
        pii a = dfs(children[v][0]);
        pii b = dfs(children[v][1]);
        // dmpn(v); dmpn(a); dmp(b);
        int new_min, new_max;
        if (is_min[v]) {
                new_min = min(a.ff, b.ff);
                new_max = a.ss - (M - b.ss + 1);
        } else {
                new_min = a.ff + b.ff;
                new_max = max(a.ss, b.ss);
        }
        return { new_min, new_max };
}

void solve() {
        string s;
        cin >> s;
        M = count(all(s), '?');
        N = M + count(all(s), 'm');
        // dmpn(N); dmp(M);

        parents.assign(N, -1);
        children.resize(N);
        is_min.resize(N);
        int v = -1, maxv = -1;
        for (int i = 0; i < sz(s); i++) {
                if (s[i] == '(') {
                        maxv++;
                        // dmp(maxv);
                        if (v != -1) children[v].pb(maxv);
                        parents[maxv] = v;
                        v = maxv;
                        is_min[v] = (s[i - 1] == 'n');
                } else if (s[i] == ')') {
                        // dmp(v);
                        v = parents[v];
                } else if (s[i] == '?') {
                        maxv++;
                        // dmp(maxv);
                        children[v].pb(maxv);
                        parents[maxv] = v;
                        // dmp(maxv);
                }
        }
        pii p = dfs(0);
        cout << p.ss - p.ff + 1 << endl;
}

int32_t main() {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);

        int t = 1;
        // cin >> t;
        while (t--) {
                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...