luogu#P14978. [USACO26JAN1] Mooclear Reactor S

    ID: 15059 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>USACO广度优先搜索 BFS深度优先搜索 DFS二分图不定方程扫描线差分2026分类讨论

[USACO26JAN1] Mooclear Reactor S

Problem Description

Bessie is designing a nuclear reactor to power Farmer John's lucrative new AI data center business, CowWeave!

The reactor core consists of NN (1N21051\le N \le 2\cdot 10^5) fuel rods, numbered 11 through NN. The ii-th rod has a "stable operating range" [li,ri][l_i, r_i] (109liri109-10^9 \leq l_i \leq r_i \leq 10^9), meaning it can only generate power if its energy aia_i (chosen by Bessie) satisfies liairil_i \le a_i \le r_i; otherwise, it sits idle and does not generate power. Moreover, aia_i must always be an integer. Note that aia_i can be any integer, not limited to [109,109][-10^9, 10^9].

However, quantum interactions between the rods mean that there are MM constraints of the form (x,y,z)(x, y, z) where Bessie must satisfy ax+ay=za_x + a_y = z (1x,yN1 \leq x,y \leq N and 109z109-10^9\le z\le 10^9) to prevent the reactor from melting down.

Help Bessie find the maximum number of power-generating rods she can achieve in her design without it melting down!

Input Format

The first line contains TT (1T101\le T\le 10), the number of independent tests. Each test is specified in the following format:

  • The first line contains the two integers NN and MM.
  • The second line contains the NN integers l1,,lNl_1, \dots, l_N.
  • The third line contains the NN integers r1,,rNr_1, \dots, r_N.
  • The next MM lines each contain three integers xx, yy, and zz, each representing a constraint.

It is guaranteed that neither the sum of NN nor the sum of MM over all tests exceeds 41054\cdot 10^5.

Output Format

If no choice of rod energies exists that satisfies all constraints, output 1-1. Otherwise, output the maximum number of power-generating rods Bessie can achieve.

2
3 3
1 2 3
1 2 3
1 1 2
2 2 10
1 1 4
3 2
1 2 3
1 2 3
1 1 2
2 2 10
-1
2
1
3 2
10 -10 10
10 -10 10
1 2 0
2 3 0
3
5
3 3
1 -1 0
2 1 2
1 2 1
1 3 4
2 3 3
1 1
-100
100
1 1 3
1 1
-100
100
1 1 2
1 2
-100
100
1 1 2
1 1 4
1 2
-100
100
1 1 2
1 1 2
2
-1
1
-1
1

Hint

In the second test, the constraints require that:

  1. a1+a1=2a_1 + a_1 = 2
  2. a2+a2=10a_2 + a_2 = 10

Choosing energies a=[1,5,3]a=[1, 5, 3] results in 22 power-generating rods because:

  • l1=1a11=r1l_1 = 1 \leq a_1 \leq 1 = r_1
  • l3=3a33=r3l_3 = 3 \leq a_3 \leq 3 = r_3

and aa satisfies all required constraints.


Choosing rod energies a=[10,10,10]a=[10, -10, 10] results in 33 power-generating rods.


  • Input 4: x=yx = y for all constraints.
  • Inputs 5-7: xy=1|x-y|=1 for all constraints.
  • Inputs 8-10: xy1|x-y|\le 1 for all constraints.
  • Inputs 11-13: No additional conditions.