XOR key
Problem Statement :
Xorq has invented an encryption algorithm which uses bitwise XOR operations extensively. This encryption algorithm uses a sequence of non-negative integers as its key. To implement this algorithm efficiently, Xorq needs to find maximum value of for given integers , and , such that, . Help Xorq implement this function. For example, , , and . We test each for all values of between and inclusive: Function Description Complete the xorKey function in the editor below. It should return an integer array where each value is the response to a query. xorKey has the following parameters: x: a list of integers queries: a two dimensional array where each element is an integer array that consists of for the query at indices and respectively. Input Format The first line contains an integer , the number of test cases. The first line of each test case contains two space-separated integers and , the size of the integer array and the number of queries against the test case. The next line contains space-separated integers . Each of next lines describes a query which consists of three integers and . Output Format For each query, print the maximum value for , such that, on a new line.
Solution :
Solution in C :
In C :
#define NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct header {
int* head;
int count;
};
int find(const int* base, int count, int p, int q)
{
int left = 0;
int right = count;
int mid;
int r;
while (left < right) {
mid = (left + right) / 2;
r = base[mid];
if (r > q) {
right = mid; // else r <= q
} else if (r < p) {
left = mid + 1; // else p <= r <= q
} else {
return r;
}
}
return -1;
}
int main(void)
{
int T, t;
int N, n;
int Q, r;
int a, p, q;
struct header* info;
int* table;
int i, j;
int ret;
int bit;
int next_bit;
int low;
int hlow;
int parent;
int child_low;
int child_high;
int low_ind;
int high_ind;
info = (struct header*)malloc(sizeof(struct header) * 65535);
assert(info != NULL);
table = (int*)malloc(sizeof(int) * 1600000);
assert(table != NULL);
ret = scanf("%d", &T);
assert(ret == 1);
assert(1 <= T && T <= 6);
for (t = 0; t < T; ++t) {
ret = scanf("%d%d", &N, &Q);
assert(ret == 2);
assert(1 <= N && N <= 100000);
assert(1 <= Q && Q <= 50000);
info[0].head = table;
info[0].count = N;
low = 0;
for (n = 0; n < N; ++n) {
ret = scanf("%d", table + n);
assert(ret == 1);
assert(0 <= table[n] && table[n] < 0x8000);
if ((table[n] & 0x4000) == 0)
++low;
}
info[1].count = low;
info[2].count = N - low;
// first iteration
bit = 0x4000;
next_bit = bit >> 1;
info[1].head = table + N;
info[2].head = info[1].head + low;
low = 0;
hlow = 0;
low_ind = 0;
high_ind = 0;
for (i = 0; i < N; ++i) {
if ((table[i] & bit) == 0) {
info[1].head[low_ind++] = i;
if ((table[i] & next_bit) == 0)
++low;
} else {
info[2].head[high_ind++] = i;
if ((table[i] & next_bit) == 0)
++hlow;
}
}
assert(low_ind == info[1].count);
assert(high_ind == info[2].count);
info[3].count = low;
info[4].count = low_ind - low;
info[5].count = hlow;
info[6].count = high_ind - hlow;
parent = 0;
for (bit = next_bit; bit > 0; bit = next_bit) {
next_bit = bit >> 1;
for (i = 0; i < 0x4000; i += bit) {
++parent;
child_low = parent + parent + 1;
child_high = child_low + 1;
info[child_low].head = info[child_low - 1].head + info[child_low - 1].count;
info[child_high].head = info[child_low].head + info[child_low].count;
low = 0;
hlow = 0;
low_ind = 0;
high_ind = 0;
for (j = 0; j < info[parent].count; ++j) {
if ((table[info[parent].head[j]] & bit) == 0) {
info[child_low].head[low_ind++] = info[parent].head[j];
if ((table[info[parent].head[j]] & next_bit) == 0)
++low;
} else {
info[child_high].head[high_ind++] = info[parent].head[j];
if ((table[info[parent].head[j]] & next_bit) == 0)
++hlow;
}
}
assert(low_ind == info[child_low].count);
assert(high_ind == info[child_high].count);
if (next_bit > 0) {
j = child_low + child_low + 1;
info[j++].count = low;
info[j++].count = low_ind - low;
info[j++].count = hlow;
info[j++].count = high_ind - hlow;
}
}
}
for (r = 0; r < Q; ++r) {
ret = scanf("%d%d%d", &a, &p, &q);
assert(ret == 3);
assert(0 <= a && a < 0x8000);
assert(1 <= p && p <= q && q <= N);
--p;
--q;
i = 0;
for (bit = 0x4000; bit > 0; bit >>= 1) {
if ((a & bit) == 0) {
i = i + i + 2;
if (find(info[i].head, info[i].count, p, q) < 0)
--i;
} else {
i = i + i + 1;
if (find(info[i].head, info[i].count, p, q) < 0)
++i;
}
assert(find(info[i].head, info[i].count, p, q) >= 0);
}
j = find(info[i].head, info[i].count, p, q);
assert(j >= 0);
printf("%d\n", a ^ table[j]);
}
}
free(table);
free(info);
return 0;
}
Solution in C++ :
In C ++ :
#include <iostream>
#include <cstdio>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <ctime>
using namespace std;
const int MB = 14;
const int N = 100000;
int arr[N], t, n, q, a, b, c;
int sorted_arrs[N * (MB + 2)];
struct XorqNode {
int child[2], idx, len;
} nodes[512 + N * (MB - 6)]; //
int arr_cnt, node_cnt;
int split(int idx, int len, int bm, int r) {
int cnt = 0;
for(int i = 0; i < len; i ++) {
if((bm & arr[sorted_arrs[i + idx]]) == r) {
sorted_arrs[arr_cnt++] = sorted_arrs[i + idx];
cnt ++;
}
}
return cnt;
}
void build(int root, int idx, int len, int bit = MB) {
nodes[root].idx = idx;
nodes[root].len = len;
if(bit == -1) return;
int bm = (1 << bit);
int sidx1 = arr_cnt;
int left_cnt = split(idx, len, bm, 0);
int sidx2 = arr_cnt;
int right_cnt = split(idx, len, bm, bm);
assert(left_cnt + right_cnt == len);
if(left_cnt) {
nodes[root].child[0] = node_cnt;
build(node_cnt ++, sidx1, left_cnt, bit - 1);
} else {
nodes[root].child[0] = -1;
}
if(right_cnt) {
nodes[root].child[1] = node_cnt;
build(node_cnt ++, sidx2, right_cnt, bit - 1);
} else {
nodes[root].child[1] = -1;
}
}
bool search(int root, int from, int to) {
int left = nodes[root].idx;
int right = left + nodes[root].len - 1;
int r = N;
while(left <= right) {
int mid = (left + right) / 2;
int val = sorted_arrs[mid];
if(val >= from) {
r = val;
right = mid - 1;
} else {
left = mid + 1;
}
}
return r <= to;
}
int query(int root, int n, int from, int to, int bit = MB) {
if(bit == -1) return 0;
int mybit = ((1 << bit) & n) ? 1 : 0;
if(nodes[root].child[1 - mybit] != -1 && search(nodes[root].child[1 - mybit], from, to)) {
return query(nodes[root].child[1 - mybit], n, from, to, bit - 1) + (1 << bit);
} else {
return query(nodes[root].child[mybit], n, from, to, bit - 1);
}
}
int query2(int n, int from, int to) {
int r = 0;
for(int i = from; i <= to; i ++) {
r = max(r, arr[i] ^ n);
}
return r;
}
int main() {
int cl = clock();
int err_cnt = 0;
for(scanf("%d", &t); t--; ) {
arr_cnt = node_cnt = 0;
scanf("%d %d", &n, &q);
for(int i = 0; i < n; i ++) {
scanf("%d", &arr[i]);
}
for(int i = 0; i < n; i ++) {
sorted_arrs[arr_cnt++] = i;
}
node_cnt = 1;
build(0, 0, n);
for(int i = 0; i < q; i ++) {
scanf("%d %d %d", &a, &b, &c);
int r1 = query(0, a, b - 1, c - 1);
cout << r1 << endl;
}
}
//cerr << (clock() - cl) * 0.001 << endl;
return 0;
}
Solution in Java :
In Java :
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Solution {
private void solution() throws IOException {
int ts = in.nextInt();
while (ts-- > 0) {
int n = in.nextInt();
int q = in.nextInt();
int[] a = in.nextInts(n);
int size = 2 * Integer.highestOneBit(n);
int[][] sums = new int[size * 2][];
for (int i = 0; i < size; ++i) {
if (i < n) {
sums[i + size] = new int[] { a[i] };
} else {
sums[i + size] = new int[] {};
}
}
for (int i = size - 1; i > 0; --i) {
sums[i] = merge(sums[2 * i], sums[2 * i + 1]);
}
while (q-- > 0) {
int xor = in.nextInt();
int left = in.nextInt() - 1;
int right = in.nextInt() - 1;
int res = 0;
left += size;
right += size;
while (left <= right) {
if ((left & 1) != 0) {
res = Math.max(res, solve(sums[left++], xor));
}
if ((right & 1) == 0) {
res = Math.max(res, solve(sums[right--], xor));
}
left >>= 1;
right >>= 1;
}
out.println(res);
}
}
}
private int solve(int[] a, int xor) {
int left = 0;
int right = a.length - 1;
for (int bit = 14; bit >= 0; --bit) {
int middle = findMiddle(a, left, right, bit);
if (middle == left || middle > right) {
continue;
}
if (!get(xor, bit)) {
left = middle;
} else {
right = middle - 1;
}
}
if (a[left] != a[right]) {
throw null;
}
return a[left] ^ xor;
}
private int findMiddle(int[] a, int left, int right, int bit) {
while (left <= right) {
int middle = (left + right) >> 1;
if (!get(a[middle], bit)) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return left;
}
private boolean get(int set, int bit) {
return (((set >> bit) & 1) != 0);
}
private int[] merge(int[] a, int[] b) {
int[] res = new int[a.length + b.length];
int i = 0;
int j = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
res[i + j] = a[i];
++i;
} else {
res[i + j] = b[j];
++j;
}
}
while (i < a.length) {
res[i + j] = a[i];
++i;
}
while (j < b.length) {
res[i + j] = b[j];
++j;
}
return res;
}
public void run() {
try {
solution();
in.reader.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
private void debug(Object... objects) {
System.out.println(Arrays.toString(objects));
}
private static class Scanner {
private BufferedReader reader;
private StringTokenizer tokenizer;
public Scanner(Reader reader) {
this.reader = new BufferedReader(reader);
this.tokenizer = new StringTokenizer("");
}
public boolean hasNext() throws IOException {
while (!tokenizer.hasMoreTokens()) {
String line = reader.readLine();
if (line == null) {
return false;
}
tokenizer = new StringTokenizer(line);
}
return true;
}
public String next() throws IOException {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
tokenizer = new StringTokenizer("");
return reader.readLine();
}
public int[] nextInts(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; ++i) {
res[i] = nextInt();
}
return res;
}
public long[] nextLongs(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; ++i) {
res[i] = nextLong();
}
return res;
}
public double[] nextDoubles(int n) throws IOException {
double[] res = new double[n];
for (int i = 0; i < n; ++i) {
res[i] = nextDouble();
}
return res;
}
public String[] nextStrings(int n) throws IOException {
String[] res = new String[n];
for (int i = 0; i < n; ++i) {
res[i] = next();
}
return res;
}
}
public static void main(String[] args) throws Exception {
new Solution().run();
}
private Scanner in = new Scanner(new InputStreamReader(System.in));
private PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
Solution in Python :
In Python3 :
from sys import stderr
MAXBITS = 15
def main():
ncases = int(input())
for case in range(ncases):
arrsize, nqueries = readints()
arr = readints()
assert arrsize == len(arr)
finder = XORkeyfinder(arr)
for query in range(nqueries):
a, start, stop = readints()
print(finder.findmax(a, start-1, stop))
def readints():
return [int(f) for f in input().split()]
class XORkeyfinder:
def __init__(self, arr):
self.tbl = []
self.arr = arr
for i in range(MAXBITS):
shift = MAXBITS -i - 1
prev = [None] * (2 << i)
row = [len(arr)] * len(arr)
chain = [None] * len(arr)
for loc, x in enumerate(arr):
x = x >> shift
p = prev[x ^ 1]
while p is not None:
row[p] = loc
p = chain[p]
# chain[p] = None
prev[x ^ 1] = None
chain[loc] = prev[x]
prev[x] = loc
self.tbl.append(row)
# print(' '.join('%2d' % i for i in row), file=stderr)
def findmax(self, a, start, stop, MAXBITS=MAXBITS):
arr, tbl = self.arr, self.tbl
comp = ~a
best, bestloc = arr[start], start
for i, row in enumerate(tbl):
test = 1 << (MAXBITS - i - 1)
if comp & test != best & test:
newloc = row[bestloc]
if newloc < stop:
best, bestloc = arr[newloc], newloc
return best ^ a
main()
View More Similar Problems
Queue using Two Stacks
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que
View Solution →Castle on the Grid
You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):
View Solution →Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →Queries with Fixed Length
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon
View Solution →QHEAP1
This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element
View Solution →