Submission

Status:

[-SSSS][SSSSS][SSSSSSSSSS]

Subtask/Task Score:

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

Score: 0

User: sulinx

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

Language: cpp

Time: 0.002 second

Submitted On: 2026-02-14 10:33:28

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<pair<int, int>> ranges;
    
    for(int i = 0; i < n; i++){
        int x, y;
        cin >> x >> y;
        ranges.push_back({x, y});
    }
    
    sort(ranges.begin(), ranges.end());
    vector<pair<int, int>> merged;
    
    for(auto [x, y] : ranges){
        if(merged.empty() || merged.back().second < x){
            merged.push_back({x, y});
        } else {
            merged.back().second = max(merged.back().second, y);
        }
    }
    
    long long K = 0;
    for(auto [x, y] : merged){
        K += (y - x);
    }
    
    long long target = (K + 1) / 2;
    long long count = 0;
    
    for(auto [x, y] : merged){
        long long len = y - x;
        if(count + len >= target){
            cout << x + (target - count - 1) - 1;
            return 0;
        }
        count += len;
    }
}