#include<iostream>
#include<stdio.h>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<map>
#include<set>
#include<algorithm>
#define ll long long
#define pi pair < ll,ll >
#define mp(a,b) make_pair(a,b)
#define rep(i,a,b) for(int i = a;i < b;i++)
#define N 300004
#define INF 1e9+7
using namespace std;
ll n,q,ar[N],l,r;
char c;
bool akiro[N];
ll solve(ll i,ll j)
{
ll sum = 0;
ll ans = 0;
rep(s,i,j+1)
akiro[s] = false;
rep(s,i,j+1)
{
sum += ar[s];
if(sum < 0)
{
ans++;
akiro[s] = true;
sum++;
}
}
sum = 0;
for(int s = j;s >= i;s--)
{
sum += (!akiro[s])*ar[s];
if(sum < 0)
{
sum++;
ans++;
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> n;
rep(i,0,n)
{
cin >> c;
if(c == 'C')
ar[i] = 1;
else
ar[i] = -1;
}
cin >> q;
rep(i,0,q)
{
cin >> l >> r;
cout << solve(l-1,r-1) << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
384 KB |
Output is correct |
2 |
Correct |
14 ms |
384 KB |
Output is correct |
3 |
Correct |
7 ms |
384 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
8 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
384 KB |
Output is correct |
2 |
Correct |
14 ms |
384 KB |
Output is correct |
3 |
Correct |
7 ms |
384 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
8 ms |
384 KB |
Output is correct |
6 |
Correct |
2865 ms |
2352 KB |
Output is correct |
7 |
Execution timed out |
3035 ms |
1840 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
384 KB |
Output is correct |
2 |
Correct |
14 ms |
384 KB |
Output is correct |
3 |
Correct |
7 ms |
384 KB |
Output is correct |
4 |
Correct |
8 ms |
384 KB |
Output is correct |
5 |
Correct |
8 ms |
384 KB |
Output is correct |
6 |
Correct |
2865 ms |
2352 KB |
Output is correct |
7 |
Execution timed out |
3035 ms |
1840 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |