Submission #479647

#TimeUsernameProblemLanguageResultExecution timeMemory
479647CodeChamp_SSElection (BOI18_election)C++17
0 / 100
7 ms460 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ff first #define ss second #define pb push_back #define eb emplace_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define sz(v) (int)v.size() #define ps(y) cout << fixed << setprecision(y) #define ms(arr, v) memset(arr, v, sizeof(arr)) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define trav(x, v) for(auto &x: v) #define w(t) int t; cin >> t; while(t--) #define rep(i, a, b) for(int i = a; i <= b; i++) #define rrep(i, a, b) for(int i = a; i >= b; i--) #define rep0(i, n) rep(i, 0, n - 1) #define rrep0(i, n) rrep(i, n - 1, 0) #define rep1(i, n) rep(i, 1, n) #define rrep1(i, n) rrep(i, n, 1) #define inp(arr, n) rep0(i, n) cin >> arr[i]; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> pii; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<bool> vb; typedef vector<string> vs; typedef map<ll, ll> mii; typedef map<char, ll> mci; typedef priority_queue<ll> pq_mx; typedef priority_queue<ll, vi, greater<>> pq_mn; typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> pbds; /* * find_by_order(i) -> returns an iterator to the element at ith position (0 based) * order_of_key(i) -> returns the position of element i (0 based) */ const int N = 5e5 + 5; const int mod = 1e9 + 7; //const int mod = 998244353; const ll inf = 2e18; const ld eps = 1e-10, pi = acos(-1.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void fio() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); } ll n, q, pre[N], suf[N]; string s; struct SegTree { ll st[4 * N]{}, lazy[4 * N]{}; ll dummy = inf; ll combine(ll l, ll r) { return min(l, r); } void pushDown(ll s, ll e, int idx) { st[idx] += lazy[idx]; if (s != e) lazy[2 * idx] += lazy[idx], lazy[2 * idx + 1] += lazy[idx]; lazy[idx] = 0; } void build(ll *a, ll s = 1, ll e = n, int idx = 1) { if (s == e) { st[idx] = a[s]; return; } ll m = s + (e - s) / 2; build(a, s, m, 2 * idx), build(a, m + 1, e, 2 * idx + 1); st[idx] = combine(st[2 * idx], st[2 * idx + 1]); } void updateLazy(ll qs, ll qe, ll val, ll s = 1, ll e = n, int idx = 1) { if (lazy[idx] != 0) pushDown(s, e, idx); if (qs > e or qe < s) return; if (s >= qs and e <= qe) { lazy[idx] += val; pushDown(s, e, idx); return; } ll mid = s + (e - s) / 2; updateLazy(qs, qe, val, s, mid, 2 * idx), updateLazy(qs, qe, val, mid + 1, e, 2 * idx + 1); st[idx] = combine(st[2 * idx], st[2 * idx + 1]); } ll query(ll qs, ll qe, ll s = 1, ll e = n, int idx = 1) { if (lazy[idx] != 0) pushDown(s, e, idx); if (qs > e or qe < s) return dummy; if (s >= qs and e <= qe) return st[idx]; ll mid = s + (e - s) / 2; ll lft = query(qs, qe, s, mid, 2 * idx), rt = query(qs, qe, mid + 1, e, 2 * idx + 1); return combine(lft, rt); } } st_pre, st_suf; int main() { fio(); cin >> n >> s >> q; rep1(x, n) pre[x] = pre[x - 1] + ((s[x - 1] == 'C') ? 1 : -1); rrep1(x, n) suf[x] = suf[x + 1] + ((s[x - 1] == 'C') ? 1 : -1); st_pre.build(pre), st_suf.build(suf); rep0(x, q) { int l, r; cin >> l >> r; ll prv = pre[l - 1], nxt = suf[r + 1]; ll cur = 0; int i = l, j = r, k = -1; while (i <= j) { int m = (i + j) / 2; if (st_pre.query(m, m) - prv == -(m - l + 1)) k = m, i = m + 1; else j = m - 1; } if (k != -1) { cur += abs(st_pre.query(k, k) - prv); if (k == r) { cout << cur << '\n'; continue; } l = k + 1, prv = pre[k]; } i = l, j = r, k = -1; while (i <= j) { int m = (i + j) / 2; if (st_suf.query(m, m) - nxt == -(r - m + 1)) k = m, j = m - 1; else i = m + 1; } if (k != -1) { cur += abs(st_suf.query(k, k) - nxt); r = k - 1, nxt = suf[k]; } ll nw = min(st_pre.query(l, r) - prv, st_suf.query(l, r) - nxt); if (nw < 0) cur += abs(nw); cout << cur << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...