#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
struct segtree{
struct node{
int sm=0, L=0, R=0, A=0;
node operator+(node b){
return node{sm+b.sm,
max(L,sm+b.L),
max(b.R,b.sm+R),
max(max(sm+b.A,A+b.sm),L+b.R)};
}
};
int n;
vector<node> seg;
segtree(vi a){
n = 1;
while(n<sz(a))
n*=2;
seg.resize(n*2);
rep(i,0,sz(a)) seg[i+n] = {a[i],max(0,a[i]),max(0,a[i]),max(0,a[i])};
for(int i = n-1; i>0; --i) seg[i] = seg[i*2]+seg[i*2+1];
}
node query(int id, int l, int r, int ql, int qr){
if (r <= ql or l >= qr) return {};
else if (l >= ql and r <= qr) return seg[id];
else{
int mid = (l+r)/2;
return query(id*2,l,mid,ql,qr)+query(id*2+1,mid,r,ql,qr);
}
}
};
int main(){
cin.tie(NULL),cin.sync_with_stdio(false);
int n; cin >> n;
vi a(n);
string s; cin >> s;
rep(i,0,n){
a[i] = (s[i]=='T'?1:-1);
}
segtree seg(a);
int q; cin >> q;
while(q--){
int l,r; cin >> l >> r;
l--;
auto ret = seg.query(1,0,seg.n,l,r);
cout << ret.A << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
32 ms |
6024 KB |
Output is correct |
7 |
Correct |
30 ms |
5952 KB |
Output is correct |
8 |
Correct |
34 ms |
5948 KB |
Output is correct |
9 |
Correct |
30 ms |
5956 KB |
Output is correct |
10 |
Correct |
33 ms |
5956 KB |
Output is correct |
11 |
Correct |
33 ms |
6216 KB |
Output is correct |
12 |
Correct |
34 ms |
6144 KB |
Output is correct |
13 |
Correct |
33 ms |
6204 KB |
Output is correct |
14 |
Correct |
33 ms |
6204 KB |
Output is correct |
15 |
Correct |
33 ms |
6204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
32 ms |
6024 KB |
Output is correct |
7 |
Correct |
30 ms |
5952 KB |
Output is correct |
8 |
Correct |
34 ms |
5948 KB |
Output is correct |
9 |
Correct |
30 ms |
5956 KB |
Output is correct |
10 |
Correct |
33 ms |
5956 KB |
Output is correct |
11 |
Correct |
33 ms |
6216 KB |
Output is correct |
12 |
Correct |
34 ms |
6144 KB |
Output is correct |
13 |
Correct |
33 ms |
6204 KB |
Output is correct |
14 |
Correct |
33 ms |
6204 KB |
Output is correct |
15 |
Correct |
33 ms |
6204 KB |
Output is correct |
16 |
Correct |
286 ms |
28940 KB |
Output is correct |
17 |
Correct |
252 ms |
28480 KB |
Output is correct |
18 |
Correct |
276 ms |
28740 KB |
Output is correct |
19 |
Correct |
252 ms |
28228 KB |
Output is correct |
20 |
Correct |
278 ms |
27968 KB |
Output is correct |
21 |
Correct |
295 ms |
29760 KB |
Output is correct |
22 |
Correct |
285 ms |
29760 KB |
Output is correct |
23 |
Correct |
296 ms |
30020 KB |
Output is correct |
24 |
Correct |
281 ms |
29508 KB |
Output is correct |
25 |
Correct |
295 ms |
29248 KB |
Output is correct |