#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 5e5 + 1;
int n;
string s;
struct TNode{
int pre, suf, sum, val;
TNode operator + (TNode other){
TNode tem;
tem.val = max(max(val + other.sum, sum + other.val), pre + other.suf);
tem.pre = max(pre, other.pre + sum);
tem.suf = max(other.suf, suf + other.sum);
tem.sum = sum + other.sum;
return tem;
}
};
TNode st[maxN * 4];
void build(int id = 1, int l = 1, int r = n){
if (l == r){
if (s[l - 1] == 'T') st[id] = {1, 1, 1, 1};
else st[id] = {0, 0, -1, 0};
return;
}
int m = (l + r) / 2;
build(id * 2, l, m);
build(id * 2 + 1, m + 1, r);
st[id] = st[id * 2] + st[id * 2 + 1];
}
TNode get(int i, int j, int id = 1, int l = 1, int r = n){
if (r < i || l > j) return {0, 0, 0, 0};
if (i <= l && r <= j){
return st[id];
}
int m = (l + r) / 2;
return get(i, j, id * 2, l, m) + get(i, j, id * 2 + 1, m + 1, r);
}
void Init(){
cin >> n >> s;
int q; cin >> q;
build();
while (q--){
int l, r;
cin >> l >> r;
cout << get(l, r).val << "\n";
}
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
47 ms |
4808 KB |
Output is correct |
7 |
Correct |
43 ms |
4788 KB |
Output is correct |
8 |
Correct |
54 ms |
4756 KB |
Output is correct |
9 |
Correct |
52 ms |
4744 KB |
Output is correct |
10 |
Correct |
45 ms |
4640 KB |
Output is correct |
11 |
Correct |
48 ms |
4928 KB |
Output is correct |
12 |
Correct |
52 ms |
5016 KB |
Output is correct |
13 |
Correct |
48 ms |
4972 KB |
Output is correct |
14 |
Correct |
49 ms |
4924 KB |
Output is correct |
15 |
Correct |
46 ms |
4860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
47 ms |
4808 KB |
Output is correct |
7 |
Correct |
43 ms |
4788 KB |
Output is correct |
8 |
Correct |
54 ms |
4756 KB |
Output is correct |
9 |
Correct |
52 ms |
4744 KB |
Output is correct |
10 |
Correct |
45 ms |
4640 KB |
Output is correct |
11 |
Correct |
48 ms |
4928 KB |
Output is correct |
12 |
Correct |
52 ms |
5016 KB |
Output is correct |
13 |
Correct |
48 ms |
4972 KB |
Output is correct |
14 |
Correct |
49 ms |
4924 KB |
Output is correct |
15 |
Correct |
46 ms |
4860 KB |
Output is correct |
16 |
Correct |
548 ms |
26248 KB |
Output is correct |
17 |
Correct |
386 ms |
26544 KB |
Output is correct |
18 |
Correct |
492 ms |
26840 KB |
Output is correct |
19 |
Correct |
341 ms |
26336 KB |
Output is correct |
20 |
Correct |
466 ms |
26036 KB |
Output is correct |
21 |
Correct |
467 ms |
27816 KB |
Output is correct |
22 |
Correct |
446 ms |
27836 KB |
Output is correct |
23 |
Correct |
445 ms |
27908 KB |
Output is correct |
24 |
Correct |
463 ms |
27644 KB |
Output is correct |
25 |
Correct |
463 ms |
27192 KB |
Output is correct |