#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int, int >
#define fr first
#define sc second
#define mk make_pair
#define pb push_back
const int N = (int)5e5 + 7;
const int inf = (int)1e9 + 7;
int n;
string s;
vector < pii > query[N];
vector < int > stk;
int b[N];
struct TT {
int suf, sum;
TT() {}
};
struct T {
TT tree[N * 4];
void upd(int pos, int val, int v = 1, int l = 1, int r = n) {
if (l == r) {
tree[v].sum = val;
tree[v].suf = min(val, 0);
return ;
}
int mid = (l + r) >> 1;
if (pos <= mid) {
upd(pos, val, v + v, l, mid);
} else {
upd(pos, val, v + v + 1, mid + 1, r);
}
tree[v].sum = tree[v + v].sum + tree[v + v + 1].sum;
tree[v].suf = min(tree[v + v].suf + tree[v + v + 1].sum, tree[v + v + 1].suf);
}
pii get(int l, int r, int v = 1, int tl = 1, int tr = n) {
if (tl > r || tr < l) return mk(0, 0);
if (l <= tl && tr <= r) {
return mk(tree[v].sum, tree[v].suf);
}
int mid = (tl + tr) >> 1;
pii res1, res2;
res1 = get(l, r, v + v, tl, mid);
res2 = get(l, r, v + v + 1, mid + 1, tr);
return mk(res1.fr + res2.fr, min(res1.sc + res2.fr, res2.sc));
}
};
T tr;
void precalc() {
for (int i = 1; i <= n; i++) {
b[i] = ((s[i] == 'C') ? 1 : -1);
tr.upd(i, 1);
}
}
int ans[N];
main() {
scanf("%d", &n);
cin >> s;
s = " " + s;
precalc();
int q;
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
int l, r;
scanf("%d %d", &l, &r);
query[l].pb(mk(r, i));
}
for (int i = n; i >= 1; i--) {
if (b[i] == -1) {
stk.pb(i);
tr.upd(i, 0);
b[i] = 0;
} else if (b[i] == 1) {
if (!stk.empty()) {
tr.upd(stk.back(), -1);
b[stk.back()] = -1;
stk.pop_back();
}
}
for (pii to : query[i]) {
// cout << upper_bound(stk.rbegin(), stk.rend(), to.fr) - stk.rbegin() << ' ' << tr.get(1, to.fr).sc << endl;
ans[to.sc] = upper_bound(stk.rbegin(), stk.rend(), to.fr) - stk.rbegin() - tr.get(1, to.fr).sc;
}
}
for (int i = 1; i <= q; i++) {
printf("%d\n", ans[i]);
}
}
Compilation message
election.cpp:66:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
election.cpp: In function 'int main()':
election.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
election.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
election.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &l, &r);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12152 KB |
Output is correct |
2 |
Correct |
17 ms |
12388 KB |
Output is correct |
3 |
Correct |
18 ms |
12388 KB |
Output is correct |
4 |
Correct |
17 ms |
12420 KB |
Output is correct |
5 |
Correct |
17 ms |
12420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12152 KB |
Output is correct |
2 |
Correct |
17 ms |
12388 KB |
Output is correct |
3 |
Correct |
18 ms |
12388 KB |
Output is correct |
4 |
Correct |
17 ms |
12420 KB |
Output is correct |
5 |
Correct |
17 ms |
12420 KB |
Output is correct |
6 |
Correct |
100 ms |
16784 KB |
Output is correct |
7 |
Correct |
89 ms |
16784 KB |
Output is correct |
8 |
Correct |
128 ms |
16784 KB |
Output is correct |
9 |
Correct |
110 ms |
16784 KB |
Output is correct |
10 |
Correct |
95 ms |
16784 KB |
Output is correct |
11 |
Correct |
124 ms |
16960 KB |
Output is correct |
12 |
Correct |
123 ms |
16960 KB |
Output is correct |
13 |
Correct |
142 ms |
17192 KB |
Output is correct |
14 |
Correct |
106 ms |
17192 KB |
Output is correct |
15 |
Correct |
114 ms |
17192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12152 KB |
Output is correct |
2 |
Correct |
17 ms |
12388 KB |
Output is correct |
3 |
Correct |
18 ms |
12388 KB |
Output is correct |
4 |
Correct |
17 ms |
12420 KB |
Output is correct |
5 |
Correct |
17 ms |
12420 KB |
Output is correct |
6 |
Correct |
100 ms |
16784 KB |
Output is correct |
7 |
Correct |
89 ms |
16784 KB |
Output is correct |
8 |
Correct |
128 ms |
16784 KB |
Output is correct |
9 |
Correct |
110 ms |
16784 KB |
Output is correct |
10 |
Correct |
95 ms |
16784 KB |
Output is correct |
11 |
Correct |
124 ms |
16960 KB |
Output is correct |
12 |
Correct |
123 ms |
16960 KB |
Output is correct |
13 |
Correct |
142 ms |
17192 KB |
Output is correct |
14 |
Correct |
106 ms |
17192 KB |
Output is correct |
15 |
Correct |
114 ms |
17192 KB |
Output is correct |
16 |
Correct |
926 ms |
37576 KB |
Output is correct |
17 |
Correct |
781 ms |
37576 KB |
Output is correct |
18 |
Correct |
826 ms |
37576 KB |
Output is correct |
19 |
Correct |
831 ms |
37576 KB |
Output is correct |
20 |
Correct |
898 ms |
37576 KB |
Output is correct |
21 |
Correct |
877 ms |
39216 KB |
Output is correct |
22 |
Correct |
980 ms |
39216 KB |
Output is correct |
23 |
Correct |
847 ms |
40176 KB |
Output is correct |
24 |
Correct |
913 ms |
40176 KB |
Output is correct |
25 |
Correct |
819 ms |
40176 KB |
Output is correct |