#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define INF INT_MAX
#define MOD 1000000007
#define all(x) x.begin(), x.end()
struct nda{
int sum, cost;
};
nda merge(nda a, nda b){
if(a.sum == -1) return b;
if(b.sum == -1) return a;
int k = min(a.cost, b.sum);
a.cost -= k, b.sum -= k;
return {a.sum+b.sum, a.cost+b.cost};
}
class SegTree{
vector<nda> st; int l, r;
void Update(int node, int l, int r, int L, int R, nda val){
if(r < L or R < l) return;
if(L <= l and r <= R){
st[node] = val;
return;
}
int m = (l+r)>>1;
Update(node*2, l, m, L, R, val);
Update(node*2+1, m+1, r, L, R, val);
st[node] = merge(st[node*2], st[node*2+1]);
return;
}
nda Query(int node, int l, int r, int L, int R){
if(r < L or R < l) return {-1,0};
if(L <= l and r <= R) return st[node];
int m = (l+r)>>1;
auto v1 = Query(node*2, l, m, L, R);
auto v2 = Query(node*2+1, m+1, r, L, R);
return merge(v1, v2);
}
public:
SegTree(int l, int r){
int sz = (r-l+1); st.resize(4*sz, {0,0});
this->l = l, this->r = r;
}
void Update(int L, int R, nda val){
Update(1, l, r, L, R, val);
return;
}
int Query(int L, int R){
return Query(1, l, r, L, R).cost;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
string s; cin >> s;
int q; cin >> q;
vector<vvi> qry(n);
for(int x = 0; x < q; x++){
int l, r; cin >> l >> r;
qry[l-1].push_back({r-1, x});
}
vi ans(q);
SegTree ST(0, n-1);
deque<int> tloc;
for(int x = n-1; x >= 0; x--){
if(s[x] == 'C'){
ST.Update(x,x,{1,0});
if(!tloc.empty()){
int prev = tloc.front(); tloc.pop_front();
ST.Update(prev, prev, {0,1});
}
}else tloc.push_front(x);
for(auto qq : qry[x]){
ans[qq[1]] = upper_bound(all(tloc), qq[0]) - tloc.begin() + ST.Query(x, qq[0]);
}
}
for(int x= 0; x < q; x++)
cout << ans[x] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
640 KB |
Output is correct |
2 |
Correct |
6 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
640 KB |
Output is correct |
2 |
Correct |
6 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
243 ms |
10232 KB |
Output is correct |
7 |
Correct |
226 ms |
10360 KB |
Output is correct |
8 |
Correct |
242 ms |
10164 KB |
Output is correct |
9 |
Correct |
251 ms |
10104 KB |
Output is correct |
10 |
Correct |
247 ms |
10104 KB |
Output is correct |
11 |
Correct |
250 ms |
10400 KB |
Output is correct |
12 |
Correct |
245 ms |
10232 KB |
Output is correct |
13 |
Correct |
246 ms |
10360 KB |
Output is correct |
14 |
Correct |
242 ms |
10360 KB |
Output is correct |
15 |
Correct |
246 ms |
10360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
640 KB |
Output is correct |
2 |
Correct |
6 ms |
640 KB |
Output is correct |
3 |
Correct |
7 ms |
640 KB |
Output is correct |
4 |
Correct |
7 ms |
640 KB |
Output is correct |
5 |
Correct |
7 ms |
640 KB |
Output is correct |
6 |
Correct |
243 ms |
10232 KB |
Output is correct |
7 |
Correct |
226 ms |
10360 KB |
Output is correct |
8 |
Correct |
242 ms |
10164 KB |
Output is correct |
9 |
Correct |
251 ms |
10104 KB |
Output is correct |
10 |
Correct |
247 ms |
10104 KB |
Output is correct |
11 |
Correct |
250 ms |
10400 KB |
Output is correct |
12 |
Correct |
245 ms |
10232 KB |
Output is correct |
13 |
Correct |
246 ms |
10360 KB |
Output is correct |
14 |
Correct |
242 ms |
10360 KB |
Output is correct |
15 |
Correct |
246 ms |
10360 KB |
Output is correct |
16 |
Correct |
1976 ms |
71704 KB |
Output is correct |
17 |
Correct |
1925 ms |
72968 KB |
Output is correct |
18 |
Correct |
1922 ms |
70920 KB |
Output is correct |
19 |
Correct |
1850 ms |
71436 KB |
Output is correct |
20 |
Correct |
1985 ms |
70956 KB |
Output is correct |
21 |
Correct |
2019 ms |
73228 KB |
Output is correct |
22 |
Correct |
1991 ms |
72796 KB |
Output is correct |
23 |
Correct |
2016 ms |
73480 KB |
Output is correct |
24 |
Correct |
1979 ms |
72716 KB |
Output is correct |
25 |
Correct |
1960 ms |
72064 KB |
Output is correct |