CF1558D.Top-Notch Insertions

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Consider the insertion sort algorithm used to sort an integer sequence [a1,a2,…,an][a_1, a_2, \ldots, a_n] of length nn in non-decreasing order.

For each ii in order from 22 to nn, do the following. If ai≥ai−1a_i \ge a_{i-1}, do nothing and move on to the next value of ii. Otherwise, find the smallest jj such that ai<aja_i \lt a_j, shift the elements on positions from jj to i−1i-1 by one position to the right, and write down the initial value of aia_i to position jj. In this case we'll say that we performed an insertion of an element from position ii to position jj.

It can be noticed that after processing any ii, the prefix of the sequence [a1,a2,…,ai][a_1, a_2, \ldots, a_i] is sorted in non-decreasing order, therefore, the algorithm indeed sorts any sequence.

For example, sorting [4,5,3,1,3][4, 5, 3, 1, 3] proceeds as follows:

  • i=2i = 2: a2≥a1a_2 \ge a_1, do nothing;
  • i=3i = 3: j=1j = 1, insert from position 33 to position 11: [3,4,5,1,3][3, 4, 5, 1, 3];
  • i=4i = 4: j=1j = 1, insert from position 44 to position 11: [1,3,4,5,3][1, 3, 4, 5, 3];
  • i=5i = 5: j=3j = 3, insert from position 55 to position 33: [1,3,3,4,5][1, 3, 3, 4, 5].

You are given an integer nn and a list of mm integer pairs (xi,yi)(x_i, y_i). We are interested in sequences such that if you sort them using the above algorithm, exactly mm insertions will be performed: first from position x1x_1 to position y1y_1, then from position x2x_2 to position y2y_2, ..., finally, from position xmx_m to position ymy_m.

How many sequences of length nn consisting of (not necessarily distinct) integers between 11 and nn, inclusive, satisfy the above condition? Print this number modulo 998 244 353998\,244\,353.

考虑使用插入排序算法对长度为 nn 的整数序列 [a1,a2,…,an][a_1, a_2, \ldots, a_n] 进行非递减排序。

对每个 ii(按顺序从 22 到 nn),执行以下操作:若 ai≥ai−1a_i \ge a_{i-1},则不作任何操作,继续处理下一个 ii;否则,找出最小的 jj 使得 ai<aja_i \lt a_j,将位置 jj 到 i−1i-1 上的所有元素向右移动一位,并将 aia_i 的原始值写入位置 jj。此时称我们执行了一次从位置 ii 到位置 jj 的插入操作。

可以注意到,在处理完任意一个 ii 后,序列前缀 [a1,a2,…,ai][a_1, a_2, \ldots, a_i] 已是非递减有序的,因此该算法确实能对任意序列完成排序。

例如,对序列 [4,5,3,1,3][4, 5, 3, 1, 3] 排序的过程如下:

  • i=2i = 2:a2≥a1a_2 \ge a_1,不作操作;
  • i=3i = 3:j=1j = 1,从位置 33 插入到位置 11:[3,4,5,1,3][3, 4, 5, 1, 3];
  • i=4i = 4:j=1j = 1,从位置 44 插入到位置 11:[1,3,4,5,3][1, 3, 4, 5, 3];
  • i=5i = 5:j=3j = 3,从位置 55 插入到位置 33:[1,3,3,4,5][1, 3, 3, 4, 5]。

给定一个整数 nn 和一个包含 mm 个整数对 (xi,yi)(x_i, y_i) 的列表。我们关注满足如下条件的序列:当使用上述算法对其进行排序时,恰好执行 mm 次插入操作,且依次为:从位置 x1x_1 插入到位置 y1y_1,然后从位置 x2x_2 插入到位置 y2y_2,……,最后从位置 xmx_m 插入到位置 ymy_m。

有多少个长度为 nn 的序列(其元素为 11 到 nn 之间的整数,允许重复)满足上述条件?请输出该数目对 998 244 353998\,244\,353 取模的结果。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). Description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5; 0≤m<n0 \le m \lt n) — the length of the sequence and the number of insertions.

The ii-th of the following mm lines contains two integers xix_i and yiy_i (2≤x1<x2<…<xm≤n2 \le x_1 \lt x_2 \lt \ldots \lt x_m \le n; 1≤yi<xi1 \le y_i \lt x_i). These lines describe the sequence of insertions in chronological order.

It is guaranteed that the sum of mm over all test cases does not exceed 2⋅1052 \cdot 10^5. Note that there is no constraint on the sum of nn of the same kind.

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

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5;0≤m<n0 \le m \lt n)——分别表示序列的长度和插入操作的次数。

接下来的 mm 行中,第 ii 行包含两个整数 xix_i 和 yiy_i(2≤x1<x2<…<xm≤n2 \le x_1 \lt x_2 \lt \ldots \lt x_m \le n;1≤yi<xi1 \le y_i \lt x_i)。这些行按时间顺序描述插入操作序列。

保证所有测试用例的 mm 值之和不超过 2⋅1052 \cdot 10^5。注意:对所有测试用例的 nn 值之和并无类似限制。

输出格式

For each test case, print the number of sequences of length nn consisting of integers from 11 to nn such that sorting them with the described algorithm produces the given sequence of insertions, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出长度为 nn、由 11 到 nn 的整数构成的序列的个数(模 998 244 353998\,244\,353),使得使用所描述的算法对其进行排序时,恰好产生给定的插入序列。

输入输出样例

  • 输入#1

    3
    3 0
    3 2
    2 1
    3 1
    5 3
    3 1
    4 1
    5 3

    输出#1

    10
    1
    21

说明/提示

In the first test case, the algorithm performs no insertions — therefore, the initial sequence is already sorted in non-decreasing order. There are 1010 such sequences: [1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],[1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3][1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3].

In the second test case, the only sequence satisfying the conditions is [3,2,1][3, 2, 1].

In the third test case, [4,5,3,1,3][4, 5, 3, 1, 3] is one of the sought sequences.

在第一个测试用例中,该算法不执行任何插入操作——因此,初始序列本身已按非递减顺序排好序。满足条件的序列共有 1010 个:[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],[1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3][1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]。

在第二个测试用例中,唯一满足条件的序列是 [3,2,1][3, 2, 1]。

在第三个测试用例中,[4,5,3,1,3][4, 5, 3, 1, 3] 是所求序列之一。

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

首页