#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'
using namespace std;
const int maxn = 5e5 + 3;
int n, q;
string s;
void read()
{
cin >> n >> s >> q;
}
void solve()
{
while (q--)
{
int l, r;
cin >> l >> r;
int ans = 0;
int balance = 0;
stack <int> st;
for (int i = l; i <= r; i++)
{
if (s[i-1] == 'C')
{
balance++;
st.push(1);
}
else
{
if (balance)
{
balance--;
st.push(-1);
}
else
ans++;
}
}
balance = 0;
while (!st.empty())
{
if (st.top() == 1)
balance++;
else
{
if (balance)
balance--;
else
ans++;
}
st.pop();
}
cout << ans << endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
364 KB |
Output is correct |
2 |
Correct |
9 ms |
364 KB |
Output is correct |
3 |
Correct |
9 ms |
364 KB |
Output is correct |
4 |
Correct |
7 ms |
364 KB |
Output is correct |
5 |
Correct |
5 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
364 KB |
Output is correct |
2 |
Correct |
9 ms |
364 KB |
Output is correct |
3 |
Correct |
9 ms |
364 KB |
Output is correct |
4 |
Correct |
7 ms |
364 KB |
Output is correct |
5 |
Correct |
5 ms |
364 KB |
Output is correct |
6 |
Execution timed out |
3088 ms |
1252 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
364 KB |
Output is correct |
2 |
Correct |
9 ms |
364 KB |
Output is correct |
3 |
Correct |
9 ms |
364 KB |
Output is correct |
4 |
Correct |
7 ms |
364 KB |
Output is correct |
5 |
Correct |
5 ms |
364 KB |
Output is correct |
6 |
Execution timed out |
3088 ms |
1252 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |