(Jizhong) 1592. [Training] GDKOI musical beats (mnotes) [] + analog-half

(File IO): input: mnotes.in output: mnotes.out
time limit: 1000 ms space constraints: 131072 KB specific restrictions
Goto ProblemSet


Title Description
F J FJ ready to teach his cows playing a song, the song N ( 1 < = N < = 50 , 000 ) N(1<=N<=50,000) species syllables, numbered 1 1 to N N , and must follow from 1 1 to N N is the order of play for the first i i kind of sustained syllables B i ( 1 < = B i < = 10 , 000 ) B_i(1<=B_i<=10,000) beats, beats from 0 0 starts counting, so beat from 0 0 to beat B 1 1 B_1-1 play of the first 1 1 Zhong syllable, from B 1 B_1 To B 1 + B 2 1 B_1+B_2-1 play of the first 2 2 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, F J FJ proposed Q ( 1 < = Q < = 50 , 000 ) Q(1<=Q<=50,000) questions, format questions is "first T T times beats playing what kind of syllables, "
each corresponding to a problem T i ( 0 < = T i < = 1 ) T_i (0 <= T_i <= total number of beats -1) Please help to resolve the cows.


Input
first line of input two spaces separated by an integer N N and Q Q
first 2 2 to N + 1 N+1 lines contains an integer B i B_i
The first N + 2 N + Q + 1 N+2-N+Q+1 lines contains an integer T i T_i

Output
Output has Q Q 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);
    }
}
Published 119 original articles · won praise 8 · views 4911

Guess you like

Origin blog.csdn.net/kejin2019/article/details/104976106