#include<bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x >> (i)) & 1)
#define fi first
#define se second
#define ll long long
#define task "name"
const int oo = 1e9 + 7;
const ll loo = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int N = 5e5 + 3;
const int BASE = 10;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b)
{
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b)
{
if (a < b) {a = b; return true;} return false;
}
struct nodes
{
int sum = 0;
int pr = 0;
int sf = 0;
int ans = 0;
} node[4 * N], base;
int n;
string s;
int a[N];
nodes add(nodes a, nodes b)
{
nodes 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(int id, int l, int r, int pos, int x)
// {
// if(l > r || pos < l || r > pos) return;
// if (l == r)
// {
// node[id].sum = x;
// maximize(node[id].pr, a[l]);
// maximize(node[id].sf, a[l]);
// maximize(node[id].ans, a[l]);
// return;
// }
// int mid = (l + r)/2;
// build(id * 2, l, mid, pos, x);
// build(id * 2 + 1, mid + 1, r, pos, x);
// node[id] = add(node[id * 2], node[id * 2 + 1]);
// }
void build(int id, int l, int r)
{
if (l == r)
{
node[id].sum = a[l];
maximize(node[id].pr, a[l]);
maximize(node[id].sf, a[l]);
maximize(node[id].ans, a[l]);
return;
}
int mid = (l + r)/2;
build(id * 2, l, mid);
build(id * 2 + 1, mid + 1, r);
node[id] = add(node[id * 2], node[id * 2 + 1]);
}
nodes get(int id, int l, int r, int u, int v)
{
if (r < u || v < l) return base;
if (u <= l && r <= v) return node[id];
int mid = (l + r)/2;
return add(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(task".inp" , "r" , stdin);
//freopen(task".out" , "w" , stdout);
cin >> n >> s;
for (int i = 0; i < n; i++)
{
if(s[i] == 'T')
{
a[i + 1] = 1;
//build(1, 1, n, i + 1, 1);
}
else
{
a[i + 1] = -1;
//build(1, 1, n, i + 1, -1);
}
}
build(1, 1, n);
int q;
cin >> q;
while(q--)
{
int l, r;
cin >> l >> r;
cout << get(1, 1, n, l, r).ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
55 ms |
6064 KB |
Output is correct |
7 |
Correct |
50 ms |
6036 KB |
Output is correct |
8 |
Correct |
50 ms |
5980 KB |
Output is correct |
9 |
Correct |
47 ms |
6012 KB |
Output is correct |
10 |
Correct |
47 ms |
5948 KB |
Output is correct |
11 |
Correct |
49 ms |
6100 KB |
Output is correct |
12 |
Correct |
44 ms |
6096 KB |
Output is correct |
13 |
Correct |
56 ms |
6136 KB |
Output is correct |
14 |
Correct |
46 ms |
6092 KB |
Output is correct |
15 |
Correct |
52 ms |
6080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
55 ms |
6064 KB |
Output is correct |
7 |
Correct |
50 ms |
6036 KB |
Output is correct |
8 |
Correct |
50 ms |
5980 KB |
Output is correct |
9 |
Correct |
47 ms |
6012 KB |
Output is correct |
10 |
Correct |
47 ms |
5948 KB |
Output is correct |
11 |
Correct |
49 ms |
6100 KB |
Output is correct |
12 |
Correct |
44 ms |
6096 KB |
Output is correct |
13 |
Correct |
56 ms |
6136 KB |
Output is correct |
14 |
Correct |
46 ms |
6092 KB |
Output is correct |
15 |
Correct |
52 ms |
6080 KB |
Output is correct |
16 |
Correct |
528 ms |
28956 KB |
Output is correct |
17 |
Correct |
378 ms |
28512 KB |
Output is correct |
18 |
Correct |
476 ms |
28780 KB |
Output is correct |
19 |
Correct |
371 ms |
28304 KB |
Output is correct |
20 |
Correct |
480 ms |
27952 KB |
Output is correct |
21 |
Correct |
482 ms |
29756 KB |
Output is correct |
22 |
Correct |
460 ms |
29672 KB |
Output is correct |
23 |
Correct |
466 ms |
29924 KB |
Output is correct |
24 |
Correct |
488 ms |
29512 KB |
Output is correct |
25 |
Correct |
442 ms |
29040 KB |
Output is correct |