# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
971595 | nguyentunglam | Homework (CEOI22_homework) | C++17 | 89 ms | 114896 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int N = 2e6 + 10;
pair<int, int> st[N];
int t[N];
int operation[N];
int child[2][N], sz[N];
int l[N], r[N];
int32_t main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
if (fopen(task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
string str; cin >> str;
int p = 0;
int id = 0, _id = 0;
for(char c : str) if (c == '?') _id++;
int n = _id;
for(int i = 0; i < str.size(); i++) {
if (str[i] == 'm') {
i += 3;
st[++p] = make_pair(0, 0);
t[p] = str[i - 1] == 'x';
// cout << str[i - 1] << " " << t[p] << endl;
}
else if (str[i] == '?') {
++id;
if (st[p].first == 0) st[p].first = id;
else st[p].second = id;
}
else if (str[i] == ')') {
++_id;
assert(st[p].first && st[p].second);
child[0][_id] = st[p].first;
child[1][_id] = st[p].second;
operation[_id] = t[p];
// cout << _id << " " << st[p].first << " " << st[p].second << " " << operation[_id] << endl;
--p;
if (st[p].first == 0) st[p].first = _id;
else st[p].second = _id;
}
}
auto dfs = [&] (auto self, int u) {
if (u <= n) {
l[u] = r[u] = sz[u] = 1;
return;
}
int x0 = child[0][u], x1 = child[1][u];
self(self, x1);
self(self, x0);
sz[u] = sz[x0] + sz[x1];
if (operation[u]) {
r[u] = max(sz[x0] + r[x1], sz[x1] + r[x0]);
l[u] = l[x0] + l[x1];
}
else {
l[u] = min(l[x0], l[x1]);
r[u] = r[x0] + r[x1] - 1;
}
assert(l[u] <= r[u]);
};
dfs(dfs, _id);
cout << r[_id] - l[_id] + 1;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |