This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 24211007;
const ll infll = 1e18 + 7;
const int base = 200;
const int LOG = 29;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct IT{
int n;
vector<int> tmax, tmin;
IT(){}
IT(int _n) {
n = _n;
tmax.resize(n * 4 + 7, -inf);
tmin.resize(n * 4 + 7, inf);
}
void update(int v, int tl, int tr, int pos, int val)
{
if(tl == tr) {
tmax[v] = val;
tmin[v] = val;
}
else {
int tm = (tl + tr) / 2;
if(pos <= tm) update(v * 2, tl, tm, pos, val);
else update(v * 2 + 1, tm + 1, tr, pos, val);
tmax[v] = max(tmax[v * 2], tmax[v * 2 + 1]);
tmin[v] = min(tmin[v * 2], tmin[v * 2 + 1]);
}
}
int getmax(int v, int tl, int tr, int l, int r)
{
if(l > r) return -inf;
if(tl == l && tr == r) return tmax[v];
int tm = (tl + tr) / 2;
int m1 = getmax(v * 2, tl, tm, l, min(r, tm));
int m2 = getmax(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
return max(m1, m2);
}
int getmin(int v, int tl, int tr, int l, int r)
{
if(l > r) return inf;
if(tl == l && tr == r) return tmin[v];
int tm = (tl + tr) / 2;
int m1 = getmin(v * 2, tl, tm, l, min(r, tm));
int m2 = getmin(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
return min(m1, m2);
}
void update(int pos, int val)
{
update(1, 1, n, pos, val);
}
int getmax(int l, int r)
{
return getmax(1, 1, n, l, r);
}
int getmin(int l, int r)
{
return getmin(1, 1, n, l, r);
}
} seg;
int n;
int q;
int a[mxN];
int pre[mxN];
int maxleng[mxN];
string s;
void solve()
{
cin >> n;
cin >> s;
for(int i = 1; i <= n; i++) {
if(s[i - 1] == 'C') a[i] = 1;
else a[i] = -1;
}
for(int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
seg = IT(n);
for(int i = 1; i <= n; i++) seg.update(i, pre[i]);
for(int i = 1; i <= n; i++) {
if(a[i] == -1) maxleng[i] = 0;
else maxleng[i] = maxleng[i - 1] + a[i];
}
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
int mn = seg.getmin(l, r);
int res = 0;
if(mn < 0) res += -mn;
int lim = max(-seg.getmin(l, r), seg.getmax(l, r));
res += max(0, lim - maxleng[r]);
cout << res << endl;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(TASK".in" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
// cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |