CF2245D2.Construct an Array (Hard Version)

普及+/提高

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The difference between the versions is that in this version, n≤2⋅105n \le 2 \cdot 10^5 and 0≤m≤min⁡(106,n(n+1)2)0 \le m \le \min(10^6,\frac{n(n+1)}{2}).

You are given two integers nn and mm. You need to construct an integer array aa of length nn that satisfies mm restrictions. Each restriction can be represented by a tuple (o,i,j)(o,i,j) such that o∈1,2o \in {1,2} and 1≤i≤j≤n1 \le i \le j \le n:

  • If o=1o=1, the sum ai+aja_i+a_j must be non-negative.
  • If o=2o=2, the sum ai+aja_i+a_j must be negative.

这是该问题的困难版本。两个版本的区别在于,在本版本中,n≤2⋅105n \le 2 \cdot 10^5 且 0≤m≤min⁡(106,n(n+1)2)0 \le m \le \min(10^6,\frac{n(n+1)}{2})。

给定两个整数 nn 和 mm。你需要构造一个长度为 nn 的整数数组 aa,使其满足 mm 个限制条件。每个限制条件可表示为一个三元组 (o,i,j)(o,i,j),其中 o∈{1,2}o \in \{1,2\} 且 1≤i≤j≤n1 \le i \le j \le n:

  • 若 o=1o=1,则和 ai+aja_i+a_j 必须非负;
  • 若 o=2o=2,则和 ai+aja_i+a_j 必须为负。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n≤2⋅1051 \le n \le \color{red}{2 \cdot 10^5}, 0≤m≤min⁡(106,n(n+1)2)0 \le m \color{red}{\le} \min(10^6, \frac{n(n+1)}{2})), representing the length of the array aa you need to construct and the number of restrictions, respectively.

Each of the next mm lines contains three integers oo, ii, and jj (o∈1,2o \in {1,2}, 1≤i≤j≤n1 \le i \le j \le n), representing a restriction. It is guaranteed that each pair of integers ii and jj such that 1≤i≤j≤n1 \le i \le j \le n occurs in at most one restriction.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

It is guaranteed that the sum of mm over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤2⋅1051 \le n \le \color{red}{2 \cdot 10^5},0≤m≤min⁡(106,n(n+1)2)0 \le m \color{red}{\le} \min(10^6, \frac{n(n+1)}{2})),分别表示你需要构造的数组 aa 的长度以及限制条件的数量。

接下来的 mm 行中,每行包含三个整数 oo、ii 和 jj(o∈{1,2}o \in \{1,2\},1≤i≤j≤n1 \le i \le j \le n),表示一条限制条件。保证对于任意满足 1≤i≤j≤n1 \le i \le j \le n 的整数对 (i,j)(i,j),至多出现在一条限制条件中。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

保证所有测试用例的 mm 之和不超过 10610^6。

输出格式

For each test case, if no such array aa exists, output "NO".

Otherwise, first output "YES" on a single line. Then output nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (∣ai∣≤109|a_i| \le 10^9), representing the array aa you constructed. It can be proven that under the problem constraints, if such an array exists, there exists one where all elements in the array do not exceed 10910^9 in absolute value.

If there exist multiple arrays satisfying the requirement, you may output any of them.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

对于每个测试用例,若不存在满足条件的数组 aa,则输出 "NO"。

否则,首先在单独一行输出 "YES";然后在下一行输出 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(满足 ∣ai∣≤109|a_i| \le 10^9),表示你构造出的数组 aa。可以证明:在本题约束条件下,若满足条件的数组存在,则必存在一个所有元素绝对值均不超过 10910^9 的解。

若存在多个满足条件的数组,你可以输出其中任意一个。

你可使用任意大小写形式输出答案(例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答)。

输入输出样例

  • 输入#1

    10
    1 1
    1 1 1
    1 1
    2 1 1
    2 3
    1 1 1
    1 1 2
    1 2 2
    2 3
    1 1 1
    1 2 2
    2 1 2
    3 6
    1 1 1
    1 1 2
    1 1 3
    2 2 2
    2 2 3
    2 3 3
    3 6
    2 1 1
    1 1 2
    2 2 3
    1 3 3
    1 2 2
    2 1 3
    2 1
    2 1 2
    3 4
    1 1 2
    1 2 3
    2 1 3
    2 2 2
    4 0
    7 7
    1 1 2
    2 2 3
    1 3 4
    2 4 5
    1 5 6
    2 6 7
    1 7 7

    输出#1

    YES
    0
    YES
    -1
    YES
    0 0
    NO
    YES
    1 -1 -1
    NO
    YES
    -1 -1
    NO
    YES
    0 0 0 0
    YES
    6 -6 4 -4 2 -2 0

说明/提示

In the first test case, the only restriction is that a1+a1a_1+a_1 is non-negative, implying that a1a_1 is non-negative. Thus, a1a_1 can be any non-negative integer.

In the second test case, the only restriction is that a1+a1a_1+a_1 is negative, implying that a1a_1 is negative. Thus, a1a_1 can be any negative integer.

In the fourth test case, the first and second restrictions imply that both a1a_1 and a2a_2 are non-negative. However, the third restriction forces a1+a2a_1+a_2 to be negative, which leads to a contradiction.

In the ninth test case, there are no restrictions. Any integer array aa is valid.

In the tenth test case, [6,−6,4,−4,2,−2,0][6,-6,4,-4,2,-2,0] is a valid solution. Note that [7,−6,5,−4,3,−2,1][7,-6,5,-4, 3, -2,1] is also a valid solution.

在第一个测试用例中,唯一的限制条件是 a1+a1a_1+a_1 非负,这意味着 a1a_1 非负。因此,a1a_1 可以是任意非负整数。

在第二个测试用例中,唯一的限制条件是 a1+a1a_1+a_1 为负数,这意味着 a1a_1 为负数。因此,a1a_1 可以是任意负整数。

在第四个测试用例中,前两个限制条件表明 a1a_1 和 a2a_2 均为非负数;然而,第三个限制条件要求 a1+a2a_1+a_2 为负数,这导致矛盾。

在第九个测试用例中,没有任何限制条件。任意整数数组 aa 均有效。

在第十个测试用例中,[6,−6,4,−4,2,−2,0][6,-6,4,-4,2,-2,0] 是一个有效解。注意,[7,−6,5,−4,3,−2,1][7,-6,5,-4,3,-2,1] 同样是一个有效解。

输入解题思路,AI测评打分。不知道怎么写?

首页