201,400 Degree Star The third question xor Sum (dictionary tree + XOR operation)

Source: Internet
Author: User

Xor Sum
Time limit:2000/1000 MS (java/others) Memory limit:132768/132768 K (java/others)
Total submission (s): 4445 Accepted Submission (s): 652

Problem Description
Zeus and Prometheus did a game. Prometheus gives Zeus a collection that contains n positive integers. Prometheus will then initiate m queries to Zeus, including a positive integer S in each query. After that, Zeus needs to find a positive integer k in the set, which makes K and S differ or result the most.

Prometheus to let Zeus see the greatness of mankind. Zeus was then allowed to turn to humanity for help. Can you prove the wisdom of man?

Input
The input includes several sets of test data, each set of test data including several rows. The first line of the input is an integer t (T < 10), which means that the group data is owned together. Enter two positive integers n,m (<1=n,m<=100000) for the first row of each set of data, followed by a row containing N positive integers, representing the obtained collection of Zeus, followed by M-lines, one positive integer per line. A positive integer representing the Prometheus query. All positive integers do not exceed 2^32.

Output
For each set of data, you first need to output a separate line "case #?"

: ", where the question mark should fill in the current number of data groups. The number of groups is calculated starting from 1.

For each query, the output is a positive integer k, which makes the K and S XOR the maximum value.

Sample Input
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3

Sample Output
Case #1:
4
3
Case #2:
4

Exercises

The amount of data is very large, feel the direct violence Time Complexity O (n*m) will time out. Just think of last year's online game a problem data volume is also very large direct violence actually over so directly knocked knock, finally or tle.

Want to place the general idea of operator operators, bitwise processing, the corresponding subject or relatively easy to think of the dictionary tree. Memory limit:132768/132768 K (java/others), prompting to attempt to sacrifice space for time. Direct dictionary tree space Consumption is 2^32, but the data relative to this size is still relatively small. Total n number, 32*n bit. So the spatial complexity is O (n). can accept. The dictionary tree is created by using the number of digits in the original sequence. It then runs on the dictionary tree using the number of questions to be queried and 0xFFFFFFFF XOR in the lookup process. Finally found is the largest.

If a branch by the XOR of the query does not exist. You want to have another branch. The answer may be small, but correct. The total time complexity O (M*log (32)). Constants can be counted.

#include <iostream> #include <cstring> #include <cstdio>using namespace Std;const int maxn=100000*32+ 100;__int64 Tree[maxn][2];__int64 node[maxn];__int64 tot;void Insert (__int64 a) {__int64 j;int bit,dep=0;for (j=31;j >=0;j--) {bit= (0x1<<j) &a)? 0x1:0x0;if (tree[dep][bit]==0) {Tree[dep][bit]=tot++;memset (TREE[TREE[DEP ][bit]], 0, sizeof (Tree[tree[dep][bit]));} Dep=tree[dep][bit];} Node[dep]=a;} __int64 Solve (__int64 val) {__int64 j,bit;int dep=0;for (j=31;j>=0;j--) {bit= ((0x1<<j) &val)? 0x0:0x1;if ( Tree[dep][bit]) Dep=tree[dep][bit];else dep=tree[dep][bit^1];} return NODE[DEP];} int main () {int Cas;__int64 n,m,j,s,a;int tag=0;cin>>cas;while (cas--) {tot=1;scanf ("%i64d%i64d", &n,&m) , Memset (tree[0],0,sizeof (tree[0)), while (n--) {scanf ("%i64d", &a); insert (a);} printf ("Case #%d:\n", ++tag), while (m--) {scanf ("%i64d", &s);p rintf ("%i64d\n", Solve (s));}} return 0;}




201,400 Degree Star The third question xor Sum (dictionary tree + XOR operation)

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.