/**
* 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, 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, 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;
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;
while (Q--) {
int L, R;
cin >> L >> R;
--L;
cout << seg.query(L, R).best << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
63 ms |
4808 KB |
Output is correct |
7 |
Correct |
46 ms |
5712 KB |
Output is correct |
8 |
Correct |
46 ms |
5712 KB |
Output is correct |
9 |
Correct |
43 ms |
5712 KB |
Output is correct |
10 |
Correct |
62 ms |
5932 KB |
Output is correct |
11 |
Correct |
50 ms |
5976 KB |
Output is correct |
12 |
Correct |
48 ms |
5860 KB |
Output is correct |
13 |
Correct |
49 ms |
5944 KB |
Output is correct |
14 |
Correct |
57 ms |
6004 KB |
Output is correct |
15 |
Correct |
48 ms |
5712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
63 ms |
4808 KB |
Output is correct |
7 |
Correct |
46 ms |
5712 KB |
Output is correct |
8 |
Correct |
46 ms |
5712 KB |
Output is correct |
9 |
Correct |
43 ms |
5712 KB |
Output is correct |
10 |
Correct |
62 ms |
5932 KB |
Output is correct |
11 |
Correct |
50 ms |
5976 KB |
Output is correct |
12 |
Correct |
48 ms |
5860 KB |
Output is correct |
13 |
Correct |
49 ms |
5944 KB |
Output is correct |
14 |
Correct |
57 ms |
6004 KB |
Output is correct |
15 |
Correct |
48 ms |
5712 KB |
Output is correct |
16 |
Correct |
436 ms |
27056 KB |
Output is correct |
17 |
Correct |
395 ms |
26540 KB |
Output is correct |
18 |
Correct |
428 ms |
26820 KB |
Output is correct |
19 |
Correct |
359 ms |
26348 KB |
Output is correct |
20 |
Correct |
495 ms |
26004 KB |
Output is correct |
21 |
Correct |
442 ms |
27956 KB |
Output is correct |
22 |
Correct |
410 ms |
27880 KB |
Output is correct |
23 |
Correct |
443 ms |
28092 KB |
Output is correct |
24 |
Correct |
425 ms |
27576 KB |
Output is correct |
25 |
Correct |
442 ms |
27108 KB |
Output is correct |