@ 51nod - 1577 @ XOR make up the numbers


@description@

N total number of left to right, the numerical subscript numbered from 1 to n.

A total of m times query, asking whether each of the L number of R from the first to (L-th and including the number of R) is selected such that the number of some of them is exclusive or K.

input
of the first line of an integer n (0 <n <= 500,000 ).
The second row of n integer, 0 <Each number <2 ^ 30.
A third row number m, the number indicating an inquiry (0 <m <= 500,000) .
The next three lines per row m, L, R, K (0 <L <= R <= n, 0 <K <2 ^ 30).

Output
M lines, each behavior YES or NO

sample input
5
1 1 2 4 6
3
1 2 1
2 4 8
3 5 7
sample output
YES
NO
NO

@solution@

Some of these numbers can select the number of exclusive-OR and as K. It is not difficult to think of using a linear base to solve.

If you want to direct the interval constructed title given data range [L, R] is a linear group is not easy actually.
For example, Mo team with Radical, certainly not run block. Use the online segment tree O (nlog ^ 3 n), the partition can be done offline with O (nlog ^ 2 n), can some points but still not good enough.
The key is that we want to construct the [L, R] is a linear group, involves merges two groups linear operation, and the complexity of operation is always log ^ 2 n do not fall down.

May wish to change the angle: We given R and K, L can be found that the higher the more to the left or the value can be exclusive. So we can try to find out the first XOR K may be of L ', L and then compare L' determines the size of legitimacy.
We try to maintain for each of the R groups plurality of linearly independent linear closest number R configuration. This idea is similar to the problem-solving ideas bzoj3514.

Specific operations, we sweep from left to right, a linear R-1-yl group R is a linear Release delivery.
Our linear base from high to low. If the current number of the i-th bit is 1, the linear look at the i-th bit group is already a several. If the number does not go directly into, if several of which are more linear and the current number in the group number is closer to R, will be closer to the linear group into another insertion operation continues.
A bit like the bubble sort, the number will be closer to the R several other squeeze out.

At the same time, we can find the upper linear group taken as close to the number of R.
So when we try out different or K, if you encounter a number of positions of a bit less than L, and the median is when we XOR the K needed, it can be directly determined to be "NO".
To tell the truth here strictly to prove a lot of trouble. I limited capacity, the algorithm can not find a more concise proof of correctness.
After all, the base is linear linear algebra ah. How it may be very simple.

@accepted code@

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 500000;
int read() {
    int x = 0; char ch = getchar();
    while( ch > '9' || ch < '0' ) ch = getchar();
    while( '0' <= ch && ch <= '9' ) x = 10*x + ch - '0', ch = getchar();
    return x;
}
void insert(int *pos, int *b, int x, int p) {
    for(int i=29;i>=0;i--) {
        if( x & (1<<i) ) {
            if( b[i] == 0 ) {
                b[i] = x;
                pos[i] = p;
            }
            else if( pos[i] < p ) {
                swap(pos[i], p);
                swap(b[i], x);
            }
            x ^= b[i];
        }
    }
}
bool search(int *pos, int *b, int x, int p) {
    for(int i=29;i>=0;i--) {
        if( x & (1<<i) ) {
            if( b[i] == 0 || pos[i] < p )
                return false;
            x ^= b[i];
        }
    }
    return true;
}
int pos[MAXN + 5][30], b[MAXN + 5][30];
int main() {
    int n, m; n = read();
    for(int i=1;i<=n;i++) {
        int x = read();
        for(int j=29;j>=0;j--)
            pos[i][j] = pos[i-1][j], b[i][j] = b[i-1][j];
        insert(pos[i], b[i], x, i);
    }
    m = read();
    for(int i=1;i<=m;i++) {
        int L = read(), R = read(), K = read();
        puts(search(pos[R], b[R], K, L) ? "YES" : "NO");
    }
}

@details@

No review of long linear group. . . When writing this question come up with a fake linear base. . . WA so very tragic. . .

Group may be linear insertion analogy dynamically join equations Gaussian elimination. (Under which called linear-based i-bit binary number 1 in the most significant bit of the i-th bit ... it seems somewhat convoluted) but because we need to construct a special linear-based, so write Gaussian elimination can not meet these properties.
Further special linear group may further point (analogous to the Gaussian elimination of the simplest form), i.e. a linear yl requirements if several bit i, the i-bit binary Dir is 0 elsewhere.

It seems many people are offline do, but this question can be done online through the linear-based storage for each location.

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11123375.html