Ivan and Burgers(CF-1100F)
Problem Description
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?liliand walk to the joint?riri?(li≤ri)(li≤ri). While strolling with the?ii-th friend Ivan can visit all joints?x?which satisfy?li≤x≤ri.
For each joint Ivan knows the cost of the most expensive burger in it, it costs?ciciburles. 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⊕c?burles, where?⊕?denotes the?bitwise XOR operation.
Currently Ivan has?2^(2^100)?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?i. 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?n?(1≤n≤500000) — the number of burger shops.
The next line contains?nn?integers?c1,c2,…,cn?(0≤ci≤106), where?cici?— the cost of the most expensive burger in the burger joint?i.
The third line contains one integer?q?(1≤q≤500000) — the number of Ivan's friends.
Each of the next?qq?lines contain two integers?li?and?ri?(1≤li≤ri≤n) — pairs of numbers of burger shops between which Ivan will walk.
Output
Output?q?lines,?i-th of which containing the maximum amount of money Ivan can spend with the friend?i.
Examples
Input
4
7 2 3 4
3
1 4
2 3
1 3
Output
7
3
7
Input
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
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 —?0,?12,?14,?23,?12⊕14=2, 14⊕23=25, 12⊕23=27, 12⊕14⊕23=20. The maximum amount of money it turns out to spend, if you go to the first and third burger — 12⊕23=27.
題意:給出長度為 n 的一個序列,再給出 m 個查詢,每組查詢給出 l、r 兩個數,查詢區間 [l,r] 中的異或和最大值
思路:查詢異或和最大值可以利用線性基來做,但由于要區間查詢,那么可以利用前綴線性基來做
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #include<unordered_map> #include<bitset> #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long #define Pair pair<int,int> LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; } LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; } LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); } LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); } LL LCM(LL x,LL y){ return x/GCD(x,y)*y; } const double EPS = 1E-10; const int MOD = 1E9+7; const int N = 500000+5; const int dx[] = {-1,1,0,0,1,-1,1,1}; const int dy[] = {0,0,-1,1,-1,1,-1,1}; using namespace std;struct PrefixLinearBasis{int d[N][32];//前綴線性基int pos[N][32];//最后一個修改i這個位置的數int num;PrefixLinearBasis(){memset(d,0,sizeof(d));memset(pos,0,sizeof(pos));num=0;}void add(int x){//向線性基中添加xnum++;for(int i=0; i<32; i++){//復制前num-1個線性基d[num][i]=d[num-1][i];pos[num][i]=pos[num-1][i];}int P=num;for(int i=31; i>=0; i--){if((x>>i)&1){if(d[num][i]){//插入失敗if(pos[num][i]<P){//交換位置swap(pos[num][i], P);swap(d[num][i],x);}x^=d[num][i];//異或}else{//插入成功d[num][i]=x;pos[num][i]=P;break;}}}}int queryMax(int l,int r){//查詢[l,r]中的最大值int res=0;for (int i=31; i>=0; i--){if(pos[r][i]<l) continue;if ((res^d[r][i])>res) res^=d[r][i];}return res;}int queryMin(int l,int r) {//查詢[l,r]中的最小值for(int i=0; i<=60; i++){if(pos[r][i]<l)continue;if(d[r][i])return d[r][i];}return 0;} }PLB; int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++){int x;scanf("%d",&x);PLB.add(x);}int m;scanf("%d",&m);for(int i=1;i<=m;i++){int l,r;scanf("%d%d",&l,&r);int res=PLB.queryMax(l,r);printf("%d\n",res);}return 0; }?
總結
以上是生活随笔為你收集整理的Ivan and Burgers(CF-1100F)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: The table(CF-226D)
- 下一篇: 理论基础 —— 队列 —— 链队列