Submission
Status:
[PP-SS][SSSSS][SSSSSSSSSS]
Subtask/Task Score:
{0/20}{0/30}{0/50}
Score: 0
User: Ryuthin94
Problemset: ห้องสมุดเมือง 3M
Language: cpp
Time: 0.002 second
Submitted On: 2026-03-06 09:42:46
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long s = 0;
cin >> n;
vector<pair<long long, long long>> lib(n);
vector<long long> coords;
for (int i = 0; i < n; i++)
{
cin >> lib[i].first >> lib[i].second;
coords.push_back(lib[i].first);
coords.push_back(lib[i].second);
}
sort(lib.begin(), lib.end());
sort(coords.begin(), coords.end());
coords.erase(unique(coords.begin(), coords.end()), coords.end());
for (int i = 0; i < n; i++)
{
s = s + (lib[i].second - lib[i].first);
}
long long t = s / 2;
long long cumsum = 0;
for (int i = 0; i + 1 < (int)coords.size(); i++)
{
long long left = coords[i];
long long right = coords[i + 1];
int cnt = 0;
for (int j = 0; j < n; j++)
{
if (lib[j].first <= left && right <= lib[j].second)
cnt++;
}
long long segment_books = (right - left) * cnt;
if (cumsum + segment_books >= t)
{
long long remaining = t - cumsum;
cout << left + (remaining - 1) / cnt << endl;
return 0;
}
cumsum += segment_books;
}
}