#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 1e6 + 5;
string str;
int n;
set<int> commas_at_depth[4 * MAX_N]; // Starts at 1
int l[4 * MAX_N], r[4 * MAX_N], d[4 * MAX_N];
int type[4 * MAX_N]; // 1 = min, 2 = max, 3 = leaf
void precomp() {
for (int i = 0; i < str.size(); i++) n += (bool) (str[i] == '?');
int depth = 0;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '(') depth++;
else if (str[i] == ')') depth--;
else if (str[i] == ',') commas_at_depth[depth].insert(i);
}
l[1] = 0;
r[1] = str.size() - 1;
d[1] = 1;
for (int i = 1; i <= 4 * n; i++) {
if (d[i] == 0) continue;
if (l[i] == r[i]) {
type[i] = 3;
continue;
}
type[i] = (str[l[i] + 1] == 'i') ? 1 : 2;
int j = *commas_at_depth[d[i]].lower_bound(l[i]);
l[2 * i] = l[i] + 4; r[2 * i] = j - 1; d[2 * i] = d[i] + 1;
l[2 * i + 1] = j + 1; r[2 * i + 1] = r[i] - 1; d[2 * i + 1] = d[i] + 1;
}
// for (int i = 1; i <= 4 * n; i++) {
// cout << i << ": " << type[i] << endl;
// }
}
pii find_bounds(int u) {
if (type[u] == 3) return {0, 0};
pii l_bounds = find_bounds(2 * u), r_bounds = find_bounds(2 * u + 1);
int min_bound = (type[u] == 1) ? min(l_bounds.first, r_bounds.first) : l_bounds.first + r_bounds.first + 1;
int max_bound = (type[u] == 1) ? l_bounds.second + r_bounds.second + 1 : min(l_bounds.second, r_bounds.second);
return {min_bound, max_bound};
}
int main() {
// freopen("homework.in", "r", stdin);
cin.sync_with_stdio(false);
cin.tie(0);
cin >> str;
precomp();
pii bounds = find_bounds(1);
int ans = n - bounds.first - bounds.second;
cout << ans << '\n';
}
Compilation message
Main.cpp: In function 'void precomp()':
Main.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (int i = 0; i < str.size(); i++) n += (bool) (str[i] == '?');
| ~~^~~~~~~~~~~~
Main.cpp:16:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i = 0; i < str.size(); i++) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
237 ms |
383284 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
237 ms |
383284 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
374 ms |
497504 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
237 ms |
383284 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
237 ms |
383284 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |