#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 5e5 + 5;
int N, Q;
string s;
struct segtree {
int t[4 * MX];
void update(int v, int l, int r, int pos, int val) {
if(r < pos || l > pos) return;
if(l == r) {
t[v] = val;
return;
}
int mid = (l + r) >> 1;
update(v * 2 + 0, l, mid + 0, pos, val);
update(v * 2 + 1, mid + 1, r, pos, val);
t[v] = min(t[v * 2 + 0], t[v * 2 + 1]);
}
int query(int v, int l, int r, int ql, int qr) {
if(l > qr || r < ql) return 1e9;
if(ql <= l && qr >= r) return t[v];
int mid = (l + r) >> 1;
return min(query(v * 2 + 0, l, mid + 0, ql, qr),
query(v * 2 + 1, mid + 1, r, ql, qr));
}
} st, tt;
int sf[MX], pf[MX];
vector<int> pos;
int getLeft(int l, int k) {
if(pos.empty() || !k) return l;
auto cnt = lower_bound(pos.begin(), pos.end(), l) - pos.begin();
return pos[cnt + k - 1];
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> N >> s >> Q;
s = '#' + s;
for(int i = 0; i < 4 * MX; i++) {
st.t[i] = tt.t[i] = 1e9;
}
st.update(1, 0, N + 1, 0, 0);
tt.update(1, 0, N + 1, 0, 0);
st.update(1, 0, N + 1, N + 1, 0);
tt.update(1, 0, N + 1, N + 1, 0);
for(int i = 1; i <= N; i++) {
pf[i] += (s[i] == 'C' ? 1 : -1) + pf[i - 1];
st.update(1, 0, N + 1, i, pf[i]);
}
for(int i = N; i >= 1; i--) {
sf[i] += (s[i] == 'C' ? 1 : -1) + sf[i + 1];
tt.update(1, 0, N + 1, i, sf[i]);
}
for(int i = 1; i <= N; i++) {
if(s[i] == 'T') pos.push_back(i);
}
while(Q--) {
int l, r;
cin >> l >> r;
int lf = pf[l - 1] - st.query(1, 0, N + 1, l - 1, r); // count dari kiri
// cout << lf << '\n';
// cout << getLeft(l, lf) << '\n';
int rg = sf[r + 1] - tt.query(1, 0, N + 1, getLeft(l, lf) + 1, r + 1); // count dari kanan
cout << lf + rg << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
15956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |