#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 500005;
int n;
string s;
struct node {
int ans, l, r, s;
node(char c = 'C') {
if(c == 'C') l = r = 0, ans = -1, s = -1;
else ans = l = r = s = 1;
}
node operator + (node t) {
node res;
res.s = s + t.s;
res.l = max(l, s + t.l);
res.r = max(r + t.s, t.r);
res.ans = max({l + t.r, s + t.ans, t.s + ans});
return res;
}
} t[N * 4];
void build(int v = 1, int tl = 0, int tr = n) {
if(tr - tl == 1) {
t[v] = node(s[tl]);
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
node qry(int l, int r, int v = 1, int tl = 0, int tr = n) {
if(l <= tl && tr <= r) return t[v];
int tm = (tl + tr) / 2;
if(r <= tm) return qry(l, r, v * 2, tl, tm);
else if(l >= tm) return qry(l, r, v * 2 + 1, tm, tr);
else return qry(l, r, v * 2, tl, tm) + qry(l, r, v * 2 + 1, tm, tr);
}
signed main()
{
IO_OP;
cin >> n >> s;
build();
int q;
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
l--;
cout << max(qry(l, r).ans, 0) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
31596 KB |
Output is correct |
2 |
Correct |
21 ms |
31596 KB |
Output is correct |
3 |
Correct |
24 ms |
31596 KB |
Output is correct |
4 |
Correct |
20 ms |
31596 KB |
Output is correct |
5 |
Correct |
21 ms |
31724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
31596 KB |
Output is correct |
2 |
Correct |
21 ms |
31596 KB |
Output is correct |
3 |
Correct |
24 ms |
31596 KB |
Output is correct |
4 |
Correct |
20 ms |
31596 KB |
Output is correct |
5 |
Correct |
21 ms |
31724 KB |
Output is correct |
6 |
Correct |
68 ms |
32152 KB |
Output is correct |
7 |
Correct |
64 ms |
32108 KB |
Output is correct |
8 |
Correct |
80 ms |
32108 KB |
Output is correct |
9 |
Correct |
63 ms |
31980 KB |
Output is correct |
10 |
Correct |
71 ms |
31980 KB |
Output is correct |
11 |
Correct |
68 ms |
32236 KB |
Output is correct |
12 |
Correct |
74 ms |
32236 KB |
Output is correct |
13 |
Correct |
75 ms |
32364 KB |
Output is correct |
14 |
Correct |
75 ms |
32108 KB |
Output is correct |
15 |
Correct |
66 ms |
32108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
31596 KB |
Output is correct |
2 |
Correct |
21 ms |
31596 KB |
Output is correct |
3 |
Correct |
24 ms |
31596 KB |
Output is correct |
4 |
Correct |
20 ms |
31596 KB |
Output is correct |
5 |
Correct |
21 ms |
31724 KB |
Output is correct |
6 |
Correct |
68 ms |
32152 KB |
Output is correct |
7 |
Correct |
64 ms |
32108 KB |
Output is correct |
8 |
Correct |
80 ms |
32108 KB |
Output is correct |
9 |
Correct |
63 ms |
31980 KB |
Output is correct |
10 |
Correct |
71 ms |
31980 KB |
Output is correct |
11 |
Correct |
68 ms |
32236 KB |
Output is correct |
12 |
Correct |
74 ms |
32236 KB |
Output is correct |
13 |
Correct |
75 ms |
32364 KB |
Output is correct |
14 |
Correct |
75 ms |
32108 KB |
Output is correct |
15 |
Correct |
66 ms |
32108 KB |
Output is correct |
16 |
Correct |
496 ms |
34572 KB |
Output is correct |
17 |
Correct |
455 ms |
34304 KB |
Output is correct |
18 |
Correct |
499 ms |
34432 KB |
Output is correct |
19 |
Correct |
381 ms |
34000 KB |
Output is correct |
20 |
Correct |
507 ms |
33408 KB |
Output is correct |
21 |
Correct |
513 ms |
35200 KB |
Output is correct |
22 |
Correct |
486 ms |
35200 KB |
Output is correct |
23 |
Correct |
575 ms |
35328 KB |
Output is correct |
24 |
Correct |
532 ms |
34908 KB |
Output is correct |
25 |
Correct |
519 ms |
34560 KB |
Output is correct |