Submission #836712

#TimeUsernameProblemLanguageResultExecution timeMemory
836712abysmalElection (BOI18_election)C++14
100 / 100
422 ms28012 KiB
#include<iostream> #include<stdio.h> #include<stdint.h> #include<iomanip> #include<algorithm> #include<utility> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<deque> #include<math.h> #include<assert.h> #include<string.h> using namespace std; const int64_t INF = (int64_t) 1e18 + 100; const int64_t mINF = (int64_t) 1e6 * 2 + 100; const int64_t MOD = 1e9 + 7; const int64_t MOD2 = 998244353; const int nbit = 31; const int ndig = 10; const int nchar = 26; const int D = 4; int dr[D] = {-1, 0, 1, 0}; int dc[D] = {0, 1, 0, -1}; const int Dk = 8; int drk[Dk] = {2, 2, -2, -2, 1, 1, -1, -1}; int dck[Dk] = {1, -1, 1, -1, 2, -2, 2, -2}; struct Node { int mx; int sum; int pre; int suf; Node(int mx_, int sum_, int pre_, int suf_) : mx(mx_), sum(sum_), pre(pre_), suf(suf_) {} }; struct Solution { int n; string s; vector<Node> tree; Solution() {} void solve() { cin >> n >> s; while(__builtin_popcount(n) != 1) n++; tree.resize(2 * n, Node(0, 0, 0, 0)); for(int i = 0; i < (int) s.length(); i++) { if(s[i] == 'T') tree[n + i] = Node(1, 1, 1, 1); else tree[n + i] = Node(0, -1, 0, 0); } for(int i = n - 1; i >= 1; i--) { tree[i] = combine(tree[i * 2], tree[i * 2 + 1]); } int q; cin >> q; for(int i = 0; i < q; i++) { int l; int r; cin >> l >> r; l--; r--; cout << get(1, 0, n - 1, l, r).mx << "\n"; } } Node get(int node, int nleft, int nright, int left, int right) { if(left > nright || nleft > right) return Node(0, 0, 0, 0); if(left <= nleft && nright <= right) return tree[node]; int mid = nleft + (nright - nleft) / 2; return combine(get(node * 2, nleft, mid, left, right), get(node * 2 + 1, mid + 1, nright, left, right)); } Node combine(Node left, Node right) { Node now(0, 0, 0, 0); now.sum = left.sum + right.sum; now.pre = max(left.pre, left.sum + right.pre); now.suf = max(right.suf, right.sum + left.suf); now.mx = max({left.mx + right.sum, left.sum + right.mx, left.pre + right.suf}); return now; } void modadd(int& t1, int t2) { t1 += t2; if(t1 >= MOD) t1-= MOD; return; } void modsub(int& t1, int t2) { t1 -= t2; if(t1 < 0) t1 += MOD; return; } int modmul(int t1, int t2) { int64_t res = 1LL * t1 * t2; return res % MOD; } int Abs(int tmp) { if(tmp < 0) return -tmp; return tmp; } int MASK(int j) { return (1 << j); } int BIT(int tmp, int j) { int x = tmp & MASK(j); if(x != 0) return 1; return 0; } }; void __setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); } int main() { __setup(); int t = 1; // cin >> t; for(int i = 1; i <= t; i++) { Solution().solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...