#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
#include<string.h>
#define int long long
#define size() size() * 1ll
#define all(x) (x).begin(), (x).end()
#define allr(x, sz) (x) + 1, (x) + 1 + sz
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define MASK(x) (1LL<<(x))
#define BIT(x,i) (((x)>>(i))&1)
#define numbit(x) __builtin_popcountll(x)
using namespace std;
const int N = 1e6 + 1;
const int M = 1e3 + 1;
const long long mod = 1e9 + 7;
const long long oo = 1e18 + 7;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef long long ll;
template<class t>
bool mini(t &x,t y) {
if (x > y) {
x = y;
return 1;
}
return 0;
}
template<class t>
bool maxi(t &x,t y) {
if (x < y) {
x = y;
return 1;
}
return 0;
}
void file() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen(task.in, r, stdin);
//freopen(task.out, w, stdout);
return ;
}
int n, q;
string s;
struct IT {
struct node {
int a, s, l, r;
node(const int &_a = 0, const int &_s = 0, const int &_l = 0, const int &_r = 0): a(_a), s(_s), l(_l), r(_r) {}
};
int n;
vector<node> st;
IT(const int &_n = 0): n(_n + 1), st(4 * _n + 1, node()) {}
node merge(node l, node r) {
return node(max(l.l + r.r, max(l.s + r.a, l.a + r.s)), l.s + r.s, max(l.l, l.s + r.l), max(l.r + r.s, r.r));
}
void adj(int id, int l, int r, int i, int val) {
if (i < l || r < i) return ;
if (l == r) {
st[id].s = val;
maxi(st[id].a, val);
maxi(st[id].l, val);
maxi(st[i].r, val);
return ;
}
int mid = l + r >> 1;
adj(id << 1, l, mid, i, val);
adj(id << 1 | 1, mid + 1, r, i, val);
st[id] = merge(st[id << 1], st[id << 1 | 1]);
}
node get(int id, int l, int r, int u, int v) {
if (v < l || r < u) return node();
if (u <= l && r <= v) return st[id];
int mid = l + r >> 1;
return merge(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
};
void solve(int test = -1) {
cin >> n >> s >> q;
IT it(n);
for (int i = 1; i <= n; i++) {
if (s[i - 1] == 'T') it.adj(1, 1, n, i, 1); else it.adj(1, 1, n, i, -1);
}
while (q--) {
int l, r;
cin >> l >> r;
cout << it.get(1, 1, n, l, r).a << '\n';
}
}
int32_t main() {
file();
int T = 1;
// cin >> T;
for (int i = 1; i <= T; i++) {
solve(i);
}
return 0;
}
Compilation message
election.cpp: In member function 'void IT::adj(long long int, long long int, long long int, long long int, long long int)':
election.cpp:76:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
76 | int mid = l + r >> 1;
| ~~^~~
election.cpp: In member function 'IT::node IT::get(long long int, long long int, long long int, long long int, long long int)':
election.cpp:84:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
84 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |