#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] - pref[l-1] < mi){
mi = pref[i] - pref[l-1];
id = i;
}
}
return {abs(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] - suff[r+1] < mi){
mi = suff[i] - suff[r+1];
id = i;
}
}
return {abs(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;
if(A.nd < B.nd){
return A.st + B.st;
}
pii L = q_prefix(l, B.nd-1);
pii R = q_suffix(A.nd+1, r);
RET = A.st + R.st;
// cout<<"here : "<<RET<<" "<<L.st<<" " <<R.st<<endl;
int init_sum_l = pref[l-1];
int init_sum_r = suff[r+1];
int MIN = 1e9;
// cout<<init_sum_l<<endl;
int max_height = pref[B.nd-1];
fr(i, B.nd, A.nd){
// cout<<pref[i]<<' ';
MIN = min(MIN, (pref[i] + L.st));
// (init_sum_l - MIN) how much should we increase MIN to get above given 0 - init_sum_l
max_height = max(max_height, (pref[i] + L.st) + (init_sum_l - MIN));
}
// cout<<endl;
// cout<<max_height<<endl;
int offset = max_height - init_sum_l;
int min_point = suff[A.nd+1] + R.st - offset;
RET += max(0, init_sum_r - min_point);
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 |
6 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |