# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 5e5 + 25;
const ll inf = 1e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, q;
string s;
struct node
{
ll pref, suff, sum, ans;
} t[maxn * 4];
node combine (node l, node r)
{
node res;
res.pref = max(l.pref, l.sum + r.pref);
res.suff = max(r.suff, r.sum + l.suff);
res.sum = l.sum + r.sum;
res.ans = max ({l.ans + r.sum, r.ans + l.sum, l.pref + r.suff});
return res;
}
void build (ll v = 1, ll tl = 1, ll tr = n)
{
if (tl == tr)
{
ll val = max((ll)(s[tl - 1] == 'T' ? 1 : -1), 0LL);
t[v] = {val, val, (s[tl - 1] == 'T' ? 1 : -1), val};
return;
}
else
{
ll tm = (tl + tr) >> 1;
build (v * 2, tl, tm);;
build (v * 2 + 1, tm + 1, tr);
t[v] = combine(t[v * 2], t[v * 2 + 1]);
}
}
node get (ll l, ll r, ll v = 1, ll tl = 1, ll tr = n)
{
if (tr < l || r < tl) return {0, 0, 0, 0};
if (l <= tl && tr <= r) return t[v];
ll tm = (tl + tr) >> 1;
return combine(get(l, r, v * 2, tl, tm), get(l, r, v * 2 +1, tm + 1, tr));
}
void ma1n (/* SABR */)
{
cin >> n >> s >> q;
build();
while (q--)
{
ll l, r;
cin >> l >> r;
cout << get(l, r).ans << nl;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
57 ms |
8920 KB |
Output is correct |
7 |
Correct |
51 ms |
8924 KB |
Output is correct |
8 |
Correct |
58 ms |
8852 KB |
Output is correct |
9 |
Correct |
48 ms |
8888 KB |
Output is correct |
10 |
Correct |
55 ms |
8788 KB |
Output is correct |
11 |
Correct |
55 ms |
8968 KB |
Output is correct |
12 |
Correct |
59 ms |
9056 KB |
Output is correct |
13 |
Correct |
55 ms |
9028 KB |
Output is correct |
14 |
Correct |
56 ms |
9048 KB |
Output is correct |
15 |
Correct |
55 ms |
8944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
57 ms |
8920 KB |
Output is correct |
7 |
Correct |
51 ms |
8924 KB |
Output is correct |
8 |
Correct |
58 ms |
8852 KB |
Output is correct |
9 |
Correct |
48 ms |
8888 KB |
Output is correct |
10 |
Correct |
55 ms |
8788 KB |
Output is correct |
11 |
Correct |
55 ms |
8968 KB |
Output is correct |
12 |
Correct |
59 ms |
9056 KB |
Output is correct |
13 |
Correct |
55 ms |
9028 KB |
Output is correct |
14 |
Correct |
56 ms |
9048 KB |
Output is correct |
15 |
Correct |
55 ms |
8944 KB |
Output is correct |
16 |
Correct |
527 ms |
42648 KB |
Output is correct |
17 |
Correct |
436 ms |
42880 KB |
Output is correct |
18 |
Correct |
485 ms |
43484 KB |
Output is correct |
19 |
Correct |
493 ms |
42700 KB |
Output is correct |
20 |
Correct |
539 ms |
42464 KB |
Output is correct |
21 |
Correct |
549 ms |
44256 KB |
Output is correct |
22 |
Correct |
546 ms |
44060 KB |
Output is correct |
23 |
Correct |
527 ms |
44376 KB |
Output is correct |
24 |
Correct |
554 ms |
44068 KB |
Output is correct |
25 |
Correct |
533 ms |
43492 KB |
Output is correct |