#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 10;
struct node{
int compT;
int lefT;
int rigT;
int remT;
int frelefC;
int frerigC;
void merge(node oth){
int tmp = min(frerigC, oth.lefT);
compT += tmp;
frerigC -= tmp;
oth.lefT -= tmp;
tmp = min(rigT, oth.frelefC);
compT += tmp;
rigT -= tmp;
oth.frelefC -= tmp;
tmp = min(remT, oth.frelefC);
lefT += tmp;
remT -= tmp;
oth.frelefC -= tmp;
tmp = min(frerigC, oth.remT);
rigT += tmp;
frerigC -= tmp;
oth.remT -= tmp;
tmp = min(rigT, oth.lefT);
compT += tmp;
rigT -= tmp;
oth.lefT -= tmp;
remT += tmp;
compT += oth.compT;
lefT += oth.lefT;
rigT += oth.rigT;
remT += oth.remT;
frelefC += oth.frelefC;
frerigC += oth.frerigC;
}
void debug(){
cout << "complete T : " << compT << endl;
cout << "left C need for T : " << lefT << endl;
cout << "right C need for T : " << rigT << endl;
cout << "empty T : " << remT << endl;
cout << "Free left C's " << frelefC << endl;
cout << "Free Right C's " << frerigC << endl;
}
};
node seg[4 * maxn];
string s;
node get(int id, int L, int R, int l, int r){
if (L == l and R == r)
return seg[id];
int mid = (L + R) >> 1;
if (mid >= r)
return get(2 * id + 0, L, mid, l, r);
if (mid <= l)
return get(2 * id + 1, mid, R, l, r);
node me = get(2 * id + 0, L, mid, l, mid);
me.merge(get(2 * id + 1, mid, R, mid, r));
return me;
}
void build(int id, int L, int R){
if (L + 1 == R){
if (s[L] == 'T')
seg[id].remT ++;
else
seg[id].frelefC = seg[id].frerigC = 1;
return;
}
int mid = (L + R) >> 1;
build(2 * id + 0, L, mid);
build(2 * id + 1, mid, R);
seg[id] = seg[2 * id + 0];
seg[id].merge(seg[2 * id + 1]);
// cout << "################printing : " << L << " " << R << endl;
// seg[id].debug();
}
int par[maxn];
int main(){
ios_base::sync_with_stdio(false);
int n;
cin >> n;
cin >> s;
build(1, 0, n);
for (int i = 0; i < n; i++)
par[i] = (s[i] == 'C') + (i > 0 ? par[i - 1] : 0);
int m;
cin >> m;
for (int i = 0; i < m; i++){
int l, r;
cin >> l >> r;
l --, r --;
int rem = par[r] - (l > 0 ? par[l - 1] : 0) + get(1, 0, n, l, r + 1).compT;
cout << (r - l + 1) - rem << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
448 KB |
Output is correct |
5 |
Correct |
7 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
448 KB |
Output is correct |
5 |
Correct |
7 ms |
512 KB |
Output is correct |
6 |
Correct |
241 ms |
8284 KB |
Output is correct |
7 |
Correct |
189 ms |
8224 KB |
Output is correct |
8 |
Correct |
199 ms |
8056 KB |
Output is correct |
9 |
Correct |
173 ms |
8056 KB |
Output is correct |
10 |
Correct |
200 ms |
8056 KB |
Output is correct |
11 |
Correct |
212 ms |
8300 KB |
Output is correct |
12 |
Correct |
208 ms |
8260 KB |
Output is correct |
13 |
Correct |
204 ms |
8380 KB |
Output is correct |
14 |
Correct |
198 ms |
8312 KB |
Output is correct |
15 |
Correct |
187 ms |
8356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
448 KB |
Output is correct |
5 |
Correct |
7 ms |
512 KB |
Output is correct |
6 |
Correct |
241 ms |
8284 KB |
Output is correct |
7 |
Correct |
189 ms |
8224 KB |
Output is correct |
8 |
Correct |
199 ms |
8056 KB |
Output is correct |
9 |
Correct |
173 ms |
8056 KB |
Output is correct |
10 |
Correct |
200 ms |
8056 KB |
Output is correct |
11 |
Correct |
212 ms |
8300 KB |
Output is correct |
12 |
Correct |
208 ms |
8260 KB |
Output is correct |
13 |
Correct |
204 ms |
8380 KB |
Output is correct |
14 |
Correct |
198 ms |
8312 KB |
Output is correct |
15 |
Correct |
187 ms |
8356 KB |
Output is correct |
16 |
Correct |
1796 ms |
37224 KB |
Output is correct |
17 |
Correct |
1564 ms |
36860 KB |
Output is correct |
18 |
Correct |
1608 ms |
37260 KB |
Output is correct |
19 |
Correct |
1362 ms |
36876 KB |
Output is correct |
20 |
Correct |
1568 ms |
36364 KB |
Output is correct |
21 |
Correct |
1753 ms |
38260 KB |
Output is correct |
22 |
Correct |
1838 ms |
38108 KB |
Output is correct |
23 |
Correct |
1782 ms |
38260 KB |
Output is correct |
24 |
Correct |
1733 ms |
38028 KB |
Output is correct |
25 |
Correct |
1950 ms |
37552 KB |
Output is correct |