#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n , a[N] , q;
struct SEG{
struct NODE{
int mnp , mns , sum , ans;
NODE()
{
mnp = mns = sum = ans = 0;
}
};
NODE t[N << 2];
NODE Merge(NODE lc , NODE rc)
{
NODE res;
res.mnp = min(lc.mnp , lc.sum + rc.mnp);
res.mns = min(rc.mns , rc.sum + lc.mns);
res.sum = lc.sum + rc.sum;
res.ans = max({-lc.mnp - rc.mns , lc.ans - rc.sum , rc.ans - lc.sum});
return res;
}
void Build(int node = 1 , int nl = 0 , int nr = n)
{
if(nr - nl == 1)
{
t[node].sum = a[nl];
t[node].mnp = t[node].mns = min(a[nl] , 0);
t[node].ans = -t[node].mns;
return;
}
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
Build(lc , nl , mid); Build(rc , mid , nr);
t[node] = Merge(t[lc] , t[rc]);
//cout << nl << " " << nr << " " << t[node].ans << endl;
}
NODE Get(int l , int r , int node = 1 , int nl = 0 , int nr = n)
{
if(nr <= l || r <= nl)
{
NODE emp;
return emp;
}
if(l <= nl && nr <= r)
return t[node];
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
return Merge(Get(l , r , lc , nl , mid) , Get(l , r , rc , mid , nr));
}
} seg;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0 ; i < n ; i++)
{
char c; cin >> c;
if(c == 'C')
a[i] = 1;
else
a[i] = -1;
}
seg.Build();
cin >> q;
for(int i = 0 ; i < q ; i++)
{
int l , r; cin >> l >> r; l--;
cout << seg.Get(l , r).ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31576 KB |
Output is correct |
2 |
Correct |
6 ms |
31576 KB |
Output is correct |
3 |
Correct |
5 ms |
31580 KB |
Output is correct |
4 |
Correct |
6 ms |
31836 KB |
Output is correct |
5 |
Correct |
5 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31576 KB |
Output is correct |
2 |
Correct |
6 ms |
31576 KB |
Output is correct |
3 |
Correct |
5 ms |
31580 KB |
Output is correct |
4 |
Correct |
6 ms |
31836 KB |
Output is correct |
5 |
Correct |
5 ms |
31580 KB |
Output is correct |
6 |
Correct |
36 ms |
33140 KB |
Output is correct |
7 |
Correct |
33 ms |
33108 KB |
Output is correct |
8 |
Correct |
33 ms |
33104 KB |
Output is correct |
9 |
Correct |
33 ms |
32988 KB |
Output is correct |
10 |
Correct |
35 ms |
33116 KB |
Output is correct |
11 |
Correct |
35 ms |
33112 KB |
Output is correct |
12 |
Correct |
35 ms |
33108 KB |
Output is correct |
13 |
Correct |
36 ms |
33108 KB |
Output is correct |
14 |
Correct |
34 ms |
33112 KB |
Output is correct |
15 |
Correct |
35 ms |
33116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31576 KB |
Output is correct |
2 |
Correct |
6 ms |
31576 KB |
Output is correct |
3 |
Correct |
5 ms |
31580 KB |
Output is correct |
4 |
Correct |
6 ms |
31836 KB |
Output is correct |
5 |
Correct |
5 ms |
31580 KB |
Output is correct |
6 |
Correct |
36 ms |
33140 KB |
Output is correct |
7 |
Correct |
33 ms |
33108 KB |
Output is correct |
8 |
Correct |
33 ms |
33104 KB |
Output is correct |
9 |
Correct |
33 ms |
32988 KB |
Output is correct |
10 |
Correct |
35 ms |
33116 KB |
Output is correct |
11 |
Correct |
35 ms |
33112 KB |
Output is correct |
12 |
Correct |
35 ms |
33108 KB |
Output is correct |
13 |
Correct |
36 ms |
33108 KB |
Output is correct |
14 |
Correct |
34 ms |
33112 KB |
Output is correct |
15 |
Correct |
35 ms |
33116 KB |
Output is correct |
16 |
Correct |
254 ms |
43348 KB |
Output is correct |
17 |
Correct |
249 ms |
42836 KB |
Output is correct |
18 |
Correct |
246 ms |
43132 KB |
Output is correct |
19 |
Correct |
223 ms |
42580 KB |
Output is correct |
20 |
Correct |
237 ms |
42276 KB |
Output is correct |
21 |
Correct |
253 ms |
44096 KB |
Output is correct |
22 |
Correct |
241 ms |
43984 KB |
Output is correct |
23 |
Correct |
244 ms |
44372 KB |
Output is correct |
24 |
Correct |
234 ms |
43860 KB |
Output is correct |
25 |
Correct |
245 ms |
43496 KB |
Output is correct |