1. 程式人生 > >2017 ICPC 西安站現場賽 A.XOR (線段樹+線性基)

2017 ICPC 西安站現場賽 A.XOR (線段樹+線性基)

getchar tput 線性 calculate ext following case all pri

XOR

Consider an array A with n elements. Each of its element is A[i] (1 ≤ i ≤ n). Then gives two integers
Q, K, and Q queries follow. Each query, give you L, R, you can get Z by the following rules.
To get Z, at first you need to choose some elements from A[L] to A[R], we call them A[i1], A[i2],
. . . , A[it], Then you can get number Z = K or (A[i1], A[i2], . . . , A[it]).
Please calculate the maximum Z for each query .

Input
Several test cases.
First line an integer T (1 ≤ T ≤ 10). Indicates the number of test cases.
Then T test cases follows. Each test case begins with three integer N, Q, K (1 ≤ N ≤ 10000,1 ≤
Q ≤ 100000, 0 ≤ K ≤ 100000). The next line has N integers indicate A[1] to A[N] (0 ≤ A[i] ≤ 108).
Then Q lines, each line two integer L, R (1 ≤ L ≤ R ≤ N).

Output
For each query, print the answer in a single line.

Sample Input
1
5 3 0
1 2 3 4 5
1 3
2 4
3 5

Sample Output
3
7
7

題意:給出一個1個長度為n的數組A,然後給出q個詢問對於每個詢問,每次在下標為[l,r]的數中,選取一部分數,使得其異或值OR上k後最大,輸出這個最大值

分析:根據題意,能夠想到需要用到線性基。然後因為是要求最後OR上k後最大,根據OR運算的性質,可以先將數組中的數的二進制位上,將k為1的位置都變為0.因為這些位置在最後OR上k後都會變為1,所以暫時不討論它。比如現在數x為5(101),k為4(100),那麽需要將x變為1.

轉換完成後,線性基的最大值OR上k就是答案,又因為題目是區間查詢,所以需要用到線段樹來維護線性基

代碼如下:

#include <cstdio>
#include 
<iostream> #include <algorithm> #include <cstring> using namespace std; const int MAXN=1e4+100; typedef long long LL; LL t,n,q,k,l,r,pd; int vis[35]; long long read(){ long long res = 0; int flag = 0; char ch; if ((ch = getchar()) == -){ flag = 1; } else
if(ch >= 0 && ch <= 9){ res = ch - 0; } while ((ch = getchar()) >= 0 && ch <= 9){ res = res * 10 + (ch - 0); } return flag ? -res : res; } struct L_B{ long long d[61]; int cnt; L_B() { memset(d,0,sizeof(d)); cnt=0; } void clear() { memset(d,0,sizeof(d)); cnt=0; } void insert(long long val) { for (int i=31;i>=0;i--) if (val&(1LL<<i)) { if (!d[i]) { d[i]=val; break; } val^=d[i]; } } long long query_max() { long long ret=0; for (int i=31;i>=0;i--) { if ((ret^d[i])>ret) ret^=d[i]; } return ret; } }; L_B R; L_B merge(const L_B &n1,const L_B &n2) { L_B ret=n1; for (int i=31;i>=0;i--) if (n2.d[i]) ret.insert(n2.d[i]); return ret; } struct node { int l; int r; L_B A; }tree[MAXN<<2]; void PushUp(int rt) { tree[rt].A=merge(tree[rt<<1].A,tree[rt<<1|1].A); } void BuildTree(int l,int r,int rt) { tree[rt].l=l; tree[rt].r=r; if(l==r) { LL x; x=read(); x=x&pd;//轉換後,再插入到線性基中 tree[rt].A.clear(); tree[rt].A.insert(x); return; } int mid=(tree[rt].l+tree[rt].r)/2; BuildTree(l,mid,rt<<1); BuildTree(mid+1,r,rt<<1|1); PushUp(rt); } void Query(int l,int r,int rt) { if(tree[rt].l==l&&tree[rt].r==r) { R=merge(R,tree[rt].A); return; } int mid=(tree[rt].l+tree[rt].r)/2; if(r<=mid)Query(l,r,rt<<1); else if(l>mid)Query(l,r,rt<<1|1); else { Query(l,mid,rt<<1); Query(mid+1,r,rt<<1|1); } } int main() { t=read(); while(t--) { pd=0; memset(vis,0,sizeof(vis)); n=read(),q=read(),k=read(); for (int i=31;i>=0;i--) { if(k&(1LL<<i)) vis[i]=1; else pd+=(1LL<<i);// 用來進行轉換,pd&x即為轉換後的值 } BuildTree(1,n,1); while(q--) { l=read(),r=read(); R.clear(); Query(l,r,1); LL ans=(R.query_max()|k); printf("%lld\n",ans); } } return 0; }

2017 ICPC 西安站現場賽 A.XOR (線段樹+線性基)