DMOPC '17 Contest 5 P5 - XOR Bridges

View as PDF

Submit solution


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

Author:
Problem types

In a distant land, there are N islands. Each island has a value of accessibility denoted by vi. 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 vivjM. Now, your boss has given you Q queries, each asking if islands pi and qi will be connected by the bridges.

Constraints

For all subtasks:
1N,Q200000
0M,vi109
1pi,qiN

Subtask 1 [5%]

1N,Q100
0M,vi100

Subtask 2 [5%]

1N,Q5000
0M5000

Subtask 3 [10%]

M=2k1 for some non-negative integer k and 0M109.

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, vi, denoting the accessibility of island i.
The next Q lines contain two space-separated integers pi and qi, a query asking if islands pi and qi 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

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

Sample Output

Copy
NO
YES
NO
YES

Comments


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

    how can I deal with bad_alloc ??


    • 3
      r3mark  commented on March 15, 2018, 7: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.