Eugeny and Play List

Source: Internet
Author: User
Eugeny and Play Listtime limit per test

2 seconds

Memory limit per test

256 megabytes

Input

Standard input

Output

Standard output

Eugeny loves listening to music. He hasNSongs in his play list. We know that song numberIHas
The durationTIMinutes.
Eugeny listens to each song, perhaps more than once. He listens to song numberI CITimes.
Eugeny's play list is organized as follows: first song number 1 playsC1 times,
Then song number 2 playsC2 times ,...,
In the end the song numberNPlaysCNTimes.

Eugeny took a piece of paper and wrote outMMoments of time when he liked a song. Now for each such moment he wants to know the number of the song that
Played at that moment. The momentXMeans that Eugeny wants to know which song was playing duringX-Th
Minute of his listening to the play list.

Help Eugeny and calculate the required numbers of songs.

Input

The first line contains two integersN,M(1 digit ≤ DigitN, Bytes,MLimit ≤ limit 105 ).
The nextNLines contain pairs of integers.I-Th
Line contains integersCI, Bytes,TI(1 digit ≤ DigitCI, Bytes,TILimit ≤ limit 109 )-
The description of the play list. It is guaranteed that the play list's total duration doesn't exceed 109.

The next line containsMPositive integersV1, bytes,V2, middle..., middle ,...,VM,
That describe the moments Eugeny has written out. It is guaranteed that there isn't such moment of timeVI,
When the music doesn' t play any longer. It is guaranteed thatVILatency <latencyVILimit + limit 1 (ILatency <latencyM).

The moment of timeVIMeans
That Eugeny wants to know which song was playing duringVI-Th
Munite from the start of listening to the playlist.

Output

PrintMIntegers-I-Th number must equal
Number of the song that was playing duringVI-Th
Minute after Eugeny started listening to the play list.

Sample test (s) input
1 22 81 16
Output
11
Input
4 91 22 11 12 21 2 3 4 5 6 7 8 9
Output
112234444

Q: There is a CD played in sequence, each playing C times, each playing T minutes, give a time point, find the playing CD number at this time

Idea: Because the width of the CD playback time reaches 10 ^ 9, the array cannot be saved directly, but the total time width is only 10 ^ 9 at the same time, in addition, there are only 10 ^ 5 CD records in total, so long as the corresponding time point is recorded, that is, there are only 10 ^ 5 + 1 records at most. If you directly query, O (N) but there are m = 10 ^ 5 groups of query arrays, TLE, so binary query is enough, log (n) seckilling, because it is a monotonic and orderly rising moment.

AC Program:

# Include <algorithm> # include <iostream> # include <iomanip> # include <cstdio> # include <cmath> # include <cstdlib> # include <cstring> typedef long ll; # defineclr (a) memset (a), 0, sizeof (a) # definerep (I, a, B) for (int I = (); I <(int) (B); I ++) # defineper (I, a, B) for (int I = (a)-1 ); I >=( int) (B); I --) # defineinf0x7ffffff # defineeps1e-6using namespace std; int d [100005]; int flag; void binary_search (int q, int, int B) {// jump out while a = B-1 if (flag) return; if (d [a]> q | d [B] <q) return; if (a = b-1) {cout <B <endl; flag = 1; return;} binary_search (q, a, (a + B)/2 ); // binary_search (q, a, B/2); binary_search (q, (a + B)/2, B);} int main () {// keep the n + 1 segment. The n and n + 1 segments are the cd int n, m of the n label. cin >>> n> m; d [0] = 1; int kg = 1; int c, t; int sum = 0; for (int I = 0; I <n; I ++) {cin> c> t; sum + = c * t; d [kg + +] = sum;} int q; while (m --) {cin> q; flag = 0; binary_search (q, 0, kg-1);} // system ("pause"); return 0 ;}

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.