Submission #572233

#TimeUsernameProblemLanguageResultExecution timeMemory
572233hoanghq2004Election (BOI18_election)C++14
100 / 100
1103 ms48172 KiB
#include <bits/stdc++.h> #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template <typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 5e5 + 10; int n, q; int p[N], s[N], L[N], R[N]; char c[N]; int flag[N]; struct SegmentTree { int st[N * 4]; int lazy[N * 4]; void push(int id, int delta) { st[id] += delta; lazy[id] += delta; } void add(int id, int L, int R, int u, int v, int delta) { if (u > R || L > v) return; if (u <= L && R <= v) { push(id, delta); return; } push(id * 2, lazy[id]); push(id * 2 + 1, lazy[id]); lazy[id] = 0; int mid = L + R >> 1; add(id * 2, L, mid, u, v, delta); add(id * 2 + 1, mid + 1, R, u, v, delta); st[id] = min(st[id * 2], st[id * 2 + 1]); } int get(int id, int L, int R, int u, int v) { if (u > R || L > v) return 1e9; if (u <= L && R <= v) return st[id]; push(id * 2, lazy[id]); push(id * 2 + 1, lazy[id]); lazy[id] = 0; int mid = L + R >> 1; return min(get(id * 2, L, mid, u, v), get(id * 2 + 1, mid + 1, R, u, v)); } } F, B; int ans[N]; int main() { ios :: sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> c[i]; for (int i = 1; i <= n; ++i) p[i] = p[i - 1] + (c[i] == 'C' ? 1 : -1); vector <int> stk; for (int i = n; i >= 0; --i) { while (stk.size() && p[i] <= p[stk.back()]) { L[stk.back()] = i; stk.pop_back(); } stk.push_back(i); } while (stk.size()) { L[stk.back()] = -1; stk.pop_back(); } for (int i = 1; i <= n; ++i) F.add(1, 1, n, i, n, (c[i] == 'C' ? 1 : -1)); for (int i = 1; i <= n; ++i) B.add(1, 1, n, 1, i, (c[i] == 'C' ? 1 : -1)); cin >> q; vector <tuple <int, int, int>> work; for (int i = 0; i < q; ++i) { int u, v; cin >> u >> v; ans[i] = - min(0, F.get(1, 1, n, u, v) - p[u - 1]); work.push_back({u, v, i}); } sort(work.begin(), work.end()); vector <pair <int, int>> s; for (int i = 1; i <= n; ++i) s.push_back({L[i] + 1, i}); sort(s.begin(), s.end()); int j = 0; for (auto [u, v, i]: work) { while (j < s.size() && s[j].first < u) { B.add(1, 1, n, 1, s[j].second, 1); ++j; } int b = (v == n ? 0 : B.get(1, 1, n, v + 1, v + 1)); ans[i] -= min(0, B.get(1, 1, n, u, v) - b); } for (int i = 0; i < q; ++i) cout << ans[i] << '\n'; }

Compilation message (stderr)

election.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
election.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      | 
election.cpp: In member function 'void SegmentTree::add(int, int, int, int, int, int)':
election.cpp:36:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |         int mid = L + R >> 1;
      |                   ~~^~~
election.cpp: In member function 'int SegmentTree::get(int, int, int, int, int)':
election.cpp:47:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |         int mid = L + R >> 1;
      |                   ~~^~~
election.cpp: In function 'int main()':
election.cpp:86:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   86 |     for (auto [u, v, i]: work) {
      |               ^
election.cpp:87:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         while (j < s.size() && s[j].first < u) {
      |                ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...