/*
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\
\\ //
// 271828___182845__904523__53602__ \\
\\ 87___47____13______52____66__24_ //
// 97___75____72______47____09___36 \\
\\ 999595_____74______96____69___67 //
// 62___77____24______07____66__30_ \\
\\ 35___35____47______59____45713__ //
// \\
\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
*/
#include <algorithm>
#include <bitset>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using LL = long long;
const int N = 1e5 + 5;
const LL mod = 1e9 + 7, inf = 1e9;
vector<int> dx = { 1, 0, 0, -1, 1, 1, -1, -1 };
vector<int> dy = { 0, 1, -1, 0, 1, -1, 1, -1 };
int n, q;
string s;
struct node {
int l, r, sm, a;
};
vector<node> seg;
node merge(node left, node right) {
node cur;
cur.l = max(left.l, left.sm + right.l);
cur.r = max(right.r, right.sm + left.r);
cur.sm = left.sm + right.sm;
cur.a = max({left.a + right.sm, left.sm + right.a, left.l + right.r});
return cur;
}
void build(int lx, int rx, int u){
if(lx == rx) {
if(lx < n) {
if(s[lx] == 'T') {
seg[u].l = seg[u].r = seg[u].sm = seg[u].a = 1;
}
else {
seg[u].l = seg[u].r = 0;
seg[u].sm = -1;
seg[u].a = 0;
}
}
else {
seg[u].l = seg[u].r = seg[u].sm = seg[u].a = 0;
}
return;
}
int m = (lx + rx) / 2;
build(lx, m, 2 * u + 1), build(m + 1, rx, 2 * u + 2);
seg[u] = merge(seg[2 * u + 1], seg[2 * u + 2]);
}
node get(int ll, int rr, int lx, int rx, int u){
if(lx >= ll && rx <= rr){
return seg[u];
}
if(lx > rr || rx < ll) {
return {0, 0, 0, 0};
}
int m = (lx + rx) / 2;
return merge(get(ll, rr, lx, m, 2 * u + 1), get(ll, rr, m + 1, rx, 2 * u + 2));
}
void solve() {
cin >> n >> s >> q;
int sz = 1;
while(sz < n) {
sz <<= 1;
}
seg = vector<node> (2 * sz);
build(0, sz - 1, 0);
while(q--){
int l, r; cin >> l >> r;
cout << get(l - 1, r - 1, 0, sz - 1, 0).a << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// int t; cin >> t; while(t--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
2 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 |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
60 ms |
5812 KB |
Output is correct |
7 |
Correct |
53 ms |
5708 KB |
Output is correct |
8 |
Correct |
55 ms |
5724 KB |
Output is correct |
9 |
Correct |
49 ms |
5736 KB |
Output is correct |
10 |
Correct |
75 ms |
5680 KB |
Output is correct |
11 |
Correct |
60 ms |
5824 KB |
Output is correct |
12 |
Correct |
58 ms |
5848 KB |
Output is correct |
13 |
Correct |
53 ms |
5828 KB |
Output is correct |
14 |
Correct |
56 ms |
5804 KB |
Output is correct |
15 |
Correct |
66 ms |
5792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
60 ms |
5812 KB |
Output is correct |
7 |
Correct |
53 ms |
5708 KB |
Output is correct |
8 |
Correct |
55 ms |
5724 KB |
Output is correct |
9 |
Correct |
49 ms |
5736 KB |
Output is correct |
10 |
Correct |
75 ms |
5680 KB |
Output is correct |
11 |
Correct |
60 ms |
5824 KB |
Output is correct |
12 |
Correct |
58 ms |
5848 KB |
Output is correct |
13 |
Correct |
53 ms |
5828 KB |
Output is correct |
14 |
Correct |
56 ms |
5804 KB |
Output is correct |
15 |
Correct |
66 ms |
5792 KB |
Output is correct |
16 |
Correct |
513 ms |
26888 KB |
Output is correct |
17 |
Correct |
475 ms |
26552 KB |
Output is correct |
18 |
Correct |
492 ms |
27036 KB |
Output is correct |
19 |
Correct |
427 ms |
26336 KB |
Output is correct |
20 |
Correct |
526 ms |
26176 KB |
Output is correct |
21 |
Correct |
504 ms |
27824 KB |
Output is correct |
22 |
Correct |
507 ms |
27704 KB |
Output is correct |
23 |
Correct |
480 ms |
27992 KB |
Output is correct |
24 |
Correct |
536 ms |
27560 KB |
Output is correct |
25 |
Correct |
495 ms |
27172 KB |
Output is correct |