D. Viktoria and resentments
CF 2016-2017 BSUIR Contest
http://codeforces.com/gym/102134/problem/D
D. Viktoria and resentments
time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output
Viktoria is so easily offended. She was also offended by Alex during their walk. It is known that Alex and Viktoria were walking n minutes. And every minute Viktoria was either offended or not offended by Alex. Since we are all programmers, let's denote these n minutes of Viktoria's state as a string of n bits. So, the i-th bit of the string is 1 if she was offended in the i-th minute, otherwise this bit is 0.
Alex considers that Viktoria takes offense at him too often. And he likes to prove everything. That's why Alex wants to count the number of time periods where her offence was strictly greater than X. A pair of numbers (l, r), where 1 ≤ l ≤ r ≤ n, is considered to be a time period. Viktoria's offense during some period is a ratio of the amount of minutes she took offense to the total amount of minutes in a period. More formally, Viktoria's offense during the period (l, r) is , where cnt1 is an amount of ones in the segment (l, r) of the bit string, and cntall is the total amount of characters in the bit string from the l-th position to the r-th position.
Help Alex to prove to Viktoria that she takes offense too often!
Input
The first line contains the description of X in the form of two space-separated integers: P and Q. (0 ≤ P ≤ 1000, 1 ≤ Q ≤ 1000, P ≤ Q), i. e. X is equal to .
The next line contains a string s. It consists of the characters '0' and '1'. The length of the string is positive and doesn't exceed 200 000.
Output
Output the number of segments (l, r), where Viktoria's offense is strictly greater than X.
Examples
input
Copy
3 10 10110
output
Copy
13
input
Copy
9 10 010100101101
output
Copy
7
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10;
char s[maxn];
int a[maxn],b[maxn],c[maxn*2];
int tree[maxn*4],sum[maxn];
void update(int k,int l,int r,int rt)
{
if(l==r)
{
tree[rt]++;
return;
}
int mid=(l+r)/2;
if(k<=mid) update(k,l,mid,2*rt);
else update(k,mid+1,r,2*rt+1);
tree[rt]=tree[rt*2]+tree[2*rt+1];
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
return tree[rt];
}
int left=0,right=0;
int mid=(l+r)/2;
if(L<=mid) left+=query(L,R,l,mid,2*rt);
if(R>mid) right+=query(L,R,mid+1,r,2*rt+1);
return left+right;
}
int main()
{
int P,Q,tot=0;
scanf("%d%d%s",&P,&Q,s+1);
int len=strlen(s+1);
for(int i=1;i<=len;i++)
{
if(s[i]=='1') sum[i]=sum[i-1]+1;
else sum[i]=sum[i-1];
}
for(int i=1;i<=len;i++)
{
//c[++tot]=a[i]=Q*sum[i-1]-P*i+P;
//c[++tot]=b[i]=Q*sum[i]-P*i;
c[++tot]=a[i]=P*i-P-Q*sum[i-1]; //通过sum[r]-sum[l-1]/r-l+1 > P/Q推出的
c[++tot]=b[i]=P*i-Q*sum[i];
}
//离散化
sort(c+1,c+tot+1);
tot=unique(c+1,c+tot+1)-c-1;
ll ans=0;
for(int i=1;i<=len;i++)
{
a[i]=lower_bound(c+1,c+tot+1,a[i])-c;
b[i]=lower_bound(c+1,c+tot+1,b[i])-c;
}
for(int i=len;i>=1;i--)
{
update(b[i],1,tot,1); //倒着插入b[i] ,这样从后往前推,可以直接用之前插入的
//if(a[i]<tot) ans+=query(a[i]+1,tot,1,tot,1);
if(a[i]>1) ans+=query(1,a[i]-1,1,tot,1); //因为a[i]>b[i],所以要查1-(a[i]-1)中有多少b[i]
}
printf("%lld\n",ans);
return 0;
}