problems / 240

Which Chunk Holds Byte X?

easy binary-search prefix-sums arrays 1000 ms · 256 MB

Which Chunk Holds Byte X?

An object store keeps a large file as n consecutive chunks, numbered 1 to n. Chunk i holds s_i bytes, and some chunks may be empty (s_i = 0). Byte offsets in the file are 0-based: chunk 1 holds the first s_1 bytes, chunk 2 the next s_2 bytes, and so on.

You receive q read requests, each asking for the byte at global offset x. For each request, report the chunk that holds this byte and the byte's 0-based offset inside that chunk. An empty chunk holds no bytes, so it is never the answer. If x is at or past the end of the file, the byte doesn't exist.

Input

The first line contains two integers n and q (1 ≤ n ≤ 2·10^5, 1 ≤ q ≤ 2·10^5).

The second line contains n integers s_1, s_2, …, s_n (0 ≤ s_i ≤ 10^9), the chunk sizes in bytes.

Each of the next q lines contains one integer x (0 ≤ x ≤ 10^15), the requested global offset.

The file can be far larger than 2^32 bytes, so offsets and running totals need 64-bit integers.

Output

For each request, in input order, print one line. If the byte exists, print two integers: the chunk index i (1-based) and the offset k inside chunk i (0 ≤ k < s_i). Otherwise print OUT OF RANGE.

Notes

In the first example the chunk sizes are 4, 0, 3, 5, so chunk 1 holds bytes 0–3, chunk 2 holds nothing, chunk 3 holds bytes 4–6 and chunk 4 holds bytes 7–11. Byte 4 is the first byte of chunk 3, not of the empty chunk 2. The file has 12 bytes, so offset 12 is out of range.

In the second example the file is 5 000 000 000 bytes long, more than 2^32.

Example 1

Input

4 6
4 0 3 5
0
3
4
7
11
12

Output

1 0
1 3
3 0
4 0
4 4
OUT OF RANGE

Example 2

Input

8 5
0 1000000000 1000000000 0 0 1000000000 1000000000 1000000000
4999999999
2000000000
1999999999
5000000000
0

Output

8 999999999
6 0
3 999999999
OUT OF RANGE
2 0

Hints · reveal one at a time if you're stuck

Hint 1

With up to 2·10^5 chunks and 2·10^5 requests, scanning the chunks for every request is too slow. Which global offset does each chunk start at, and how do those values change from one chunk to the next?

Hint 2

The running totals of chunk sizes never decrease, so you can binary search them for each request. Check your search on the first example: byte 4 must land in chunk 3, not in the empty chunk 2 or in chunk 1. Keep the totals in 64-bit integers.

report an issue Prefer your own editor? sooks prep 240 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.