text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; inline void fileio(const char* in, const char* out) { freopen(in, "r", stdin); freopen(out, "w", stdout); } typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> VI; typedef vector<bool> VB; typedef vector<ll> VL; const char nl = '\n'; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fll; const ld EPS = 1e-9, PI = acos(-1); void deal(int T) { int a, b, n; cin >> a >> b >> n; int ans; switch (n % 3) { case 0: ans = a; break; case 1: ans = b; break; case 2: ans = a ^ b; break; } cout << ans << nl; } int main() { atexit([]() { cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl; }); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; for (int i = 0; i < (T); ++i) deal(i); return 0; }
#include <bits/stdc++.h> using namespace std; double const EPS = 1e-8, PI = acos(-1); const int N = 2e3 + 9, M = 1e6 + 19, OO = 1e9 + 9; int main() { cout << fixed << setprecision(0), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); ; long long a, b, n; int t; cin >> t; while (t--) { cin >> a >> b >> n; long long arr[3]; arr[0] = a, arr[1] = b, arr[2] = (a ^ b); cout << arr[n % 3] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, a, b, n; scanf("%d", &t); for (; t--;) { scanf("%d%d%d", &a, &b, &n); n %= 3; if (n == 0) printf("%d\n", a); else if (n == 1) printf("%d\n", b); else printf("%d\n", a ^ b); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int a, b, t; cin >> a >> b >> t; if (t % 3 == 0) cout << a << endl; else if (t % 3 == 1) cout << b << endl; else cout << (a ^ b) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long a, b, n; cin >> a >> b >> n; n %= 3; if (n == 0) cout << a << "\n"; else if (n == 1) cout << b << "\n"; else cout << (a ^ b) << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { int t, T; cin >> T; for (t = 0; t < T; t++) { int a, b, n; cin >> a >> b >> n; int c = a ^ b; int z = n % 3; if (z == 0) { cout << a << endl; } else if (z == 1) { cout << b << endl; } else { cout << c << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, i; cin >> n; int l[n], r[n]; for (i = 0; i < n; i++) { cin >> l[i] >> r[i]; } if (n == 1) cout << 0 << endl; else { sort(l, l + n); sort(r, r + n); if (l[n - 1] > r[0]) { int ans = abs(r[0] - l[n - 1]); cout << ans << endl; } else cout << 0 << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n, t, a[100001], b[100001]; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } sort(a, a + n, greater<int>()); sort(b, b + n); if (n == 1 || (a[0] - b[0]) < 0) cout << 0 << endl; else cout << a[0] - b[0] << endl; } }
#include <bits/stdc++.h> int k; int n; int a, b; int maxla; int minrb; using namespace std; int main() { cin >> k; while (k--) { cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; if (i == 0) { maxla = a; minrb = b; } else { maxla = max(maxla, a); minrb = min(minrb, b); } } int ans = 0; if (maxla - minrb > 0) { ans = maxla - minrb; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; long long a, b; if (n == 1) { cin >> a >> b; cout << "0" << endl; return; } vector<pair<int, int> > f; for (int i = 0; i < n; i++) { cin >> a >> b; f.push_back({a, -1}); f.push_back({b, 0}); } sort(f.begin(), f.end()); long long zakr = 0, otkr = 0, l = 0, r = 0; for (int i = 0; i < f.size(); i++) { if (f[i].second == -1) { otkr++; if (otkr == n) { if (zakr == 0) { cout << "0" << endl; return; } else { r = f[i].first; break; } } if (zakr == 0) { l = f[i].first; } } else { if (zakr == 0) { l = f[i].first; } zakr++; } } cout << r - l << " "; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; while (n--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int i = 0; int main() { int t, n; cin >> t; for (int k = 0; k < t; k++) { int a = 1e9, b = 0, ans; cin >> n; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; if (r < a) { a = r; } if (l > b) { b = l; } } ans = b - a; if (ans < 0) { ans = 0; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } const int mod = 1e9 + 7; inline int add(int x, int y) { x += y; if (x >= mod) return x - mod; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) return x + mod; return x; } inline int mul(int x, int y) { return (x * 1ll * y) % mod; } inline int power(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = mul(ans, x); x = mul(x, x); y >>= 1; } return ans; } inline int inv(int x) { return power(x, mod - 2); } const long long FACTORIAL_SIZE = 1.1e6; long long fact[FACTORIAL_SIZE], ifact[FACTORIAL_SIZE]; void gen_factorial(long long n) { fact[0] = fact[1] = ifact[0] = ifact[1] = 1; for (long long i = 2; i <= n; i++) { fact[i] = (i * fact[i - 1]) % mod; } ifact[n] = inv(fact[n]); for (long long i = n - 1; i >= 2; i--) { ifact[i] = ((i + 1) * ifact[i + 1]) % mod; } } long long nck(long long n, long long k) { long long den = (ifact[k] * ifact[n - k]) % mod; return (den * fact[n]) % mod; } const int N = 2e5 + 5; void solve() { long long n; cin >> n; long long mxl = 0, mnr = 1e12; for (long long i = 0; i < n; i++) { long long x, y; cin >> x >> y; mxl = max(mxl, x); mnr = min(mnr, y); } cout << max(0ll, mxl - mnr) << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long tc = 1; cin >> tc; long long i = 1; while (tc--) { solve(); i++; } return 0; }
#include <bits/stdc++.h> using namespace std; class AMathProblem { public: void solve(std::istream& in, std::ostream& out) { long long n; in >> n; vector<pair<long long, long long> > data(2 * n); for (int i = 0; i < 2 * n; i++) { in >> data[i].first; data[i].second = i % 2; } sort(data.begin(), data.end()); long long left; long long right; for (pair<long long, long long> t : data) { if (t.second == 1) { left = t.first; break; } } reverse(data.begin(), data.end()); for (pair<long long, long long> t : data) { if (t.second == 0) { right = t.first; break; } } long long ans = max(0LL, (right - left)); out << ans << endl; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); AMathProblem solver; std::istream& in(std::cin); std::ostream& out(std::cout); int n; in >> n; for (int i = 0; i < n; ++i) { solver.solve(in, out); } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n, l, r; cin >> n; long long dl, dr; dl = 0, dr = 1e9 + 5; for (long long i = 0; i < n; i++) { cin >> l >> r; dl = max(dl, l), dr = min(dr, r); } cout << max(0ll, dl - dr) << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); ; int t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int Z = (int)1e5 + 111; const int inf = (int)1e9 + 111; const long long llinf = (long long)1e18 + 111; const long double pi = acos(-1.0); long long t, n, m; int main() { cin >> t; while (t-- > 0) { cin >> n; int mn = inf, mx = -inf; for (int i = 1, x, y; i <= n; i++) { cin >> x >> y; mn = min(y, mn); mx = max(x, mx); } if (n == 1) { cout << 0 << "\n"; continue; } if (mx - mn < 0) { cout << 0 << "\n"; } else { cout << mx - mn << "\n"; } } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t, a, b, n, mini = 1000000000000, maxi = 0; cin >> t; while (t--) { cin >> n; mini = 1000000000000; maxi = 0; while (n--) { cin >> a; cin >> b; if (a > maxi) { maxi = a; } if (b < mini) { mini = b; } } if (maxi - mini > 0) cout << maxi - mini << "\n"; else cout << 0 << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int dx[] = {1, 0, 0, -1}; int dy[] = {0, 1, -1, 0}; const int MOD = 1e9 + 7; long long power(long long x, long long y) { if (y == 0) return 1; long long s = power(x, y / 2); s *= s; if (y & 1) s *= x; return s; } long long Ceil(long long x, long long y) { return (x + y - 1) / y; } bool check_intersections(vector<pair<int, int>>& a, int n) { int cnt = 0; for (int i = 0; i < a.size(); i++) { if (cnt == n) return 1; if (a[i].second == 1) cnt++; if (a[i].second == -1) return 0; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { int n; cin >> n; vector<pair<int, int>> a(n), b; int minimum_end = 1e9, maximum_begin = -1; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; minimum_end = min(minimum_end, y); maximum_begin = max(maximum_begin, x); a[i] = {x, y}; } cout << max(0, maximum_begin - minimum_end) << "\n"; } cin.get(); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; vector<pair<long long, long long> > v(n); for (long long i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } sort(v.begin(), v.end()); long long a, b; a = v[n - 1].first; for (long long i = 0; i < n; i++) swap(v[i].first, v[i].second); sort(v.begin(), v.end()); b = v[0].first; if (a - b <= 0) cout << 0 << endl; else cout << a - b << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 50; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; long long maxL = 0; long long minR = LLONG_MAX; while (n--) { long long l, r; cin >> l >> r; maxL = max(l, maxL); minR = min(r, minR); } cout << max(0ll, -minR + maxL) << '\n'; } }
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { return b == 0 ? a : gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return a * (b / gcd(a, b)); } int dr[] = {+1, -1, +0, +0}; int dc[] = {+0, +0, +1, -1}; int X[] = {+0, +0, +1, -1, -1, +1, -1, +1}; int Y[] = {-1, +1, +0, +0, +1, +1, -1, -1}; int KX[] = {-2, -2, -1, -1, 1, 1, 2, 2}; int KY[] = {-1, 1, -2, 2, -2, 2, -1, 1}; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int rmin = INT_MAX, lmax = INT_MIN; while (n--) { int p, q; cin >> p >> q; lmax = max(lmax, p); rmin = min(rmin, q); } cout << max(0, lmax - rmin) << endl; } }
#include <bits/stdc++.h> #pragma optimize("SEX_ON_THE_BEACH") using namespace std; const long double pi = 3.1415926; const int MOD = (int)(1e9) + 7; const int INF = (int)(1e9) + 1e3; const long long LINF = (long long)(1e18) + 1e6; const int MAXN = (int)(1e5); const int MAXQ = (int)(1e5); void solve() { int n; cin >> n; vector<int> l(n); vector<int> r(n); for (int i = 0; i < n; ++i) { cin >> l[i] >> r[i]; } sort(l.begin(), l.end()); sort(r.rbegin(), r.rend()); cout << max(0, l.back() - r.back()) << '\n'; } void isolve() {} void generator() {} void validator() {} void stress() {} signed main() { ios_base::sync_with_stdio(false); srand(time(nullptr)); cout.setf(ios::fixed); cout.precision(10); cout.tie(nullptr); cin.tie(nullptr); int T = 1; cin >> T; while (T--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; string lowerCase = "abcdefghijklmnopqrstuvwxyz"; string upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const int fxKing[] = {+1, +1, +0, -1, -1, -1, +0, +1}; const int fyKing[] = {+0, +1, +1, +1, +0, -1, -1, -1}; const int fxKnight[] = {+2, +1, -1, -2, -2, -1, +1, +2}; const int fyKnight[] = {+1, +2, +2, +1, -1, -2, -2, -1}; bool isVowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'Y'); } bool isChar(char c) { return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); } long long t, q, m, n, a, b, c, x, y, cnt, sum, ck; long long odd, even, k, r, Mx, Mn; string s, ans; long long arr[100]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; Mx = 0, Mn = 1e9; for (int i = 0; i < n; i++) { cin >> x >> y; if (x > Mx) Mx = x; if (y < Mn) Mn = y; } long long ans = Mx - Mn; if (ans < 0) cout << 0 << endl; else cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("fast-math") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") const long double eps = 1e-7; const long long INF = 1e18; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; long long l[n], r[n]; long long n1 = INF, n2 = 0; for (long long i = 0; i < n; i++) { cin >> l[i] >> r[i]; n1 = min(n1, r[i]); n2 = max(n2, l[i]); } cout << max(0ll, n2 - n1) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void bboy() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long int find(double p) { return ceil(sqrt(2 * 365 * log(1 / (1 - p)))); } long long int power(long long int x, unsigned long long int y, long long int p) { long long int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } int32_t main() { bboy(); long long int t; cin >> t; while (t--) { long long int n; cin >> n; vector<pair<long long int, long long int> > v; for (long long int i = 0; i < n; ++i) { long long int l, r; cin >> l >> r; v.push_back({l, r}); } long long int mn = v[0].second, mx = v[0].first, posmn = 0, posmx = 0; for (long long int i = 0; i < n; ++i) { if (v[i].second < mn) { mn = v[i].second; posmn = i; } if (v[i].first > mx) { mx = v[i].first; posmx = i; } } long long int l = v[posmn].second, r = v[posmx].first; if (r - l > 0) cout << r - l << endl; else cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, e, f, g, mn, mx, l = -1, r = 1000000000, h[100000], h2[100000]; bool flag = 0; cin >> a; for (int k = 1; k <= a; k++) { l = -1, r = 1000000000; mn = 1e23; mx = 0; flag = 0; cin >> b; for (int i = 1; i <= b; i++) { cin >> h[i] >> h2[i]; c = h[i]; d = h2[i]; mn = min(d, mn); mx = max(c, mx); if (l < c) l = c; if (r > d) r = d; } if (r < l) { flag = 1; } else for (int i = 1; i <= b; i++) { if (l < h[i]) { flag = 1; break; } if (r > h2[i]) { flag = 1; break; } } if (flag == 0) { cout << 0 << '\n'; } else cout << mx - mn << '\n'; } }
#include <bits/stdc++.h> using namespace std; long long int t, n, l[1000001], p[1000001], maxl = 0, minp = 0; int main() { cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> l[i] >> p[i]; if (i == 0) minp = p[0]; if (l[i] > maxl) maxl = l[i]; if (p[i] < minp) minp = p[i]; } if (maxl - minp < 0) cout << "0" << endl; else cout << maxl - minp << endl; maxl = 0; minp = 0; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int INF = (long long)1e15; void solve() { int n; cin >> n; int l = 0, r = 2 * 1e9; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; l = max(l, a); r = min(r, b); } if (n == 1) cout << 0 << endl; else cout << max(0, l - r) << endl; } int main() { int t = 1; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long n; cin >> n; long long arr[n + 2], ar[n + 2], minn = 1000000000003, maxx = -1; for (int i = 0; i < n; i++) { cin >> arr[i] >> ar[i]; if (maxx < arr[i]) maxx = arr[i]; if (minn > ar[i]) minn = ar[i]; } if (n > 1 && maxx > minn) cout << maxx - minn << '\n'; else cout << 0 << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; long long n; long long ans[100]; long count = 0; long long m2 = 1000000001, b1 = 0; long long a, b; for (long long i = 0; i < t; i++) { cin >> n; for (long long k = 0; k < n; k++) { cin >> a >> b; if (a > b1) { b1 = a; } if (b < m2) { m2 = b; } } if (b1 - m2 > 0) { ans[count] = b1 - m2; count++; } else { ans[count] = 0; count++; } m2 = 1000000001, b1 = 0; } for (int i = 0; i < count; i++) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int t; cin >> t; long long maxleft = 0, minright = 1e9 + 1; for (int j = 0; j < t; j++) { long long a, b; cin >> a >> b; if (a >= maxleft) { maxleft = a; } if (b <= minright) { minright = b; } } if ((t == 1) || (maxleft - minright < 0)) { cout << 0 << endl; } else { cout << maxleft - minright << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; for (int j = 0; j < t; j++) { int n; cin >> n; int ansx = 0, ansy = 1e9 + 1; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; ansx = max(ansx, x); ansy = min(ansy, y); } cout << max((ansx - ansy), 0) << '\n'; ; } }
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; for (int i = 0; i < q; i++) { int n; cin >> n; int mi = 1000 * 1000 * 1000 + 10; int mi2 = 0; int ma = 0; int ma2 = 0; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; if (a > ma) { ma = a; } if (b < mi) { mi = b; } } if (ma > mi) { cout << ma - mi << endl; } else { cout << 0 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, l = 1e9, r = -1e9; cin >> n; pair<int, int> tp[n]; for (int i = 0; i < n; i++) { cin >> tp[i].first >> tp[i].second; l = min(l, tp[i].second); r = max(r, tp[i].first); } cout << max(r - l, 0) << endl; } }
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535897932238460; int main() { int t; cin >> t; while (t--) { long long a, b; long long c = 0, d = 1000000001; int n; cin >> n; if (n == 1) { cin >> a >> b; cout << 0 << "\n"; } else { while (n--) { cin >> a >> b; if (a > c) c = a; if (b < d) d = b; } if (c - d < 0) cout << 0 << "\n"; else cout << c - d << "\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; int arr1[n]; for (int i = 0; i < n; i++) { cin >> arr[i] >> arr1[i]; } sort(arr, arr + n); sort(arr1, arr1 + n); int x = arr[n - 1] - arr1[0]; if (x > 0) cout << arr[n - 1] - arr1[0] << endl; else cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; long long int s[t]; for (int y = 0; y < t; y++) { long long int n; cin >> n; long long int a[n], b[n], bmin, amax; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; if (i == 0) { bmin = b[i]; amax = a[i]; } else if (bmin > b[i]) bmin = b[i]; if (amax < a[i]) amax = a[i]; } if (amax > bmin) s[y] = amax - bmin; else s[y] = 0; } for (int i = 0; i < t; i++) { cout << s[i] << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long lm = 0, rm = 0; for (long long i = 0; i < n; i++) { pair<long long, long long> a; cin >> a.first >> a.second; if (i == 0) { lm = a.second; rm = a.first; } else { lm = min(lm, a.second); rm = max(rm, a.first); } } long long m = 0; cout << max(m, rm - lm) << endl; } return 0; }
#include <bits/stdc++.h> using segment_t = std::pair<unsigned, unsigned>; template <typename T, typename U> std::istream& operator>>(std::istream& input, std::pair<T, U>& v) { return input >> v.first >> v.second; } template <typename T> std::istream& operator>>(std::istream& input, std::vector<T>& v) { for (T& a : v) input >> a; return input; } void answer(unsigned v) { std::cout << v << '\n'; } void solve(std::vector<segment_t>& s) { const size_t n = s.size(); segment_t t = {s[0].second, s[0].first}; for (size_t i = 1; i < n; ++i) { t.first = std::min(t.first, s[i].second); t.second = std::max(t.second, s[i].first); } answer(t.first < t.second ? t.second - t.first : 0); } void test_case() { size_t n; std::cin >> n; std::vector<segment_t> s(n); std::cin >> s; solve(s); } int main() { size_t t; std::cin >> t; while (t-- > 0) test_case(); return 0; }
#include <bits/stdc++.h> using namespace std; struct shitClass {}; template <class T> shitClass& operator<<(shitClass& c1, const T&) { return c1; }; shitClass cerr1; ostream& operator<<(ostream& os, const vector<long long>& a) { for (int i = 0; i < a.size(); i++) os << a[i] << " "; return os; } ostream& operator<<(ostream& os, const pair<long long, long long>& a) { os << a.first << " " << a.second; return os; } ostream& operator<<(ostream& os, const vector<int>& a) { for (int i = 0; i < a.size(); i++) os << a[i] << " "; return os; } istream& operator>>(istream& is, pair<long long, long long>& a) { is >> a.first >> a.second; return is; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; for (int counter = 0; counter < t; counter++) { long long n; cin >> n; vector<pair<long long, long long> > a(n); for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; long long mnR = (long long)(1e17 + 10), mxL = -(long long)(1e17 + 10); for (int i = 0; i < n; i++) { mnR = min(mnR, a[i].second); mxL = max(mxL, a[i].first); } cout << max(0ll, mxL - mnR) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxN = 100000; int l[maxN]; int r[maxN]; int t; int n; int main() { scanf("%d", &t); for (t; t > 0; t--) { scanf("%d", &n); int L = -1e9, R = 1e9; for (int i = 0; i < n; i++) { scanf("%d %d", &l[i], &r[i]); if (r[i] < R) R = r[i]; if (l[i] > L) L = l[i]; } if (L < R) printf("0\n"); else printf("%d\n", L - R); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long x = INT_MAX, y = INT_MIN; long long ans = 0; for (long long i = 0; i < n; i++) { long long a, b; cin >> a >> b; x = min(x, b); y = max(y, a); } ans = max(ans, y - x); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; long long a, b; if (n == 1) { cin >> a >> b; cout << "0" << endl; return; } vector<pair<int, int> > f; for (int i = 0; i < n; i++) { cin >> a >> b; f.push_back({a, -1}); f.push_back({b, 0}); } sort(f.begin(), f.end()); long long zakr = 0, otkr = 0, l = 0, r = 0; for (int i = 0; i < f.size(); i++) { if (f[i].second == -1) { otkr++; if (otkr == n) { if (zakr == 0) { cout << "0" << endl; return; } else { r = f[i].first; break; } } if (zakr == 0) { l = f[i].first; } } else { if (zakr == 0) { l = f[i].first; } zakr++; } } cout << r - l << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; while (n--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { int n, t, l[100000], r[100000], ma, mi, i; cin >> t; while (t--) { cin >> n; for (i = 0; i < n; i++) { cin >> l[i] >> r[i]; } if (n == 1) { cout << "0\n"; } else { ma = l[0]; for (i = 1; i < n; i++) { if (ma < l[i]) ma = l[i]; } mi = r[0]; for (i = 1; i < n; i++) { if (mi > r[i]) mi = r[i]; } if (ma - mi <= 0) cout << "0" << endl; else cout << ma - mi << endl; } } }
#include <bits/stdc++.h> using namespace std; long double PI = acosl(-1); bool compare_int(int a, int b) { return (a > b); } bool compare_string(string a, string b) { return a.size() < b.size(); } bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { if (a.second == b.second) return a.first > b.first; else return (a.second > b.second); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, n, x, y; cin >> t; while (t--) { vector<pair<int, int> > a; cin >> n; int kace = INT_MAX; for (int i = 0; i < n; i++) { cin >> x >> y; a.push_back({x, y}); kace = min(kace, y); } sort(a.begin(), a.end()); if (a[n - 1].first > kace) { cout << a[n - 1].first - kace << endl; } else cout << 0 << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ul = unsigned long long; using ld = long double; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll t, n, ml, mr, l, r; cin >> t; for (int z = 0; z < (t); z++) { cin >> n; cin >> ml >> mr; for (int i = 0; i < (n - 1); i++) { cin >> l >> r; ml = max(ml, l); mr = min(mr, r); } cout << max(ml - mr, 0ll) << '\n'; } }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 1; void solve() { int n; cin >> n; int l[n], r[n]; int mil = INF, mar = -INF; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; if (mil > r[i]) mil = r[i]; if (mar < l[i]) mar = l[i]; } if (mar < mil) cout << 0; else cout << mar - mil; } int main() { int t; cin >> t; while (t--) { solve(); cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; for (long long i = 0; i < n; i++) { long long k; cin >> k; vector<vector<long long>> q(k); for (long long j = 0; j < k; j++) { q[j].resize(2); cin >> q[j][0] >> q[j][1]; } long long low = 9999999999; for (long long j = 0; j < k; j++) if (q[j][1] < low) low = q[j][1]; long long high = 0; for (long long j = 0; j < k; j++) if (q[j][0] > high) high = q[j][0]; if (high - low < 0) cout << 0 << endl; else cout << high - low << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int eps = 1e-10; const long long INF = 1e9 + 10; int n, m, t, l, r, x, q, k, pos; int a[N]; string s; vector<pair<int, int>> v; void solve() { int n, l, r; cin >> n; vector<pair<int, int>> sc; for (int i = 0; i < n; ++i) { cin >> l >> r; sc.push_back({l, 1}); sc.push_back({r, -1}); } sort(sc.begin(), sc.end(), [](pair<int, int> a, pair<int, int> b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; }); int f = 0; l = 0; r = 0; for (int i = 0; i < sc.size(); ++i) { if (sc[i].second == -1 && f == 0) l = sc[i].first; if (sc[i].second == 1 && f) r = sc[i].first; if (sc[i].second == -1) f = 1; } if (r == 0) cout << 0 << "\n"; else cout << abs(l - r) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> int t, n, l, r, minr, maxl, i, j; int main() { scanf("%d", &t); for (i = 1; i <= t; i++) { scanf("%d", &n); minr = 1000000001; maxl = 0; for (j = 1; j <= n; j++) { scanf("%d %d", &l, &r); if (l > maxl) maxl = l; if (r < minr) minr = r; } if (maxl >= minr) printf("%d\n", maxl - minr); else printf("0\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long l = 0, r = 1000000007; for (int i = 0; i < n; i++) { long long a, b; cin >> a >> b; l = max(l, a); r = min(r, b); } if (n != 1) { cout << (l > r ? (l - r) : 0) << endl; } else cout << 0 << endl; } }
#include <bits/stdc++.h> using namespace std; bool prime(long long int n) { bool x = true; int i; for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) { x = false; } } return x; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long int co = 0, i, n, y, m, d, j, k, t, x = 0; cin >> t; while (t--) { cin >> n; long long int l, r; long long int lma = 0, rmi = 9999999999; for (i = 0; i < n; i++) { cin >> x >> y; lma = max(x, lma); rmi = min(rmi, y); } cout << max((long long int)0, lma - rmi) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); const int inf = 1e9; int tt; cin >> tt; while (tt--) { int n; cin >> n; int mxl = -inf, mnr = inf; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; mxl = max(mxl, a); mnr = min(mnr, b); } if (n == 1) { cout << "0\n"; } else { cout << max(0, mxl - mnr) << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); void solve() { int n; cin >> n; int lef = 1e9 + 1, rig = 0; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; lef = min(lef, y); rig = max(rig, x); } if (lef <= rig) { cout << rig - lef << "\n"; } else { cout << 0 << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t; cin >> t; while (t-- > 0) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; ++i) { int n; cin >> n; int mn = 1e9 + 1, mx = 0; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; mn = min(mn, b); mx = max(mx, a); } cout << max(0, mx - mn) << '\n'; } }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; using pii = pair<int, int>; const int MOD = 1e9 + 7, N = 2e3 + 10; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long n; cin >> n; long long m1 = 1e9, m2 = 0; while (n--) { long long x, y; cin >> x >> y; m1 = min(m1, y); m2 = max(m2, x); } if (m2 > m1) cout << m2 - m1 << "\n"; else cout << 0 << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; vector<pair<long long int, long long int>> v; for (long long int i = 0; i < n; i++) { long long int x, y; cin >> x >> y; v.push_back(make_pair(x, y)); } long long int min = LLONG_MAX, max = LLONG_MIN; for (auto it = v.begin(); it != v.end(); it++) { if (it->second < min) min = it->second; if (it->first > max) max = it->first; } if (min >= max) cout << "0" << endl; else cout << abs(min - max) << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; for (long long int tt = 0; tt < t; tt++) { long long int n; cin >> n; long long int l[n], r[n]; for (long long int i = 0; i < n; i++) cin >> l[i] >> r[i]; if (n == 1) { cout << '0' << "\n"; } else { sort(l, l + n); sort(r, r + n); if (l[n - 1] >= r[0]) cout << l[n - 1] - r[0] << "\n"; else cout << '0' << "\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15); long long t, n, l, r; cin >> t; while (t--) { cin >> n; long long mn = 1e18, mx = -1e18; for (int i = (0); i <= ((n)-1); i += (+1)) { cin >> l >> r; mx = max(mx, l); mn = min(mn, r); } cout << max(0ll, mx - mn) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int64_t INF = 10000000000013LL; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long lmx = INT_MIN, rmin = INT_MAX, x, y; while (n--) { cin >> x >> y; if (x > lmx) lmx = x; if (y < rmin) rmin = y; } if (lmx - rmin <= 0) cout << 0 << endl; else cout << (lmx - rmin) << endl; } }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(time(0)); void solve() { int n; cin >> n; pair<int, int> a[n]; for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second; sort(a, a + n); int min = 1e9, max = -1e9; for (int i = 0; i < n; ++i) { min = std::min(min, a[i].second); max = std::max(max, a[i].first); } cout << std::max(0, max - min) << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long q = 239017; const long long mod = 1e9 + 7; const long long mod2 = 1e9 + 13; const long long MAXN = 1e5 + 100; const long long MAXM = 4294967291; const long long L = 20; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void $main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long mx = -(long long)1e18, mn = (long long)1e18; for (long long i = 0; i < n; ++i) { long long l, r; cin >> l >> r; mx = max(mx, l); mn = min(mn, r); } cout << max(0ll, mx - mn); cout << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); $main(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_NUM = 1e9 + 33; const double EPS = 1e-3; const double PI = acos(-1); int main() { int t; cin >> t; while (t--) { int n; cin >> n; int r_st = -MAX_NUM, l_end = MAX_NUM; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; r_st = max(r_st, a); l_end = min(l_end, b); } cout << max(r_st - l_end, 0) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; long long min_r = 1e9 + 9, max_l = 0; for (int i = 0; i < n; i++) { long long l, r; cin >> l >> r; min_r = min(min_r, r); max_l = max(max_l, l); } cout << max(max_l - min_r, 0ll) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; void re() {} template <typename T, typename... args> void re(T &x, args &...tail) { cin >> x; re(tail...); } void pr() {} template <typename T> void pr(const T &x) { cout << x; } template <typename T, typename... args> void pr(const T &x, const args &...tail) { cout << x << " "; pr(tail...); } template <typename... args> void prln(const args &...tail) { pr(tail...); cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int l[n], r[n]; for (long long int i = 0; i < n; i++) { cin >> l[i] >> r[i]; } sort(l, l + n); sort(r, r + n); cout << max(0ll, l[n - 1] - r[0]) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; pair<int, int> a[100000]; int d[100000]; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; if (n == 1) { cout << 0 << endl; continue; } int min = 2e9, max = -1; for (int i = 0; i < n; i++) { if (a[i].first > max) max = a[i].first; if (a[i].second < min) min = a[i].second; } if (max - min < 0) cout << 0; else cout << max - min; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, t; int main() { cin >> t; while (t--) { cin >> n; int min_ = 1000000005, max_ = 0; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; min_ = min(min_, b); max_ = max(max_, a); } cout << max(0, max_ - min_) << endl; } }
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<int, int>> v(n); int left = 1e9 + 1, right = -1; for (int i = 0; i < n; ++i) { cin >> v[i].first >> v[i].second; left = min(left, v[i].second); right = max(right, v[i].first); } if (n == 1) { cout << 0 << "\n"; continue; } int ans = right - left; cout << max(0, ans) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e3 * 2 + 1; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int ansx = 1e9 + 7, ansy = 0; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; ansx = min(y, ansx); ansy = max(x, ansy); } cout << max(ansy - ansx, 0) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 1e18; int main() { ios::sync_with_stdio(0); int t; cin >> t; for (int z = 0; z < t; z++) { int n; cin >> n; vector<pair<int, int> > a(n); for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } int mx = -1, mn = 1000000007; for (int i = 0; i < n; i++) { mx = max(mx, a[i].first); mn = min(mn, a[i].second); } cout << max(0, mx - mn) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 2; const int M = 1e5 + 2; const int mod = 1e9 + 7; const int inf = 1e9; const long long INF = 1e18; void data() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); } int t, n; pair<int, int> p[N]; int main() { data(); scanf("%d", &t); while (t--) { scanf("%d", &n); int ml = 0, mr = inf; for (int i = 0, l, r; i < n; ++i) { scanf("%d %d", &l, &r); ml = max(ml, l); mr = min(mr, r); } if (ml < mr) printf("0\n"); else printf("%d\n", ml - mr); } }
#include <bits/stdc++.h> using namespace std; void op() { int n; cin >> n; vector<pair<long long int, long long int>> v(n); for (int i = 0; i < n; i++) { long long int a, b; cin >> a >> b; v[i] = {a, b}; } sort(v.begin(), v.end()); long long int mi = v[0].second, mx = v[n - 1].first; for (int i = 0; i < n; i++) { mi = min(mi, v[i].second); } int p = 0; for (int i = 0; i < n; i++) { if (mx < v[i].first || mx > v[i].second) break; p++; } if (p == n) cout << 0; else cout << abs(mx - mi); } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { op(); cout << "\n"; } }
#include <bits/stdc++.h> using namespace std; mt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count()); signed main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int minim = 1e9, maxim = 0; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; minim = min(minim, r); maxim = max(maxim, l); } cout << max(0, maxim - minim) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, max = 0, min = 1000000000; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; if (n == 1) cout << 0 << endl; else { for (int i = 0; i < n; i++) { max = a[i] > max ? a[i] : max; min = b[i] < min ? b[i] : min; } if (min < max) cout << abs(max - min) << endl; else cout << 0 << endl; } } }
#include <bits/stdc++.h> using namespace std; int32_t main() { long long t, n, i, j; cin >> t; while (t--) { cin >> n; long long t1, t2, leftmax = 0, rightmin = INT_MAX; long long flag = 0; for (i = 0; i < n; i++) { cin >> t1 >> t2; if (n == 1) { cout << "0\n"; flag = 1; break; } leftmax = max(leftmax, t1); rightmin = min(rightmin, t2); } if (flag == 1) continue; long long val = leftmax - rightmin; if (val <= 0) cout << "0\n"; else cout << val << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; for (long long p = 0; p < t; p++) { long long n; cin >> n; vector<vector<int>> a(n, vector<int>(2)); for (long long i = 0; i < n; i++) { cin >> a[i][0] >> a[i][1]; } long long l = 1; long long r = 1000000000; for (long long i = 0; i < n; i++) { if (a[i][0] > l) { l = a[i][0]; } if (a[i][1] < r) { r = a[i][1]; } } if (l < r) { cout << 0 << endl; } else { cout << l - r << endl; } } }
#include <bits/stdc++.h> using namespace std; const long long dx[4] = {-1, 0, 0, 1}; const long long dy[4] = {0, -1, 1, 0}; void faster() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long a, b, n, z, k, ans, x, y, ta, tb, h, m, ar[456789], c, mn[456789], minn; string s, t; vector<long long> vv; void dii() { faster(); cin >> n; c = 2e9; b = 0; for (int i = 0; i < n; i++) { cin >> x >> y; b = max(b, x); c = min(c, y); } if (b <= c) { cout << 0; cout << endl; return; } cout << abs(b - c); cout << endl; } int main() { faster(); int q = 1; cin >> q; while (q--) { dii(); } return 0; }
#include <bits/stdc++.h> std::vector<long long> a; int main() { long long t; std::cin >> t; for (int i = 0; i < t; i++) { long long n; std::cin >> n; std::vector<std::pair<long long, long long>> a(n), b(n); for (int i = 0; i < n; i++) { std::cin >> a[i].first >> a[i].second; b[i].first = a[i].second; b[i].second = a[i].first; } std::sort(a.begin(), a.end()); std::sort(b.begin(), b.end()); if (n == 1) { std::cout << 0 << '\n'; continue; } long long r = a[n - 1].first; long long l = b[0].first; if (l >= r) { std::cout << 0 << '\n'; } else { std::cout << r - l << '\n'; } } }
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int t, n; cin >> t; for (int i = 0; i < t; i++) { cin >> n; vector<int> s(n); vector<int> f(n); for (int k = 0; k < n; k++) { cin >> s[k]; cin >> f[k]; } sort(s.begin(), s.end()); sort(f.begin(), f.end()); int a = f[0] - s[n - 1]; if (a < 0) { cout << a * (-1) << endl; } else { cout << 0 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; long a, b, maxa, minb; while (t--) { maxa = 0; minb = LONG_MAX; cin >> n; while (n--) { cin >> a >> b; if (a > maxa) maxa = a; if (b < minb) minb = b; } if (maxa - minb > 0) { cout << maxa - minb << endl; } else { cout << 0 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; void run() { long long n, rr, c, first, second; long long t; cin >> t; for (long long k = 0; k < t; ++k) { cin >> n; vector<pair<long long, long long> > ans; for (long long i = 0; i < n; ++i) { cin >> first >> second; ans.push_back(make_pair(first, -1)); ans.push_back(make_pair(second, 1)); } sort(ans.begin(), ans.end()); long long i = 0; long long j = 2 * n - 1; while (ans[i].second != 1) ++i; while (ans[j].second != -1 && i < j) --j; if (n == 1) cout << 0 << endl; else cout << (long long)abs(ans[j].first - ans[i].first) << endl; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); run(); long long pause; cin >> pause; }
#include <bits/stdc++.h> using namespace std; void sort(int& a, int& b, int& c) { if (a < b) swap(a, b); if (b < c) swap(b, c); if (a < b) swap(a, b); } int main() { int t; cin >> t; for (int i = 0; i < t; ++i) { int n, max_left = 0, min_right = 1e9; cin >> n; for (int j = 0; j < n; ++j) { int x1, x2; cin >> x1 >> x2; max_left = max(max_left, x1); min_right = min(min_right, x2); } if (max_left - min_right < 0) cout << 0 << endl; else cout << max_left - min_right << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int maxl = -1e9; int minr = 1e9; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; maxl = max(maxl, a); minr = min(minr, b); } cout << max(maxl - minr, 0) << endl; } }
#include <bits/stdc++.h> using namespace std; long long fact[1000002]; long long power(long long a, long long n) { long long r = 1; while (n > 0) { if (n & 1) { r = (r * a) % 1000000007; } a = a * a % 1000000007; n >>= 1; } return r; } long long ipow(long long a, long long b, long long c = 1000000007) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % 1000000007; a = a * a % 1000000007; b >>= 1; } return ans; } long long pre() { fact[0] = 1; for (long long i = 1LL; i < 1000002; i++) { fact[i] = fact[i - 1] * i; if (fact[i] >= 1000000007) fact[i] %= 1000000007; } } long long inverPow(long long num) { return ipow(num, 1000000007 - 2, 1000000007); } long long nCr(long long N, long long K) { long long ans = fact[N]; long long temp = inverPow(fact[N - K]); ans = ans * temp % 1000000007; temp = inverPow(fact[K]); ans = ans * temp % 1000000007; return ans; } void solve() { long long n; cin >> n; long long x, y; long long left = 0; long long right = 1000000007; for (int i = 0; i < n; i++) { cin >> x >> y; left = max(left, x); right = min(right, y); } cout << max(0ll, left - right) << "\n"; } int main() { long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; vector<pair<long long, long long>> v(n); for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second; sort(v.begin(), v.end()); for (int i = 1; i < n; i++) { v[i].second = min(v[i].second, v[i - 1].second); } v[n - 1].first > v[n - 1].second ? cout << v[n - 1].first - v[n - 1].second << "\n" : cout << "0\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long tt; cin >> tt; while (tt--) solve(); }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, i; cin >> n; int l[n], r[n]; for (i = 0; i < n; i++) { cin >> l[i] >> r[i]; } if (n == 1) cout << 0 << endl; else { sort(l, l + n); sort(r, r + n); if (l[n - 1] > r[0]) { int ans = abs(r[0] - l[n - 1]); cout << ans << endl; } else cout << 0 << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, minimum, maximum, a, b; cin >> n; while (n--) { cin >> t; minimum = 1000000000 + 5; maximum = 0; if (t == 1) { cout << "0\n"; cin >> a >> b; continue; } while (t--) { cin >> a >> b; maximum = max(maximum, a); minimum = min(minimum, b); } maximum = (maximum - minimum > 0) ? (maximum - minimum) : 0; cout << maximum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { long long int t = 1; cin >> t; while (t--) { long long int n; cin >> n; vector<pair<long long int, long long int> > v; long long int x, y; for (long long int i = 0; i < n; i++) { cin >> x >> y; v.push_back(make_pair(x, y)); } sort(v.begin(), v.end()); x = v[0].second; y = v[n - 1].first; for (long long int i = 1; i < n; i++) { if (v[i].second < x) x = v[i].second; } if (y < x) y = x; cout << y - x << '\n'; } }
#include <bits/stdc++.h> using namespace std; int main() { int t, n, i, j, mi, ma, a, b; cin >> t; for (i = 0; i < t; i++) { cin >> n; mi = 1100000000; ma = 0; for (j = 0; j < n; j++) { cin >> a >> b; ma = max(a, ma); mi = min(b, mi); } if (ma - mi <= 0) cout << 0; else cout << ma - mi; cout << endl; } return 0; }
#include <bits/stdc++.h> int solve(const std::vector<std::pair<int, int> >& segments); int main() { int testCasesCount; std::cin >> testCasesCount; std::vector<int> results; for (auto i = 0; i < testCasesCount; ++i) { int n; std::cin >> n; std::vector<std::pair<int, int> > segments; for (auto j = 0; j < n; ++j) { int l, r; std::cin >> l >> r; segments.push_back({l, r}); } results.push_back(solve(segments)); } for (auto result : results) { std::cout << result << std::endl; } return 0; } int solve(const std::vector<std::pair<int, int> >& segments) { if (segments.size() < 2) { return 0; } int lmaxPos = 0, rminPos = 0; for (auto i = 1; i < segments.size(); ++i) { if (segments[i].first > segments[lmaxPos].first) { lmaxPos = i; } if (segments[i].second < segments[rminPos].second) { rminPos = i; } } if (segments[lmaxPos].first < segments[rminPos].second) { return 0; } return segments[lmaxPos].first - segments[rminPos].second; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, l = 0, r = LLONG_MAX, ans = 0; cin >> n; while (n--) { long long x, y; cin >> x >> y; l = max(l, x); r = min(r, y); } cout << max(ans, l - r) << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; v.push_back(make_pair(x, y)); } sort(v.begin(), v.end()); int rmin = 1e9 + 5; for (auto x : v) { if (x.second <= rmin) { rmin = x.second; } } cout << max(0, v[n - 1].first - rmin) << endl; } return 0; }
#include <bits/stdc++.h> int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int a = 0, b = 1000000001, l, r; for (int i = 0; i < n; i++) { scanf("%d%d", &l, &r); if (l > a) a = l; if (r < b) b = r; } int d; d = a - b; if (d > 0) printf("%d\n", d); else printf("0\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; long double PI = acosl(-1); bool compare_int(int a, int b) { return (a > b); } bool compare_string(string a, string b) { return a.size() < b.size(); } bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { if (a.second == b.second) return a.first > b.first; else return (a.second > b.second); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, n, x, y; cin >> t; while (t--) { vector<int> xx, yy; cin >> n; int kace = INT_MAX; for (int i = 0; i < n; i++) { cin >> x >> y; xx.push_back(x); yy.push_back(y); } sort(xx.begin(), xx.end()); sort(yy.begin(), yy.end()); cout << max(*max_element(xx.begin(), xx.end()) - *min_element(yy.begin(), yy.end()), 0) << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int z, n, m, a, b, p, k; cin >> z; for (int q = 0; q < z; ++q) { cin >> n; p = -1; k = 1000000000; for (int i = 0; i < n; ++i) { cin >> a >> b; p = max(p, a); k = min(k, b); } if (p < k) { cout << 0 << "\n"; } else { cout << p - k << "\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, n; cin >> t; while (t--) { cin >> n; int maxl, minr, l, r; cin >> maxl >> minr; while (--n) { cin >> l >> r; maxl = max(l, maxl); minr = min(r, minr); } cout << max(0, maxl - minr) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; for (int i = 0; i < a; i++) { int n; cin >> n; vector<int> l(n); vector<int> r(n); for (int j = 0; j < n; j++) { cin >> l[j] >> r[j]; } sort(l.begin(), l.end()); sort(r.begin(), r.end()); reverse(l.begin(), l.end()); if (r[0] >= l[0]) { cout << 0 << endl; } else { cout << l[0] - r[0] << endl; } } }
#include <bits/stdc++.h> using namespace std; const long long inf = 2e9 + 7; const double eps = 1e-8; const long long MOD = 1e9 + 7; const long long N = 110; const long long M = 10010; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; for (long long j = 0; j < t; ++j) { long long n; cin >> n; vector<pair<long long, long long> > a(n); for (long long i = 0; i < n; ++i) cin >> a[i].first >> a[i].second; sort(a.begin(), a.end()); long long l = a[0].second, r = a[n - 1].first; for (long long i = 0; i < n; ++i) { if (a[i].second < l) l = a[i].second; if (a[i].first > r) r = a[i].first; } long long ans = r - l; if (ans < 0) ans = 0; cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long T; cin >> T; while (T--) { long long n, mxl = -1, mnr = 1e18, l, r; cin >> n; while (n--) cin >> l >> r, mxl = max(mxl, l), mnr = min(mnr, r); if (n == 1) cout << "0\n"; else cout << max(0LL, mxl - mnr) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto x : v) os << x << " "; return os << "\n"; } template <class U, class V> ostream &operator<<(ostream &os, const pair<U, V> &p) { return os << "(" << p.first << "," << p.second << ")"; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<int, int> > seg(n); for (int i = 0; i < (n); ++i) cin >> seg[i].first >> seg[i].second; int a = INT_MAX, b = 0; for (int i = 0; i < (n); ++i) { a = min(a, seg[i].second); b = max(b, seg[i].first); } cout << max(0, b - a) << "\n"; } }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<pair<long long int, long long int>> v(n); long long int m = LLONG_MAX; for (auto &i : v) { cin >> i.first; cin >> i.second; m = min(m, i.second); } sort(v.begin(), v.end()); if (v[n - 1].first <= m) { cout << "0" << endl; } else cout << v[n - 1].first - m << endl; } int main() { long long int t; cin >> t; while (t--) { solve(); } return 0; }