// Balkan Olympiad in Informatics 2018 - election
#include <bits/stdc++.h>
#define lf (h << 1)
#define mf ((l + r) >> 1)
#define rf ((h << 1) | 1)
#define tree int h, int l, int r
#define left lf, l, mf
#define right rf, mf + 1, r
using namespace std;
const int N = 5e5 + 5;
int n, q, L, R, a[N];
struct node {
int pr = 0;
int sf = 0;
int sum = 0;
int ans = 0;
};
node v[4 * N], base;
node comb(node a, node b) {
node c;
c.sum = a.sum + b.sum;
c.pr = max(a.pr, a.sum + b.pr);
c.sf = max(b.sf, b.sum + a.sf);
c.ans = max(a.sum + b.ans, a.ans + b.sum);
c.ans = max(c.ans, a.pr + b.sf);
return c;
}
void build(tree) {
if (l == r) {
v[h].sum = a[l];
v[h].pr = max(v[h].pr, a[l]);
v[h].sf = max(v[h].sf, a[l]);
v[h].ans = max(v[h].ans, a[l]);
return ;
}
build(left), build(right);
v[h] = comb(v[lf], v[rf]);
}
node get(tree) {
if (r < L || R < l) return base;
if (L <= l && r <= R) return v[h];
return comb(get(left), get(right));
}
main () {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; ++i) {
char c;
cin >> c;
if (c == 'T')
a[i] = 1;
else
a[i] = -1;
}
build(1, 1, n);
cin >> q;
for (int i = 1; i <= q; ++i) {
cin >> L >> R;
cout << get(1, 1, n).ans << "\n";
}
}
Compilation message
election.cpp:55:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
55 | main () {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
58 ms |
5784 KB |
Output is correct |
7 |
Correct |
47 ms |
5828 KB |
Output is correct |
8 |
Correct |
48 ms |
5752 KB |
Output is correct |
9 |
Correct |
53 ms |
5824 KB |
Output is correct |
10 |
Correct |
60 ms |
5700 KB |
Output is correct |
11 |
Correct |
50 ms |
5956 KB |
Output is correct |
12 |
Correct |
52 ms |
5900 KB |
Output is correct |
13 |
Correct |
56 ms |
5984 KB |
Output is correct |
14 |
Correct |
63 ms |
5904 KB |
Output is correct |
15 |
Correct |
51 ms |
5840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
58 ms |
5784 KB |
Output is correct |
7 |
Correct |
47 ms |
5828 KB |
Output is correct |
8 |
Correct |
48 ms |
5752 KB |
Output is correct |
9 |
Correct |
53 ms |
5824 KB |
Output is correct |
10 |
Correct |
60 ms |
5700 KB |
Output is correct |
11 |
Correct |
50 ms |
5956 KB |
Output is correct |
12 |
Correct |
52 ms |
5900 KB |
Output is correct |
13 |
Correct |
56 ms |
5984 KB |
Output is correct |
14 |
Correct |
63 ms |
5904 KB |
Output is correct |
15 |
Correct |
51 ms |
5840 KB |
Output is correct |
16 |
Correct |
469 ms |
28352 KB |
Output is correct |
17 |
Correct |
425 ms |
27864 KB |
Output is correct |
18 |
Correct |
433 ms |
28180 KB |
Output is correct |
19 |
Correct |
364 ms |
27600 KB |
Output is correct |
20 |
Correct |
429 ms |
27380 KB |
Output is correct |
21 |
Correct |
448 ms |
29116 KB |
Output is correct |
22 |
Correct |
446 ms |
29048 KB |
Output is correct |
23 |
Correct |
453 ms |
29408 KB |
Output is correct |
24 |
Correct |
443 ms |
28996 KB |
Output is correct |
25 |
Correct |
432 ms |
28484 KB |
Output is correct |