#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <string.h>
#include <bitset>
#include <numeric>
#include <utility>
#define fr(i, n, m) for(int i = (n); i <= (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int mxn = 2e5;
const ll mod = 998244353;
const ll inf = 1e18;
int n;
string s;
int pref[mxn];
int suff[mxn];
pair<int,int> q_prefix(int l, int r){
int mi = 0;
int id = l-1;
for(int i = l; i <= r; i ++){
if(pref[i] < mi){
mi = pref[i];
id = i;
}
}
if(mi == 0) return {0, id};
return {pref[l-1] - mi,id};
}
pair<int,int> q_suffix(int l, int r){
int mi = 0;
int id = r+1;
for(int i = r; i >= l; i --){
if(suff[i] < mi){
mi = suff[i];
id = i;
}
}
if(mi == 0) return {0, id};
return {suff[r+1] - mi,id};
}
int solve(int l, int r){
int RET = 0;
pii A = q_prefix(l, r);
pii B = q_suffix(l, r);
if(A.st == 0) return B.st;
if(B.st == 0) return A.st;
pii C = q_suffix(A.nd+1, r);
RET += C.st;
pii D = q_suffix(l, A.nd);
pii E = q_prefix(l, D.nd-1);
RET += E.st;
int REM1 = A.st - E.st;
int REM2 = D.st - ((suff[A.nd+1] - suff[r+1]) + C.st);
RET += max({0, REM1, REM2});
return RET;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
cin >> s;
for(int i = 1; i <= n; i ++){
pref[i] = pref[i-1];
if(s[i-1] == 'C') pref[i] += 1;
if(s[i-1] == 'T') pref[i] -= 1;
}
for(int i = n; i >= 1; i --){
suff[i] = suff[i+1];
if(s[i-1] == 'C') suff[i] += 1;
if(s[i-1] == 'T') suff[i] -= 1;
}
int Q;
cin >> Q;
fr(i, 1, Q){
int l, r;
cin >> l >> r;
cout<<solve(l, r)<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |