Submission #1349369

#TimeUsernameProblemLanguageResultExecution timeMemory
1349369matus192Homework (CEOI22_homework)C++20
100 / 100
103 ms132496 KiB
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;

#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define IMAX INT_MAX
#define IMIN INT_MIN
#define LMAX LLONG_MAX
#define LMIN LLONG_MIN
#define MOD 1000000007
#define SIR 1000000009

int n = 0;

struct obj {
    int type;  // no, min, max
    obj *l, *r;
    int vl, vr;
    obj() {
        type = 0;
        l = nullptr, r = nullptr;
        vl = 0;
        vr = 0;
    };

    void spracuj() {
        if (type == 0) return;
        if (type == 1) {
            vl = min(l->vl, r->vl);
            vr = l->vr + r->vr + 1;
        } else {
            vr = min(l->vr, r->vr);
            vl = l->vl + r->vl + 1;
        }
    }
};

pair<obj*, int> read(string& s, int cur) {
    obj* nw = new obj();

    if (s[cur] == '?') return {nw, cur + 2};

    auto tmp1 = read(s, cur + 4);
    nw->l = tmp1.ff;
    auto tmp2 = read(s, tmp1.ss);
    nw->r = tmp2.ff;

    if (s[cur + 1] == 'a') {
        nw->type = 2;
    } else {
        nw->type = 1;
    }

    nw->spracuj();

    return {nw, tmp2.ss + 1};
}

string pomoc = "?NX";
void out(obj* x) {
    if (x->type == 0) {
        cout << '?';
        return;
    }
    cout << pomoc[x->type] << '(';
    out(x->l);
    cout << ',';
    out(x->r);
    cout << ')';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin >> s;

    for (auto& c : s) n += (c == '?');

    auto [root, koniec] = read(s, 0);

    int mn = root->vl + 1;
    int mx = n - root->vr;

    cout << mx - mn + 1 << '\n';

    return 0;
}
#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...