During one of the space missions, humans have found an evidence of previous life at one of the planets. They were lucky enough to find a book with birth and death years of each individual that had been living at this planet. What's interesting is that these years are in the range (1, 10^9)! Therefore, the planet was named Longlifer.
In order to learn more about Longlifer's previous population, scientists need to determine the year with maximum number of individuals that were alive, as well as the number of alive individuals in that year. Your task is to help scientists solve this problem!
Input
The first line contains an integer n (1 ≤ n ≤ 10^5) — the number of people.
Each of the following n lines contain two integers b and d (1 ≤ b < d ≤ 10^9) representing birth and death year (respectively) of each individual.
Output
Print two integer numbers separated by blank character, y — the year with a maximum number of people alive and k — the number of people alive in year y.
In the case of multiple possible solutions, print the solution with minimum year.
Examples
Input
3
1 5
2 4
5 6
Output
2 2
Input
4
3 4
4 5
4 6
8 10
Output
4 2
Note
You can assume that an individual living from b to d has been born at the beginning of b and died at the beginning of d, and therefore living for d - b years.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to compute the year with maximum number of people alive. Input: n lines, each with birth b and death d (b<d). The individual is considered alive during [b, d) (i.e., inclusive of b, exclusive of d) because they are born at beginning of b and die at beginning of d. So alive in years b, b+1, ..., d-1. So the duration is d-b years. We need to count alive per integer year. Range of years is large (1 to 1e9). But n is up to 1e5, so we need to use sweep line.
We can treat events: at year b, +1; at year d, -1. Then we can sort all events, and then sweep through sorted distinct years. Compute the maximum alive count and the smallest year at which it occurs.
We need to handle that the person is alive at year b and not at year d. So event at b: add +1; at d: add -1. Then for any year between events, the count is constant. So we can process sorted events, maintain current count. But note: The years are integer. However, we need to consider that the count changes at the event year itself. Since we are interested in the count at each integer year, we need to know how events affect exactly. Let's define:
At start of year b, person becomes alive. So for year b, they are alive.
At start of year d, person dies, so they are not alive for year d. So for year d, they are dead. So the -1 should affect year d and beyond.
Thus, if we define events at points: +1 at b, -1 at d. Then if we consider the "step function" that is constant on intervals [year, next_year) with integer boundaries, we need to decide representation. Typically, we can treat the count for year y as the value after applying all events with event year <= y? But careful: Because events happen at the beginning of the year. Let's model:
Let f(y) = number alive during year y. At year y, a person is alive if b <= y < d.
Thus, if we process events sorted by year, and maintain a current count that applies to all years from current year up to next event year - 1. At the exact event year, the change takes effect at the beginning of that year. So for year b, after the +1 event, the person is alive. So we should apply the +1 before counting year b. Similarly, at year d, the -1 applies at the beginning, so person not alive for year d. So before counting year d, we apply -1.
So in sweep, we can iterate over sorted unique event years. For each event year y, we first apply all events at year y (i.e., update count). Then after applying, the count is the number of people alive for year y (and for subsequent years until next event). But careful: If multiple events at the same year, both births and deaths happen at that year. They all happen at the beginning, so order among them? Birth and death at same year? b and d can be equal? No, b<d, so no person dies same year they are born? Actually b<d, so b and d are distinct. But there could be multiple persons with same b or same d. For a given year y, both births and deaths could occur: some persons are born (b=y) and some die (d=y). Since both happen at beginning of year, the deaths occur before the births? Or simultaneously? We need to define convention: At the beginning of year, births and deaths happen simultaneously? But then for that year, are those who die considered alive? Typically, if someone dies at the beginning of year, they are not alive during that year. And if someone is born at beginning, they are alive during that year. So both events effectively happen at the same time. But the order may affect count for that year if we treat them as sequential. However, if we consider that the count during year y should reflect births at beginning but not deaths, because those who die at beginning are not alive for any part of the year. Meanwhile, births at beginning are alive for whole year (assuming they live the entire year). So the net effect for year y should be: add all births at y, subtract all deaths at y. So the count after processing all events at y (both + and -) is the correct alive count for year y. Because the deaths at year y remove people who would otherwise be alive that year, but they are dead right at start, so they are not counted. The births add people. So it's fine to apply all events at year y before counting year y.
Thus algorithm: collect events: for each person, an event (b, +1), (d, -1). Sort all events by year (ascending). Then iterate through events grouped by year. For each distinct year y, we first add the net delta from all events at y to current count. Then after updating, the count is the number alive at year y (and onward until next event). Then we can check if this count > max_count or (count == max_count and y < best_year). Note: Since the count is constant for years from y to next_event_year-1, but we only care about the maximum count and earliest year achieving it. However, there might be a scenario where the maximum count occurs in a year between event years, but the count is constant across that interval. So the maximum count for any year in that interval is the same constant. The earliest year achieving that max would be the start of that interval if that interval contains the max. In our sweep, after processing events at year y, the count becomes c, which holds for years y, y+1, ..., next_y - 1. So if c is greater than current max, we set best_year = y (since that's the earliest year where this new max appears). If c equals current max, we might need to keep current best_year if it's earlier; but if the current best_year is earlier, we keep it. However, consider case where the maximum count occurs across multiple intervals, and the earlier interval has same count but earlier year. So we pick the minimal year among those achieving max. Since we sweep in increasing year order, when we encounter a new interval with count > max, we set best_year = current y (which is start of interval). If later we get same count, we ignore because year is larger. That's fine.
But there's a nuance: Could the maximum occur in a year before the first event? The first event year might be >1. For years from 1 to first_event_year - 1, count is 0 (since no births yet). So max might be 0? But constraints n>=1, so there will be births, so max >=1. But if all births start later, max would be >=1 later, so we don't worry about year 1 being zero. But we need to handle case where maximum count is zero? That would only happen if n=0, but n>=1, so at least one person alive at their birth year, so max>=1. So it's fine.
Edge case: What about years after last event? After last death, count becomes 0. Not max if max>0.
Implementation details: Use vector of pairs (year, delta). For each person: push (b, +1) and (d, -1). Then sort by year. Then iterate: i=0 while i < 2n. While i < 2n, get current year = events[i].year; accumulate delta sum for that year (while i < 2*n and events[i].year == current year) { cur_delta += events[i].delta; i++; } Then apply delta: cur_alive += cur_delta. Now cur_alive is count for that year and onward until next event year (exclusive). Then check if cur_alive > max_alive: update max_alive and best_year = current year. Else if cur_alive == max_alive: if current year < best_year, update? But since we are scanning in increasing year, if we get equal later, it will be larger year, so no need to update. But careful: If we have two intervals with same count and the earlier interval started earlier, we already recorded the earlier year. So later equal intervals shouldn't replace. So only > case.
But is it possible that the max count occurs not at an event year but between events? Since count is constant between events, the maximum count occurs at the start of an interval if the interval's count is the maximum. However, consider scenario: The count changes at year y due to events. After processing events at y, the count is c for years y to next_event-1. If c is maximum, then the earliest year achieving that is y. That's fine.
But what about the possibility that the maximum count appears exactly at the year of an event but before applying the events? For instance, suppose at year b, before applying births, the count might be high, but after applying births it becomes even higher? But we must count the year b with births included, because they are alive during b. So applying before counting is correct. However, we must ensure we are not missing the possibility that the maximum is before applying the events at a particular year? Could that be the case if there are deaths at year d? For year d, before applying deaths, the count includes those who die at d? But they are not alive during d because they die at beginning. So we should apply deaths at d before counting year d. So our method is correct.
Alternatively, we can think in terms of prefix sum: f(y) = number of births <= y minus number of deaths <= y? But careful: Since death at d should not count for year d, we need to count deaths with year < y? Because if d=y, then we don't want to count that person. So f(y) = # of births b such that b <= y minus # of deaths d such that d <= y? That would subtract deaths at year y as well, so f(y) = births up to y minus deaths up to y. But that counts person with d=y as not alive at y because both b<=y and d<=y true, so they are subtracted. However, what about person with b=y? b<=y true, and d>y (since d > b), so d>y, so not subtracted. So that yields f(y) = number of people with b <= y < d. So indeed f(y) = (# births with b <= y) - (# deaths with d <= y). So we can compute f(y) using two sorted arrays? But we need max over all y. So we can compute the change points and sweep as above. But careful: The function f(y) is piecewise constant, and changes at points b and d. So events approach is simpler.
Sorted events:
(1,+1)
(2,+1)
(4,-1)
(5,+1) and (5,-1) from first and third? Actually we have (5,-1) from first, (5,+1) from third. So two events at 5.
(6,-1)
Now sweep:
Start cur=0, max=0.
Year 1: delta +1 => cur=1. cur>max? yes -> max=1, year=1.
Year 2: delta +1 => cur=2. max=2, year=2.
Year 4: delta -1 => cur=1. cur=1 < max.
Year 5: delta = (+1 -1) = 0? Actually we need to combine: at year 5, events: -1 (from first) and +1 (from third). So net = 0. So cur remains 2? Wait after year 4, cur=1. Then at year 5, we apply net 0, cur stays 1? But wait, after year 4 we had cur=1. Then at year 5, we should account that the person born at 5 is alive at 5, and the person who died at 5 is not alive at 5. The person with (1,5) died at 5, so they are not alive at 5. The person with (5,6) is alive at 5. So we have: person (2,4) died at 4, so not alive at 5. So only person (5,6) alive at 5? Actually also person (1,5) is dead at 5, so not alive. So only one person alive at 5. So cur=1 is correct. But is there any person alive at 5? Only the one born at 5. So cur=1. But example output says max=2 at year=2. So correct.
Now check if any year gives 2? Year 2: both (1,5) and (2,4) are alive. So max=2. So algorithm gives best_year=2, max=2. Good.
Sweep:
cur=0.
Year 3: delta +1 => cur=1. max=1,year=3.
Year 4: net +1 => cur=2. cur>max => max=2, year=4.
Year 5: delta -1 => cur=1.
Year 6: delta -1 => cur=0.
Year 8: delta +1 => cur=1.
Year 10: delta -1 => cur=0.
So best_year=4, max=2. Matches.
Thus algorithm works.
Complexity: O(n log n) due to sorting 2n events.
Potential pitfalls: Input size up to 1e5, so 2e5 events, fine.
Now implement in C++.
But careful: The years can be up to 1e9, but we only handle events.
Edge Cases:
Multiple events same year, net zero: still need to apply, but count unchanged.
If the maximum count appears at year y, but later at same year due to events? Actually we apply all events at same year before counting, so we get one count for that year.
What about year intervals with constant count after last event? That is zero, not max if max>0.
n=1: then one person from b to d-1. So count=1 for years b..d-1. So max=1, earliest year = b. Our method: events: (b,+1), (d,-1). Sweep: at b: cur=1 -> max=1, year=b. Good.
Cases where the maximum count occurs in a year that is not an event year but after an event? Actually after processing events at year y, the count holds for years y... next_event-1. So the earliest year with that count is y. So we capture it.
But what about scenario where the maximum count is achieved at a year that is strictly between event years, but the count at the start of interval is not maximum because maybe the count increased at an event and then decreased later, but the maximum might be at the start of interval if the count is higher than previous. So it's fine.
But consider scenario: The events happen at year y and y+10. After processing at y, cur = c. This holds for years y, y+1, ..., y+9. The max may be c. We set best_year = y. But suppose that there is another year between y and y+10 that is earlier than y? That can't be. So it's correct.
Now confirm handling of earliest year in case of ties: We choose the smallest year with max count. Since we process in increasing order and only update on strictly greater count, we keep earliest.
But careful: Could there be a tie where the max count appears at some year that is earlier than the start of an interval because the count might be equal to max before applying events at that year? For example, at year y, before applying events, the count might be max, but after applying events it becomes lower. But we must consider the count for year y after applying events. Because the events happen at the beginning of the year, so the count for year y is after applying them. So it's not possible that the count for year y before applying events is relevant. So our method is correct.
Thus the solution is straightforward.
Implementation details:
We'll read n, then loop n times reading b and d. Use vector<pair<int, int>> events (year, delta). push_back({b, 1}), push_back({d, -1}). Then sort events by year.
Then sweep:
long long cur = 0; // maybe up to n=1e5, so int is fine, but use int.
int max_alive = 0, best_year = 0;
int i=0;
while(i<events.size()){
int year = events[i].first;
int delta_sum = 0;
while(i<events.size() && events[i].first == year){
delta_sum += events[i].second;
i++;
}
cur += delta_sum;
if(cur > max_alive){
max_alive = cur;
best_year = year;
}
}
Then print best_year and max_alive.
Check with examples: works.
But wait: In the first example, after processing year 5, cur becomes 1. But is that correct for year 5? The events at year 5: one death and one birth. Both happen at beginning. After applying both, cur=1. Good. For year 5, the person born at 5 is alive, person who died at 5 is not. So correct.
Potential nuance: What about person with d = b+1? For example, born at 1, dies at 2. Then alive only at year 1. Events: (1,+1), (2,-1). Sweep: at year 1: cur=1; at year 2: cur=0. So count for year 1 = 1, year 2 = 0. Good.
Now consider if we have person with b=1, d=1? Not allowed since b<d.
Thus solution seems correct.
But we should consider that the number of events can be up to 2*10^5, sorting is fine.
Alternative approach: use map to aggregate deltas, but sorting vector is faster.
We can also compress years: but we don't need to, as we just sort.
Edge case: If there are births and deaths at same year? Not per person, but different persons could have b = some year and d = same year. That's allowed. At that year, we need to net the deltas. Our grouping handles that.
Now consider scenario: Two events at same year: birth (+1) and death (-1). Net zero. But after applying, count unchanged. That's fine.
Now confirm output format: two integers separated by blank.
Now test with a custom case: n=2: (1,3) and (2,4). Alive: year1:1, year2:2, year3:1 (since first alive until 3? Actually first alive years 1,2; second alive years 2,3). So max=2 at year2. Events: (1,+1),(3,-1),(2,+1),(4,-1). Sorted: (1,+1),(2,+1),(3,-1),(4,-1). Sweep: y1: cur=1; max=1; y2: cur=2; max=2; y3: cur=1; y4: cur=0. Output 2 2. Good.
Another test: n=2: (1,5) and (5,10). Alive: year1-4:1, year5: both? Person1 dies at beginning of 5, person2 born at beginning of 5, so at year5 person1 is dead, person2 is alive, so only 1. So max=1 at year1. Events: (1,+1),(5,-1),(5,+1),(10,-1). Sorted: (1,+1),(5,-1),(5,+1),(10,-1). Sweep: y1: cur=1; max=1; y5: net 0? Actually after grouping: at y5, deltas: -1 and +1 => net 0, so cur remains 1? Wait after y1 cur=1. Then at y5 we apply net 0, cur=1. So we would say cur=1 at year5 also. That would indicate count=1 at year5, which is correct. However, we then have cur=1 equal to max, but we don't update because not greater. So best_year remains 1. That's correct because max=1 earliest year is 1. But is there any year with count 1? Yes, both years 1-4 and 5-9 have count 1. The earliest is 1. Good.
But careful: The sweep after y1, cur=1 and holds for years 1,2,3,4. At y5, we apply events and cur stays 1, so for years 5-9, cur=1. Our algorithm only checks at year5, cur=1 == max but not >, so best_year remains 1. That's correct earliest.
Now consider case where max count occurs after a net-zero event at that year? For example, suppose before year y, count = M-1. At year y, there is both a birth and death of a person? Actually net-zero but the composition changes? But count remains M-1. If later at year y+1, another birth occurs raising to M, and that M is the maximum. Then earliest year with M is y+1. Our sweep will capture it when processing y+1. So fine.
Now let's test scenario with overlapping intervals that produce a high count that starts at a death? That's impossible because death reduces count. So high count always starts at a birth event.
Thus solution done.
But wait: There's nuance about the interpretation: "You can assume that an individual living from b to d has been born at the beginning of b and died at the beginning of d, and therefore living for d - b years." So indeed alive for integer years b, b+1, ..., d-1. So our model is correct.
Now consider potential off-by-one if we treat death event as -1 at d. Then the count for year d should be after applying -1. As we do.
Hence implementation is straightforward.
One more nuance: The year range is up to 1e9. But we use int (32-bit) for year? 1e9 fits in 32-bit signed int (max 2.147e9). So int is fine. But some languages may require long long, but C++ int is fine up to 2e9.
We need to read up to 1e5 lines, use fast IO.
Now ensure that we store events as pair<int,int>. Use vector. Sorting pair<int,int> sorts by first then second. But order of deltas within same year doesn't matter because we sum them.
Now complexity: O(n log n). n=100k, fine.
Edge case: if there is a person with b=10^9, d=10^9+? but d <=10^9? Actually d ≤ 10^9, so b can be 10^9? but b<d and d ≤ 10^9, so b < 10^9, so max b is 10^9-1. So year fits.
Now test with random.
One more corner: If the maximum count occurs at year that is not an event start because the count increases at a death? That doesn't happen. Only births increase count, deaths decrease. So the start of a constant interval is at an event year (either birth or death). So it's safe.
But consider this: The count may increase at a birth event, but after applying that event, the count becomes high. But could there be a death event at the same year that reduces it? Then net could be positive, negative, or zero. So effectively the count changes at that year to a new value. So the earliest year with that new value is that year.
Now confirm with a scenario: Suppose we have two persons: (1,10) and (2,3). Events: (1,+1),(10,-1),(2,+1),(3,-1). Sorted: (1,+1),(2,+1),(3,-1),(10,-1). Sweep: y1: cur=1; y2: cur=2; y3: cur=1; y10: cur=0. Max=2 at y2. Good.
Now scenario where the max appears at year 3? Not here.
Thus algorithm is correct.
Now we need to output two integers: y and k.
Now we must consider that if there are multiple years with same maximum, we need the minimum year. Our algorithm picks the first occurrence when a new maximum is found. Because if later intervals have same max, we don't update. However, is it possible that the maximum appears at an earlier year than the first time we encounter that max? For example, suppose the maximum count is 5 and appears at year 5, but later at year 10 we also get 5. Our algorithm will set best_year = 5 when we first see count 5, because it's > previous max. That's earliest. But what if the maximum count first appears at a year that is not the earliest because we didn't consider years before that event? For instance, suppose we have an interval from year 1 to year 100 with count 0? Actually max wouldn't be zero. But if the maximum count appears in the middle, it's the first time we achieve that count. Could there be a situation where the maximum count is achieved earlier but we missed because we didn't consider years between events? For example, after an event at year y, the count becomes c, and that c is the maximum. But perhaps there was a year before y that also had the same maximum count? That would mean that before applying events at y, count was already that maximum. But then count before y would be constant from previous event to y-1. If that constant was also the maximum, we would have set best_year at that earlier event year when that count first appeared. Because when we processed that earlier event, we updated count and then that count held for the interval. So the earliest occurrence of that count is the start of that interval. So we would have captured it earlier. So no.
Thus algorithm yields earliest.
Now, we should think about potential pitfalls with negative count? Shouldn't happen because deaths only happen after births, but we might have events order that could temporarily cause negative if we mis-order? But since we apply all events at same year together, it's fine. However, consider we have death event before birth event at same year? For same person, b<d, so never death before birth. For different persons, it's possible that a person dies at year y and another is born at same year y. At year y, we apply both events. The net could be zero, positive, or negative. Could net be negative? That would mean more deaths than births at that year. But deaths at year y correspond to people who were born earlier, so they were alive in previous year. So before year y, count includes them. After applying deaths and births, if net negative, count decreases. It's possible that before year y, count was, say, 10, with 3 deaths and 1 birth at y, net -2, so new count = 8. That's fine. But could count become negative? No, because deaths cannot exceed the number of people alive. But if input is inconsistent? Not possible because each death corresponds to a person who was born earlier and not yet died? But there is no guarantee that the data is consistent? Possibly they could have a death without a corresponding birth? But input is just list of individuals: each has b and d. So for each individual, they have a birth. So each death is paired with a birth of the same individual. So overall, the number of births up to any point is at least the number of deaths up to that point. So prefix sum always non-negative. But in our event processing, as long as we apply events in correct order, we maintain non-negative count. However, if we grouped events by year and apply net delta, it could be negative for that year's net? That would mean in that year, more people died than were born. That's possible: e.g., 2 deaths and 1 birth at year y, net -1. But before applying, count should be at least 2, so after net -1, count is >=1. So fine.
So no negative.
Now memory: vector of size 2n.
Edge Cases with large n: 1e5, sorting 2e5 integers is fine.
Now double-check for potential overflow: cur is up to n (1e5), int fine.
Now think about alternative interpretation: Some classic "Maximum number of people alive" problem usually defines that person is alive from birth year to death year inclusive? But here note: they say living for d-b years, so exclusive of d. So our interpretation is correct.
Now to be thorough, let's verify with a simple case: b=1, d=2 => alive for 1 year (2-1=1). That's year 1 only. So correct.
Now confirm if the year with maximum might be after the last death? That would be zero, not max unless max is zero, but n>=1 so max>=1.
Now output format: two integers separated by blank. In examples, they have a blank line before input? The problem statement shows input format with blank line? Actually the example shows:
Input
3
1 5
2 4
5 6
There is an empty line before 3. Probably just formatting. We ignore.
Now code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
code
int n;
cin >> n;
vector<pair<int, int>> events;
events.reserve(2*n);
for (int i = 0; i < n; ++i) {
int b, d;
cin >> b >> d;
events.emplace_back(b, 1);
events.emplace_back(d, -1);
}
sort(events.begin(), events.end());
int cur = 0, max_alive = 0, best_year = 0;
int i = 0;
while (i < events.size()) {
int year = events[i].first;
int delta = 0;
while (i < events.size() && events[i].first == year) {
delta += events[i].second;
++i;
}
cur += delta;
if (cur > max_alive) {
max_alive = cur;
best_year = year;
}
}
cout << best_year << " " << max_alive << "\n";
return 0;
}
That should work.
But wait: The year with maximum could be at the very first year, but we initialize best_year to 0. If max_alive is 0 initially and we never update? But max_alive will become >0 at first birth year, so best_year will be set. So fine.
Now what about multiple events at same year with net zero and cur remains same as max? Suppose we have max already at 5 from earlier interval, and later at year y, after net zero, cur stays 5. But since cur == max, we don't update. That's fine because year y is later, we want earliest.
But what if earlier we set best_year to some year, but later we have another interval with same max, but the year might be earlier than best_year? That can't because we process in increasing order.
Now consider a case: The maximum occurs at a year that is not an event year? For instance, suppose events: birth at 1 (+1) -> cur=1, then death at 3 (-1) -> cur=0. So count is 1 for years 1 and 2. So max=1. The earliest year with max=1 is year 1. That's an event year. But could max occur at year 2? That's not an event year, but count 1 is same max. However earliest is year 1. So answer year=1. That's fine. But what if max appears only at year 2 but not at any event year? That would mean that count is higher at year 2 than at year 1 and year 3. But count only changes at events. So if count is higher at year 2 than at year 1, then it must have increased at an event at year 2. Because count is constant on intervals, so if it's higher in (year 2, year 3) than in (year 1, year 2), then there is an increase at the boundary, i.e., at year 2. But the boundary is an event year. So the new count starts at year 2. So the earliest occurrence of that new count is year 2. So we capture it at event year 2. So no issue.
Thus algorithm is solid.
Potential pitfall: The event at death d reduces count for year d. But what about year d-1? It's included. So we are fine.
Now think about input constraints: n up to 1e5, years up to 1e9. Use int.
Now we must output two integers separated by space. Good.
Now consider the possibility of using map to aggregate deltas to simplify grouping. That would be O(n log n) as well. But sorting vector is fine.
Now final code.
But wait: The problem statement says "Print two integer numbers separated by blank character, y — the year with a maximum number of people alive and k — the number of people alive in year y. In the case of multiple possible solutions, print the solution with minimum year." So that's what we do.
Now test with example 2: Output 4 2, matches.
Now we should test with some random data to ensure correct. I'll simulate a small case manually.
n=3: (1,3), (2,5), (4,6). Alive years:
Person1: 1,2
Person2: 2,3,4
Person3: 4,5
So per year: year1:1, year2:2 (p1,p2), year3:1 (p2), year4:2 (p2,p3), year5:1 (p3). So max=2 at years 2 and 4, earliest =2.
Events: (1,+1),(3,-1),(2,+1),(5,-1),(4,+1),(6,-1). Sorted: (1,+1),(2,+1),(3,-1),(4,+1),(5,-1),(6,-1). Sweep: y1: cur=1, max=1,y=1; y2: cur=2, max=2,y=2; y3: cur=1; y4: cur=2; since cur=2 == max but not >, keep y=2; correct.
Now test with scenario where there is a death that reduces count but later at same year a birth raises count to new max: e.g., (1,10) and (10,20)? Actually (10,20) birth at 10, death at 20. For year 10, person1 dies at beginning, person2 born at beginning, so count=1. So max may be 1 if only these two? But person1 alive years 1-9, person2 alive years 10-19. So max=1 at years 1-9 and 10-19, earliest=1. Our events: (1,+1),(10,-1),(10,+1),(20,-1). Sorted: (1,+1),(10,-1),(10,+1),(20,-1). Group: y1: cur=1; y10: net 0? Actually at y10, deltas -1+1=0, cur stays 1. So max=1, best_year=1. Good.
But suppose we have (1,10), (9,11): Person1: alive 1-9; Person2: alive 9-10? Actually Person2 born 9, dies 11: alive years 9,10. So alive: year1-8:1 (p1), year9:2 (p1,p2), year10:1 (p2). So max=2 at year9. Events: (1,+1),(10,-1),(9,+1),(11,-1). Sorted: (1,+1),(9,+1),(10,-1),(11,-1). Sweep: y1: cur=1; y9: cur=2; y10: cur=1; y11: cur=0. max=2, best_year=9. Correct.
Now what about (1,5), (2,3), (4,7): Alive: p1:1-4, p2:2, p3:4-6. So years:1:1,2:2 (p1,p2),3:1 (p1),4:2 (p1? actually p1 alive? p1 b=1 d=5 => alive years1-4, so at year4 alive; p2 d=3, so dead at 3; p3 b=4 => alive year4; so year4: p1,p3 =2), year5: p3? p1 died at beginning of 5? p1 d=5 => not alive year5; p3 alive year5; so year5:1, year6:1 (p3), year7:0. So max=2 at years2 and4, earliest=2. Events: (1,+1),(5,-1),(2,+1),(3,-1),(4,+1),(7,-1). Sorted: (1,+1),(2,+1),(3,-1),(4,+1),(5,-1),(7,-1). Sweep: y1:1, y2:2->max, y3:1, y4:2 (equal, keep y2), y5:1, y7:0. Good.
Now consider case where multiple events at same year produce a new max that is higher than before, but the net increase may be due to births > deaths. For example, before year 10 count=5. At year 10, there are 3 births and 1 death => net +2 => count=7. So max=7 at year10. Our grouping will sum net +2, cur=7, check >, update. Good.
Now potential subtlety: Could there be a scenario where after processing events at year y, the count becomes max, but earlier in that same year, before applying events, the count was also max? For year y, the count after applying events is the count for year y. So we don't care about before applying. So fine.
Now think about performance: sorting 200k pairs, fine.
Now output: Use endl? We can use "\n".
Now final answer: I'll provide the solution.
But wait: The problem statement says "During one of the space missions, ..." Blah. Input: first line integer n (1 ≤ n ≤ 10^5). Each following n lines contain two integers b and d (1 ≤ b < d ≤ 10^9). Output: Print two integer numbers separated by blank character, y — the year with a maximum number of people alive and k — the number of people alive in year y. In the case of multiple possible solutions, print the solution with minimum year.
We must ensure to handle large input fast. Using scanf/printf is also fine, but with ios_base sync it's okay.
Now possible alternative: Use map<int,int> to accumulate deltas, then iterate over map in order. That would be simpler: for each person, mp[b]++, mp[d]--. Then iterate over sorted keys, maintain cur, and update max. But map insertion O(log m) per event, with m up to 2n distinct years. That's O(n log n) as well. But using vector and sort is generally faster due to constants. But both acceptable. I'll use vector.
One nuance: When using vector<pair<int,int>> and sorting, if there are many events at same year, grouping as we do is efficient. Alternatively, we could accumulate deltas in map first, then iterate, which would automatically group. But map may be simpler to code. However, with up to 1e5 events, map is fine. But I think vector sorting is fine.
But careful: There is a subtle bug: In our grouping loop, we update cur after processing all events at that year. But what about the interval between that year and the next event year? The count for years year+1 to next_year-1 is also cur. But if that cur is greater than max, the earliest year with that cur would be year+1, which is earlier than next event year? Actually, if cur is greater than max, we would have updated at year. But if the max occurs in that interval but not at year, then cur must be the same as at year, because it's constant. So if cur > previous max, then we update at year and get earliest year (year). So it's fine. If cur == previous max and the previous max was set at a year earlier, we keep earlier. So no issue.
But what about the scenario: Suppose we have events: at year 5, net delta = -1, decreasing count from 10 to 9. But the maximum count might be 10, which occurred in years 1-4. We captured that at year 1 (or earliest year when count became 10). Good.
Now, could there be a scenario where the maximum occurs at a year that is strictly between events, and the count at the start of the interval (event year) is not the maximum because it's lower, but then later, due to an event at the same year? That can't happen because count is constant on interval.
Thus algorithm correct.
Now answer.
But wait, we need to be careful: The problem might expect that the year with maximum alive should be reported as the year with the maximum number of individuals alive, and in case of multiple, the minimum year. However, what if the maximum occurs over a range of years, and the earliest year in that range is not an event year? For example, suppose we have births at year 1 and 2, deaths at year 5 and 6. So count: year1:1, year2:2, year3:2, year4:2, year5:1? Actually if deaths at 5 and 6, then for person1 b=1,d=5 => alive years 1-4; person2 b=2,d=6 => alive years 2-5. So alive: year1:1, year2:2, year3:2, year4:2, year5:1, year6:0. So maximum =2 occurs at years 2,3,4. Earliest year with max is 2, which is an event year (birth). So again event year.
But can we construct a case where the maximum count range starts at a year that is not an event year? That would require that the count increases not at an event boundary. But count only changes at events, so the start of a new constant segment is always at an event year. So the earliest year of a constant segment is an event year. So max will be caught at an event year. So it's fine.
Now test with scenario: count is constant from year 1 to 10 at 0? Not max.
Thus algorithm is correct.
Now discuss potential issues with using int for year and delta. Fine.
Now I'm going to produce final answer.
But wait: In the sample input there is a blank line before the first integer? That's likely formatting. We'll ignore.
Now final code.
I think it's done.
One more thought: Could there be any off-by-one due to the death event being applied at d, but if we consider year d, the person is dead, so we shouldn't count them. Our method does that. Good.
Now answer.
But before finalizing, I will check a tricky case: n=2: (1,2) and (2,3). Person1 alive year1. Person2 alive year2. So alive: year1:1, year2:1. max=1. Which year is earliest? 1. Events: (1,+1),(2,-1),(2,+1),(3,-1). Sorted: (1,+1),(2,-1),(2,+1),(3,-1). Group at year2: net 0? Actually -1+1=0, so after year2 cur stays 1? Let's compute: start cur=0. Year1: cur=1 -> max=1, best=1. Year2: apply net 0, cur=1. Since cur == max, we keep best=1. Year3: cur=0. So output 1 1. That is correct earliest.
Now another: (1,3) and (2,3)? Person1:1,2; Person2:2? Actually Person2: b=2, d=3 => alive year2 only. So alive: year1:1, year2:2, year3:0. Max=2 at year2. Events: (1,+1),(3,-1),(2,+1),(3,-1). Sorted: (1,+1),(2,+1),(3,-1),(3,-1). Year1: cur=1; year2: cur=2 -> max=2; year3: net -2, cur=0. So output 2 2. Good.
Now scenario: (1,4), (2,5), (3,6). Alive: year1:1, year2:2, year3:3, year4:2? Actually person1 alive years1-3, person2 alive years2-4, person3 alive years3-5. So year3: all three alive => 3. So max=3 at year3. Events: (1,+1),(4,-1),(2,+1),(5,-1),(3,+1),(6,-1). Sorted: (1,+1),(2,+1),(3,+1),(4,-1),(5,-1),(6,-1). Sweep: y1:1; y2:2; y3:3 -> max=3; y4:2; y5:1; y6:0. So best_year=3.
All good.
Now I think it's ready.
But wait: We must consider that the birth and death years are up to 1e9, but we use int. In C++, int is typically 32-bit and can hold up to 2e9, so okay.
Now finalize.
But maybe there is a more subtle nuance: The problem states "living from b to d has been born at the beginning of b and died at the beginning of d, and therefore living for d - b years." This is exactly as we used. So answer.
Now I'm done.
However, I recall a known problem "Maximum population year" from LeetCode, but there they use inclusive of death? Actually LeetCode 1854? "Maximum Population Year" uses inclusive of death? Let's recall: LeetCode 1854: "Maximum Population Year" gives logs with birth and death years, and they consider that the person is alive from birth to death inclusive? Actually in that problem, they say: "A person's birth year is x and death year is y, they were alive in years [x, y] inclusive of both." But here it's different: they are born at beginning of b and die at beginning of d, so not alive in year d. So our handling is correct.
Now implementation details: use vector and sort. But be careful about memory: 2e5 pairs of ints is fine.
Now we can write a more concise version using map:
map<int,int> mp;
for each person: mp[b]++; mp[d]--;
int cur=0, max=0, year=0;
for(auto& p: mp) { cur+=p.second; if(cur>max) { max=cur; year=p.first; } }
But using map will also work. However, map may be slower due to tree overhead, but for 1e5 it's okay. I'll stick with vector for speed.
Now final answer.
But there is a potential nuance: In the map approach, if we do cur+=p.second and then check, we are effectively grouping by year. That's simpler. But we need to ensure that for the same year, we apply all deltas before checking. The map iteration does that because we have aggregated deltas. So it's fine.
We'll decide to use map for simplicity? Actually both are easy. I'll use vector and manual grouping for demonstration of algorithm, but map is also fine. However, we need to be mindful that using map might cause slower performance if many distinct years, but 2e5 distinct years is max, but map operations are O(log n) each, and 2e5 is fine. But sorting 2e5 ints might be faster. I'll go with vector.
Now the answer.
But before final, I want to ensure that the solution passes edge cases with large n. The only potential issue is integer overflow in cur? n max 1e5, so cur fits in int.
Now let's think about if there are multiple events at the same year and we process them all together, we must ensure that we are not missing any possible maximum that occurs between events of the same year? That's not possible.
Now what about the case where the maximum occurs at a year that is earlier than the first event? That would be count 0, but n>=1 so max>0. So fine.
Now code ready.
Now provide final answer.
But perhaps the problem expects the output as two integers separated by space, and newline. So we'll do.
Now consider possible misinterpretation: The statement "living for d - b years" implies they are alive in years b, b+1, ..., d-1, which is d-b distinct years. So we are correct.
Now final code.
I will include necessary headers: #include , #include , #include using namespace std;.
Now finalize.
One more thought: The sweep algorithm as described works if we process events in order and apply all events at a given year before counting that year. However, there's an alternative common approach: treat events as points, sort them, and then as we iterate, we update count after processing each event? But that would be wrong because if we process a death event at year d before counting year d, we need to subtract before counting. But if we process events one by one and update after each event, then the count after processing an event corresponds to the count for years >= that event's year? But if we encounter a death event at year d, and we update count -=1, then the count for year d would be after death. That's correct. But what about births? For a birth event at year b, updating count +=1 before counting year b would also be correct. But what about the interval between events? For example, after processing event at year b (birth), the count becomes new value, which should hold for years from b to next event year - 1. But if we check for maximum after each event update, we need to consider that the new count applies to the current event year. However, if we process events sequentially, we might have multiple events at same year; we need to handle them together or order matters. For example, if we have a death and a birth at same year, and we process death first then birth, then after death count decreases, after birth count increases. The intermediate count after death might be lower, but that lower count does not correspond to any year (since both happen at beginning simultaneously). So we need to process all events at same year together. So grouping is necessary. So either group manually or use map.
Thus our grouping is correct.
Now final answer.
I'll output the code.
But wait: The problem statement says "n (1 ≤ n ≤ 10^5)". It's typical.
Now answer.
I'll also test with large input: n=100000 all with b=1, d=10^9. Then events: (1,+100000) and (10^9,-100000). Sorted: two events. Sweep: year1: cur=100000, max=100000, best_year=1; year10^9: cur=0. So output 1 100000. That's fine.
Now possible integer overflow for cur if we sum many large numbers? cur at most n, so int is fine.
Now final answer.
But I'll also consider the scenario where the maximum occurs at year d of some person if there is a net positive? But we already considered.
Now finalize.
Potential improvement: Because we only need to know the year where max occurs, and we process sorted events, we need to ensure we update only when cur > max, not when equal. And since we process in increasing year, earliest year is automatically chosen.
Now the answer.
Now deliver final solution. ```cpp
#include
#include
#include
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n;
cin >> n;
vector<pair<int, int>> events;
events.reserve(2 * n);
for (int i = 0; i < n; ++i) {
int b, d;
cin >> b >> d;
events.emplace_back(b, 1); // birth
events.emplace_back(d, -1); // death
}
sort(events.begin(), events.end());
int cur = 0; // current number of alive people
int maxAlive = 0; // maximum found so far
int bestYear = 0; // earliest year with that maximum
size_t i = 0;
while (i < events.size()) {
int year = events[i].first;
int delta = 0;
while (i < events.size() && events[i].first == year) {
delta += events[i].second;
++i;
}
cur += delta;
if (cur > maxAlive) {
maxAlive = cur;
bestYear = year;
}
}
cout << bestYear << " " << maxAlive << "\n";
return 0;