#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx,avx2,fma")
#include"bits/stdc++.h"
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class x>
using ordered_set = tree<x, null_type,less<x>, rb_tree_tag,tree_order_statistics_node_update>;
#define int long long
#define endl '\n'
#define mod 1000000007
//\
#define mod 1686876991
const int maxn = 500001;
const int N = exp2(ceil(log2(maxn)));
int Left, Right;
struct node {
int sum;
int mp, ms;
int ans;
};
node operator +(const node a, const node b) {
node c;
c.sum = a.sum + b.sum;
c.mp = max(a.mp, b.mp + a.sum);
c.ms = max(b.ms, a.ms + b.sum);
c.ans = max({a.mp + b.ms, b.ans + a.sum, a.ans + b.sum});
return c;
}
node seg[2 * N];
node Query (int l = 1, int r = N, int ind = 1) {
if (l > Right || r < Left) assert(0);
if (l >= Left and r <= Right) return seg[ind];
int m = (l + r) / 2;
if (Left > m) return Query(m + 1, r, ind * 2 + 1);
if (Right <= m) return Query(l, m, ind * 2);
return Query(l, m, ind * 2) + Query(m + 1, r, ind * 2 + 1);
}
signed main () {
cin.tie(0)->sync_with_stdio(0);
int n, q;
string s;
cin >> n >> s >> q;
for (int i = 0 ; i < n ; i++) {
if (s[i] == 'T') {
seg[N + i] = {1, 1, 1, 1};
} else {
seg[N + i] = {-1, 0, 0, 0};
}
}
for (int i = N - 1 ; i ; i--) seg[i] = seg[i * 2] + seg[i * 2 + 1];
while (q--) {
cin >> Left >> Right;
cout << Query().ans << endl;
}
}
Compilation message
election.cpp:17:1: warning: multi-line comment [-Wcomment]
17 | //\
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16724 KB |
Output is correct |
2 |
Correct |
18 ms |
16828 KB |
Output is correct |
3 |
Correct |
16 ms |
16804 KB |
Output is correct |
4 |
Correct |
15 ms |
16820 KB |
Output is correct |
5 |
Correct |
15 ms |
16836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16724 KB |
Output is correct |
2 |
Correct |
18 ms |
16828 KB |
Output is correct |
3 |
Correct |
16 ms |
16804 KB |
Output is correct |
4 |
Correct |
15 ms |
16820 KB |
Output is correct |
5 |
Correct |
15 ms |
16836 KB |
Output is correct |
6 |
Correct |
72 ms |
19324 KB |
Output is correct |
7 |
Correct |
68 ms |
19340 KB |
Output is correct |
8 |
Correct |
69 ms |
19288 KB |
Output is correct |
9 |
Correct |
54 ms |
19272 KB |
Output is correct |
10 |
Correct |
60 ms |
19220 KB |
Output is correct |
11 |
Correct |
69 ms |
19396 KB |
Output is correct |
12 |
Correct |
71 ms |
19500 KB |
Output is correct |
13 |
Correct |
62 ms |
19424 KB |
Output is correct |
14 |
Correct |
72 ms |
19328 KB |
Output is correct |
15 |
Correct |
59 ms |
19340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16724 KB |
Output is correct |
2 |
Correct |
18 ms |
16828 KB |
Output is correct |
3 |
Correct |
16 ms |
16804 KB |
Output is correct |
4 |
Correct |
15 ms |
16820 KB |
Output is correct |
5 |
Correct |
15 ms |
16836 KB |
Output is correct |
6 |
Correct |
72 ms |
19324 KB |
Output is correct |
7 |
Correct |
68 ms |
19340 KB |
Output is correct |
8 |
Correct |
69 ms |
19288 KB |
Output is correct |
9 |
Correct |
54 ms |
19272 KB |
Output is correct |
10 |
Correct |
60 ms |
19220 KB |
Output is correct |
11 |
Correct |
69 ms |
19396 KB |
Output is correct |
12 |
Correct |
71 ms |
19500 KB |
Output is correct |
13 |
Correct |
62 ms |
19424 KB |
Output is correct |
14 |
Correct |
72 ms |
19328 KB |
Output is correct |
15 |
Correct |
59 ms |
19340 KB |
Output is correct |
16 |
Correct |
534 ms |
41948 KB |
Output is correct |
17 |
Correct |
475 ms |
42276 KB |
Output is correct |
18 |
Correct |
532 ms |
42436 KB |
Output is correct |
19 |
Correct |
370 ms |
42000 KB |
Output is correct |
20 |
Correct |
472 ms |
41696 KB |
Output is correct |
21 |
Correct |
514 ms |
43616 KB |
Output is correct |
22 |
Correct |
475 ms |
43408 KB |
Output is correct |
23 |
Correct |
494 ms |
43596 KB |
Output is correct |
24 |
Correct |
489 ms |
43460 KB |
Output is correct |
25 |
Correct |
472 ms |
42776 KB |
Output is correct |