#P5352. Terrible Homework

Terrible Homework

Description

Student benben hates doing homework the most.

Now, the teacher has assigned nn homework tasks to benben. Each task has a difficulty value AiA_i.

At the beginning, every homework task is independent. There are some operations: each time, you add an edge between two tasks x,yx, y or delete an edge.

Because the teacher’s mood changes often, there are also operations that apply xorxor with a value to the difficulty values of all tasks on the path between two tasks x,yx, y.

Also, to satisfy benben’s curiosity, you need to answer, for all tasks on the path between x,yx, y, the bitwise andand sum, bitwise oror sum, bitwise xorxor sum, and the ordinary sum of difficulty values.

Input Format

The first line contains two positive integers n,qn, q, representing the number of homework tasks and the number of operations.

The next line contains nn non-negative integers A1,...,AnA_1, ..., A_n, representing the difficulty value of each task.

Then follow qq lines, each describing one operation:

  • L x yL\ x\ y: Add an edge between tasks xx and yy. (It is guaranteed that no cycle will be formed, i.e. the graph is always a forest.)
  • C x yC\ x\ y: Delete the edge between xx and yy. (It is guaranteed that this edge exists and will not be deleted more than once.)
  • U x y vU\ x\ y\ v: Apply xorxor with vv to the difficulty values of all tasks on the path between xx and yy. (Including xx and yy, and it is guaranteed that they are connected; the same applies below.)
  • A x yA\ x\ y: Query the bitwise andand sum of the difficulty values of all tasks on the path between xx and yy.
  • O x yO\ x\ y: Query the bitwise oror sum of the difficulty values of all tasks on the path between xx and yy.
  • X x yX\ x\ y: Query the bitwise xorxor sum of the difficulty values of all tasks on the path between xx and yy.
  • S x yS\ x\ y: Query the ordinary sum of the difficulty values of all tasks on the path between xx and yy.

Output Format

For each A,O,X,SA, O, X, S operation, output the answer.

4 10
1 2 3 4
L 1 2
L 2 3
L 2 4
A 1 4
U 3 4 2
O 1 4
C 2 4
L 3 4
X 1 4
S 1 3
0
7
6
2

Hint

For 10%10\% of the testdata, n=100,q=100n = 100, q = 100.

For another 10%10\% of the testdata, n=5000,q=5000n = 5000, q = 5000.

For another 20%20\% of the testdata, n=10000,q=10000n = 10000, q = 10000.

For 100%100\% of the testdata, 2n,q1000002 \le n, q \le 100000, 0Ai<2300 \le A_i < 2^{30}.

Translated by ChatGPT 5