/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 22;
int n, q;
array<int, 3> T[N];
string s;
vector<int> pref;
array<int, 3> combine(array<int, 3> x, array<int, 3> y){
return {
min(x[0], x[2] + y[0]),
min(y[1], x[1] + y[2]),
x[2] + y[2]
};
}
void build(int l, int r, int k){
if(l == r){
T[k][0] = s[l - 1] == 'T' ? -1 : 0;
T[k][1] = T[k][0];
T[k][2] = s[l - 1] == 'T' ? -1 : 1;
return;
}
int m = l+r>>1;
build(l, m, k<<1);
build(m+1, r, k<<1|1);
T[k] = combine(T[k<<1], T[k<<1|1]);
}
array<int, 3> get(int l, int r, int ql, int qr, int k){
if(ql > r || qr < l) return {0, 0, 0};
if(ql <= l && r <= qr) return T[k];
int m = l + r >> 1;
auto x = get(l, m, ql, qr, k<<1);
auto y = get(m+1, r, ql, qr, k<<1|1);
return combine(x, y);
}
void solve(){
cin >> n >> s;
cin >> q;
pref.resize(n + 1);
pref[0] = 0;
for(int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + (s[i-1] == 'T');
build(1, n, 1);
for(int i = 0; i < q; ++i){
int l, r; cin >> l >> r;
// cout << get(1, n, l, r, 1)[1] << ' ' <<get(1, n, l, r, 1)[1] << ' ' << pref[r] - pref[l-1] << '\n';
cout << -min(get(1, n, l, r, 1)[0], get(1, n, l, r, 1)[1]) << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;
while(tt--){
solve();
// en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
election.cpp: In function 'void build(int, int, int)':
election.cpp:32:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int m = l+r>>1;
| ~^~
election.cpp: In function 'std::array<int, 3> get(int, int, int, int, int)':
election.cpp:40:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int m = l + r >> 1;
| ~~^~~
election.cpp: In function 'int main()':
election.cpp:62:15: warning: unused variable 'aa' [-Wunused-variable]
62 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |