#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
using namespace std;
signed main() {
int n, q, x, y, temp, ma;
string s;
cin >> n >> s;
vector<int> a(n), pref(n);
for(int i = 0; i < n; i++) {
if(s[i] == 'C') a[i] = 1;
else a[i] = -1;
pref[i] = a[i] + (i ? pref[i - 1] : 0);
}
function<int(int,int)> sum = [&](int l, int r) {
return pref[r] - (l ? pref[l - 1] : 0);
};
cin >> q;
while(q--) {
cin >> x >> y;
x--, y--;
temp = 0, ma = LLONG_MIN;
for(int i = x; i <= y; i++) {
temp = min(temp, sum(x, i));
ma = max(ma, sum(x, i) - temp);
}
cout << max(ma - sum(x, y), abs(temp)) << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |