Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1e5 + 10;
- int n, k, a[maxn];
- struct persistent_trie {
- static const int maxsz = maxn * 32;
- int tot, rt[maxn], sz[maxsz], tid[maxsz], ch[2][maxsz];
- void ins(int& k, int rt, int id, int x, int now) {
- sz[k = ++tot] = sz[rt] + 1;
- ch[0][k] = ch[0][rt], ch[1][k] = ch[1][rt];
- if (now == -1) {
- tid[k] = id; return;
- }
- int chk = x >> now & 1;
- ins(ch[chk][k], ch[chk][rt], id, x, now - 1);
- }
- inline void build() {
- for (int i = 1; i <= n; i++) {
- ins(rt[i], rt[i - 1], i, a[i], 30);
- }
- }
- inline int query(int l, int r, int x) {
- int p = rt[l - 1], q = rt[r];
- for (int i = 30; ~i; i--) {
- int tmp = x >> i & 1, chk = sz[ch[tmp][q]] - sz[ch[tmp][p]] ? tmp : tmp ^ 1;
- p = ch[chk][p], q = ch[chk][q];
- }
- return tid[q];
- }
- } trie;
- struct node {
- int st, l, r, pos;
- node(int _s = 0, int _l = 0, int _r = 0) :
- st(_s), l(_l), r(_r), pos(trie.query(l, r, a[st])) {}
- inline int get_val() const {
- return a[pos] ^ a[st];
- }
- inline bool operator < (const node& o) const {
- return get_val() > o.get_val();
- }
- };
- priority_queue <node> Q;
- int main() {
- scanf("%d %d", &n, &k);
- for (int i = 1; i <= n; i++) {
- scanf("%d", a + i);
- }
- trie.build();
- for (int i = 1; i < n; i++) {
- Q.push(node(i, i + 1, n));
- }
- while (k--) {
- node p = Q.top();
- printf("%d ", p.get_val()), Q.pop();
- if (p.l < p.pos) Q.push(node(p.st, p.l, p.pos - 1));
- if (p.pos < p.r) Q.push(node(p.st, p.pos + 1, p.r));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement