Engineering platform~15 min2s · 256 MBtarget O(n + q × n) time, O(n) spacerecursion
A build system runs a task with a recursive function. run(n) prints a start line, runs run(n - 1) and then run(n - 2), and prints an end line. A call with n <= 0 prints nothing.
For n = 3, the log has 8 lines: start 3, start 2, start 1, end 1, end 2, start 1, end 1, end 3. For large n, the log is far too long to print. On-call asks how many tasks are running right after a given line.
Write call_depths(n, ks).
run(n) is the list of lines that the function above prints. Lines are numbered from 1.k in ks, return the number of open calls right after line k is printed. A call is open from its start line until its end line. After the last line, no call is open.k is between 1 and the number of lines in the log.k, in order. Do not print or store the log.Input: n = 3, ks = [1, 3, 4, 6, 8]
Output: [1, 3, 2, 2, 0]Take n = 3. The log has 8 lines, and the answer is the number of open calls after each of the asked lines.
| Line | Text | Open calls after it |
|---|---|---|
| 1 | start 3 | 1 |
| 2 | start 2 | 2 |
| 3 | start 1 | 3 |
| 4 | end 1 | 2 |
| 5 | end 2 | 1 |
| 6 | start 1 | 2 |
| 7 | end 1 | 1 |
| 8 | end 3 | 0 |
The lines 1, 3, 4, 6 and 8 give 1, 3, 2, 2 and 0.
Input: n = 1, ks = [1, 2]
Output: [1, 0]The call run(1) prints a start line and an end line, because both its children return at once. After line 1 one call is open, and after line 2 none is.
Input: n = 4, ks = [1, 4, 9, 14]
Output: [1, 4, 1, 0]The log of run(4) has 14 lines. Line 4 is the start of run(1), four calls deep. Line 9 is the end of run(3), which leaves run(4) open, and line 14 is the end of run(4).
1 ≤ n ≤ 701 ≤ ks.length ≤ 5,0001 ≤ ks[i] ≤ 10^15, and ks[i] is at most the number of lines in the log of run(n)The log of run(70) has 996,908,023,758,526 lines, so it cannot be printed. But it has only 70 levels. Each question walks down at most 70 calls, skipping whole blocks of lines on the way.