Submission #981860

#TimeUsernameProblemLanguageResultExecution timeMemory
981860LOLOLOElection (BOI18_election)C++17
0 / 100
10 ms29348 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; #define f first #define s second #define pb push_back #define ep emplace #define eb emplace_back #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin()) #define mem(f,x) memset(f , x , sizeof(f)) #define sz(x) (int)(x).size() #define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b)) #define mxx *max_element #define mnn *min_element #define cntbit(x) __builtin_popcountll(x) #define len(x) (int)(x.length()) const int N = 1e6 + 30; const int M = 5e5 + 5; int cnt = 0; int psum[N]; vector <pair <int, int>> ask[N]; int pos[N], n, ans[M]; struct seg{ vector <int> laz, seg; void ass(int n) { laz.assign(n * 4 + 100, 0); seg.assign(n * 4 + 100, 0); } void push(int id) { int t = laz[id]; laz[id * 2] += t; laz[id * 2 + 1] += t; seg[id * 2] += t; seg[id * 2 + 1] += t; laz[id] = 0; } void upd(int id, int l, int r, int u, int v, int c) { if (l > v || r < u) return; if (l >= u && r <= v) { seg[id] += c; laz[id] += c; return; } push(id); int m = (l + r) / 2; upd(id * 2, l, m, u, v, c); upd(id * 2 + 1, m + 1, r, u, v, c); seg[id] = max(seg[id * 2], seg[id * 2 + 1]); } int get(int id, int l, int r, int u, int v) { if (u == v && u == n + 1) return 0; if (l > v || r < u) return -1e9; if (l >= u && r <= v) { return seg[id]; } push(id); int m = (l + r) / 2; return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v)); } }; struct fen{ vector <int> f; int n; void ass(int N) { n = N; f.assign(n + 1, 0); } void upd(int i, int x) { for (; i <= n; i += i & (-i)) f[i] += x; } int get(int i) { int s = 0; for (; i; i -= i & (-i)) s += f[i]; return s; } int range(int l, int r) { return get(r) - get(l - 1); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); fen f; seg st; cin >> n; st.ass(n); f.ass(n); for (int i = 1; i <= n; i++) { char c; cin >> c; if (c == 'T') { st.upd(1, 1, n, 1, i, 1); psum[i]++; } else { psum[i]--; st.upd(1, 1, n, 1, i, -1); } psum[i] += psum[i - 1]; } int q; cin >> q; for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; ask[l].pb({r, i}); } for (auto x : ask[n]) { if (psum[n] - psum[n - 1] == 1) { ans[x.s] = 1; } else { ans[x.s] = 0; } } pos[psum[n]] = n; for (int i = n - 1; i >= 1; i--) { if (psum[i] < psum[i + 1]) { int p = pos[psum[i] + 1]; st.upd(1, 1, n, 1, p, -1); f.upd(p, 1); } else { int p = pos[psum[i]]; if (p) { st.upd(1, 1, n, 1, p, 1); f.upd(p, -1); } } pos[psum[i]] = i; for (auto x : ask[i]) { ans[x.s] = f.range(i, x.f) + max(0, st.get(1, 1, n, i, x.f) - st.get(1, 1, n, x.f + 1, x.f + 1)); } } for (int i = 1; i <= q; i++) { cout << ans[i] << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...