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;
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 type[4 * MAX_N]; // 1 = min, 2 = max, 3 = leaf
void build(int u, int l, int r, int d) {
// cout << u << " " << l << " " << r << " " << d << endl;
if (l == r) {
type[u] = 3;
return;
}
type[u] = (str[l + 1] == 'i') ? 1 : 2;
int i = *commas_at_depth[d].lower_bound(l);
build(2 * u, l + 4, i - 1, d + 1);
build(2 * u + 1, i + 1, r - 1, d + 1);
}
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);
}
build(1, 0, str.size() - 1, 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 >> str;
precomp();
pii bounds = find_bounds(1);
int ans = n - bounds.first - bounds.second;
cout << ans << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'void precomp()':
Main.cpp:24:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i = 0; i < str.size(); i++) n += (bool) (str[i] == '?');
| ~~^~~~~~~~~~~~
Main.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i = 0; i < str.size(); i++) {
| ~~^~~~~~~~~~~~
# | 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... |