(File IO): input: mnotes.in output: mnotes.out
time limit: 1000 ms space constraints: 131072 KB specific restrictions
Goto ProblemSet
Title Description
ready to teach his cows playing a song, the song
species syllables, numbered
to
, and must follow from
to
is the order of play for the first
kind of sustained syllables
beats, beats from
starts counting, so beat from
to beat
play of the first
Zhong syllable, from
To
play of the first
Zhong syllable, and so on.
Cows are not interested in playing for recently, and they feel too boring. So in order to keep the focus on the cows,
proposed
questions, format questions is "first
times beats playing what kind of syllables, "
each corresponding to a problem
Please help to resolve the cows.
Input
first line of input two spaces separated by an integer
and
first
to
lines contains an integer
The first
lines contains an integer
Output
Output has
lines, each line of output corresponding to the answer to the question.
Sample input
. 3. 5
2
. 1
. 3
2
. 3
. 4
0
. 1
Sample Output
2
. 3
. 3
. 1
. 1
Data range limit
Solving ideas
bipartite + analog
Code
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int n,q,a[50010],x,l,r,mid;
bool flag;
int main(){
freopen("mnotes.in","r",stdin);
freopen("mnotes.out","w",stdout);
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]+=a[i-1];
}
for(int i=1;i<=q;i++)
{
scanf("%d",&x);
++x;
l=1;r=n;
flag=0;
while(l<r)
{
mid=(l+r)/2;
if(a[mid]>x)
r=mid;
if(a[mid]<x)
l=mid+1;
if(a[mid]==x)
{
printf("%d\n",mid);
flag=1;
break;
}
}
if(!flag)
printf("%d\n",l);
}
}