F. Ivan and Burgers
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Ivan loves burgers and spending money. There are nn burger joints on the street where Ivan lives. Ivan has qq friends, and the ii-th friend suggested to meet at the joint lili and walk to the joint riri (li≤ri)(li≤ri). While strolling with the ii-th friend Ivan can visit all joints xx which satisfy li≤x≤rili≤x≤ri.
For each joint Ivan knows the cost of the most expensive burger in it, it costs cici burles. Ivan wants to visit some subset of joints on his way, in each of them he will buy the most expensive burger and spend the most money. But there is a small issue: his card broke and instead of charging him for purchases, the amount of money on it changes as follows.
If Ivan had dd burles before the purchase and he spent cc burles at the joint, then after the purchase he would have d⊕cd⊕c burles, where ⊕⊕denotes the bitwise XOR operation.
Currently Ivan has 22100−122100−1 burles and he wants to go out for a walk. Help him to determine the maximal amount of burles he can spend if he goes for a walk with the friend ii. The amount of burles he spends is defined as the difference between the initial amount on his account and the final account.
Input
The first line contains one integer nn (1≤n≤5000001≤n≤500000) — the number of burger shops.
The next line contains nn integers c1,c2,…,cnc1,c2,…,cn (0≤ci≤1060≤ci≤106), where cici — the cost of the most expensive burger in the burger joint ii.
The third line contains one integer qq (1≤q≤5000001≤q≤500000) — the number of Ivan's friends.
Each of the next qq lines contain two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — pairs of numbers of burger shops between which Ivan will walk.
Output
Output qq lines, ii-th of which containing the maximum amount of money Ivan can spend with the friend ii.
Examples
input
Copy
4 7 2 3 4 3 1 4 2 3 1 3
output
Copy
7 3 7
input
Copy
5 12 14 23 13 7 15 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5 4 4 4 5 5 5
output
Copy
12 14 27 27 31 14 25 26 30 23 26 29 13 13 7
Note
In the first test, in order to spend the maximum amount of money with the first and third friends, Ivan just needs to go into the first burger. With a second friend, Ivan just go to the third burger.
In the second test for a third friend (who is going to walk from the first to the third burger), there are only 8 options to spend money — 00, 1212, 1414, 2323, 12⊕14=212⊕14=2, 14⊕23=2514⊕23=25, 12⊕23=2712⊕23=27, 12⊕14⊕23=2012⊕14⊕23=20. The maximum amount of money it turns out to spend, if you go to the first and third burger — 12⊕23=2712⊕23=27.
分析:因为不带修改操作,所以离线在线都能做。
首先排除线段树维护,复杂段显然不对。
离线的话我们可以将区间的右端点排序,然后维护一个1到r的线性基,并且对于每位,我们只存id最大的那个元素,即相当于循环到第i位时,如果x的最高位是i并且x的id大于原来这个位置的元素的下标,那么我们将x放在这里,把原来的向后推,只需要swap一下就可以了。这样相当于一个贪心的过程,即保证高位的元素一定出现在低位的元素的后面,正确性是显而易见的。查询的时候则多判断一下左端点就可以了。
在线的话同理,开5e5个数组的前缀的线性基就可以了,和离线同理。
#include <bits/stdc++.h>
using namespace std;
struct L_B{
int d[22],pos[22];
void init()
{
memset(d,0,sizeof(d));
memset(pos,0,sizeof(pos));
}
bool insert(int val,int x)
{
for (int i=20;i>=0;i--)
if (val&(1LL<<i))
{
if (!d[i])
{
d[i]=val;
pos[i]=x;
break;
}
if(x > pos[i]){
swap(x,pos[i]);
swap(val,d[i]);
}
val^=d[i];
}
return val>0;
}
int query_max(int x)
{
int ret=0;
for (int i=20;i>=0;i--)
if ((ret^d[i])>ret && pos[i]>= x)
ret^=d[i];
return ret;
}
}lb[500004];
int main(){
int n,x;
cin>>n;
lb[0].init();
for (int i = 1; i <= n; ++i) {
lb[i].init();
for (int j = 0; j <= 20; ++j) {
lb[i].d[j]=lb[i-1].d[j];
lb[i].pos[j]=lb[i-1].pos[j];
}
scanf("%d",&x);
lb[i].insert(x,i);
}
int q;cin>>q;
while (q--){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",lb[r].query_max(l));
}
return 0;
}