Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace __gnu_pbds;
- typedef tree< double, null_type, less_equal<double>, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
- const int mx=2e5+123;
- long long sum0[mx],sum1[mx];
- int main()
- {
- int q;
- cin>>q;
- for(int j=1;j<=q;j++)
- {
- string s;
- int k;
- ordered_set ps;
- cin>>s>>k;
- sum0[0]=0;
- sum1[0]=0;
- for(int i=0;i<s.size();i++)
- {
- if(s[i]=='0')
- {
- sum0[i+1]=sum0[i]+1;
- sum1[i+1]=sum1[i];
- }
- else
- {
- sum1[i+1]=sum1[i]+1;
- sum0[i+1]=sum0[i];
- }
- }
- long long ans=0;
- ps.insert(0);
- map<long long, int>cnt;
- cnt[0]++;
- for(int i=1;i<=s.size();i++)
- {
- ans+=ps.order_of_key(sum0[i]-(k*sum1[i]))+cnt[sum0[i]-(k*sum1[i])];
- cnt[sum0[i]-(k*sum1[i])]++;
- ps.insert(sum0[i]-(k*sum1[i]));
- }
- cout<<ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement