PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000195. Đảo màu đỉnh và tổng thành phần liên thông (Vertex Flip Query)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đảo màu đỉnh và tổng thành phần liên thông (Vertex Flip Query)

Vertex Flip Query

Source: AtCoder Beginner Contest 460 – G

Edition: Phuoc Hung OJ; original semantics and input format retained

Problem Statement

You are given an undirected tree of NN vertices numbered from 11 to NN. Every vertex vv has a positive integer weight WvW_v and a binary color Cv∈{0,1}C_v\in\{0,1\}.

The monochromatic connected component containing vv consists of all vertices uu such that every vertex on the unique path from vv to uu has color CvC_v. Vertex vv itself is included. A vertex of a different color blocks the path, even if further vertices share the original color.

Process QQ operations in the given order:

  • 1 v: Flip the color of vertex vv: Cv←1−CvC_v\leftarrow1-C_v. Its weight is unchanged.
  • 2 v x: Add xx to the weight of vertex vv: Wv←Wv+xW_v\leftarrow W_v+x. This is addition, not assignment.
  • 3 v: Output the sum of the weights of all vertices in the monochromatic connected component containing vv under the current colors and weights.

Every modification takes effect immediately. A query of type 3 never modifies the tree.

Input

  • The first line contains N,QN,Q.
  • The second line contains W1,…,WNW_1,\ldots,W_N.
  • The third line contains C1,…,CNC_1,\ldots,C_N.
  • Each of the next N−1N-1 lines contains an edge ai,bia_i,b_i.
  • The last QQ lines contain operations in one of the forms 1 v, 2 v x, 3 v.

Vertices are 1-indexed; there is exactly one test case.

Output

For each type-3 query, output one integer on a separate line in query order. If there are no such queries, output nothing. No modulus is applied; use a 64-bit integer.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le30; 1≤Q≤1001\le Q\le100; 1≤Wi≤1091\le W_i\le10^9; Ci∈{0,1}C_i\in\{0,1\}; the N−1N-1 edges satisfy 1≤ai<bi≤N1\le a_i<b_i\le N and form a connected tree; all queries satisfy 1≤v≤N1\le v\le N, and type-2 operations satisfy 1≤x≤1091\le x\le10^9.
  • Subtask 2 (30 points): 1≤N≤50001\le N\le5000; 1≤Q≤100001\le Q\le10000; 1≤Wi≤1091\le W_i\le10^9; Ci∈{0,1}C_i\in\{0,1\}; the N−1N-1 edges satisfy 1≤ai<bi≤N1\le a_i<b_i\le N and form a connected tree; all queries satisfy 1≤v≤N1\le v\le N, and type-2 operations satisfy 1≤x≤1091\le x\le10^9.
  • Subtask 3 (50 points): 1≤N≤3000001\le N\le300000; 1≤Q≤2000001\le Q\le200000; 1≤Wi≤1091\le W_i\le10^9; Ci∈{0,1}C_i\in\{0,1\}; the N−1N-1 edges satisfy 1≤ai<bi≤N1\le a_i<b_i\le N and form a connected tree; all queries satisfy 1≤v≤N1\le v\le N, and type-2 operations satisfy 1≤x≤1091\le x\le10^9.

Note: These are PHOJ training subtasks; the original AtCoder task specifies only the full constraints, not these three scoring groups.

Examples

Example 1

Input

5 9
1 10 100 1000 10000
0 0 0 0 0
1 2
2 3
3 4
2 5
3 1
1 2
3 1
3 2
3 3
1 3
1 2
2 1 1
3 5

Output

11111
1
10
1100
10012

Step-by-step explanation

  1. 3 1: all five vertices have color 00 and form one connected component. The sum is 1+10+100+1000+10000=111111+10+100+1000+10000=11111.
  2. 1 2: flip vertex 22 to color 11, separating vertex 11 from vertices 3,4,53,4,5.
  3. 3 1: the component is {1}\{1\} with sum 11.
  4. 3 2: the component is {2}\{2\} with sum 1010.
  5. 3 3: the component is {3,4}\{3,4\} with sum 100+1000=1100100+1000=1100.
  6. 1 3: flip vertex 33 to color 11, joining vertex 22 but separating vertex 44.
  7. 1 2: flip vertex 22 back to color 00; the component containing vertex 55 becomes {1,2,5}\{1,2,5\}.
  8. 2 1 1: increase the weight of vertex 11 from 11 to 22.
  9. 3 5: sum the weights of {1,2,5}\{1,2,5\} to get 2+10+10000=100122+10+10000=10012.

Only five queries are of type 3, so exactly five output lines appear.

Example 2

Input

10 25
1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000
0 1 0 0 0 1 0 0 0 0
1 2
1 3
2 4
4 5
4 6
2 7
5 8
8 9
6 10
3 4
1 8
2 6 100000
3 3
1 3
1 7
1 7
1 7
3 8
2 1 1
2 6 100000
2 8 10000000
1 5
2 5 10000
1 9
2 7 1000000
1 7
2 10 1000000000
1 9
1 6
1 9
1 1
3 7
2 5 10000
3 6

Output

110011000
101
10000000
2000000
2000301000

Step-by-step explanation

  • Query 1, 3 4: component {4,5,8,9}\{4,5,8,9\}, sum 1000+10000+10000000+100000000=1100110001000+10000+10000000+100000000=110011000.
  • Query 4, 3 3: after the preceding changes, component {1,3}\{1,3\}, sum 1+100=1011+100=101.
  • Query 9, 3 8: component {8}\{8\}, current weight 1000000010000000.
  • Query 23, 3 7: component {7}\{7\}, whose weight has increased to 20000002000000.
  • Query 25, 3 6: component {4,6,10}\{4,6,10\}, whose weights are now 1000,300000,20000000001000,300000,2000000000, totaling 20003010002000301000.