#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
string s;
cin >> s;
while (Q--) {
int L, R;
cin >> L >> R;
L--; R--;
int balance = 0;
int removeForward = 0;
for (int i = L; i <= R; i++) {
if (s[i] == 'C') balance++;
else balance--;
if (balance < 0) {
removeForward++;
balance++;
}
}
balance = 0;
int removeBackward = 0;
for (int i = R; i >= L; i--) {
if (s[i] == 'C') balance++;
else balance--;
if (balance < 0) {
removeBackward++;
balance++;
}
}
cout << max(removeForward, removeBackward) << "\n";
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |