Codeforces Hello2015 first question cursed Query

Source: Internet
Author: User

English problem face:

De Prezer loves movies and series. He had watched theTroy for like the Times and also he was a big fan of theSupernatural series. So, he does some researches and found a cursed object which hadn lights on it and initially all of them were Turne D off. Because of theTroy, he called that object Troy.

He looked and saw a note on the IT in khikulish (language of people of Khikuland): "Ma in Hame rah Umadim, h Ala Migi ...? ... Mage man De prezer am k mikhay mano ...? .... Man SE sale ... To Boro .... Beshur manam miram ... o mishuram ".

He doesn ' t know khikulish, so just ignored the note and tested theTroy.

he realized, the light number i stays Turned on for exactly a i seconds, and Then it turns itself off (if it was turned on, in Time t , in time t ? +? a i ?-? 1 It'll be turned on, but on Time t ? +? A i it won ' t is) and the next light would be turned on (If i ? <? n , next light number i ? +?1 , otherwise it's light with Number 1 ).

For example if n-=?2 and we turn on the first light in time0, it'll be is turned on hole inte Rval [0,? A1) and in hole interval [a1,? A1?+? A2) The second light is turned on and so on.

In time 0 He turns on the light number1.

De Prezer also loves query. So he gives you Q queries. In each query he'll give you an integer T (the time a revengeful ghost attacked him) and you should print the numb Er of the light which is turned on, in timet.

Input

The first line of input contains the integers n and Q.

The second line contains n space separated integers,a1,? A2,?...,? An .

The next Q lines, each line contains a single integert .

1?≤? n,? q. ≤?105

1?≤? a i? ≤?109

1?≤? t. ≤?10

Output

For each query, print the answer on one line.

That is, there are n lights, and then give each lamp on the time of the light (the first light from the moment 0 light up until the moment 0+a1-1, the second in the A1 moment, and so on), and then the question to ask the Q Group, ask the content is a T, you give the T moment when the light of the lamp. My practice is that every time I ask for a two-point search, the complexity of log2n 10^5 times to ask to accept.

Sample Test (s):

Input
5 71 2 3 4 51237141516

Output
2234512

This set of test data is still the industry conscience, burst out of the hole may be wrong, this string of lights is the cycle of the end-to-end light, so that T is 10^18 so large, to take the mold.

Code:

#include <iostream> #include <stdio.h>using namespace std;long long int start[100005];//Each lamp starts to light at a time long long    int end[100005];//each lamp on the finish time int n,q,a;long long int t;int work (long long int ask)//binary lookup function {int p=1;    int p1=1;    int p2=n; while (ask<start[p]| |        Ask>end[p]) {p= (P1+P2)/2;        if (Ask>=start[p]&&ask<=end[p]) {break;            }else if (Ask>=start[p1]&&ask<=end[p1]) {p=p1;        Break            }else if (Ask>=start[p2]&&ask<=end[p2]) {p=p2;        Break        }else if (Ask<start[p]&&ask>end[p1]) {p2=p;        }else if (Ask>end[p]&&ask<start[p2]) {p1=p;    }} return p;//p is the lamp}int main () {scanf ("%d%d", &n,&q);    start[1]=0;        for (int i=1;i<=n;i++) {scanf ("%d", &a);        End[i]=start[i]+a-1;    Start[i+1]=start[i]+a;  } while (q--) {      scanf ("%i64d", &t);    printf ("%d\n", Work (t%start[n+1])); } return 0;}


Codeforces Hello2015 first question cursed Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.