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>
using namespace std;
const int MX = 2e6 + 5;
int N;
vector<int> g[MX];
bool type[MX];
int n, m;
pair<int, int> dfs(int v) {
// cout << "traverse " << v << " type " << type[v] << '\n';
if(g[v].empty()) {
return make_pair(1, m);
}
auto [l1, r1] = dfs(g[v][0]);
auto [l2, r2] = dfs(g[v][1]);
assert(1 <= l1 && r1 <= m);
assert(1 <= l2 && r2 <= m);
if(!type[v]) {
return make_pair(min(l1, l2), min(r1, r2) - (m - max(r1, r2)) - 1);
} else {
return make_pair(max(l1, l2) + min(l1, l2), max(r1, r2));
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
string s;
cin >> s;
N = s.size();
vector<int> v;
for(int i = 0; i < N; i++) {
// cout << i << " " << s[i] << '\n';
if(s[i] == '?') {
m++;
n++;
if(n > 1)
g[v.back()].push_back(n);
continue;
}
if(s[i] == ')') {
v.pop_back();
}
if(s[i] != 'm') continue;
if(s[i + 1] == 'a') {
n++;
type[n] = 1;
if(n > 1)
g[v.back()].push_back(n);
v.push_back(n);
} else {
n++;
type[n] = 0;
if(n > 1)
g[v.back()].push_back(n);
v.push_back(n);
}
}
auto [l, r] = dfs(1);
// cout << l << " " << r << '\n';
cout << r - l + 1 << '\n';
}
# | 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... |