Codeforces Round 1052 (Div. 2)
D2
Max Sum OR (Hard Version)
Codeforces
2000
原题
← D1E →
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version, $0\leq l\leq r \lt 2^{30}$. You can hack only if you solved all versions of this problem.

翻译中…

You are given two integers $l$ and $r$ ($l\le r$).

翻译中…

Let $n = r - l + 1$. We will create two arrays $a$ and $b$, both consisting of $n$ integers. Initially, both $a$ and $b$ are equal to $[l, l+1, \ldots, r]$. You have to reorder the array $a$ arbitrarily to maximize the following value:

翻译中…

$$\sum_{i=1}^n \left (a_i\;|\;b_i \right ).$$

翻译中…

Here, $|$ denotes the bitwise OR operation.

翻译中…

You also need to construct a possible way to reorder the array $a$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The only line of each test case contains two integers $l$ and $r$ ($0\leq l \leq r \lt 2^{30}$) — the minimum and maximum elements in $a$.

翻译中…

Let $n = r - l + 1$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, print a single integer in the first line of output — the maximum value of $\sum\limits_{i=1}^n \left (a_i\;|\;b_i \right )$.

翻译中…

Then, print $n$ distinct integers $a_1, a_2, \ldots,a_n$ in the second line — the array $a$ after reordering.

翻译中…

If there are multiple answers, you may print any of them.

翻译中…

Note

翻译中…

In the first test case, the reordered array $a$ is $[3,2,1,0]$. The value of the expression is $(3\;|\;0)+(2\;|\;1)+(1\;|\;2)+(0\;|\;3)=3+3+3+3=12$. It can be proved that this is the maximum possible value of the expression.

翻译中…

In the second test case, the reordered array $a$ is $[7,8,5,4,3,2,9,0,1,6]$. The value of the expression is $90$. It can be proved that this is the maximum possible value of the expression.

翻译中…

In the third test case, it can be proved that $240$ is the maximum possible value of the expression.

翻译中…

In the fourth test case, the reordered array $a$ is $[10,8,7,6,9]$. The value of the expression is $(10\;|\;6)+(8\;|\;7)+(7\;|\;8)+(6\;|\;9)+(9\;|\;10)=14+15+15+15+11=70$. It can be proved that this is the maximum possible value of the expression.

翻译中…

样例

Input #1
7
0 3
0 9
0 15
6 10
1 4
3 6
1000000007 1000000009
Output #1
12
3 2 1 0
90
7 8 5 4 3 2 9 0 1 6
240
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
70
10 8 7 6 9
20
2 1 4 3
28
4 3 6 5
3000000039
1000000008 1000000007 1000000009
InputCopy OutputCopy
题目信息
题号
2146D2
时间限制
2 s
内存限制
256 MB
标签
位运算
构造
分治
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。