#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 500002;
const int MOD = 1000000007;
struct node {
int lmx, rmx, sum, ans;
node operator+(node o) {
node ret;
ret.lmx = max(lmx, o.lmx+sum);
ret.rmx = max(rmx+o.sum, o.rmx);
ret.sum = sum+o.sum;
ret.ans = max(max(ans+o.sum, sum+o.ans), lmx+o.rmx);
return ret;
}
};
int n, q;
bool votes[MAXN];
node st[1<<20];
void build(int l=1, int r=n, int x=1) {
if (l == r) {
if (votes[l]) st[x] = {1, 1, 1, 1};
else st[x] = {0, 0, -1, 0};
} else {
int mid = (l+r)/2;
build(l, mid, 2*x);
build(mid+1, r, 2*x+1);
st[x] = st[2*x]+st[2*x+1];
}
}
node qry(int a, int b, int l=1, int r=n, int x=1) {
if (a <= l && b >= r) return st[x];
node ret = {0, 0, 0, 0};
int mid = (l+r)/2;
if (a <= mid) ret = ret+qry(a, b, l, mid, 2*x);
if (b > mid) ret = ret+qry(a, b, mid+1, r, 2*x+1);
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen("elections.in", "r", stdin);
freopen("elections.out", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
char c; cin >> c;
votes[i] = c == 'T';
}
build();
cin >> q;
for (int i = 0; i < q; i++) {
int a, b; cin >> a >> b;
cout << qry(a, b).ans << '\n';
}
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:52:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
52 | freopen("elections.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
53 | freopen("elections.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
179 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
179 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
179 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |