Maximum And Queries (hard version)
Contest: Codeforces Round 912 (Div. 2)
Difficulty rating: 2500
Tags: bitmasks, divide and conquer, dp, greedy

This is the hard version of the problem. The only difference between the two versions is the constraint on $$$n$$$ and $$$q$$$, the memory and time limits. You can make hacks only if all versions of the problem are solved.

Theofanis really likes to play with the bits of numbers. He has an array $$$a$$$ of size $$$n$$$ and an integer $$$k$$$. He can make at most $$$k$$$ operations in the array. In each operation, he picks a single element and increases it by $$$1$$$.

He found the maximum bitwise AND that array $$$a$$$ can have after at most $$$k$$$ operations.

Theofanis has put a lot of work into finding this value and was very happy with his result. Unfortunately, Adaś, being the evil person that he is, decided to bully him by repeatedly changing the value of $$$k$$$.

Help Theofanis by calculating the maximum possible bitwise AND for $$$q$$$ different values of $$$k$$$. Note that queries are independent.

Input format:
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 10^6$$$) — the size of the array and the number of queries.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^6$$$) — the elements of the array.

Next $$$q$$$ lines describe the queries. The $$$i$$$-th line contains one integer $$$k_i$$$ ($$$0 \le k_i \le 10^{18}$$$) — the number of operations that can be done in the $$$i$$$-th query.

Output format:
For each query, print one integer — the maximum bitwise AND that array $$$a$$$ can have after at most $$$k_i$$$ operations.

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

Note:
In the first test case, in the first query, we add $$$1$$$ in the first and last elements of the array.

Thus, the array becomes $$$[2,3,7,6]$$$ with bitwise AND equal to $$$2$$$.

In the second test case, in the first query, we add $$$1$$$ in the first element, $$$5$$$ in the second, and $$$3$$$ in the third and now all the elements are equal to $$$5$$$.

Example input:
4 2
1 3 7 5
2
10

Example output:
2
6

Example input:
3 5
4 0 2
9
8
17
1
3

Example output:
5
4
7
0
1

Editorial:
Let $$$S = \sum (2^{20} - a_i)$$$. If $$$k \ge S$$$ then the answer is $$$2^{20} + \lfloor \frac{k-S}{n} \rfloor$$$.
Similarly to D1 let's construct the answer bit by bit. Let $$$x$$$ be the current answer and $$$b$$$ be the bit we want to add.
Let's look at the amount of operations we need to do on the $$$i$$$-th element to change our answer from $$$x$$$ to $$$x+2^b$$$.
• if $$$x$$$ is not a submask of $$$a_i$$$, then after constructing answer $$$x$$$ it has $$$0$$$s on all bits not greater than $$$b$$$. In this case we need to increase the $$$i$$$-th element by $$$2^b$$$.
• if $$$x+2^b$$$ is a submask of $$$a_i$$$, then we do not need to increase the $$$i$$$-th element.
• otherwise we need to increase the $$$i$$$-th element by $$$2^b - a_i$$$ $$$mod$$$ $$$2^b$$$.
We can handle all three cases efficiently if we precompute the following two arrays:
$$$A(mask)$$$ – how many elements from the array is $$$mask$$$ a submask of.
$$$B(mask, b)$$$ – sum of $$$a_i$$$ $$$mod$$$ $$$2^b$$$ over all $$$a_i$$$ for which $$$x$$$ is a submask.
Both arrays can be calculated efficiently using SOS dp.
This allows us to answer the queries in $$$O$$$($$$q \times log$$$ $$$a$$$) with $$$O$$$($$$a$$$ $$$log^2$$$ $$$a$$$) preprocessing