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 valL = 0, valR = 0, valLC = 0, valRC = 0;
};
Node t[4*max_n];
// a > 0, a tiene t[a] platos sin pagar
Node merge(Node a, Node b) {
Node ans;
ans.valLC = max(0, a.valLC+b.valLC-b.valL-a.valL);
ans.valRC = max(0, a.valRC+b.valRC-b.valR-a.valR);
ans.valL = a.valL + max(0, b.valL-a.valLC);
ans.valR = b.valR + max(0, a.valR-b.valRC);
return ans;
}
Node updateN(Node x, int p) {
if (p > 0) x.valL = x.valR = x.valL+1;
else x.valLC = x.valRC = x.valLC+1;
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 << max(ans.valL, ans.valR) << '\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... |