Submission

Status:

[PPPPP][PPPPP][PPPPPPPPPP]

Subtask/Task Score:

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

Score: 100

User: erng

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

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-03 23:05:07

#include <bits/stdc++.h>

using namespace std;

#define ll long long
int n;
ll a, b, pv, mx=LLONG_MIN, mn=LLONG_MAX, l=LLONG_MAX, r=LLONG_MIN, total, cnt,med, cu;
vector<pair<ll,ll>> v;

int main()
{
    // cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<=n; i++)
    {
        cin>>a>>b;
        mx=max(mx, b);
        mn=min(mn, a);
        total+=(b-a);
        v.push_back({a, b});
    }
    total=floor(total/2);
    sort(v.begin(), v.end());
    l=mn;
    r=mx;
    while (l<=r)
    {
        ll md=(r-l)/2+l;
        cnt=0;
        // cout<<md<<" ";
        for (auto [first, last] : v)
        {
            cu=min(last, md+1)-first;
            if (cu>0)
            {
                cnt+=cu;
            }
        }
        if (cnt>=total)
        {
            med=md;
            r=md-1;
        }
        else 
        {
            l=md+1;
        }
        
    }
    cout<<med;
}