time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array consisting of nn integers a1a1, a2a2, ..., anan. Initially ax=1ax=1, all other elements are equal to 00.
You have to perform mm operations. During the ii-th operation, you choose two indices cc and dd such that li≤c,d≤rili≤c,d≤ri, and swap acac and adad.
Calculate the number of indices kk such that it is possible to choose the operations so that ak=1ak=1 in the end.
Input
The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then the description of tt testcases follow.
The first line of each test case contains three integers nn, xx and mm (1≤n≤1091≤n≤109; 1≤m≤1001≤m≤100; 1≤x≤n1≤x≤n).
Each of next mm lines contains the descriptions of the operations; the ii-th line contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n).
Output
For each test case print one integer — the number of indices kk such that it is possible to choose the operations so that ak=1ak=1 in the end.
Example
input
Copy
3 6 4 3 1 6 2 3 5 5 4 1 2 2 4 1 2 3 3 2 2 3 1 2
output
Copy
6 2 3
Note
In the first test case, it is possible to achieve ak=1ak=1 for every kk. To do so, you may use the following operations:
- swap akak and a4a4;
- swap a2a2 and a2a2;
- swap a5a5 and a5a5.
In the second test case, only k=1k=1 and k=2k=2 are possible answers. To achieve a1=1a1=1, you have to swap a1a1 and a1a1 during the second operation. To achieve a2=1a2=1, you have to swap a1a1 and a2a2 during the second operation.
解題說明:水題,只需要找到能交換數字的區間,然後計算即可。
#include<stdio.h>
int main()
{
int t, m;
long int n, x, l, r, ma, mi;
scanf("%d", &t);
while (t--)
{
scanf("%ld %ld %d", &n, &x, &m);
ma = x;
mi = x;
for (int i = 0; i<m; i++)
{
scanf("%ld %ld", &l, &r);
if (mi <= r)
{
if (mi > l)
{
mi = l;
}
}
if (ma >= l)
{
if (ma < r)
{
ma = r;
}
}
}
printf("%ld\n", ma - mi + 1);
}
return 0;
}