#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define vint vector<int>
#define pb push_back
#define sc second
#define fr first
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
int now = 0;
vint pref(n+2), suffix(n+2);
for(int i = 1; i <= n; i++)
{
if(s[i-1] == 'C')
{
pref.push_back(--now);
}
else
{
pref.push_back(++now);
}
}
now = 0;
for(int i = n; i >= 1; i--)
{
if(s[i-1] == 'C')
{
suffix[i] = --now;
}
else
{
suffix[i] = ++now;
}
}
vector<vector<pair<int, int>>> sparsePrefix, sparseSuffix;
sparsePrefix.assign(20, vector<pair<int, int>>(n+5));
sparseSuffix.assign(20, vector<pair<int, int>>(n+5));
for(int i = 1; i <= n; i++)
{
sparsePrefix[0][i] = {pref[i], i};
sparseSuffix[0][i] = {suffix[i], i};
}
for(int l = 1; l < 19; l++)
{
for(int i = 1; i <= n; i++)
{
if(i+(1<<l)-1 > n){break;}
//PREFIX
int mx = max(sparsePrefix[l-1][i].fr, sparsePrefix[l-1][i+(1<<(l-1))].fr);
int ind = 0;
if(mx == sparsePrefix[l-1][i].fr){ind = sparsePrefix[l-1][i].sc;}
else{ind = sparsePrefix[l-1][i+(1<<(l-1))].sc;}
sparsePrefix[l][i] = {mx, ind};
//SUFFIX
mx = max(sparseSuffix[l-1][i].fr, sparseSuffix[l-1][i+(1<<(l-1))].fr);
ind = 0;
if(mx == sparseSuffix[l-1][i+(1<<(l-1))].fr){ind = sparseSuffix[l-1][i+(1<<(l-1))].sc;}
else{ind = sparseSuffix[l-1][i].sc;}
sparseSuffix[l][i] = {mx, ind};
}
}
int q;
cin >> q;
while(q--)
{
int l, r;
cin >> l >> r;
//PREFIX
int dist = 31-__builtin_clz(r-l+1);
int mxPref = max(sparsePrefix[dist][l].fr, sparsePrefix[dist][r-(1<<(dist))+1].fr);
int indPref = 0;
if(mxPref == sparsePrefix[dist][l].fr){indPref = sparsePrefix[dist][l].sc;}
else{indPref = sparsePrefix[dist][r-(1<<(dist))+1].sc;}
mxPref-=pref[l-1];
int mxSuff = max(sparseSuffix[dist][l].fr, sparseSuffix[dist][r-(1<<(dist))+1].fr);
int indSuff = 0;
if(mxSuff == sparseSuffix[dist][r-(1<<(dist))+1].fr){indSuff = sparseSuffix[dist][r-(1<<(dist))+1].sc;}
else{indSuff = sparseSuffix[dist][l].sc;}
mxSuff-=suffix[r+1];
assert(mxPref >= 0);
assert(mxSuff >= 0);
mxPref = max(0, mxPref);
mxSuff = max(0, mxSuff);
int ans = mxPref+mxSuff;
if(indPref < indSuff || mxPref == 0 || mxSuff == 0)
{
cout << ans << endl;
continue;
}
assert(1 == 0);
int nwr = indSuff-1;
int beforePref = 0;
if(nwr >= l)
{
int temp = 31-__builtin_clz(nwr-l+1);
beforePref+=max(sparsePrefix[temp][l].fr, sparsePrefix[temp][nwr-(1<<(temp))+1].fr)-pref[l-1];
beforePref = max(0, beforePref);
}
int nwl = indPref+1;
int beforeSuff = 0;
if(nwl <= r)
{
int temp = 31-__builtin_clz(r-nwl+1);
beforeSuff+=max(sparseSuffix[temp][nwl].fr, sparseSuffix[temp][r-(1<<(temp))+1].fr)-suffix[r+1];
beforeSuff = max(0, beforeSuff);
}
ans-=min(mxPref-beforePref, mxSuff-beforeSuff);
cout << ans << endl;
}
}
int main(){
int t = 1;
//cin >> t;
while (t--) {
solve();
}
}
/*
11
CCCTTTTTTCC
3
1 11
4 9
1 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1748 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1748 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
1748 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |