DMOPC '17 Contest 5 P5 - XOR Bridges

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 2.5s
Memory limit: 256M

Problem types

In a distant land, there are N islands. Each island has a value of accessibility denoted by v_i. You are a bridge building contractor that has been given the task of trying to connect the islands by building bridges. However, because you want to save resources, you have decided to only build bridges such that the XOR of the accessibilities of the islands you connect with a bridge, is less than or equal to M. That is, you will build a bridge between island i and j if and only if v_i \oplus v_j \le M. Now, your boss has given you Q queries, each asking if islands p_i and q_i will be connected by the bridges.


For all subtasks:
1 \le N, Q \le 200\,000
0 \le M, v_i \le 10^9
1 \le p_i, q_i \le N

Subtask 1 [5%]

1 \le N, Q \le 100
0 \le M, v_i \le 100

Subtask 2 [5%]

1 \le N, Q \le 5\,000
0 \le M \le 5\,000

Subtask 3 [10%]

M=2^k-1 for some non-negative integer k and 0\le M \le 10^9

Subtask 4 [80%]

No additional constraints.

Input Specification

The first line contains three space-separated integers N, M, Q.
The second line contains N space-separated integers, v_i, denoting the accessibility of island i.
The next Q lines contain two space-separated integers p_i and q_i, a query asking if islands p_i and q_i are connected by a set of bridges.

Output Specification

For each of the queries, print YES if the islands are connected, or NO otherwise.

Sample Input

4 5 4
1 2 3 10
1 4
2 3
3 4
1 2

Sample Output



  • 0
    anasschoukri2  commented on March 15, 2018, 3:13 p.m.

    how can I deal with bad_alloc ??

    • 3
      r3mark  commented on March 15, 2018, 3:37 p.m.

      You're using too much memory. You can pass the first two subtasks by making your array sizes smaller, but your approach won't work for subtasks 3 and 4.

  • -2
    pulkitsja  commented on March 14, 2018, 11:01 p.m.

    Can anyone provide some ideas to approach this problem? Thanks.

    • 3
      r3mark  commented on March 14, 2018, 11:27 p.m.

      An editorial will be released sometime today or tomorrow.