Ralph And His Tour in Binary Country
Contest: Codeforces Round 447 (Div. 2)
Difficulty rating: 2200
Tags: brute force, data structures, trees

Ralph is in the Binary Country. The Binary Country consists of n cities and (n - 1) bidirectional roads connecting the cities. The roads are numbered from 1 to (n - 1), the i-th road connects the city labeled $$\left\lfloor \frac{i+1}{2} \right\rfloor$$ (here ⌊ x⌋ denotes the x rounded down to the nearest integer) and the city labeled (i + 1), and the length of the i-th road is Li.

Now Ralph gives you m queries. In each query he tells you some city Ai and an integer Hi. He wants to make some tours starting from this city. He can choose any city in the Binary Country (including Ai) as the terminal city for a tour. He gains happiness (Hi - L) during a tour, where L is the distance between the city Ai and the terminal city.

Ralph is interested in tours from Ai in which he can gain positive happiness. For each query, compute the sum of happiness gains for all such tours.

Ralph will never take the same tour twice or more (in one query), he will never pass the same city twice or more in one tour.

Input format:
The first line contains two integers n and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 105).

(n - 1) lines follow, each line contains one integer Li (1 ≤ Li ≤ 105), which denotes the length of the i-th road.

m lines follow, each line contains two integers Ai and Hi (1 ≤ Ai ≤ n, 0 ≤ Hi ≤ 107).

Output format:
Print m lines, on the i-th line print one integer — the answer for the i-th query.

Constraints:
Time limit: 2.5 s
Memory limit: 512.0 MB

Note:
Here is the explanation for the second sample.

Ralph's first query is to start tours from city 2 and Hi equals to 4. Here are the options:

- He can choose city 5 as his terminal city. Since the distance between city 5 and city 2 is 3, he can gain happiness 4 - 3 = 1.
- He can choose city 4 as his terminal city and gain happiness 3.
- He can choose city 1 as his terminal city and gain happiness 2.
- He can choose city 3 as his terminal city and gain happiness 1.
- Note that Ralph can choose city 2 as his terminal city and gain happiness 4.
- Ralph won't choose city 6 as his terminal city because the distance between city 6 and city 2 is 5, which leads to negative happiness for Ralph.

So the answer for the first query is 1 + 3 + 2 + 1 + 4 = 11.

Example input:
2 2
5
1 8
2 4

Example output:
11
4

Example input:
6 4
2
1
1
3
2
2 4
1 3
3 2
1 7

Example output:
11
6
3
28

Editorial:
Before answering each query, pre-process on the tree. On each vertice, we can get a sorted array of all the vertices in its subtree sorted by distance to this vertex. And it costs O(nlog(n)) time using merge sort or O(n(log(n))2) time using std::sort. If you use std::sort, you should implement it carefully or it won't be able to fit in the time limit. Because the tree is an almost complete binary tree, one vertex will appear at most [log(n)] times in all n sorted arrays,so the memory complexity is O(nlog(n)).
To answer each query, we can iterate on the highest vertex on the tour and do binary search on the sorted array to get the answer. We'll do at most [log(n)] times of iteration and the binary search is O(log(n)) per iteration, so we can answer each query in O((log(n))2) time.
Overall, the time complexity is O(nlog(n) + m(log(n))2) and the memory complexity is O(nlog(n)). If you use std::sort, the time complexity will be O((n + m)(log(n))2) and the memory complexity is the same.