#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5.1e5;
int N, Q;
string S;
int pref[MAXN];
struct seg {
seg* ch[2];
int incr;
int maxVal;
};
seg nodes[MAXN*2];
int V;
void propagate(seg* n) {
for (int i = 0; i < 2; i++) {
n->ch[i]->maxVal += n->incr;
n->ch[i]->incr += n->incr;
}
n->incr = 0;
}
void update(seg* n) {
n->maxVal = max(n->ch[0]->maxVal, n->ch[1]->maxVal);
}
seg* build(int x = 0, int y = N+1) {
seg* n = &nodes[V++];
if (y - x == 1) {
n->maxVal = pref[x];
} else {
int z = (x + y) / 2;
n->ch[0] = build(x, z);
n->ch[1] = build(z, y);
update(n);
}
return n;
}
void update(int l, int r, int v, seg* n, int x = 0, int y = N+1) {
assert(max(l, x) < min(r, y));
if (l <= x && y <= r) {
n->maxVal += v;
n->incr += v;
} else {
propagate(n);
int z = (x + y) / 2;
if (l < z) {
update(l, r, v, n->ch[0], x, z);
}
if (z < r) {
update(l, r, v, n->ch[1], z, y);
}
update(n);
}
}
int query(int l, int r, seg* n, int x = 0, int y = N+1) {
assert(max(l, x) < min(r, y));
if (l <= x && y <= r) {
return n->maxVal;
} else {
propagate(n);
int ans = -MAXN;
int z = (x + y) / 2;
if (l < z) {
ans = max(ans, query(l, r, n->ch[0], x, z));
}
if (z < r) {
ans = max(ans, query(l, r, n->ch[1], z, y));
}
return ans;
}
}
int bit[MAXN];
void update(int i, int v) {
for (i++; i <= N+5; i += (i & -i)) {
bit[i] += v;
}
}
int query(int i) {
int ans = 0;
for (; i; i -= (i & -i)) {
ans += bit[i];
}
return ans;
}
int query(int l, int r) { // half open
return query(r) - query(l);
}
const int MAXQ = 5.1e5;
int ans[MAXQ];
vector<pair<int, int>> queries[MAXN];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N >> S;
pref[0] = 0;
for (int i = 0; i < N; i++) {
pref[i+1] = pref[i] + (S[i] == 'C' ? +1 : -1);
}
seg* root = build();
cin >> Q;
for (int q = 0; q < Q; q++) {
int l, r; cin >> l >> r; l--;
queries[l].emplace_back(r, q);
}
vector<int> st;
for (int x = N; x >= 0; x--) {
while (!st.empty() && pref[st.back()] >= pref[x]) {
int i = st.back();
update(i, -1);
update(i, N+1, -1, root);
st.pop_back();
}
for (auto q : queries[x]) {
int r = q.first;
int ind = q.second;
ans[ind] = query(x+1, r+1) + query(x, r+1, root) - query(r, r+1, root);
}
{
update(x, 1);
update(x, N+1, 1, root);
st.push_back(x);
}
}
for (int q = 0; q < Q; q++) {
cout << ans[q] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12536 KB |
Output is correct |
2 |
Correct |
15 ms |
12536 KB |
Output is correct |
3 |
Correct |
15 ms |
12536 KB |
Output is correct |
4 |
Correct |
15 ms |
12536 KB |
Output is correct |
5 |
Correct |
15 ms |
12536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12536 KB |
Output is correct |
2 |
Correct |
15 ms |
12536 KB |
Output is correct |
3 |
Correct |
15 ms |
12536 KB |
Output is correct |
4 |
Correct |
15 ms |
12536 KB |
Output is correct |
5 |
Correct |
15 ms |
12536 KB |
Output is correct |
6 |
Correct |
138 ms |
19064 KB |
Output is correct |
7 |
Correct |
128 ms |
18936 KB |
Output is correct |
8 |
Correct |
130 ms |
18808 KB |
Output is correct |
9 |
Correct |
131 ms |
19064 KB |
Output is correct |
10 |
Correct |
138 ms |
19128 KB |
Output is correct |
11 |
Correct |
135 ms |
19444 KB |
Output is correct |
12 |
Correct |
140 ms |
19352 KB |
Output is correct |
13 |
Correct |
132 ms |
19512 KB |
Output is correct |
14 |
Correct |
135 ms |
19384 KB |
Output is correct |
15 |
Correct |
138 ms |
19272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12536 KB |
Output is correct |
2 |
Correct |
15 ms |
12536 KB |
Output is correct |
3 |
Correct |
15 ms |
12536 KB |
Output is correct |
4 |
Correct |
15 ms |
12536 KB |
Output is correct |
5 |
Correct |
15 ms |
12536 KB |
Output is correct |
6 |
Correct |
138 ms |
19064 KB |
Output is correct |
7 |
Correct |
128 ms |
18936 KB |
Output is correct |
8 |
Correct |
130 ms |
18808 KB |
Output is correct |
9 |
Correct |
131 ms |
19064 KB |
Output is correct |
10 |
Correct |
138 ms |
19128 KB |
Output is correct |
11 |
Correct |
135 ms |
19444 KB |
Output is correct |
12 |
Correct |
140 ms |
19352 KB |
Output is correct |
13 |
Correct |
132 ms |
19512 KB |
Output is correct |
14 |
Correct |
135 ms |
19384 KB |
Output is correct |
15 |
Correct |
138 ms |
19272 KB |
Output is correct |
16 |
Correct |
1245 ms |
62392 KB |
Output is correct |
17 |
Correct |
1091 ms |
58580 KB |
Output is correct |
18 |
Correct |
1212 ms |
59636 KB |
Output is correct |
19 |
Correct |
1091 ms |
61268 KB |
Output is correct |
20 |
Correct |
1262 ms |
61448 KB |
Output is correct |
21 |
Correct |
1381 ms |
64056 KB |
Output is correct |
22 |
Correct |
1433 ms |
63408 KB |
Output is correct |
23 |
Correct |
1350 ms |
64424 KB |
Output is correct |
24 |
Correct |
1267 ms |
63596 KB |
Output is correct |
25 |
Correct |
1352 ms |
62788 KB |
Output is correct |