Codeforces Round 1112 (Div. 2)
C
排名子序列
Rank Subsequence
Codeforces
未评级
原题
← BD →

排名子序列AI (CI翻译)

You are given $n$ elements arranged in a line, numbered $1, 2, \ldots, n$ from left to right.

你有 $n$ 个元素排成一行,从左到右编号为 $1, 2, \ldots, n$。AI (CI翻译)

You may delete any number of elements (possibly none). The remaining elements form a subsequence and keep their relative order. Let the length of this subsequence be $m$. If the element with original index $i$ becomes the $j$-th element of the subsequence ($1 \le j \le m$), define

你可以删除任意数量的元素(也可以不删除)。剩余元素构成一个子序列,并保持它们的相对顺序。设该子序列的长度为 $m$。如果原下标为 $i$ 的元素成为子序列中的第 $j$ 个元素($1 \le j \le m$),则定义:AI (CI翻译)

  • its left rank as $j$, and
  • its right rank as $m - j + 1$.
  • 它的左排名为 $j$;
  • 它的右排名为 $m - j + 1$。
AI (CI翻译)

For each element, you are given two integer segments $[l_i, r_i]$ and $[u_i, v_i]$. The $i$-th element is valid in a subsequence of length $m$ if and only if, when it occupies position $j$ in that subsequence, both of the following hold:

对于每个元素,给定两个整数区间 $[l_i, r_i]$ 和 $[u_i, v_i]$。当第 $i$ 个元素位于子序列中的位置 $j$ 时,如果且仅如果以下两个条件同时满足,则称该元素在长度为 $m$ 的子序列中是有效的:AI (CI翻译)

  • Its left rank is not in the range $[l_i, r_i]$ (i.e., $j \notin [l_i, r_i]$);
  • Its right rank is not in the range $[u_i, v_i]$ (i.e., $m - j + 1 \notin [u_i, v_i]$).
  • 它的左排名不在区间 $[l_i, r_i]$ 中(即 $j \notin [l_i, r_i]$);
  • 它的右排名不在区间 $[u_i, v_i]$ 中(即 $m - j + 1 \notin [u_i, v_i]$)。
AI (CI翻译)

A subsequence is valid if every element remaining in it is valid.

如果子序列中剩余的每个元素都有效,则称该子序列是有效的。AI (CI翻译)

Find the maximum possible length of a valid subsequence. The answer may be $0$.

求有效子序列可能达到的最大长度。答案可能为 $0$。AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 5000$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains a single integer $n$ ($1\le n\le 5000$) — the number of elements.

每个测试用例的第一行包含一个整数 $n$($1\le n\le 5000$),表示元素的数量。AI (CI翻译)

Then $n$ lines follow, the $i$-th line containing the four integers $l_i$, $r_i$, $u_i$, and $v_i$ ($1\le i\le n$, $1\le l_i\le r_i\le n$, $1\le u_i\le v_i\le n$).

接下来 $n$ 行中,第 $i$ 行包含四个整数 $l_i$、$r_i$、$u_i$ 和 $v_i$($1\le i\le n$,$1\le l_i\le r_i\le n$,$1\le u_i\le v_i\le n$)。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $5000$.

保证所有测试用例的 $n$ 之和不超过 $5000$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, print one integer — the maximum possible length of a valid subsequence.

对于每个测试用例,输出一个整数,即有效子序列可能达到的最大长度。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, the only element cannot form a valid subsequence of length $1$, so the answer is $0$.

在第一个测试用例中,唯一的元素无法构成长度为 $1$ 的有效子序列,因此答案为 $0$。AI (CI翻译)

In the second test case, all $4$ elements can be kept. Their pairs of left and right ranks are $(1, 4)$, $(2, 3)$, $(3, 2)$, and $(4, 1)$, respectively, and all of them are valid.

在第二个测试用例中,可以保留全部 $4$ 个元素。它们的左排名和右排名分别为 $(1, 4)$、$(2, 3)$、$(3, 2)$ 和 $(4, 1)$,并且它们全部有效。AI (CI翻译)

In the third test case, one optimal choice is to keep the elements with original indices $2$, $3$, and $5$.

在第三个测试用例中,一种最优选择是保留原下标为 $2$、$3$ 和 $5$ 的元素。AI (CI翻译)

In the fourth test case, keeping both elements yields a valid subsequence of length $2$. Note that no valid subsequence of length $1$ exists.

在第四个测试用例中,保留两个元素可以得到一个长度为 $2$ 的有效子序列。注意,不存在长度为 $1$ 的有效子序列。AI (CI翻译)

In the fifth test case, one optimal choice is to keep the elements with original indices $3$, $4$, and $5$.

在第五个测试用例中,一种最优选择是保留原下标为 $3$、$4$ 和 $5$ 的元素。AI (CI翻译)

样例

Input #1
5
1
1 1 1 1
4
2 2 2 2
1 1 1 1
1 1 1 1
1 1 2 2
5
1 1 3 3
2 2 2 2
3 3 1 1
1 5 1 5
5 5 5 5
2
2 2 1 1
1 1 2 2
6
1 1 6 6
1 2 5 6
2 3 4 5
3 4 3 4
4 5 2 3
5 6 1 2
Output #1
0
4
3
2
3
InputCopy OutputCopy
题目信息
题号
2250C
时间限制
2 s
内存限制
256 MB

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