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>
using namespace std;
const int max_n = 5e5+2;
struct Node{
int l = 0, r = 0, sum = 0, sol = 0;
};
Node t[4*max_n];
// a > 0, a tiene t[a] platos sin pagar
Node merge(Node a, Node b) {
Node ans;
ans.l = max(a.l, a.sum+b.l);
ans.r = max(b.r, b.sum+a.r);
ans.sum = a.sum+b.sum;
ans.sol = max(a.l+b.r, max(a.sum+b.sol, a.sol+b.sum));
return ans;
}
Node updateN(Node x, int p) {
if (p > 0) x.l = x.r = x.sum = x.sol = x.sum+p;
else x.sum += p;
return x;
}
void update_p(int n, int l, int r, int p, int x) {
if (l > p || r < p) return;
if (l == r) {
t[n] = updateN(t[n],x);
}
else {
int mid = (r+l)/2;
update_p(2*n, l, mid, p, x);
update_p(2*n+1, mid+1, r, p, x);
t[n] = merge(t[2*n], t[2*n+1]);
//cout << l << ' ' << r << ' ' << t[n].valL << ' ' << t[n].valR << '\n';
}
}
Node val(int n, int l, int r, int l2, int r2) {
if (l > r2 || r < l2) return Node();
if (l >= l2 && r <= r2) return t[n];
int mid = (r+l)/2;
Node L = val(2*n, l, mid, l2, r2);
Node R = val(2*n+1, mid+1, r, l2, r2);
return merge(L, R);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
string s;
cin >> n >> s;
for (int i = 0; i < n; ++i) {
if (s[i] == 'C') update_p(1, 0, max_n-1, i, -1);
else update_p(1, 0, max_n-1, i, 1);
}
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
--x;--y;
Node ans = val(1, 0, max_n-1, x, y);
//cout << ans.valL << ' ' << ans.valR << '\n';
cout << ans.sol << '\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... |