Submission
Status:
[PPPPP][PPPPP][PPPPPPPPPP]
Subtask/Task Score:
{20/20}{30/30}{50/50}
Score: 100
User: tha_smith
Problemset: ห้องสมุดเมือง 3M
Language: cpp
Time: 0.003 second
Submitted On: 2026-03-03 20:11:14
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0),cin.tie(0);
int N,mx=0,mn=INT_MAX; long long total=0; vector<pair<int,int>> R;
cin >> N;
for(int i=0; i<N; i++) {
int x,y;
cin >> x >> y;
R.push_back({x,y});
mn = min(mn,x);
mx = max(mx,y);
total += (long long)(y-x);
}
int target=floor(total/2),ans=0,l=mn,r=mx;
while(l<=r) {
int mid = ((r-l)/2)+l,test=0;
for(auto n:R) {
int u = n.first;
int v = n.second;
long long cnt = min(mid+1,v)-u;
if(cnt>0) {
test+=cnt;
}
}
if(test>=target) {
ans=mid;
r=mid-1;
}
else {
l=mid+1;
}
}
cout << ans;
}