UTS Open '18 P6 - Subset Sum

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 1.0s
Java 3.0s
Memory limit: 256M

Author:
Problem type

One day, PlasmaVortex gave insect a question to solve: the Subset Sum Problem! However, insect proved that it was NP-complete, so PlasmaVortex makes up a new problem about subset sums:

Each of the 2N (1N18) subsets of the set {1,2,,N} has an N-bit identifier s, where the ith bit (1iN) of s is 1 if the set contains i, and 0 if the set doesn't contain i. Each set also has a value Vs (0s<2N,1Vs106). There are Q queries that come in two different types:

  1. 1 s v The set whose N-bit identifier is s has its value changed to v. (0s<2N,1v106)
  2. 2 a b Let A and B be the sets with identifiers a and b (0a,b<2N). Output the sum of the values of all sets X such that AXB. (Output 0 if there are no such sets X).

Help insect solve this modified subset sum problem!

Input Specification

The first line contains N and Q. (1N18,1Q105)

The next line contains V0,V1,V2,,V2N1, the values of the 2N subsets of {1,2,,N}. (1V0,V1,V2,,V2N1106)

Each of the next Q lines contains a query in the format specified above.

Output Specification

Output the answer to each type 2 query on a separate line.

Constraints

Subtask 1 [20%]

1N10

Subtask 2 [30%]

a=0 for all type 2 queries.

Subtask 3 [50%]

No additional constraints.

Sample Input

Copy
3 4
1 1 2 3 5 8 13 21
2 4 7
2 1 2
1 3 7
2 1 3

Sample Output

Copy
47
0
8

Explanation for Sample Output

In the first query, a=4=1002 and b=7=1112 correspond to sets A={1} and B={1,2,3}. There are 4 possible sets X that satisfy AXB, which are {1},{1,2},{1,3},{1,2,3}, and the sum of their values is 5+13+8+21=47.

In the second query, a=1=0012 and b=2=0102, so A={3} and B={2}. No sets X satisfy AXB, so the answer is 0.

The third query changed the value of {2,3} to 7, and in the fourth query, the possible sets X with A={3}X{2,3} are X={3} and X={2,3}. The sum of their values is 1+7=8.


Comments

There are no comments at the moment.