#include #include #include #include #include using namespace std; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } template T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N; cin >> N; vector> LR(N); for (auto &[l, r] : LR) cin >> l >> r; sort(LR.begin(), LR.end()); int all_l_max = -1, all_r_min = 1 << 30; for (auto [l, r] : LR) { chmax(all_l_max, l); chmin(all_r_min, r); } auto find_feasible = [&]() -> int { int now = -1; for (auto [L, R] : LR) { chmax(now, (int)L - 1); ++now; } return now - (N - 1); }; auto check = [&](int mid) -> bool { // [mid, mid + 1, ..., mid + N - 1] vector> dp(N); for (auto [l, r] : LR) { int left = max(l, mid) - mid, right = min(r, mid + N - 1) - mid; if (left <= right) dp.at(left).push_back(right); } priority_queue, greater> pq; REP(i, N) { for (auto r : dp.at(i)) pq.push(r); while (pq.size() and pq.top() < i) pq.pop(); if (pq.empty()) return false; pq.pop(); } return true; }; auto binsearch = [&](int ok, int ng) -> int { while (abs(ok - ng) > 1) { const int mid = floor_div(ok + ng, 2); (check(mid) ? ok : ng) = mid; } return ok; }; const int feasible = find_feasible(); if (!check(feasible)) { puts("0"); } else { int lo = binsearch(feasible, all_l_max - N); int hi = binsearch(feasible, all_r_min + 1); cout << abs(hi - lo) + 1 << '\n'; } }