1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <cstdio> #include <queue> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef long long L; typedef unsigned int U; const int N = 5e5 + 10, M = 32; int n, m; U w[N]; int idx, rt[N]; struct Node { int s[2], v, id; } tr[N * (M + 1)]; void insert (int &x, int k, int id, int t = M - 1) { tr[++idx] = tr[x], x = idx; ++tr[x].v, tr[x].id = id; if (!~t) return; insert(tr[x].s[k >> t & 1], k, id, t - 1); } int query (int x, int y, int k, int t = M - 1) { if (!~t) return tr[y].id; int c = k >> t & 1; if (tr[tr[y].s[c ^ 1]].v > tr[tr[x].s[c ^ 1]].v) return query(tr[x].s[c ^ 1], tr[y].s[c ^ 1], k, t - 1); return query(tr[x].s[c], tr[y].s[c], k, t - 1); } struct Data { int a, b, l, r; U calc () const { return w[r] ^ w[l]; } bool operator < (const Data &_) const { return calc() < _.calc(); } }; int main () { read(n, m); insert(rt[1], 0, 0); for (int i = 1; i <= n; ++i) read(w[i]), insert(rt[i + 1] = rt[i], w[i] ^= w[i - 1], i); priority_queue <Data> q; for (int i = 1; i <= n; ++i) q.push((Data){0, i - 1, query(rt[0], rt[i], w[i]), i}); L res = 0; while (m--) { Data t = q.top(); q.pop(); res += t.calc(); if (t.l < t.b) q.push((Data){t.l + 1, t.b, query(rt[t.l + 1], rt[t.b + 1], w[t.r]), t.r}); if (t.l > t.a) q.push((Data){t.a, t.l - 1, query(rt[t.a], rt[t.l], w[t.r]), t.r}); } write(res); return 0; }
|