#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
constexpr int INF = (int)2e9;
constexpr int N = (int)5e5 + 111;
constexpr int md = (int)998244353;
constexpr int mdPower = (int)1e9+7 - 1;
constexpr double eps = 1e-7;
typedef __int128 _uint;
typedef long long ll;
mt19937_64 rnd(time(nullptr));
void add(int& a,int b){
a += b; if(a >= md) a -= md;
}
void sub(int& a,int b){
a -= b; while(a < 0) a += md;
}
void add(__int128& a,int b){
a += b;
}
void sub(__int128& a,int b){
a -= b;
}
int bpow(int a,int b){
if(b == 0)
return 1;
if(b % 2 == 0){
int t = bpow(a,b>>1);
return 1ll*t*t%md;
}
return 1ll * a*bpow(a,b-1) % md;
}
int inv(int a){
return bpow(a,md-2);
}
//int fac[N],invfac[N];
//
//void init(){
// fac[0] = 1;
// for(int i = 1; i < N; i++){
// fac[i] = (fac[i-1] * i) % md;
// }
// invfac[N-1] = inv(fac[N-1]);
// for(int i = N-2; i >= 0; i--){
// invfac[i] = (invfac[i+1] * (i+1))%md;
// }
// return;
//}
//
//int C(int n,int k){
// if(k > n)
// return 0;
// return fac[n] * invfac[k] % md * invfac[n-k] % md;
//}
//
//int A(int n,int k){
// if(k > n)
// return 0;
// return fac[n] * invfac[n-k] % md;
//
int t[2][4*N];
int a[2][N];
void build(int v,int l,int r){
if(l == r){
for(int i = 0; i < 2; i++)
t[i][v] = a[i][l];
return;
}
int m = (l+r)>>1;
build(v<<1,l,m);
build(v<<1|1,m+1,r);
for(int i = 0; i < 2; i++)
t[i][v] = min(t[i][v<<1],t[i][v<<1|1]);
return;
}
int get(int v,int l,int r,int tl,int tr,int T){
if(l > r || tl > tr)
return INF;
if(l == tl && r == tr)
return t[T][v];
int m = (l+r)>>1;
return min(get(v<<1,l,m,tl,min(tr,m),T),get(v<<1|1,m+1,r,max(tl,m+1),tr,T));
}
int b[N];
void solve(){
int n;
cin >> n;
string s;
cin >> s;
s = "#" + s;
for(int i = 1; i <= n; i++){
b[i] = (s[i] == 'T' ? -1 : 1);
}
for(int i = 1; i <= n; i++){
a[0][i] = a[0][i-1] + (s[i] == 'T' ? -1 : 1);
}
for(int i = n; i > 0; i--){
a[1][i] = a[1][i+1] + (s[i] == 'T' ? -1 : 1);
}
// for(int i = 1; i <= n; i++){
// cout << a[i] << " ";
// }
// cout << "\n";
build(1,1,n);
int q;
cin >> q;
while(q--){
int l,r;
cin >> l >> r;
int mn = INF;
int pos = 0;
int sum = 0;
for(int i = l; i <= r; i++){
sum += b[i];
if(a[0][i] < mn){
mn = a[0][i];
pos = i;
}
}
mn -= a[0][l-1];
mn = -mn;
int cnt = 0;
vector<int> S;
for(int i = l; i <= r && mn > 0; i++){
if(b[i] == -1){
cnt++;
S.pb(i);
b[i] = 0;
mn--;
}
}
mn = INF;
pos = -1;
sum = 0;
for(int i = r; i >= l; i--){
sum += b[i];
if(sum < mn){
mn = sum;
pos = i;
}
}
mn = -mn;
cnt += max(0ll,mn);
for(auto& x : S)
b[x] = -1;
cout << cnt << "\n";
}
return;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int tests = 1;
// cin >> tests;
for(int test = 1; test <= tests; test++){
// cerr << "test = " << test << "\n";
solve();
}
return 0;
}
/**
x2 x1 x0
[x2+2*x1+x0] [x1+x0] [x0]
1 0 0
2 1 0
1 1 1
1 0 0
-2 1 0
1 -1 1
**/
Compilation message
election.cpp: In function 'void solve()':
election.cpp:134:13: warning: variable 'pos' set but not used [-Wunused-but-set-variable]
134 | int pos = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |