#include "bits/stdc++.h"
#define endl '\n'
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
int Q;
cin >> Q;
while(Q--) {
int l, r;
cin >> l >> r;
--l;
const int len = r - l;
string tmp = s.substr(l, len);
int ans = 0, balance = 0;
for(int i = 0; i < len; ++i) {
assert(balance >= 0);
if(tmp[i] == 'C') {
balance++;
continue;
}
assert(tmp[i] == 'T');
if(balance == 0) {
tmp[i] = 'X';
++ans;
}
else {
balance--;
}
}
reverse(tmp.begin(), tmp.end());
balance = 0;
for(int i = 0; i < len; ++i) {
assert(balance >= 0);
if(tmp[i] == 'C') {
balance++;
continue;
}
else if(tmp[i] == 'X') {
continue;
}
assert(tmp[i] == 'T');
if(balance == 0) {
tmp[i] = 'X';
++ans;
}
else {
balance--;
}
}
cout << ans << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// signed t; cin >> t; while(t--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Execution timed out |
3073 ms |
1108 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
348 KB |
Output is correct |
2 |
Correct |
4 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Execution timed out |
3073 ms |
1108 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |