-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTRSUB.cpp
More file actions
43 lines (40 loc) · 994 Bytes
/
Copy pathSTRSUB.cpp
File metadata and controls
43 lines (40 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int lowerBound(int key, int low, int high, ll a[]){
int ans=INT_MAX;
while(low<=high){
int mid = low+(high-low)/2;
if(a[mid]>=key){
ans=min(ans, mid);
high = mid-1;
}
else low = mid+1;
}
return ans-1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t, n, k, q, l, r;
string s;
cin>>t;
while(t--){
cin>>n>>k>>q;
cin>>s;
ll j=-1, temp[2] = {0}, valj[n+1], sum[n+1]={0};
for(int i=0;i<n;i++){
while(temp[0] <= k and temp[1] <= k and ++j<n) ++temp[s[j]-'0'];
valj[i+1] = j;
--temp[s[i]-'0'];
}
for(int i=1;i<=n;i++) sum[i] = sum[i-1]+valj[i];
while(q--){
cin>>l>>r;
int z = lowerBound(r, l, r, valj);
ll ans = (r-z)*r+((l-1)*l)/2-(r*(r+1))/2+(r-l+1)+sum[z]-sum[l-1];
cout<<ans<<"\n";
}
}
return 0;
}