Operation HDU - 6579 (linear-yl)

Operation (HDU - 6579)

Extra:

Beginner linear group recommended a linear-based study notes .

Linear two-element group is a two exclusive set of elements or no redundancy.

We in the set every record a contribution to the current digital bits, you can query intervals or different extremes.

Meaning of the questions:

To an initial sequence, required to support two modes of operation:

  • XOR query elements within a maximum interval.
  • Adding an element to the end of the sequence.

answer:

We consider doing a prefix linear base and then record the coordinates to insert an element at the same time, to ensure that the contribution of the high elements as much as possible to the right. This ensures that each query interval linear base and in time, queries to the number as large as possible (position contributed as high as possible). When the query only query coordinate greater than or equal to L on it.

#include <bits/stdc++.h>
#define fopi freopen("in.txt", "r", stdin)
#define fopo freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 5;
typedef pair<int, int> Pair;

struct LinearBasis{
    #define N 30
    Pair a[N+1];

    void init() {
        memset(a, 0, sizeof(a));
    }

    bool insert(int val, int pos) {
        for (int i = N; i >= 0; i--) if (val & (1ll << i)) {
            if (a[i].first == 0) {
                a[i].first = val;
                a[i].second = pos;
                break;
            }
            else if (a[i].second < pos) {
                swap(val, a[i].first);
                swap(pos, a[i].second);
            }
            val ^= a[i].first;
        }
        return val > 0;
    }

    int query_Max(int l) {
        int res = 0;
        for (int i = N; i >= 0; i--)
            if ((res^a[i].first) > res && a[i].second >= l)
                res ^= a[i].first;
        return res;
    }
}LB[maxn];

int T, n, m;
LL x;

int main() {

    //fopi;

    scanf("%d", &T);
    for (int ca = 1; ca <= T; ca++) {
        LB[0].init();

        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            scanf("%lld", &x);
            LB[i] = LB[i-1];
            LB[i].insert(x, i);
        }

        int ans = 0;
        for (int i = 1; i <= m; i++) {
            int op, x, y;
            scanf("%d%d", &op, &x);
            if (op == 0) {
                scanf("%d", &y);
                x = (x^ans) % n + 1, y = (y^ans) % n + 1;
                if (x > y) swap(x, y);
                printf("%d\n", ans = LB[y].query_Max(x));
            }
            else {
                ++n;
                LB[n] = LB[n-1];
                LB[n].insert(x^ans, n);
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/ruthank/p/11354434.html