Spectral::Cup 2026 Round 3 (Codeforces Round 1110, Div. 1 + Div. 2)
D2
构造数组(困难版)
Construct an Array (Hard Version)
Codeforces
2000
原题
← D1E →

构造数组(困难版)AI (CI翻译)

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

翻译中…

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

翻译中…
  • If $o=1$, the sum $a_i+a_j$ must be non-negative.
  • If $o=2$, the sum $a_i+a_j$ must be negative.
翻译中…

Input

输入格式

AI (CI翻译)

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

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

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

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

It is guaranteed that the sum of $m$ over all test cases does not exceed $10^6$.

每个测试包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。

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

接下来的 $m$ 行中,每行包含三个整数 $o$、$i$ 和 $j$($o \in \{1,2\}$,$1 \le i \le j \le n$),表示一条限制。

保证对于每一对满足 $1 \le i \le j \le n$ 的整数 $i$ 和 $j$,至多存在一条对应的限制。

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

保证所有测试用例的 $m$ 之和不超过 $10^6$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

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

对于每个测试用例,如果不存在满足条件的数组 $a$,输出“NO”。AI (CI翻译)

Otherwise, first output "YES" on a single line. Then output $n$ integers $a_1,a_2,\ldots,a_n$ ($|a_i| \le 10^9$), representing the array $a$ 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 $10^9$ in absolute value.

否则,首先在单独一行输出“YES”。然后输出 $n$ 个整数 $a_1,a_2,\ldots,a_n$($|a_i| \le 10^9$),表示你构造的数组 $a$。可以证明,在题目的限制下,如果存在满足条件的数组,那么一定存在一个所有元素绝对值都不超过 $10^9$ 的数组。AI (CI翻译)

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

如果存在多个满足条件的数组,可以输出其中任意一个。AI (CI翻译)

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.

输出的答案可以使用任意大小写。例如,字符串“yEs”、“yes”、“Yes”和“YES”都会被识别为肯定回答。AI (CI翻译)

Note

说明

AI (CI翻译)

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

在第一个测试用例中,唯一的限制是 $a_1+a_1$ 非负,这意味着 $a_1$ 非负。因此,$a_1$ 可以是任意非负整数。AI (CI翻译)

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

在第二个测试用例中,唯一的限制是 $a_1+a_1$ 为负数,这意味着 $a_1$ 为负数。因此,$a_1$ 可以是任意负整数。AI (CI翻译)

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

在第四个测试用例中,前两条限制意味着 $a_1$ 和 $a_2$ 都非负。然而,第三条限制要求 $a_1+a_2$ 为负数,这就产生了矛盾。AI (CI翻译)

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

在第九个测试用例中,没有任何限制。任意整数数组 $a$ 都是合法的。AI (CI翻译)

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

在第十个测试用例中,$[6,-6,4,-4,2,-2,0]$ 是一个合法解。注意,$[7,-6,5,-4, 3, -2,1]$ 也是一个合法解。AI (CI翻译)

样例

Input #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
Output #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
InputCopy OutputCopy
题目信息
题号
2245D2
时间限制
4 s
内存限制
1024 MB
标签
2-SAT
构造
DFS 及类似
图论
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。