Submission

Status:

[-SSSS][SSSSS][SSSSSSSSSS]

Subtask/Task Score:

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

Score: 0

User: Zonezonee

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

Language: cpp

Time: 0.003 second

Submitted On: 2025-12-31 12:50:05

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    map<int, int> sweep;
    int total = 0;
    int a1, b1;
    for(int i = 0, x, y; i < n; ++i){
        cin >> x >> y;
        if(n == 1){
            cout << (y-x)/2+x << '\n';
            return 0;
        }
        if(i == 0) a1 = x, b1 = y;
        total += y-x;
        sweep[x]++;
        sweep[y]--;
    }
    int sum = 1, cnt = 0;
    for(auto [a2, b2] : sweep){
        if(a1 == a2) continue;
        // cout << a2 << ' ' << b2 << '\n';
        cnt += (a2-a1)*sum;
        if(cnt >= total/2){
            cnt -= (a2-a1)*sum;
            int x = total/2 - cnt;
            cout << a1 + (x)/sum << '\n';
            return 0;
        }
        sum += b2;
        // cout << a2 << ' ' << cnt << '\n';
        a1 = a2;
        b1 = b2;
    }
}
/*
2
1 7
5 15
*/