fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=1e9+7;
  5. const ll INF = 10000000000000;
  6. const int N = 1e6+7;
  7.  
  8. void solve() {
  9. int n,m,x;
  10. cin >> n >> x >> m;
  11. int mi = x, ma = x;
  12. while(m--){
  13. int a,b;cin >> a >> b;
  14. if(mi<=b ){
  15. mi=min(a,mi);
  16. ma=max(b,ma);
  17. }
  18. }
  19. cout << ma-mi+1 << '\n';
  20. }
  21. int main(){
  22. ios::sync_with_stdio(false);
  23. cin.tie(nullptr);
  24.  
  25. int t;
  26. cin >> t;
  27. while (t--) solve();
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 5312KB
stdin
3
6 4 3
1 6
2 3
5 5
4 1 2
2 4
1 2
3 3 2
2 3
1 2
stdout
6
4
3