A16383 | Satisfying Constraints
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Alex is solving a problem. He has $n$ constraints on what the integer $k$ can be. There are three types of constraints:
1. $k$ must be greater than or equal to some integer $x$ ;
2. $k$ must be less than or equal to some integer $x$ ;
3. $k$ must be not equal to some integer $x$ .
Help Alex find the number of integers $k$ that satisfy all $n$ constraints. It is guaranteed that the answer is finite (there exists at least one constraint of type $1$ and at least one constraint of type $2$ ). Also, it is guaranteed that no two constraints are the exact same.
1. $k$ must be greater than or equal to some integer $x$ ;
2. $k$ must be less than or equal to some integer $x$ ;
3. $k$ must be not equal to some integer $x$ .
Help Alex find the number of integers $k$ that satisfy all $n$ constraints. It is guaranteed that the answer is finite (there exists at least one constraint of type $1$ and at least one constraint of type $2$ ). Also, it is guaranteed that no two constraints are the exact same.
输入格式
Each test consists of multiple test cases. The first line contains a single integer $t$ ( $1 \leq t \leq 500$ ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ( $2 \leq n \leq 100$ ) — the number of constraints.
The following $n$ lines describe the constraints. Each line contains two integers $a$ and $x$ ( $a \in \{1,2,3\}, \, 1 \leq x \leq 10^9$ ). $a$ denotes the type of constraint. If $a=1$ , $k$ must be greater than or equal to $x$ . If $a=2$ , $k$ must be less than or equal to $x$ . If $a=3$ , $k$ must be not equal to $x$ .
It is guaranteed that there is a finite amount of integers satisfying all $n$ constraints (there exists at least one constraint of type $1$ and at least one constraint of type $2$ ). It is also guaranteed that no two constraints are the exact same (in other words, all pairs $(a, x)$ are distinct).
The first line of each test case contains a single integer $n$ ( $2 \leq n \leq 100$ ) — the number of constraints.
The following $n$ lines describe the constraints. Each line contains two integers $a$ and $x$ ( $a \in \{1,2,3\}, \, 1 \leq x \leq 10^9$ ). $a$ denotes the type of constraint. If $a=1$ , $k$ must be greater than or equal to $x$ . If $a=2$ , $k$ must be less than or equal to $x$ . If $a=3$ , $k$ must be not equal to $x$ .
It is guaranteed that there is a finite amount of integers satisfying all $n$ constraints (there exists at least one constraint of type $1$ and at least one constraint of type $2$ ). It is also guaranteed that no two constraints are the exact same (in other words, all pairs $(a, x)$ are distinct).
输出格式
For each test case, output a single integer — the number of integers $k$ that satisfy all $n$ constraints.
输入输出样例
输入 #1
6 4 1 3 2 10 3 1 3 5 2 1 5 2 4 10 3 6 3 7 1 2 1 7 3 100 3 44 2 100 2 98 1 3 3 99 6 1 5 2 10 1 9 2 2 3 2 3 9 5 1 1 2 2 3 1 3 2 3 3 6 1 10000 2 900000000 3 500000000 1 100000000 3 10000 3 900000001
输出 #1
7 0 90 0 0 800000000
In the first test case, $k \geq 3$ and $k \leq 10$ . Furthermore, $k \neq 1$ and $k \neq 5$ . The possible integers $k$ that satisfy the constraints are $3,4,6,7,8,9,10$ . So the answer is $7$ .
In the second test case, $k \ge 5$ and $k \le 4$ , which is impossible. So the answer is $0$ .
In the second test case, $k \ge 5$ and $k \le 4$ , which is impossible. So the answer is $0$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted