Submission

Status:

[PPPPP][PPPPP][PPPPPPPPPP]

Subtask/Task Score:

{20/20}{30/30}{50/50}

Score: 100

User: Few500

Problemset: ห้องสมุดเมือง 3M

Language: cpp

Time: 0.003 second

Submitted On: 2026-03-21 19:55:13

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin >> n;
    long long total = 0;
    vector<pair<int, int>> library(n);
    for (int i = 0; i < n; i++)
    {
        cin >> library[i].first >> library[i].second;
        total += library[i].second - library[i].first;
    }
    if (total == 1)
    {
        cout << library[0].first;
        return 0;
    }

    int l = 0, r = 2e8;
    while (l <= r)
    {
        int mid = l + (r - l) / 2;
        long long cnt = 0;

        for (auto [start, end] : library)
        {
            if (start <= mid)
                cnt += min(mid, end - 1) - start + 1;
        }

        if (cnt >= total / 2)
            r = mid - 1;
        else
            l = mid + 1;
    }

    cout << l << '\n';
    return 0;
}