You are given an array consisting of n integers a1, a2, …, an. Initially ax=1, all other elements are equal to 0.

You have to perform m operations. During the i-th operation, you choose two indices c and d such that li≤c,d≤ri, and swap ac and ad.

Calculate the number of indices k such that it is possible to choose the operations so that ak=1 in the end.

Input
The first line contains a single integer t (1≤t≤100) — the number of test cases. Then the description of t testcases follow.

The first line of each test case contains three integers n, x and m (1≤n≤109; 1≤m≤100; 1≤x≤n).

Each of next m lines contains the descriptions of the operations; the i-th line contains two integers li and ri (1≤li≤ri≤n).

Output
For each test case print one integer — the number of indices k such that it is possible to choose the operations so that ak=1 in the end.

Example
Input
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
6
2
3
Note
In the first test case, it is possible to achieve ak=1 for every k. To do so, you may use the following operations:

swap ak and a4;
swap a2 and a2;
swap a5 and a5.
In the second test case, only k=1 and k=2 are possible answers. To achieve a1=1, you have to swap a1 and a1 during the second operation. To achieve a2=1, you have to swap a1 and a2 during the second operation.
思路:只要区间中包含1,那么这一个区间中所有的位置都有可能为1.设置左右两个指针,不断的更新有可能为1的区间长度就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e2+10;
struct node{int x,y;
}p[maxx];
int n,x,m;int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&x,&m);int l=x,r=x;for(int i=1;i<=m;i++){scanf("%d%d",&p[i].x,&p[i].y);if(!(p[i].x>r||p[i].y<l)) l=min(l,p[i].x),r=max(r,p[i].y);}cout<<r-l+1<<endl;}return 0;
}

努力加油a啊,(o)/~

Shuffle CodeForces - 1366B(思维)相关推荐

  1. CodeForces 798D 思维,贪心

    CodeForces 798D 题意:长度为 n的两个数组 a[]和 b[],要找出 k ( k<=n/2+1 )个下标,使得在两个数组中这 k个数的和乘上 2 要大于所有数的和. tags: ...

  2. B. Bogosort codeforces(思维)

    outputstandard output You are given an array a1,a2,-,an. Array is good if for each pair of indexes i ...

  3. codeforces - 1315C - 思维题

    原题链接:https://codeforces.com/problemset/problem/1315/C 翻译: 这是一个猜谜游戏,你需要猜中一个序列,谜题是一个序列,我们设为b,长度为n.你需要根 ...

  4. Balanced Substring CodeForces - 873B (思维+前缀和)

    Balanced Substring CodeForces - 873B You are given a string s consisting only of characters 0 and 1. ...

  5. Codeforces 1093C (思维+贪心)

    题面 传送门 题目大意: 有一个长n(n为偶数)的序列a 已知a满足 \(a_1≤a_2≤⋯≤a_n\) 给出一个长度为\(\frac{n}{2}\) 的序列b,定义\(b_i=a_i+a_{n-i+ ...

  6. Count Subrectangles CodeForces - 1323B(思维)

    You are given an array a of length n and array b of length m both consisting of only integers 0 and ...

  7. Dead Pixel CodeForces - 1315A(思维)

    Screen resolution of Polycarp's monitor is a×b pixels. Unfortunately, there is one dead pixel at his ...

  8. Three Integers CodeForces - 1311D(思维+暴力)

    You are given three integers a≤b≤c. In one move, you can add +1 or −1 to any of these integers (i.e. ...

  9. Interesting Array CodeForces - 483D(思维+线段树)

    We'll call an array of n non-negative integers a[1], a[2], -, a[n] interesting, if it meets m constr ...

最新文章

  1. 中国人民大学文继荣:搜索,从相关性到有用性
  2. Android分享功能
  3. 用sqlSessionFactoryBeanName而不用sqlSessionFactory的原因
  4. node创建web服务器代码示例
  5. mybatis完整增删改查入门实例
  6. solaris linux nfs,solaris 10 nfs服务配置
  7. kubernetes 简介:kube-dns 和服务发现
  8. 浅谈技术管理之个体能力建设
  9. Android 贝塞尔曲线
  10. Zabbix篇四:钉钉机器人报警
  11. 中国航空零部件制造行业业十四五投资发展规划及发展前景预测报告2022-2028年版
  12. shawn0102_播客:留下每年35万美元的工作以学习编码-Shawn Wang访谈
  13. 谈谈杭州市未来15年城市发展重点和杭州第二机场位置预测
  14. 为何要学习游戏引擎底层技术
  15. 千千静听播放器下载2015 v9.1.6 官网最新版
  16. (CVPR-2021)动态区域感知卷积
  17. 《SpringBoot实战》读书笔记
  18. Altium Designer 入门
  19. [Vue][面试]你怎么理解vue中的diff算法?
  20. 中台为什么做不好?拆系统“烟囱”容易,拆思维“烟囱”难!

热门文章

  1. TreeView,TreeNode,TreeNodeCollection 的使用
  2. 免师计算机与学前教育,2014届河南免师生源分布.doc
  3. linux shell date 1,linux shell date命令的坑(date 1 month ago 的诡异现象)
  4. pycharm同一目录下无法import明明已经存在的.py文件
  5. 四川大学计算机学院 期末考试安排,四川大学《计算机组成原理》2018期末考试B卷.doc...
  6. 支持字典_【多测师小课堂】python数据类型之列表、字典、元祖、字符串
  7. mysql 导出dmp文件_MySQL数据库基础讲解
  8. 如何更改指定用户在windows系统目录的权限
  9. win10下安装SQLServer2000
  10. Android开发之开发工具之Android Studio出现全屏的解决办法