[2017 Xi'an tournament: A] XOR (+ linear segment tree-yl)

Introduction: Although there are many explanations, but still want to write according to their own understanding.

 

Ideas: Firstly title

   First, the operation interval - segment tree

   Second, the XOR operation - yl linear

   This is not difficult to think two, the next step is critical skills

    "Or" calculating two binary numbers is in, as long as there is a corresponding bit, so that the result is 1 bit, so in order k "or" the result of operation as large as possible,

   It requires a number of exclusive-OR 1 as much as possible on individual bits.

   Operation of the linear group, XOR can be determined and the maximum range, but we need the results are "OR" operation.

   So we can be negated k, then all the numbers before the addition of a linear group, all "and" operation again, and then added to the linear group.

   Thus, each of a linear group, all enables the number k becomes large, since the position of each bit of the binary 1 is k, 0 are the linear group.

   Therefore, we only need to claim, k isopropyl group or a linear or maximum and exclusive.

  

//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#pragma GCC optimize(2)
#include <bits/stdc++.h>
#include<unordered_set>

using namespace std;
typedef double dou;
typedef long long ll;
typedef pair<int, int> pii;
typedef map<int, int> mii;

#define pai acos(-1.0)
#define M 10050
#define inf 0x3f3f3f3f
#define mod 1000000007
#define IN inline
#define W(a) while(a)
#define lowbit(a) a&(-a)
#define left k<<1
#define right k<<1|1
#define lson L, mid, left
#define rson mid + 1, R, right
#define ms(a,b) memset(a,b,sizeof(a))
#define Abs(a) (a ^ (a >> 31)) - (a >> 31)
#define random(a,b) (rand()%(b+1-a)+a)
#define false_stdio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

int T;
int n, q, k;
int tmp, x, y, z;

struct Data {
    int num[35];
    void insert(int t) {
        for (int i = 31; i >= 0; i--) {
            if (t >> i & 1) {
                if (!num[i]) { num[i] = t; break; }
                t ^= num[i];
            }
        }
    }
}tree[M << 2], ans;

IN void Updata(int L, int R, int k,int pos) {
    tree[k].insert(tmp);
    if (L == R)return;
    int mid = L + R >> 1;
    if (pos <= mid)Updata(lson, pos);
    else Updata(rson, pos);
}

IN void Query(int L, int R, int k) {
    if(x<=L && R<=y){
        for (int i = 31; i >= 0; i--) {
            if (tree[k].num[i])ans.insert(tree[k].num[i]);
        }
        return;
    }
    int mid = L + R >> 1;
    if (x <= mid)Query(lson);
    if (y > mid)Query(rson);
}

IN int read() {//快读
    int v = 0, f = 0; char ch = getchar();
    W(!isdigit(ch)) { f |= ch == '-'; ch = getchar(); }
    W(isdigit(ch)) { v = (v << 3) + (v << 1) + (ch ^ 48); ch = getchar(); }
    return f ? -v : v;
}

int main() {
    T = read();
    W(T--) {
        n = read(), q = read(), k = read();
        for (int i = 1; i <= n; i++) {
            tmp = read();
            tmp &= (~k);//关键的一步
            Updata(1, n, 1, i);
        }
        W(q--) {
            ms(ans.num, 0);
            x = read(), y = read();
            Query(1, n, 1);
            z = k;
            for (int i = 31; i >= 0; i--)z = max(z, z ^ ans.num[i]);
            printf("%d\n", z);
        }
    }
    return 0;
}

 

   

   

Guess you like

Origin www.cnblogs.com/caibingxu/p/11788485.html