CF2146F.Bubble Sort

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You have just learned the algorithm bubble sort, which is able to sort an array in non-descending order. Let's define the function sort(a)\text{sort}(a) as in the following pseudocode:

function sort(array a):    rounds := 0    n := length of a    while a is not non-decreasing:        rounds := rounds + 1        for i from 1 to n - 1:            if a[i] > a[i + 1]:                swap(a[i], a[i + 1])    return rounds

As it is shown, the return value of sort(a)\text{sort}(a) represents the number of rounds needed to make array aa sorted in non-descending order by using bubble sort.

You are given an integer nn, as well as mm integer tuples (ki,li,ri)(k_i,l_i,r_i) (1≤i≤m1\le i\le m). Count the number of permutations∗^{\text{∗}} pp of length nn, modulo 998 244 353998\,244\,353, so that the following restrictions are satisfied:

  • For each 1≤i≤n1\le i\le n, let bi=sort([p1,p2,…,pi])b_i=\text{sort}([p_1,p_2,\ldots,p_{i}]), then
  • For each 1≤j≤m1\le j\le m, let xx be the number of indices yy (1≤y≤n1\le y\le n) such that by≤kjb_y\le k_j, then lj≤x≤rjl_j\le x\le r_j holds.

∗^{\text{∗}}A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

你刚刚学习了冒泡排序算法,该算法可以将一个数组按非降序排列。我们定义函数 sort(a)\text{sort}(a) 如下伪代码所示:

function sort(array a):    rounds := 0    n := length of a    while a is not non-decreasing:        rounds := rounds + 1        for i from 1 to n - 1:            if a[i] > a[i + 1]:                swap(a[i], a[i + 1])    return rounds

如上所示,sort(a)\text{sort}(a) 的返回值表示使用冒泡排序将数组 aa 变为非降序所需进行的轮数(round)。

给定一个整数 nn,以及 mm 个整数三元组 (ki,li,ri)(k_i,l_i,r_i)(其中 1≤i≤m1\le i\le m)。请计算满足以下约束条件的长度为 nn 的排列∗^{\text{∗}} pp 的个数,结果对 998 244 353998\,244\,353 取模:

  • 对每个 1≤i≤n1\le i\le n,令 bi=sort([p1,p2,…,pi])b_i=\text{sort}([p_1,p_2,\ldots,p_{i}]);
  • 对每个 1≤j≤m1\le j\le m,令 xx 表示满足 by≤kjb_y\le k_j 的下标 yy(其中 1≤y≤n1\le y\le n)的个数,则需满足 lj≤x≤rjl_j\le x\le r_j。

∗^{\text{∗}} 长度为 nn 的排列是指由 11 到 nn 这 nn 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

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 (2≤n≤1062\leq n\leq 10^6, 0≤m≤10000\leq m\leq 1000).

Then mm lines follow, the ii-th line containing three integers kik_i, lil_i, and rir_i (0≤ki≤n−10\le k_i\le n - 1, 1≤li≤ri≤n1\le l_i\le r_i\le n) — the restrictions.

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

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

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤1062\leq n\leq 10^6,0≤m≤10000\leq m\leq 1000)。

接下来是 mm 行,其中第 ii 行包含三个整数 kik_i、lil_i 和 rir_i(0≤ki≤n−10\le k_i\le n - 1,1≤li≤ri≤n1\le l_i\le r_i\le n)—— 表示限制条件。

保证所有测试用例中 m2m^2 的总和不超过 10610^6。

输出格式

For each test case, output a single integer — the number of possible permutations, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——可能的排列数对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#1

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

    输出#1

    2
    1
    8
    80
    192600
    373341033

说明/提示

In the first test case, only permutations [3,1,4,2][3,1,4,2] and [4,1,3,2][4,1,3,2] satisfy the restrictions. Take [3,1,4,2][3,1,4,2] as an example. Let's calculate the array bb:

  • sort([3])=0\text{sort}([3])=0;
  • sort([3,1])=1\text{sort}([3,1])=1;
  • sort([3,1,4])=1\text{sort}([3,1,4])=1;
  • sort([3,1,4,2])=2\text{sort}([3,1,4,2])=2.

Thus, b=[0,1,1,2]b=[0,1,1,2]. It is easy to show that it satisfies all the restrictions.

In the second test case, only permutation [1,2,3][1,2,3] satisfies the restrictions.

In the third test case, the following 88 permutations satisfy the restrictions:

  • [2,3,1,4][2,3,1,4];
  • [2,4,1,3][2,4,1,3];
  • [3,2,1,4][3,2,1,4];
  • [3,4,1,2][3,4,1,2];
  • [3,4,2,1][3,4,2,1];
  • [4,2,1,3][4,2,1,3];
  • [4,3,1,2][4,3,1,2];
  • [4,3,2,1][4,3,2,1].

在第一个测试用例中,仅有排列 [3,1,4,2][3,1,4,2] 和 [4,1,3,2][4,1,3,2] 满足限制条件。以 [3,1,4,2][3,1,4,2] 为例,我们来计算数组 bb:

  • sort([3])=0\text{sort}([3])=0;
  • sort([3,1])=1\text{sort}([3,1])=1;
  • sort([3,1,4])=1\text{sort}([3,1,4])=1;
  • sort([3,1,4,2])=2\text{sort}([3,1,4,2])=2。

因此,b=[0,1,1,2]b=[0,1,1,2]。容易验证其满足所有限制条件。

在第二个测试用例中,仅有排列 [1,2,3][1,2,3] 满足限制条件。

在第三个测试用例中,以下 88 个排列满足限制条件:

  • [2,3,1,4][2,3,1,4];
  • [2,4,1,3][2,4,1,3];
  • [3,2,1,4][3,2,1,4];
  • [3,4,1,2][3,4,1,2];
  • [3,4,2,1][3,4,2,1];
  • [4,2,1,3][4,2,1,3];
  • [4,3,1,2][4,3,1,2];
  • [4,3,2,1][4,3,2,1]。

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

首页