Submission #729259

#TimeUsernameProblemLanguageResultExecution timeMemory
729259YENGOYANElection (BOI18_election)C++17
100 / 100
536 ms27992 KiB
/* //\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\ \\ // // 271828___182845__904523__53602__ \\ \\ 87___47____13______52____66__24_ // // 97___75____72______47____09___36 \\ \\ 999595_____74______96____69___67 // // 62___77____24______07____66__30_ \\ \\ 35___35____47______59____45713__ // // \\ \\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\// */ #include <algorithm> #include <bitset> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; using LL = long long; const int N = 1e5 + 5; const LL mod = 1e9 + 7, inf = 1e9; vector<int> dx = { 1, 0, 0, -1, 1, 1, -1, -1 }; vector<int> dy = { 0, 1, -1, 0, 1, -1, 1, -1 }; int n, q; string s; struct node { int l, r, sm, a; }; vector<node> seg; node merge(node left, node right) { node cur; cur.l = max(left.l, left.sm + right.l); cur.r = max(right.r, right.sm + left.r); cur.sm = left.sm + right.sm; cur.a = max({left.a + right.sm, left.sm + right.a, left.l + right.r}); return cur; } void build(int lx, int rx, int u){ if(lx == rx) { if(lx < n) { if(s[lx] == 'T') { seg[u].l = seg[u].r = seg[u].sm = seg[u].a = 1; } else { seg[u].l = seg[u].r = 0; seg[u].sm = -1; seg[u].a = 0; } } else { seg[u].l = seg[u].r = seg[u].sm = seg[u].a = 0; } return; } int m = (lx + rx) / 2; build(lx, m, 2 * u + 1), build(m + 1, rx, 2 * u + 2); seg[u] = merge(seg[2 * u + 1], seg[2 * u + 2]); } node get(int ll, int rr, int lx, int rx, int u){ if(lx >= ll && rx <= rr){ return seg[u]; } if(lx > rr || rx < ll) { return {0, 0, 0, 0}; } int m = (lx + rx) / 2; return merge(get(ll, rr, lx, m, 2 * u + 1), get(ll, rr, m + 1, rx, 2 * u + 2)); } void solve() { cin >> n >> s >> q; int sz = 1; while(sz < n) { sz <<= 1; } seg = vector<node> (2 * sz); build(0, sz - 1, 0); while(q--){ int l, r; cin >> l >> r; cout << get(l - 1, r - 1, 0, sz - 1, 0).a << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // int t; cin >> t; while(t--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...