This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: NimaAryan
* created: 2023-10-25 19:58:10
**/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
struct Info {
int pref, suff, sum;
int best;
};
Info operator+(Info a, Info b) {
Info res;
res.pref = max(a.pref, a.sum + b.pref);
res.suff = max(b.suff, b.sum + a.suff);
res.sum = a.sum + b.sum;
res.best = max(a.pref + b.suff, max(a.sum + b.best, b.sum + a.best));
return res;
}
struct SegmentTree {
vector<Info> info;
int n;
SegmentTree(int n) : n(n) {
info.assign(4 << __lg(n), Info());
}
void modify(int p, int l, int r, int x, const Info& v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
info[p] = info[2 * p] + info[2 * p + 1];
}
void modify(int x, const Info& v) {
modify(1, 0, n, x, v);
}
Info query(int p, int l, int r, int lx, int rx) {
if (l >= rx || r <= lx) {
return Info();
}
if (l >= lx && r <= rx) {
return info[p];
}
int m = (l + r) / 2;
query(2 * p, l, m, lx, rx);
query(2 * p + 1, m, r, lx, rx);
return query(2 * p, l, m, lx, rx) +
query(2 * p + 1, m, r, lx, rx);
}
Info query(int lx, int rx) {
return query(1, 0, n, lx, rx);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
string S;
cin >> S;
SegmentTree seg(N);
for (int i = 0; i < N; ++i) {
if (S[i] == 'C') {
seg.modify(i, Info{0, 0, -1, 0});
} else {
seg.modify(i, Info{1, 1, +1, 1});
}
}
int Q;
cin >> Q;
vector<int> L(Q), R(Q);
for (int i = 0; i < Q; ++i) {
cin >> L[i] >> R[i];
--L[i];
}
for (int i = 0; i < Q; ++i) {
cout << seg.query(L[i], R[i]).best << "\n";
}
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... |