Submission #942294

#TimeUsernameProblemLanguageResultExecution timeMemory
942294GhettoHomework (CEOI22_homework)C++17
100 / 100
511 ms325104 KiB
#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
int n_nodes;
int l_child[4 * MAX_N], r_child[4 * MAX_N];
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);
    l_child[u] = ++n_nodes; r_child[u] = ++n_nodes;
    build(l_child[u], l + 4, i - 1, d + 1);
    build(r_child[u], 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);
    }
    
    n_nodes = 1;
    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(l_child[u]), r_bounds = find_bounds(r_child[u]);
    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: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++) n += (bool) (str[i] == '?');
      |                     ~~^~~~~~~~~~~~
Main.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |     for (int i = 0; i < str.size(); i++) {
      |                     ~~^~~~~~~~~~~~
#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...