#P6185. [NOI Online #1 提高组] 序列

[NOI Online #1 提高组] 序列

Problem Description

Little D has an integer sequence a1na_{1 \dots n} of length nn. She wants to transform it into the sequence bib_i through several operations.

Little D has mm types of operations to choose from. The ii-th operation can be described by a triple (ti,ui,vi)(t_i, u_i, v_i):

  • If ti=1t_i = 1, then she can either increase both auia_{u_i} and avia_{v_i} by 11, or decrease both by 11.
  • If ti=2t_i = 2, then she can either decrease auia_{u_i} by 11 and increase avia_{v_i} by 11, or increase auia_{u_i} by 11 and decrease avia_{v_i} by 11. Therefore, when ui=viu_i = v_i, this operation is equivalent to doing nothing.

Little D can perform the operations in any order, and each operation can be used an unlimited number of times. Now you are given the sequences and all operations. Please determine whether there exists a plan to transform aia_i into bib_i. It is guaranteed that both sequences have length nn. If such a plan exists, output YES; otherwise, output NO.

Input Format

This input contains multiple test cases.

The first line contains a positive integer TT, the number of test cases. For each test case:

  • The first line contains two integers n,mn, m, representing the sequence length and the number of operation types.
  • The second line contains nn integers representing aia_i.
  • The third line contains nn integers representing bib_i.
  • The next mm lines each contain three integers ti,ui,vit_i, u_i, v_i. Line ii describes operation ii.

Note: The same triple (ti,ui,vi)(t_i, u_i, v_i) may appear multiple times in the input.

Output Format

For each test case, output one line containing a string YES or NO, indicating the answer.

3
1 1
1
3
1 1 1
2 3
1 2
4 5
1 1 2
2 1 2
1 1 2
3 3
1 2 3
5 5 4
1 1 2
1 1 3
2 2 3
YES
YES
YES

Hint

Explanation for Sample 1

For the first test case: use operation 11 once.
For the second test case: use operation 11 three times.
For the third test case: use operation 11 three times to increase both a1a_1 and a2a_2 by 33, then use operation 22 once to increase both a1a_1 and a3a_3 by 11.


Constraints and Notes

For test points 151 \sim 5: n=2n = 2, m=1m = 1, ai,bi99a_i, b_i \le 99, u1v1u_1 \ne v_1, t1=1t_1 = 1.
For test points 6106 \sim 10: n=2n = 2, m=1m = 1, ai,bi99a_i, b_i \le 99, u1v1u_1 \ne v_1, t1=2t_1 = 2.
For test points 111211 \sim 12: n=2n = 2, ai,bi99a_i, b_i \le 99, uiviu_i \ne v_i.
For test points 131613 \sim 16: ti=2t_i = 2.
For test point 1717: n,m20n, m \le 20.
For test point 1818: n,m103n, m \le 10^3.
For all test points: 1T101 \le T \le 10, 1n,m1051 \le n, m \le 10^5, 1ai,bi1091 \le a_i, b_i \le 10^9, ti{1,2}t_i \in \{1, 2\}, 1ui,vin1 \le u_i, v_i \le n.

Translated by ChatGPT 5