#include <bits/stdc++.h>
using namespace std;
#define ar array< int , 2>
#define MASK(i) (1 << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
template<typename T1, typename T2> bool minN(T1 &a, T2 b) {if (a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxX(T1 &a, T2 b) {if (a < b) a = b; else return 0; return 1;}
const int MAX = 2e6 + 1000;
int n, k, Size, x, y;
string s;
struct Node
{
int sum = 0, minEraL = 0, minEraR = 0, ans = 0;
};
Node f[MAX], base;
Node Cal(Node a, Node b)
{
Node c;
c.sum = a.sum + b.sum;
c.minEraL = max(a.minEraL, a.sum + b.minEraL);
c.minEraR = max(b.minEraR, b.sum + a.minEraR);
c.ans = max({a.minEraL + b.minEraR, a.sum + b.ans, a.ans + b.sum});
return c;
}
void Build(int id, int l, int r)
{
if (l > r) return;
if (l == r)
{
int temp = (s[l] == 'C' ? -1 : (s[l] == 'T') ? 1 : 0);
//cout << l << " " << temp << " " << id << '\n' ;
f[id].sum = temp;
maxX(f[id].minEraL, temp);
maxX(f[id].minEraR, temp);
maxX(f[id].ans, temp);
return ;
}
int mid = (l + r) >> 1;
Build(id * 2, l, mid);
Build(id * 2 + 1, mid + 1, r);
f[id] = Cal(f[id * 2], f[id * 2 + 1]);
}
Node Get(int id, int l, int r, int ll, int rr)
{
if (ll > rr || rr < l || ll > r) return base;
if (l <= ll && rr <= r) return f[id];
int mid = (ll + rr) >> 1;
return Cal(Get(id * 2, l, r, ll, mid), Get(id * 2 + 1, l, r, mid + 1, rr));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
Size = 1;
while (Size < n) Size *= 2;
cin >> s;
s = "#" + s;
//for (int i = n; i <= Size; i++) s = s + "#";
Build(1, 1, n);
cin >> k;
for (int i = 1; i <= k; i++)
{
cin >> x >> y;
cout << Get(1, x, y, 1, n).ans << '\n' ;
}
//cout << Get(1, 4, 9, 1, n).sum << '\n' ;
}
/*
11
CCCTTTTTTCC
3
1 11
4 9
1 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
46 ms |
4804 KB |
Output is correct |
7 |
Correct |
45 ms |
4784 KB |
Output is correct |
8 |
Correct |
48 ms |
4808 KB |
Output is correct |
9 |
Correct |
44 ms |
4788 KB |
Output is correct |
10 |
Correct |
54 ms |
4680 KB |
Output is correct |
11 |
Correct |
45 ms |
4912 KB |
Output is correct |
12 |
Correct |
45 ms |
4932 KB |
Output is correct |
13 |
Correct |
58 ms |
4844 KB |
Output is correct |
14 |
Correct |
72 ms |
4876 KB |
Output is correct |
15 |
Correct |
54 ms |
4808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
46 ms |
4804 KB |
Output is correct |
7 |
Correct |
45 ms |
4784 KB |
Output is correct |
8 |
Correct |
48 ms |
4808 KB |
Output is correct |
9 |
Correct |
44 ms |
4788 KB |
Output is correct |
10 |
Correct |
54 ms |
4680 KB |
Output is correct |
11 |
Correct |
45 ms |
4912 KB |
Output is correct |
12 |
Correct |
45 ms |
4932 KB |
Output is correct |
13 |
Correct |
58 ms |
4844 KB |
Output is correct |
14 |
Correct |
72 ms |
4876 KB |
Output is correct |
15 |
Correct |
54 ms |
4808 KB |
Output is correct |
16 |
Correct |
442 ms |
20192 KB |
Output is correct |
17 |
Correct |
386 ms |
20332 KB |
Output is correct |
18 |
Correct |
443 ms |
20372 KB |
Output is correct |
19 |
Correct |
419 ms |
19852 KB |
Output is correct |
20 |
Correct |
460 ms |
19572 KB |
Output is correct |
21 |
Correct |
487 ms |
21176 KB |
Output is correct |
22 |
Correct |
519 ms |
21116 KB |
Output is correct |
23 |
Correct |
471 ms |
21340 KB |
Output is correct |
24 |
Correct |
454 ms |
20964 KB |
Output is correct |
25 |
Correct |
466 ms |
20512 KB |
Output is correct |