Submission #591004

#TimeUsernameProblemLanguageResultExecution timeMemory
591004dutinmeowElection (BOI18_election)C++17
100 / 100
486 ms44812 KiB
#include <bits/stdc++.h> using namespace std; #pragma region y_combinator #ifndef Y_COMBINATOR_HPP #define Y_COMBINATOR_HPP template<class Fun> class y_combinator_result { Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {} template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } }; template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); } #endif #pragma endregion y_combinator int main() { ios_base::sync_with_stdio(0); cin.tie(0); int N; string S; cin >> N >> S; vector<int> A(N); for (int i = 0; i < N; i++) A[i] = S[i] == 'C' ? 1 : -1; using st_node_t = struct { int pre, suf, ans, sum; }; vector<st_node_t> st_tree(4 * N); auto st_pull = [&](const st_node_t &a, const st_node_t &b) -> st_node_t { return st_node_t{ max(a.pre, a.sum + b.pre), max(b.suf, b.sum + a.suf), max({a.ans, b.ans, a.suf + b.pre}), a.sum + b.sum }; }; y_combinator([&](auto st_build, int t, int tl, int tr) -> void { if (tl == tr) { st_tree[t] = st_node_t{max(A[tl], 0), max(A[tl], 0), max(A[tl], 0), A[tl]}; return; } int tm = (tl + tr) / 2; st_build(t * 2, tl, tm); st_build(t * 2 + 1, tm + 1, tr); st_tree[t] = st_pull(st_tree[t * 2], st_tree[t * 2 + 1]); })(1, 0, N - 1); auto st_query = y_combinator([&](auto st_query, int l, int r, int t, int tl, int tr) -> st_node_t { if (l > r) return st_node_t{0, 0, 0, 0}; if (l <= tl && tr <= r) return st_tree[t]; int tm = (tl + tr) / 2; if (r <= tm) return st_query(l, r, t * 2, tl, tm); if (tm < l) return st_query(l, r, t * 2 + 1, tm + 1, tr); return st_pull(st_query(l, r, t * 2, tl, tm), st_query(l, r, t * 2 + 1, tm + 1, tr)); }); int Q; cin >> Q; while (Q--) { int l, r; cin >> l >> r; l--, r--; auto q = st_query(l, r, 1, 0, N - 1); cout << q.ans - q.sum << '\n'; } }

Compilation message (stderr)

election.cpp:4: warning: ignoring '#pragma region y_combinator' [-Wunknown-pragmas]
    4 | #pragma region y_combinator
      | 
election.cpp:29: warning: ignoring '#pragma endregion y_combinator' [-Wunknown-pragmas]
   29 | #pragma endregion y_combinator
      |
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...