Submission
Status:
[PPPPP][PPPPP][PPPPPPPPPP]
Score: 100
User: Dormon
Problemset: ห้องสมุดเมือง 3M
Language: cpp
Time: 0.003 second
Submitted On: 2025-03-14 15:24:11
#include <iostream>
#include <vector>
#include <map>
using namespace std;
#ifdef DORMON
#define debug(x) cout << #x << " : " << x << " "
#else
#define debug(x)
#endif
int main()
{
int n;
cin >> n;
vector<pair<int, int>> v(n);
int book = 0, lb = 0, ub = 0;
for (auto &[l, r]:v){
cin >> l >> r;
r--;
book += r - l + 1;
ub = max(ub, r);
}
int med = book / 2, ans = 0;
if (med == 0){
cout << v[0].first << '\n';
return 0;
}
while (lb <= ub){
int mid = lb + (ub - lb) / 2;
int cnt = 0;
for (auto [l, r]:v){
if (mid >= r)
cnt += r - l + 1;
else if (mid >= l)
cnt += mid - l + 1;
}
if (cnt >= med){
ans = mid;
ub = mid - 1;
}
else
lb = mid + 1;
debug(lb);
debug(ub);
debug(mid);
debug(cnt);
}
cout << ans << '\n';
}