text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, i; cin >> n; for (i = 0; i < n; i++) ; long long l[n], r[i]; for (i = 0; i < n; i++) { cin >> l[i] >> r[i]; } if (n == 1) { cout << "0" << endl; } else { long long max1, max2; max1 = *max_element(l, l + n); max2 = *min_element(r, r + n); long long ans = max1 - max2; if (ans <= 0) { ans = 0; } cout << ans << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long MAX(long long x, long long y) { if (x > y) return x; else return y; } long long MIN(long long x, long long y) { if (x < y) return x; else return y; } int main() { long long t, n, x, y, l, r; cin >> t; for (long long i = 0; i < t; i++) { cin >> n; x = 0; y = 1000000000; for (long long j = 0; j < n; j++) { cin >> l >> r; x = MAX(x, l); y = MIN(y, r); } cout << MAX((x - y), 0) << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { 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.begin(), r.end()); cout << max(l[n - 1] - r[0], 0) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; bool sortbysec(const pair<int, int>& a, const pair<int, int>& b) { return (a.second < b.second); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t, f = 1; cin >> t; while (t--) { int n; cin >> n; int l, r, maxx = 0, minn = 1e9; if (n == 1) { cin >> l >> r; cout << 0 << '\n'; } else { while (n--) { cin >> l >> r; if (l > maxx) maxx = l; if (r < minn) minn = r; } cout << max(0, maxx - minn) << '\n'; } } }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; pair<int, int> l, r; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; if (!i) { l = r = {x, y}; } if (x > r.first) { r = {x, y}; } if (y < l.second) { l = {x, y}; } } int lft = l.second, rght = r.first; if (rght < lft) { rght = lft; } cout << rght - lft; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int i = 0; i < t; i++) { solve(); cout << '\n'; } return 0; }
#include <bits/stdc++.h> const int mod = 1e9 + 7; const int N = 1e6 + 7; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long int n; cin >> n; vector<pair<long long int, long long int>> v(n); long long int mini = 1e10, maxi = 0; for (long long int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; if (v[i].second < mini) mini = v[i].second; if (v[i].first > maxi) maxi = v[i].first; } if (maxi <= mini) cout << "0\n"; else cout << abs(maxi - mini) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void nandini() { long long int n, k, i, d = 0; cin >> n; long long int a[n][2]; for (i = 0; i < n; i++) { cin >> a[i][0] >> a[i][1]; } long long int b[n]; long long int c[n]; for (i = 0; i < n; i++) b[i] = a[i][0]; for (i = 0; i < n; i++) c[i] = a[i][1]; if (n == 1) cout << 0; else { sort(b, b + n); sort(c, c + n); if (b[n - 1] - c[0] > 0) cout << b[n - 1] - c[0]; else cout << 0; } cout << endl; } int main() { long long int t; cin >> t; while (t--) { nandini(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int j = 0; j < t; j++) { int n; long long int p, miny = 1000000000, maxx = 0, ora = 0; cin >> n; long long int x[n], y[n]; if (n == 1) { cin >> x[0] >> y[0]; cout << "0" << endl; continue; } for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; if (x[i] > maxx) { maxx = x[i]; p = i; } } y[p] = -1; for (int i = 0; i < n; i++) { if (y[i] > 0 && y[i] < miny) { miny = y[i]; } } cout << max(maxx - miny, ora) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; if (n == 1) { int a, b; cin >> a >> b; cout << "0" << endl; continue; } else { int mx = INT_MIN, mn = INT_MAX; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; mx = max(mx, a); mn = min(mn, b); } int val = (mx - mn); if (val < 0) { cout << "0" << endl; } else { cout << val << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; ll a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; sort(a, a + n); sort(b, b + n); cout << max((ll)0, a[n - 1] - b[0]) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; vector<int> ans; for (int i = 0; i < t; i++) { int n; cin >> n; vector<pair<int, int> > vect(n); int ma_l = -1, mi_r = 2 * 1e9; for (int i = 0; i < n; i++) { cin >> vect[i].first >> vect[i].second; ma_l = max(vect[i].first, ma_l); mi_r = min(mi_r, vect[i].second); } if (mi_r >= ma_l) { ans.push_back(0); continue; } ans.push_back(ma_l - mi_r); } for (auto x : ans) cout << x << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; bool debag = false; int main() { if (!debag) { cin.tie(0); cin.sync_with_stdio(0); cout.tie(0); cout.sync_with_stdio(0); } int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; int l = 0, r = 1 << 30; for (int j = 0; j < n; j++) { int lj, rj; cin >> lj >> rj; l = max(l, lj); r = min(r, rj); } if (l < r) { cout << 0 << '\n'; } else { cout << l - r << '\n'; } } if (debag) { int h; cin >> h; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; int a, b, c, d, tmp; for (int i = 0; i < n; i++) { cin >> tmp; c = 1E9; d = 0; for (int j = 0; j < tmp; j++) { cin >> a >> b; if (d < a) { d = a; } if (c > b) { c = b; } } cout << max((int)(d - c), 0) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { int t; cin >> t; for (int i = 0; i < t; ++i) { int n; cin >> n; int lefter = 1000000000, righter = 0; for (int j = 0; j < n; ++j) { int l, r; cin >> l >> r; lefter = min(lefter, r); righter = max(righter, l); } cout << max(0, righter - lefter) << '\n'; } }
#include <bits/stdc++.h> using namespace std; int min_v(vector<int> a) { int mini = INT_MAX; for (int i = 0; i < a.size(); i++) { mini = min(mini, a[i]); } return mini; } int max_v(vector<int> b) { int maxi = -1; for (int i = 0; i < b.size(); i++) { maxi = max(maxi, b[i]); } return maxi; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 0; cin >> t; int n = 0; int a = 0, b = 0; int count = 0; while (t--) { int count = 0, y = 0; cin >> n; vector<int> first; vector<int> second; while (n--) { cin >> a >> b; first.push_back(a); second.push_back(b); } cout << max(0, max_v(first) - min_v(second)) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; pair<int, int> a[n]; int mn = INT_MAX, mx = INT_MIN; for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; mx = max(mx, a[i].first); mn = min(mn, a[i].second); } cout << max(0, mx - mn) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, n; long int mn_r = 1000000001, mx_l = -1000000001, a, b; cin >> t; for (int i = 0; i < t; i++) { cin >> n; for (int j = 0; j < n; j++) { cin >> a >> b; if (a > mx_l) mx_l = a; if (b < mn_r) mn_r = b; } (mx_l - mn_r > 0) ? cout << mx_l - mn_r << endl : cout << 0 << endl; mn_r = 1000000001; mx_l = -1000000001; } return 0; }
#include <bits/stdc++.h> using namespace std; int t, n; int main() { cin >> t; while (t--) { int l, r, k = 2000000000, m = 0; cin >> n; for (int i = 1; i <= n; i++) cin >> l >> r, k = min(k, r), m = max(m, l); cout << max(m - k, 0) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long z, o, ans; long long N = 1e4 + 7; int code() { int n; cin >> n; int k = INT_MAX, y = 0; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; y = max(y, a); k = min(b, k); } cout << max(0, y - k) << endl; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tjk = 1; cin >> tjk; while (tjk--) { code(); } }
#include <bits/stdc++.h> using namespace std; int main(void) { int t, n; cin >> t; for (int i = 0; i < t; i++) { cin >> n; long long l = -2000000000, r = 2000000000, a, b; for (int j = 0; j < n; j++) { cin >> a >> b; if (a > l) l = a; if (b < r) r = b; } if (n == 1) r = l; if (r > l) r = l; cout << l - r << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; set<int> s1, s2; int a, b; for (int i = 0; i < n; i++) { cin >> a >> b; s1.insert(a), s2.insert(b); } a = *(--s1.end()); b = *s2.begin(); if (a - b <= 0) cout << '0' << "\n"; else cout << a - b << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; int maxix = 0, miniy = INT_MAX; cin >> n; bool ck = false; while (n--) { int x, y; cin >> x >> y; maxix = max(maxix, x); miniy = min(miniy, y); } (maxix - miniy > 0) ? cout << maxix - miniy << endl : cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long int a, b, n, i, x = INT_MIN, y = INT_MAX; cin >> n; for (i = 0; i < n; i++) { cin >> a >> b; x = max(x, a); y = min(y, b); } if (n == 1 || y > x) cout << 0 << endl; else cout << x - y << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int const INF = 2 * 10e5 + 123; int simple(int n) { int i, sq; if (n % 2 == 0) { return 0; } sq = (int)sqrt(n); for (i = 3; i <= sq; i++) { if ((n % i) == 0) { return 0; } } return 1; } long long gcd(long long a, long long b) { if (a > b) swap(a, b); if (!a) return b; return gcd(b % a, a); } void solve() { int n; cin >> n; int mn = 1000000000, mx = 0; for (int j = 0; j < n; ++j) { int l, r; cin >> l >> r; mn = min(mn, r); mx = max(mx, l); } cout << max(0, mx - mn) << '\n'; } int main() { int T = 1; cin >> T; while (T--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tc; cin >> tc; while (tc--) { long long n; cin >> n; vector<pair<long long, long long>> vp; while (n--) { long long x, y; cin >> x >> y; vp.push_back(make_pair(x, y)); } sort(vp.begin(), vp.end()); long long one = vp[0].first, two = vp[0].second; long long f = 1; for (int i = 1; i < vp.size(); i++) { if (two >= vp[i].first) { one = vp[i].first; two = min(two, vp[i].second); } else { one = two; break; } } cout << abs(one - vp[vp.size() - 1].first) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; int otvet[t]; for (int i = 0; i < t; i++) { cin >> n; int masmax[n]; int masmin[n]; for (int j = 0; j < n; j++) { cin >> masmax[j] >> masmin[j]; } sort(masmax, masmax + n); sort(masmin, masmin + n); otvet[i] = masmax[n - 1] - masmin[0]; } for (int i = 0; i < t; i++) { if (otvet[i] < 0) { cout << 0 << endl; } else { cout << otvet[i] << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int x, y, r = INT_MAX, l = INT_MIN; for (int i = 0; i < n; i++) { cin >> x >> y; l = max(l, x); r = min(r, y); } if (l > r) cout << abs(l - r) << endl; else cout << 0 << endl; } }
#include <bits/stdc++.h> using namespace std; bool check[300007]; int main() { int T; scanf("%d", &T); for (auto i_test = (1); i_test <= (T); ++i_test) { int n; scanf("%d", &n); int L = 1000000007, R = 0; for (auto i = (1); i <= (n); ++i) { int l, r; scanf("%d%d", &l, &r); L = min(r, L); R = max(l, R); } cout << max(0, R - L) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { int n1, min, max; cin >> n1; vector<int> i1(n1), i2(n1); for (int j = 0; j < n1; j++) { cin >> i1[j] >> i2[j]; } min = i2[0]; max = i1[0]; for (int j = 1; j < n1; j++) { if (i1[j] > max) { max = i1[j]; } if (i2[j] < min) { min = i2[j]; } } a[i] = max - min; if (a[i] < 0) { a[i] = 0; } } cout << "\n" << "\n" << "\n"; for (int i = 0; i < n; i++) { cout << a[i] << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; long long t, n, a, b, c, rm, lm, k = 0; int main() { cin.tie(0); cin >> t; for (int l = 0; l < t; ++l) { cin >> n; cin >> lm >> rm; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; lm = max(lm, a); rm = min(rm, b); } cout << max(lm - rm, k) << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int kk = 0; kk < t; ++kk) { int n; cin >> n; int max_beg = -1, min_end = 1e9 + 1; for (int i = 0; i < n; ++i) { int l, r; cin >> l >> r; max_beg = max(l, max_beg); min_end = min(r, min_end); } if (max_beg < min_end) cout << "0\n"; else cout << max_beg - min_end << "\n"; } }
#include <bits/stdc++.h> using namespace std; int32_t main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long mnr = 1e9, mxl = -1e9; for (long long i = 0; i < n; i++) { long long l, r; cin >> l >> r; mnr = min(mnr, r); mxl = max(mxl, l); } cout << (n == 1 ? 0 : max(mxl - mnr, 0ll)) << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { long long int t; ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> t; while (t--) { long long int n, l, r; cin >> n; pair<long long int, long long int> vt[n], st[n]; for (long long int i = 0; i < n; i++) { cin >> l >> r; vt[i] = {l, r}; st[i] = {r, l}; } sort(vt, vt + n); sort(st, st + n); long long int r2 = vt[n - 1].first; long long int r1 = st[0].first; if (r1 >= r2) cout << "0" << endl; else cout << r2 - r1 << endl; } 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}; b.push_back({x, 1}), b.push_back({y, -1}); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (check_intersections(b, n)) { cout << "0\n"; continue; } cout << (n == 1 ? 0 : abs(minimum_end - maximum_begin)) << "\n"; } cin.get(); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, t, i, j; cin >> t; while (t--) { cin >> n; int mx = -1, mn = 2000000000, x1, x2; for (i = 1; i <= n; i++) { cin >> x1 >> x2; mx = max(x1, mx); mn = min(mn, x2); } cout << max(mx - mn, 0) << '\n'; } }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); cout << max(0, -b[0] + a[n - 1]) << endl; } signed main() { int t; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; const int mxn = 100033; const int mod = 1e9 + 7; inline int read() { int x = 0, f = 1; char c = getchar(); while (!isdigit(c)) f = c == '-' ? -1 : 1, c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); return x * f; } inline long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long ksm(long long a, long long b, long long mod) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1; } return ans; } long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); } void exgcd(long long a, long long b, long long &x, long long &y, long long &d) { if (!b) { d = a; x = 1; y = 0; } else { exgcd(b, a % b, y, x, d); y -= x * (a / b); } } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int n; struct node { int l, r; } nd[mxn]; bool cmp(node a, node b) { return a.l < b.l; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); ; int t; cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; ++i) cin >> nd[i].l >> nd[i].r; sort(nd + 1, nd + n + 1, cmp); int res = 0, l = 0x3f3f3f3f, r = nd[n].l; for (int i = 1; i < n; ++i) if (nd[i].r < nd[n].l) { l = min(l, nd[i].r); } if (l >= r) res = 0; else res = r - l; cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200100; const long long INF = 2000000000000000; long long n, m = 0, t, x, y, min1 = INF, max1 = 0, k, c, d, p; long long a[N], b[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> t; for (int j = 0; j < t; j++) { cin >> n; max1 = 0; min1 = INF; for (int i = 0; i < n; i++) { cin >> x >> y; if (x > max1) max1 = x; if (y < min1) min1 = y; } cout << max(max1 - min1, m) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int INF = 2e9; int n; pair<int, int> a[N]; void solve() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i].first >> a[i].second; } int mnR = INF, mxL = -INF; for (int i = 1; i <= n; ++i) { mxL = max(mxL, a[i].first); mnR = min(mnR, a[i].second); } cout << max(0, mxL - mnR) << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; bool comp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } int main() { int t, n, x; cin >> t; int ans[t]; for (int i = 0; i < t; i++) { x = 0; cin >> n; vector<pair<int, int>> v(n); for (int j = 0; j < n; j++) { cin >> v[j].first >> v[j].second; } sort(v.rbegin(), v.rend()); x = v[0].first; sort(v.begin(), v.end(), comp); x -= v[0].second; ans[i] = max(0, x); } for (int i = 0; i < t; i++) { cout << ans[i] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 2100000000; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tc; cin >> tc; for (int ks = 1; ks <= tc; ks++) { int n; cin >> n; long long mxL = 0, mnR = inf; for (int i = 0; i < n; i++) { long long l, r; cin >> l >> r; mxL = max(mxL, l); mnR = min(mnR, r); } if (mnR >= mxL) { cout << "0\n"; } else { cout << mxL - mnR << "\n"; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 4; const long long INF = 1e18; const long long MOD = 1e9 + 7; const long double EPS = 1e-10; long double sq(long double a) { return a * a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(40); int t; cin >> t; while (t--) { int n; cin >> n; int rmin = 1e9, lmax = -1e9; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; rmin = min(rmin, b); lmax = max(lmax, a); } if (n == 1) { cout << 0 << '\n'; continue; } cout << max(0, lmax - rmin) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long int l, r; long long int high = -1; long long int low = 1000000001; for (int i = 0; i < n; i++) { cin >> l >> r; if (l > high) { high = l; } if (r < low) { low = r; } } if (high > low) { cout << (high - low) << endl; } else if (low >= high) { cout << 0 << endl; } } }
#include <bits/stdc++.h> using namespace std; const long long int N = 2e5 + 5, LOG = 20, INF = 1e9 + 1, SQ = 375, base = 100, MOD = 1e9 + 7; int l[N], r[N]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; } int l1 = INF, r1 = 0; for (int i = 0; i < n; i++) { l1 = min(r[i], l1); r1 = max(l[i], r1); } bool st = true; for (int i = 0; i < n; i++) { if (l1 < l[i] || l1 > r[i]) st = false; } if (st) l1 = r1; cout << abs(l1 - r1) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long t, n, i, j, k, l, r, minn, maxx, ans, x, y; cin >> t; while (t--) { cin >> n; cin >> maxx >> minn; for (i = 1; i < n; i++) { cin >> x >> y; maxx = max(maxx, x); minn = min(minn, y); } if (maxx <= minn) ans = 0; else ans = maxx - minn; cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; void work() { int n, l, r, maxl = 0, minr = 1e9; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &l, &r), minr = min(minr, r), maxl = max(maxl, l); printf("%d\n", max(0, maxl - minr)); } int main() { int t; scanf("%d", &t); while (t--) work(); return 0; }
#include <bits/stdc++.h> using namespace std; int t; int n; int main() { cin >> t; while (t--) { int t1, t2; cin >> n; int mt1 = 0, mt2 = 1e9 + 1; for (int i = 1; i <= n; i++) { scanf("%d %d", &t1, &t2); mt1 = max(mt1, t1); mt2 = min(mt2, t2); } if (mt1 <= mt2) { printf("0\n"); } else { printf("%d\n", mt1 - mt2); } } 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; long long lc = 100000000000; long long rc = -1000000000000; for (int i = 0; i < n; i++) { long long l, r; cin >> l >> r; rc = max(rc, l); lc = min(lc, r); } if (rc <= lc) { cout << 0 << endl; } else { cout << rc - lc << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long x = 0, y = 1000000007; while (n--) { long long l, r; cin >> l >> r; x = max(l, x); y = min(r, y); } long long res = max(x - y, 0ll); cout << res << endl; } }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") const long long int MX = 10010896; const long long int lmt = 3164; using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " = " << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " = " << arg1 << " "; __f(comma + 1, args...); } void solve() { long long int n; cin >> n; pair<long long int, long long int> arr[n]; for (long long int i = (long long int)0; i < (long long int)n; ++i) { cin >> arr[i].first >> arr[i].second; } vector<long long int> left, right; for (long long int i = (long long int)0; i < (long long int)n; ++i) { left.push_back(arr[i].first); right.push_back(arr[i].second); } sort(left.begin(), left.end()); sort(right.begin(), right.end()); long long int ll = left[left.size() - 1]; long long int rr = right[0]; cout << max(0LL, (ll - rr)) << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") const long long MOD = 1e9 + 7; long long n_bits(long long n) { long long x = __builtin_popcount(n); return x; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { int n; cin >> n; vector<pair<int, int>> v = {}; int x, y, l, r; for (int i = 0; i < n; i++) { cin >> x >> y; v.push_back({y, x}); } sort(v.begin(), v.end()); l = v[0].first; r = v[0].first; for (int i = 1; i < n; i++) { if (l < v[i].second) r = max(v[i].second, r); } cout << r - l << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18 + 7; const long double EPS = 1e-8; const long long MAXI = 20000; const long long MOD = 16714589; const long long MAXST = 2000000; const long long P = 40; const long double PI = 3.14159265358979323; const long long MAXN = 1e5; pair<long long, long long> a[100005]; signed main() { { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); }; long long t; cin >> t; for (long long i = 0; i < t; i++) { long long n; cin >> n; long long mx = -INF, mn = INF; for (long long j = 0; j < n; j++) { cin >> a[j].first >> a[j].second; mn = min(mn, a[j].second); mx = max(mx, a[j].first); } if (mx - mn < 0) cout << 0 << '\n'; else cout << mx - mn << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t, n, a, b, l, r; cin >> t; while (t--) { cin >> n; for (long long int i = 0; i < n; i++) { cin >> a >> b; if (i == 0) { l = a; r = b; } if (l < a) { l = a; } if (r > b) { r = b; } } if (l - r >= 0) cout << l - r << endl; else cout << "0" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, n, i, r; vector<int> a, b; cin >> t; while (t > 0) { cin >> n; for (i = 0; i < n; i++) { cin >> r; a.push_back(r); cin >> r; b.push_back(r); } cout << max(0, (*max_element(a.begin(), a.end()) - *min_element(b.begin(), b.end()))) << endl; t--; a.clear(); b.clear(); } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n, u, v, mn = INT_MAX, mx = -1; cin >> n; for (int i = 0; i < n; i++) { cin >> u >> v; mn = min(mn, v); mx = max(mx, u); } cout << max(mx - mn, 0LL) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n; int l[N], r[N]; void solv() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", &l[i], &r[i]); int minr = r[1]; int maxl = l[1]; for (int i = 2; i <= n; ++i) { minr = min(minr, r[i]); maxl = max(maxl, l[i]); } printf("%d\n", max(0, maxl - minr)); } int main() { int tt; scanf("%d", &tt); while (tt--) { solv(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> v1(n), v2(n); for (long long i = 0; i < n; i++) { cin >> v1[i]; cin >> v2[i]; } sort(v1.begin(), v1.end()); long long pp = v1[n - 1]; sort(v2.begin(), v2.end()); long long qq = v2[0]; if (pp > qq) cout << pp - qq; else cout << 0; cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int 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; vector<long long> a[2]; long long x, y; long long m_y = INT_MAX; for (long long i = 0; i < n; i++) { cin >> x >> y; a[0].push_back(x); a[1].push_back(y); m_y = min(m_y, y); } long long m_x = INT_MIN; for (long long i = 0; i < n; i++) { if (m_y < a[0][i]) { m_x = max(m_x, a[0][i]); } } if (m_x == INT_MIN) { cout << 0 << endl; } else cout << m_x - m_y << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n, i, j, ans; cin >> n; long long int l[n]; long long int r[n]; for (i = 0; i < n; i++) { cin >> l[i] >> r[i]; } long long int lans = *max_element(l, l + n); long long int rans = *min_element(r, r + n); ans = lans - rans; if (ans > 0) std::cout << ans << std::endl; else cout << 0 << "\n"; } return 0; }
#include <bits/stdc++.h> void sorting(long long int a[][2], int n) { int i, j, t; for (i = 0, j = n - 1; i < n - 1; i++, j--) { if (a[i][0] > a[i + 1][0]) { t = a[i][0]; a[i][0] = a[i + 1][0]; a[i + 1][0] = t; } if (a[j][1] < a[j - 1][1]) { t = a[j][1]; a[j][1] = a[j - 1][1]; a[j - 1][1] = t; } } } using namespace std; int main() { int t, n, k; cin >> t; while (t--) { k = 0; cin >> n; long long int val[n][2]; for (int i = 0; i < n; i++) { cin >> val[i][0] >> val[i][1]; } if (n == 1) cout << 0 << endl; else { sorting(val, n); for (int i = 0; i < n; i++) { if (val[0][1] >= val[i][0] && val[0][1] <= val[i][1]) continue; else { k = 1; break; } } if (k == 1) cout << (val[n - 1][0] > val[0][1] ? val[n - 1][0] - val[0][1] : val[0][1] - val[n - 1][0]) << endl; else cout << 0 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q-- > 0) { int n; cin >> n; int a1 = INT_MAX, a2 = 0; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; a1 = min(a1, b); a2 = max(a2, a); } cout << max(0, a2 - a1) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e16 + 5; const int MOD = 1e9 + 7; const double EPS = 1e-8; template <class T = int> inline T next() { T x; cin >> x; return x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = next(); while (t--) { int n = next(); vector<pair<int, int> > points; for (int i = 0; i < n; i++) { points.push_back({next(), 1}); points.push_back({next(), -1}); } sort(points.begin(), points.end()); int l = 0, r = 0; for (int i = 0; i < points.size(); i++) { if (points[i].second == -1) { l = points[i].first; break; } } for (int i = points.size() - 1; i != -1; i--) { if (points[i].second == 1) { r = points[i].first; break; } } bool center = false; int count = 0; for (int i = 0; i < points.size() - 1; i++) { count += points[i].second; if (count == n) { center = true; } } if (center) cout << 0 << '\n'; else cout << r - l << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n; cin >> n; vector<pair<bool, int> > a; int x, y; for (int i = 0; i < n; ++i) { cin >> x >> y; a.push_back({0, x}); a.push_back({1, y}); } sort((a).begin(), (a).end()); int ind_b = 0, ind_e = 0; for (int i = 0; i < n * 2; ++i) { if (a[i].first == 1) { ind_b = a[i].second; break; } } for (int i = n * 2 - 1; i >= 0; --i) { if (a[i].first == 0) { ind_e = a[i].second; break; } } if (ind_b >= ind_e) { cout << 0; } else { cout << ind_e - ind_b; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long N, maxl = 0, minr = 1000000000; cin >> N; long n[N]; long long l, r; for (int i = 0; i < N; i++) { cin >> n[i]; for (int j = 0; j < n[i]; j++) { cin >> l >> r; maxl = max(maxl, l); minr = min(minr, r); } if (n[i] != 1) { if (maxl - minr < 0) { cout << 0 << endl; } else cout << maxl - minr << endl; } else cout << 0 << endl; maxl = 0; minr = 1000000000; } }
#include <bits/stdc++.h> using namespace std; long long pwr(long long a, long long b) { long long result = 1; while (b > 0) { if (b & 1) result = result * a; a *= a; b >>= 1; } return result; } int reverser(int n) { long long rn = 0, i = 0, x; while (n > 0) { rn = rn * 10; rn += n % 10; n = n / 10; } return rn; } int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long ans, ansl = 0, ansr = 100000000000, l, r; vector<pair<long long, long long> > v; for (int i = 0; i < n; i++) { cin >> l >> r; ansl = max(l, ansl); ansr = min(r, ansr); } sort(v.begin(), v.end()); ans = ansl - ansr; if (ans < 0) cout << "0\n"; else cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(NULL); int t; cin >> t; while (t--) { int n, a, b, r = 0, l = 2000000000; cin >> n; while (n--) { cin >> a >> b; r = max(r, a), l = min(l, b); } cout << (r <= l ? 0 : r - l) << endl; } }
#include <bits/stdc++.h> using namespace std; inline bool intersect(int l1, int r1, int l2, int r2) { return l2 <= r1 && l1 <= r2; } inline pair<int, int> getIntesection(int l1, int r1, int l2, int r2) { return {max(l1, l2), min(r1, r2)}; } void solve() { int n; cin >> n; int left = 1; int right = 1000000000; while (n--) { int l, r; cin >> l >> r; right = min(right, r); left = max(left, l); } cout << max(0, left - right) << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long q, n; cin >> q; while (q--) { cin >> n; long long a_max = -1, b_min = 1e10; long long a, b; for (long long i = 0; i < n; i++) { cin >> a >> b; a_max = max(a, a_max); b_min = min(b, b_min); } cout << max(0LL, a_max - b_min) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int min_v(vector<int> a) { int mini = INT_MAX; for (int i = 0; i < a.size(); i++) { mini = min(mini, a[i]); } return mini; } int max_v(vector<int> b) { int maxi = -1; for (int i = 0; i < b.size(); i++) { maxi = max(maxi, b[i]); } return maxi; } int main() { int t = 0; cin >> t; int n = 0; int a = 0, b = 0; int count = 0; while (t--) { int count = 0, y = 0; cin >> n; vector<int> first; vector<int> second; while (n--) { cin >> a >> b; first.push_back(a); second.push_back(b); } cout << max(0, max_v(first) - min_v(second)) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; bool isinter(pair<long long int, long long int> a, pair<long long int, long long int> b) { if (a.second < b.first || a.first > b.second) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long int t, n, i, a, b; cin >> t; while (t--) { vector<pair<long long int, long long int>> notnice; vector<pair<long long int, long long int>> all; cin >> n; for (i = 0; i < n; ++i) { cin >> a >> b; all.push_back(make_pair(a, b)); } vector<pair<long long int, long long int>>::iterator itr; for (auto &it : all) { for (itr = notnice.begin(); itr != notnice.end(); ++itr) { if (isinter(*itr, it)) { a = (*itr).first; b = (*itr).second; (*itr).first = max(a, it.first); (*itr).second = min(b, it.second); break; } } if (itr == notnice.end()) notnice.push_back(it); } a = (*notnice.begin()).first; b = (*notnice.begin()).second; if (notnice.size() == 1) { cout << 0 << "\n"; continue; } for (auto &it : notnice) { if (it.second < b) b = it.second; if (it.first > a) a = it.first; } cout << a - b << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { long long n; cin >> n; int a[n], b[n], k, k1; for (int j = 0; j < n; j++) { long long x, y; cin >> x >> y; a[j] = x; b[j] = y; } k = *max_element(a, a + n); k1 = *min_element(b, b + n); if (k - k1 < 0) cout << '0' << endl; else cout << k - k1 << endl; } }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } int lcm(int a, int b) { return a / gcd(a, b) * b; } bool comp(pair<int, pair<int, char>> a, pair<int, pair<int, char>> b) { if (a.first < b.first) { return 1; } if (a.first == b.first && a.second.second - 'a' < b.second.second - 'a') { return 1; } return 0; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int t; cin >> t; int mini = 1000000000, maxi = 0; for (int j = 0; j < t; j++) { int a, b; cin >> a >> b; mini = min(b, mini); maxi = max(a, maxi); } int l = maxi - mini; if (l < 0) { l = 0; } cout << l << endl; } }
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)2e9; const long long inf = (long long)2e18; const long double eps = (long double)1e-6; const long long mod = (long long)1e9 + 7; const long long MAXN = (long long)5e5 + 1; const long long MAXC = (long long)6e6; const long long MAXLOG = 19; const long long maxlen = (long long)1e5; const long long asci = (long long)256; const long long block = 707; template <class T> istream &operator>>(istream &in, vector<T> &arr) { for (T &cnt : arr) { in >> cnt; } return in; }; void solve() { int n; cin >> n; int mn = INF, mx = -1; for (int i = 0; i < n; ++i) { int l, r; cin >> l >> r; mn = min(mn, r); mx = max(l, mx); } if (mx <= mn) cout << "0\n"; else cout << mx - mn << "\n"; } int main() { srand(time(0)); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.precision(30); int t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n; cin >> t; for (int j = 0; j < t; j++) { cin >> n; vector<int> l(n); vector<int> r(n); for (int i = 0; i < n; i++) { cin >> l[i]; cin >> r[i]; } sort(l.begin(), l.end()); sort(r.begin(), r.end()); if (l[n - 1] - r[0] >= 0) cout << l[n - 1] - r[0] << "\n"; else cout << 0 << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long first[n], second[n], maxi = 0, mini; for (int i = 0; i < n; i++) { cin >> first[i] >> second[i]; if (i == 0) mini = second[i]; if (first[i] > maxi) maxi = first[i]; if (second[i] < mini) mini = second[i]; } if (n == 1) cout << "0" << endl; else { if ((maxi - mini) < 0) cout << "0" << endl; else cout << maxi - mini << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; long long minr = 1e9, maxl = 0; for (int i = 0; i < n; i++) { long long l, r; cin >> l >> r; minr = min(minr, r); maxl = max(l, maxl); } if (minr >= maxl) cout << 0 << endl; else cout << maxl - minr << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long int maxii(long long int a, long long int b) { if (a > b) return a; return b; } long long int minii(long long int a, long long int b) { if (a > b) return b; return a; } int main() { int t; cin >> t; for (int i = 0; i < t; ++i) { int n; cin >> n; long long int maxi(-1), maxi2(100000000000); for (int i = 0; i < n; ++i) { long long int y, c; cin >> y >> c; maxi = maxii(maxi, y); maxi2 = minii(maxi2, c); } cout << maxii(0, maxi - maxi2) << "\n"; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; void start_func() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } signed main() { start_func(); long long t; cin >> t; while (t--) { long long n; cin >> n; long long lo, hi; lo = 2000000000; hi = 0; for (long long i = 0; i < n; i++) { long long a, b; cin >> a >> b; if (b < lo) lo = b; if (a > hi) hi = a; } if (hi <= lo) cout << 0 << "\n"; else cout << hi - lo << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n; cin >> n; long long int mn = 1e10 + 1; long long int mx = -1; long long int a, b; while (n--) { cin >> a >> b; mn = min(b, mn); mx = max(a, mx); } cout << max((long long int)0, mx - mn) << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<int, int>> lol; while (n--) { pair<int, int> lin; cin >> lin.first >> lin.second; lol.push_back(lin); } if (lol.size() == 1) cout << 0 << '\n'; else { pair<int, int> minsecond, maxfirst; minsecond = lol[0]; maxfirst = lol[0]; for (int i = 1; i < lol.size(); i++) { if (lol[i].second < minsecond.second) minsecond = lol[i]; if (lol[i].first >= maxfirst.first) maxfirst = lol[i]; } if (minsecond == maxfirst) cout << 0 << '\n'; else { int ans = maxfirst.first - minsecond.second; if (ans < 0) ans = 0; cout << ans << '\n'; } } } }
#include <bits/stdc++.h> using namespace std; int main() { int t, l, r, n; cin >> t; while (t--) { cin >> n; int mx = 0, mn = 1000000001; while (n--) { cin >> l >> r; mx = max(l, mx); mn = min(mn, r); } if (mn >= mx) cout << 0 << endl; else cout << mx - mn << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t-- > 0) { int n; cin >> n; int mnr = 1e9, mxl = 0; for (int i = 0; i < n; ++i) { int l, r; cin >> l >> r; mnr = min(mnr, r); mxl = max(mxl, l); } if (n == 1) { cout << 0 << '\n'; } else { cout << max(0, mxl - mnr) << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> prime; void SieveOfEratosthenes(int n) { bool prme[n + 1]; memset(prme, true, sizeof(prme)); for (int p = 2; p * p <= n; p++) { if (prme[p] == true) { for (int i = p * p; i <= n; i += p) prme[i] = false; } } for (int p = 2; p <= n; p++) if (prme[p]) prime.push_back(p); } void solve() { int n; cin >> n; vector<long long> mx, mn; for (int i = 0; i < n; i++) { long long a, b; cin >> a >> b; mx.push_back(a); mn.push_back(b); } sort(mx.rbegin(), mx.rend()); sort(mn.begin(), mn.end()); cout << max(0ll, mx[0] - mn[0]) << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; int n, maxl, minr, i, r, l, t; vector<int> v; vector<int> v2; int main() { cin >> t; while (t--) { cin >> n; for (i = 0; i < n; i++) { cin >> l >> r; v.push_back(l); v2.push_back(r); } sort(v.begin(), v.end()); sort(v2.begin(), v2.end()); cout << max(v.back() - v2[0], 0) << endl; v.clear(); v2.clear(); } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long l, r, l1 = 0, r1 = INT_MAX; int n1 = n; vector<pair<int, int> > v; while (n1--) { cin >> l >> r; v.push_back(make_pair(l, r)); l1 = max(l1, l); r1 = min(r1, r); } if (n == 1) cout << 0 << endl; else { bool ok1 = true, ok2 = true; for (int i = 0; i < v.size(); i++) { if (l1 >= v[i].first && l1 <= v[i].second) continue; else { ok1 = false; break; } } for (int i = 0; i < v.size(); i++) { if (r1 >= v[i].first && r1 <= v[i].second) continue; else { ok2 = false; break; } } if (ok1 || ok2) cout << 0 << endl; else cout << abs(l1 - r1) << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const long long INFLL = 0x3f3f3f3f3f3f3f3f; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int menorr = INF, maiorl = 0; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; menorr = min(menorr, r); maiorl = max(maiorl, l); } cout << max(0, maiorl - menorr) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { vector<pair<int, int>> st, re; int a, b; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; st.emplace_back(a, b); re.emplace_back(b, a); } sort(st.begin(), st.end()); sort(re.begin(), re.end()); cout << max(st[n - 1].first - re[0].first, 0) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int t, n; int main() { scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int mn = INF, mx = 0; for (int i = 1; i <= n; ++i) { int l, r; scanf("%d%d", &l, &r); mn = min(r, mn); mx = max(l, mx); } printf("%d\n", mx > mn ? mx - mn : 0); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, t, n, mn, mx; cin >> t; while (t--) { mx = 0; mn = 1000000000; cin >> n; while (n--) { cin >> a >> b; if (a >= mx) mx = a; if (b <= mn) mn = b; } if (mx >= mn) cout << mx - mn << "\n"; else cout << "0\n"; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e4 + 5; int arr[N]; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { fast(); int test; cin >> test; while (test--) { int n; cin >> n; vector<pair<long long int, long long int>> vc; long long int mini = 1e18, maxi = 0; for (int i = 0; i < n; ++i) { long long int x, y; cin >> x >> y; vc.push_back({x, y}); mini = min(mini, y); maxi = max(maxi, x); } cout << max(0ll, maxi - mini) << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); int left = 1e9, right = 0; for (int i = 1; i <= n; i++) { int l, r; scanf("%d %d", &l, &r); left = min(left, r); right = max(right, l); } printf("%d\n", max(0, (right - left))); } }
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, k; long long t; cin >> t; while (t--) { long long n; cin >> n; map<long long, long long> v, u; for (i = 0; i < n; i++) { long long a, b; cin >> a >> b; v[a]++; u[b]++; } if (v.size() == 1 || u.size() == 1) { cout << 0 << endl; continue; } auto it = v.end(); it--; auto itr = u.begin(); if (it->first > itr->first) { cout << abs(it->first - itr->first) << endl; } else cout << 0 << endl; } }
#include <bits/stdc++.h> using namespace std; long long i, j, k, l, n, m, mx = 0, mn = ((long long)1e18 + 9ll); int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int nt; cin >> nt; while (nt--) { cin >> n; mx = 0; mn = ((long long)1e18 + 9ll); for (i = 1; i <= n; i++) { cin >> k >> l; mn = min(mn, l); mx = max(mx, k); } cout << max(0ll, mx - mn) << '\n'; } exit(0); }
#include <bits/stdc++.h> using namespace std; const int INF = 2000000000 + 9; const long long BIG_INF = 4000000000000000000 + 9; const int mod = 1000000000 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<int, int> > a(n); int mxl = -INF, mnr = INF; for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; mnr = min(mnr, a[i].second); mxl = max(mxl, a[i].first); } if (mxl <= mnr) { cout << 0 << endl; continue; } sort(a.begin(), a.end()); cout << mxl - mnr << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, t, l, r, mi, ma; cin >> t; for (int i = 1; i <= t; i++) { ma = 0; mi = INT_MAX; cin >> n; for (int j = 1; j <= n; j++) { cin >> l >> r; if (l > ma) ma = l; if (r < mi) mi = r; } if (n == 1) { cout << 0 << endl; } else if (ma > mi) { cout << ma - mi << endl; } else { cout << 0 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, n, maxstart, minend, tmp; cin >> t; int* res = new int[t]; for (int i = 0; i < t; i++) { cin >> n; maxstart = 0; minend = numeric_limits<int>::max(); for (int s = 0; s < n; s++) { cin >> tmp; if (tmp > maxstart) maxstart = tmp; cin >> tmp; if (tmp < minend) minend = tmp; } res[i] = (maxstart < minend ? 0 : maxstart - minend); } for (int i = 0; i < t; i++) cout << res[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t, m; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; int an = 0, bn = 1e9 + 1; for (int j = 0; j < n; j++) { int a, b; cin >> a >> b; an = max(an, a); bn = min(bn, b); } if (an - bn > 0) cout << an - bn << '\n'; else { cout << 0 << '\n'; } } }
#include <bits/stdc++.h> using namespace std; int main() { int t, n, a, b; cin >> t; while (t--) { int rmin = INT_MAX, lmax = -1; cin >> n; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { cin >> a >> b; if (b < rmin) rmin = b; if (a > lmax) lmax = a; sort(v.begin(), v.end()); } cout << max(0, lmax - rmin) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, n; int x, y; cin >> t; for (int h = 0; h < t; ++h) { int fir = 1000000001, sec = -1; cin >> n; for (int i = 0; i < n; ++i) { cin >> x >> y; if (sec < x) { sec = x; } if (fir > y) { fir = y; } } if (fir >= sec) { cout << 0 << endl; } else { cout << sec - fir << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; vector<pair<long long, long long>> nums(n); long long end = INT_MAX, begin = 0; for (long long i = 0; i < n; i++) { cin >> nums[i].first >> nums[i].second; if (end > nums[i].second) { end = nums[i].second; } if (begin < nums[i].first) { begin = nums[i].first; } } if (begin - end <= 0) { cout << 0 << endl; } else { cout << begin - end << endl; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { solve(); } return 0; }