Bzoj3689 exclusive or

Source: Internet
Author: User
N non-negative integers A [1], a [2],..., A [n].
For each pair (I, j) Satisfying 1 <= I <j <= n, a new number A [I] XOR A [J] is obtained. in this case, there are N * (n-1)/two new numbers. Evaluate the first K of these numbers (excluding a [I.

Note: XOR corresponds to "XOR" in Pascal and "^" in C ++ ".


Idea: Same as noi2010 super piano. Http://blog.csdn.net/wyfcyx_forever/article/details/40400327

We only need to first put each number in the global heap and the minimum value after the number exclusive, and then put the k + 1 small value in the heap every time the heap goes out. This repeat K times.

Then the problem becomes to quickly find the K small value of XOR for a given number in a certain range. We can maintain the size by using the persistent trie and directly divide it into two points on the tree.

Time complexity is still O (klogn ).


Code:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std; #define N 100010int w[N]; int ch[N<<5][2], size[N<<5], ind;int root[N];int Newadd(int Last, int bit, int x) {    int q = ++ind;    ch[q][0] = ch[Last][0], ch[q][1] = ch[Last][1];    size[q] = size[Last] + 1;         if (bit < 0)        return q;         if ((x >> bit) & 1)        ch[q][1] = Newadd(ch[Last][1], bit - 1, x);    else        ch[q][0] = Newadd(ch[Last][0], bit - 1, x);    return q;}int getkth(int Left, int Right, int x, int k) {    int res = 0, better;    bool d;    for(int dep = 30; dep >= 0; --dep) {        d = (x >> dep) & 1;        better = size[ch[Right][d]] - size[ch[Left][d]];        if (better >= k)            Left = ch[Left][d], Right = ch[Right][d];        else            Left = ch[Left][d ^ 1], Right = ch[Right][d ^ 1], res += (1<<dep), k -= better;    }         return res;} struct st {    int end, kth, val;    st(int _end = 0, int _kth = 0, int _val = 0):end(_end),kth(_kth),val(_val){}    bool operator < (const st &B) const {        return val < B.val;    }}; #define K 250010struct Heap {    st a[K];    int top;    Heap():top(0){}    inline void up(int x) {        for(; x != 1; x >>= 1)            if (a[x]<a[x>>1])                swap(a[x],a[x>>1]);            else                break;    }    inline void down(int x) {        int son;        for(; (x<<1)<=top; ) {            son=(((x<<1)==top)||(a[x<<1]<a[(x<<1)|1]))?(x<<1):((x<<1)|1);            if (a[son]<a[x])                swap(a[son],a[x]),x=son;            else                break;        }    }    inline void push(const st &x) {        a[++top] = x;        up(top);    }    st Min() {        return a[1];    }    inline void pop() {        a[1] = a[top--];        down(1);    }}H; int main() {    int n, k;    scanf("%d%d", &n, &k);         register int i, j;    for(i = 1; i <= n; ++i) {        scanf("%d", &w[i]);        root[i] = Newadd(root[i - 1], 30, w[i]);    }         for(i = 2; i <= n; ++i)        H.push(st(i, 1, getkth(root[0], root[i - 1], w[i], 1)));         for(i = 1; i <= k; ++i) {        st tmp = H.Min();        H.pop();        if (i != 1)            putchar(' ');        printf("%d", tmp.val);        if (tmp.kth != tmp.end - 1)            H.push(st(tmp.end, tmp.kth + 1, getkth(root[0], root[tmp.end - 1], w[tmp.end], tmp.kth + 1)));    }         return 0;}


Bzoj3689 exclusive or

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.