UESTC 1582 magic of miracles, reappeared! Binary tree (a special case of a dictionary tree)

Source: Internet
Author: User
Tags data structures time limit

The Magic of Miracles, come again. time limit:3000/1000ms (java/others) Memory limit:131072/131072kb (java/others)


Submit Status
What I want to say to you, like a mountain like a sea. --asahinamirai




After the decisive battle, the magic World separated from the magic, in order to meet Rico again, Mirai must be tested to awaken the magic of miracles.
The test was--there was a non-negative integer a1,a2,..., an,
For M-Times, the first JJ asks for a given positive integer XJ,
Output MAX{A1XORXJ,A2XORXJ,..., Anxorxj}.


XOR operation: 0xor1=1,1xor0=1,0xor0=0,1xor1=0.
Bitwise XOR OPERATION: An XOR operation is performed on a bits of two numbers.
Also do not understand the XOR operation please go to Baidu or Google a bit: XOR.


The first behavior of input inputs is a positive integer n,
The next line is n non-negative integer a1,a2,..., an.
Next is a positive integer m,
Next line, X1,x2,..., XM for M non-negative integers.


output m lines, 1 values per line, represent the answer you are seeking.


Sample input and output
sample input Sample output
5
1 8 0) 5 14
5
7 14 10) 4 2
15
15
15
12
12


Hint
1≤n≤100000,
1≤m≤100000,
0≤ai≤2147483647,
0≤xi≤2147483647


Source 17 pre-summer training-data structure topics by Autsky_jadek, ideas are not original

UESTC Training for Data structures

UESTC 1582 magic of Miracles, reappeared.


My Solution

Test instructions: gives n non-negative integer a1,a2,..., an
For M-Times, J asks for a given positive integer XJ,
Output MAX{A1XORXJ,A2XORXJ,..., Anxorxj}.


Binary tree (a special case of a dictionary tree)
First, put all the AI into the binary tree according to 31 bits (not enough equivalent to 0 in front),
CHILD[X][K] indicates that x is the parent node, and K is the child node of the edge.
SZ[X] Represents the value of this node, when the value is 0, the node does not exist


At the time of the query, walk from the root, try to go and Val's current binary is not equal to the node,
Binary XOR at this time is 1,k = ((val >> i) & 1) ^ 1, res ^= 1 << i;
If you do not move the binary to the same node and remove the added (1<<i) again
if (!sz[child[x][k]]) k ^= 1, res ^= 1 << i;
Every time you go straight through 31 bits (no time is +0, no effect), res is the answer.
Complexity O (N*32)


#include <iostream> #include <cstdio> #include <cstring> using namespace std;
typedef long Long LL;

const int MAXN = 1e5 + 8;
int child[maxn*31][2], sz[maxn*31], tot = 1;
    inline void Modify (int val, int d) {int k, x = 1;
        for (int i = +; I >= 0; i--) {k = (val >> i) & 1;
        if (!child[x][k]) child[x][k] = ++tot;
        SZ[X] + = D;
    x = Child[x][k];
} Sz[x] + = D;
    } inline int query (int val) {int k, x = 1, res = 0;
        for (int i = +; I >= 0; i--) {k = ((val >> i) & 1) ^ 1, res ^= 1 << i;
        if (!sz[child[x][k]]) k ^= 1, res ^= 1 << i;
    x = Child[x][k];
} return res;
    } int main () {#ifdef LOCAL freopen ("F.txt", "R", stdin);
    Freopen ("F.txt", "w", stdout);
    int T = 1;

    while (t--) {#endif//LOCAL//ios::sync_with_stdio (FALSE); Cin.tie (0);
    int n, m, I, X;
    scanf ("%d", &n); for (i = 0; i < n; i++) {scanf ("%d", &amP;X);
    Modify (x, 1);
    } scanf ("%d", &m);
        for (i = 0; i < m; i++) {scanf ("%d", &x);
    printf ("%d\n", query (x));
    } #ifdef LOCAL cout << Endl;
} #endif//LOCAL return 0;
 }


Thank you!

------from Prolights

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.