We need to simulate scheduling of n jobs (with given preferred start day s_i and duration d_i) onto a single machine. The machine can only do one job at a time (i.e., days are occupied by jobs, no overlap). Jobs are processed in order i=1..n. For each job i:
- First check if the interval [s_i, s_i + d_i - 1] is entirely free (i.e., no previously scheduled jobs occupy any day in that range). If yes, assign the job to that interval.
- Otherwise, find the smallest positive integer x such that the interval [x, x + d_i - 1] is completely free (i.e., no overlapping with existing jobs). Assign the job to start at x (could be less than s_i). The condition is "the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." So essentially we need to find the earliest start time x such that the d_i consecutive days are all free.
We must output for each job the start and finish days (presumably start and start+d_i-1).
Constraints: n ≤ 200, di ≤ 5*10^6, si up to 1e9. So durations are relatively small (max 5e6) but start days can be huge. However n is only 200, so total schedule length might be up to sum(d_i) ≤ 200 * 5e6 = 1e9. That's large but we can't simulate day by day. However we can maintain a set of occupied intervals (maybe start, end) and for each job find placement.
We need to efficiently check if a given interval [s, s+d-1] is free. With n ≤ 200, we can store intervals as we schedule jobs and simply check overlaps by iterating over existing intervals. Since n is small, O(n^2) is fine (200^2=40000). But the check for "earliest start x" could be more tricky if we scan from 1 to large numbers. We need an efficient algorithm: given a set of intervals (disjoint, sorted by start, merged maybe) and a duration d, find the smallest x ≥ 1 such that the interval [x, x+d-1] does not intersect any existing interval.
We can think of the free intervals (gaps) between occupied intervals. The schedule is one-dimensional timeline of occupied intervals. Initially empty. After scheduling some jobs, we have a set of occupied intervals (non-overlapping, but might be adjacent? Since jobs are scheduled consecutively? They could be adjacent, but if a job finishes at day t and another starts at t+1, they are not overlapping, but the days are fully occupied consecutively. That is fine; we treat them as separate intervals or merge them into continuous occupied blocks. However, when checking for free days, we need contiguous free days of length d. Adjacent occupied intervals create a gap of zero days between them? Actually if one ends at day t and another starts at t+1, then day t+1 is occupied, so there is no free day at t+1? Wait: if job A occupies [a, b] and job B occupies [b+1, c], then days b+1 is occupied. So there is no free day between them. They are effectively continuous block from a to c. So we could merge them into a single occupied interval [a, c]. That simplifies representation: maintain a sorted list of disjoint, non-overlapping, non-touching intervals? Actually if they are exactly touching, [a,b] and [b+1,c] then they are adjacent but there is no gap; they can be merged into [a,c] because there is no free day between them. For checking free contiguous days, we only care about intervals of free days. So it's easier to maintain a sorted list of occupied intervals that are disjoint and not touching (i.e., merged to maximal continuous blocks). Then the free intervals are the gaps between them, and also before the first occupied interval (from 1 to first_start-1) and after the last (maybe infinite). However, when looking for earliest x, we must consider x starting from 1 onward. So we need to check if there is a gap of length at least d before the first occupied interval, if yes, the earliest such x is 1 if d fits in the gap from 1 to first_start-1? But careful: the first occupied interval may start at some day >1. If the gap from day 1 to first_start-1 has length >= d, then x=1 works. Otherwise, we need to check after each occupied interval.
Because we want the smallest x (>=1) such that the d-length interval fits entirely in free space. So we can iterate over the free gaps. The candidate x is the start of the gap (if gap start >=1). Actually if the gap is from L to R inclusive, then any starting x such that x >= L and x + d -1 <= R works. The smallest x in that gap is L (assuming L+d-1 <= R). So we check gaps in order: first gap before first occupied interval (from 1 to first_start-1 if first_start >1), then gaps between intervals, then infinite gap after last occupied interval (but we need to consider that it goes to infinity; so the earliest start after last occupied interval is last_end+1). Since we want smallest x overall, we just examine gaps in increasing order and take the first where length >= d, and set x = gap_start. But we need to consider that maybe there is no gap before first occupied? If first occupied interval starts at 1, then gap from 1 to 0? Actually the free before first occupied is from 1 to first_start-1. If first_start=1, then this gap is empty.
Also, there is an extra nuance: The job's preferred start s_i may affect the first step. But the second step is independent: it always picks the earliest possible x (globally) if the preferred interval is not free. So we need to simulate accordingly.
Because n is only 200, we can even simulate by scanning x from 1 upward until we find a free block of length d. But x could be as high as 1e9 + something, but scanning linearly would be too slow. However, we can use the intervals to compute efficiently.
Given that sum of d_i is up to 1e9, but number of intervals is at most n (if not merged). So we can compute in O(n) per job for checking preferred interval and for finding earliest gap.
Implementation plan:
-
Maintain a list of occupied intervals, merged as we schedule jobs. Represent each as [start, end] inclusive. Initially empty.
-
For each job i with (s_i, d_i):
-
First, compute the preferred interval: L = s_i, R = s_i + d_i - 1.
-
Check if this interval is free. To check, we need to verify that there is no overlap with any existing occupied intervals. Since intervals are merged and disjoint, we can find the first occupied interval with end >= L (or start <= R). Or just iterate through all intervals (max 200) and check if any overlaps: overlap if not (interval.end < L or interval.start > R). If no overlap, then it's free.
- If free: assign to [L, R]. Then we need to insert this interval into occupied list and merge as necessary (i.e., combine with adjacent intervals if they touch or overlap).
-
Else (not free): find the earliest x.
-
To find x: we need to consider candidate start days. The earliest possible start is 1. But we need to check if [1, d_i-1] is free? Actually we need the smallest x such that [x, x+d_i-1] does not intersect any occupied intervals.
-
Approach: iterate over potential starting points: the start of each gap, and also maybe after the end of each occupied interval? Actually the earliest start in a gap is the gap start. But also we might need to consider if the gap length > d, but the gap start could be after some occupied interval, but x could be something later than gap start? But we want the smallest x overall, so if gap_start works, that's the smallest in that gap. However, we must also consider the possibility that the interval could start before the first occupied interval, i.e., at 1. So we treat the initial gap from 1 to (first_start -1) if any. Also, we need to consider that after each occupied interval, the gap starts at end+1. But what about if an occupied interval ends at some day, and the next occupied interval starts at some later day, the gap is (end+1, next_start-1). So candidate x = end+1. So we can simply consider all gaps as (free_start, free_end) inclusive where free_start = 1 or (prev_end+1), free_end = next_start-1 or infinity (some large number). Since d_i is at most 5e6, and si up to 1e9, but infinity can be represented as a large sentinel, maybe 2e9 or something? Actually we can treat the gap after last occupied interval as unlimited, so we can set free_end = INF (like 2e18) but we only need to check if free_end - free_start + 1 >= d_i. Since free_end is infinite, it's always true. Then the earliest x in that gap is free_start.
-
However, we must also consider the case where the preferred interval was not free, but the earliest x might be less than s_i or greater than s_i. So we compute x = min possible according to gaps.
-
But careful: There's also a nuance: The rule says "Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." So it's exactly scanning from day 1 upward until a block of d free days is found. That matches checking gaps.
But we also have to check the case where the preferred interval is free; but then we assign it directly. However, we must consider that the earliest free block might be earlier than s_i, but the rule says if the preferred interval is free, we use that; we do not look for earlier. So the first step is only a check: if it's free, we schedule at s_i regardless of any earlier possible slots. So the rule is as described.
Now, we need to ensure that our check for preferred interval being free is correct. For it to be free, the interval must not intersect any existing occupied interval. Since occupied intervals are merged, we need to verify that it lies completely in a free gap. But we can simply test for overlap with any existing interval. Overlap if not (occupied.end < L or occupied.start > R). So if all intervals satisfy that condition, it's free.
But there is a nuance: what if the preferred interval exactly touches an existing interval? For example, existing interval [1,3] and preferred [4,5] (touching at 4 = 3+1). That is free because days 4 and 5 are not occupied. The rule says "idle for di days starting from si". Since day 4 is idle, it's fine. So touching is allowed (i.e., there is no overlap). So our check of overlap using strict non-overlap condition (occupied.end < L or occupied.start > R) will treat touching as not overlapping because occupied.end < L if end=3 and L=4 => 3<4 true => no overlap. So it's fine.
Now, for merging after insertion: we should treat intervals that are adjacent (new.start = existing.end + 1 or new.end = existing.start - 1) as merging to create a larger block. Because they become contiguous occupied days. For checking future jobs, if there is a gap of zero days, it's not a gap. So merging them simplifies gap computation. So we should merge intervals that are touching.
Now, for the "find earliest x" step, we need to consider gaps correctly after merging. Since we keep merged intervals, the gaps are exactly the free intervals.
Implementation details:
-
Store occupied intervals in a sorted list (e.g., vector<pair<long long, long long>>) where each pair (l, r) with l <= r, and intervals are non-overlapping, non-touching? Actually after merging, they will be disjoint and also non-touching (since we merge touching). So the condition is intervals are such that next.l > current.r + 1.
-
Initially list empty.
-
For each job:
-
Let s, d.
-
Compute L = s, R = s + d - 1.
-
Check free: For each interval (a,b) in occupied, if b < L: continue; if a > R: break (since sorted, later intervals have larger start, so if a > R, no more overlaps). Actually we can early break if a > R because later intervals have even larger starts. So iterate in order.
- If any interval overlaps (i.e., not (b < L or a > R)), then not free.
-
If free:
-
Else:
- Compute x = find_earliest(d).
- start = x, end = x + d - 1.
-
Insert interval (start, end) into occupied list, merging as needed.
-
Output start, end.
Now, find_earliest(d):
-
If occupied list empty: return 1.
-
Consider first gap: from 1 to (first_l - 1). Let gap_start = 1, gap_end = first_l - 1. If gap_end >= gap_start (i.e., gap exists) and gap_end - gap_start + 1 >= d: return 1.
-
Then iterate through consecutive intervals: for i from 0 to len-2:
- gap_start = occupied[i].r + 1
- gap_end = occupied[i+1].l - 1
- if gap_start <= gap_end and gap_end - gap_start + 1 >= d: return gap_start
-
Finally, after last interval: gap_start = last.r + 1, return gap_start (since infinite free after that; we assume gap length infinite).
But we must also consider the possibility that the earliest x could be less than the first interval but the gap from 1 to first_l-1 has length < d, then we proceed.
One more nuance: The earliest x could be exactly at some day that is inside a gap, but the gap_start might be earlier than that but due to the length, the earliest x that fits might be after gap_start if the gap is longer than d but we could still start at gap_start; since gap_start is the earliest day in that gap, that is the smallest x in that gap. So returning gap_start is correct.
But what about the scenario where the gap before the first interval is from 1 to first_l-1 but first_l is 1? Then gap is empty.
Now, after we insert a new interval, we must maintain merged intervals. Since we have at most 200 intervals, we can simply rebuild the list each time by inserting the new interval and merging.
Simpler approach: keep a list of intervals sorted. After we have start,end, we can:
- Create a new list = [].
- Iterate over existing intervals and the new one, merging overlapping or touching.
But because n is small, we could also simply add the new interval to the list and then sort and merge from scratch each time. That's O(n log n) per job, trivial. But we need to be careful: there may be up to 200 jobs, so total 200 * 200 = 40k operations, fine.
Simpler: after each job, push the new interval (start,end) into a vector, sort by start, then merge adjacent intervals where next.start <= current.end+1 (i.e., overlapping or touching). That ensures the list remains merged.
We need to compute find_earliest(d) using the current merged list. That function must consider the merged intervals (non-touching). So we should compute it before adding the new interval, because the schedule currently has only previous jobs. So we need to compute on the current occupied list (without the new job). That is fine.
Implementation steps:
Now, we need to implement find_earliest(occupied, d). We need to consider that occupied is sorted and non-overlapping, and also non-touching after merging. But careful: after merging, intervals are such that there is at least one free day between them (since we merged touching). Actually if intervals were touching (end+1 = next.start), they would have been merged. So after merging, there is no touching; there is a gap of at least 1 day between intervals. So we can rely on that.
Implementation:
function find_earliest(occupied, d):
if occupied.empty(): return 1
// check before first
first_l = occupied[0].first
if first_l - 1 >= d: // days 1..first_l-1 count = first_l-1
return 1
// check gaps between intervals
for i from 0 to len(occupied)-2:
gap_start = occupied[i].second + 1
gap_end = occupied[i+1].first - 1
gap_len = gap_end - gap_start + 1
if gap_len >= d:
return gap_start
// after last
last_r = occupied.back().second
return last_r + 1
But note: In the check before first, we used condition first_l - 1 >= d. The number of free days from 1 to first_l-1 inclusive is first_l-1. Because days 1,2,...,first_l-1 are free, total count = first_l-1. So if first_l-1 >= d, then x=1 works.
But also need to consider if first_l == 1, then gap length = 0, condition fails.
Between intervals: gap_len = (occupied[i+1].first - 1) - (occupied[i].second + 1) + 1 = occupied[i+1].first - occupied[i].second - 1. If that's >= d, then gap_start works.
After last: we assume infinite free days, so return last_r+1.
But we must also consider that the earliest x could be in the gap that contains the preferred start? However, we are in the else branch where preferred interval is not free. But still the earliest x might be before or after s. The function above will find the earliest overall, which is correct.
Now, what about the scenario where the preferred interval is not free, but there is a gap that starts before s but maybe the earliest x is after s? The function will still give the earliest overall. That matches the rule: find the first day x from 1 onward that has d free days. So it's independent of s. So we don't need to consider s at all in the else case.
Now, the check for free must also ensure that the preferred interval is not overlapping with any existing interval. But note that there might be a scenario where the preferred interval is partially overlapping but there is an earlier gap that could accommodate the job, but because preferred is not free we go to else. That's fine.
Now, double-check with examples.
Example 1:
n=3
1: s=9,d=2 => L=9,R=10. occupied empty -> free true. schedule 9-10. occupied: [9,10]
2: s=7,d=3 => L=7,R=9. Check free? Occupied interval [9,10] overlaps because L=7,R=9, overlaps at day 9? Actually day 9 is occupied by first job, so not free. So else. find_earliest: occupied = [9,10]. Check before first: first_l=9 => gap 1-8 length=8 >=3 => x=1. So start=1, end=3. Insert: occupied: [1,3] and [9,10] initially. After merging? They are not touching because 3+1=4 <9, so separate. Sort: [1,3],[9,10].
3: s=2,d=4 => L=2,R=5. Check free: [1,3] overlaps (days 2,3), so not free. else find_earliest: occupied = [1,3],[9,10].
Check before first: first_l=1 => gap length=0 <4.
Between: gap_start = 3+1=4, gap_end=9-1=8, length=5 (4-8 inclusive is 5 days). >=4 => return 4. So start=4, end=7. Insert: now intervals: [1,3],[4,7],[9,10]. After merging? [1,3] and [4,7] are touching? 3+1=4, so they are adjacent, should be merged into [1,7]. Because they become a continuous block of occupied days from 1 to 7. And then [9,10] is separate. After merging we get [1,7],[9,10]. Output matches example: 9 10, 1 3, 4 7. Indeed start days: first 9,10; second 1,3; third 4,7. But note that our output for second is 1 3, which matches.
Example 2:
n=4
1: s=1e9, d=1e6 => L=1e9, R=1e9+999999. occupied empty -> free -> schedule at s. Output 1e9 .. 1e9+999999 (i.e., 1000000000 1000999999). (Check: 1e9 + 1e6 -1 = 1e9+999999, yes.)
2: s=1e9, d=1e6 => L=1e9, R=1e9+999999. Check free? Occupied now has [1e9, 1e9+999999] so overlaps -> not free. find_earliest: occupied = [1e9, 1e9+999999]. Before first: gap from 1 to 1e9-1, length = 1e9-1 >= 1e6 => so x=1. So start=1, end=1000000. Output 1 1000000. Insert: now we have [1,1000000] and [1e9, 1e9+999999]. After merging? They are far apart, so separate.
3: s=100000000, d=1000000 => L=100000000, R=100999999. Check free? occupied: intervals [1,1000000] (ends 1e6) and [1e9, 1e9+999999] (starts 1e9). So free? L=100,000,000, R=100,999,999. Neither interval overlaps: first interval ends at 1,000,000 < L; second starts at 1,000,000,000 > R. So it's free. So schedule at s: start=100000000, end=100999999. Output as given: 100000000 100999999. Insert: now intervals: [1,1000000], [100000000,100999999], [1000000000,1000999999]. After merging? They are separate. But note the example output for third is 100000000 100999999. Good.
4: s=1000000000, d=1000000 => L=1e9, R=1e9+999999. Check free? Occupied now includes [1e9, 1e9+999999] from first job, so overlap -> not free. find_earliest: occupied = three intervals sorted: [1,1000000], [100000000,100999999], [1000000000,1000999999].
Check before first: gap 1 to (1-1?) first_l=1, gap length 0.
Between first and second: gap_start = 1000000+1=1000001, gap_end = 100000000-1=99999999? Actually 100,000,000 - 1 = 99,999,999. So gap_len = 99,999,999 - 1,000,001 + 1 = 99,000, - let's compute: 99,999,999 - 1,000,001 = 98,999,998? Actually 99,999,999 - 1,000,001 = 98,999,998, plus 1 = 98,999,999. That's huge, >1e6. So earliest gap is from 1000001 to 99999999. gap_start = 1000001. So return 1000001. That matches example output: 1000001 2000000? Wait output: 1000001 2000000. That means start=1000001, end=2000000? Since d=1e6, start=1000001 gives end=1000001+999999 = 2000000. Yes. So that matches.
Our algorithm would return 1000001. So correct.
Now, we need to consider edge cases: when merging after insertion, we must treat touching intervals as merging. In the example, after adding second job (1,1000000) and first (1e9,...), they are separate. After adding third (100000000,100999999) they are separate. After adding fourth (1000001,2000000), we need to merge with existing intervals. Let's see: after third, intervals: [1,1000000], [100000000,100999999], [1000000000,1000999999]. Adding fourth: [1000001,2000000]. Now this new interval touches? [1,1000000] ends at 1000000, new starts at 1000001, so they are touching (1000000+1 = 1000001). Therefore they should be merged into a single block [1,2000000] because new ends at 2000000 and it touches the first block. Also does it overlap with [100000000,100999999]? It ends at 2000000, which is less than 100000000, so no. So after merging, we get [1,2000000] and [100000000,100999999] and [1000000000,1000999999]. That's fine.
Now, we need to ensure that the find_earliest function works correctly when occupied intervals are not merged (i.e., touching). However, we always merge after each insertion, so they will be non-touching. But just to be safe, we can implement find_earliest to be robust even if intervals are touching. In that case, there would be no gap between touching intervals (gap length 0). Our calculation gap_start = occupied[i].second + 1, gap_end = occupied[i+1].first - 1; if they are touching, then occupied[i].second + 1 = occupied[i+1].first, so gap_start = occupied[i+1].first, gap_end = occupied[i+1].first -1 => gap_start > gap_end, gap length negative or zero. So we should only consider if gap_start <= gap_end. That would automatically skip zero-length gaps. So it's fine. Also for before first gap: if first_l = 1, gap_end = 0, condition fails.
Thus algorithm is correct.
But we must be careful with data types: si up to 1e9, di up to 5e6, so sum can go up to 1e9 + something, but individual start could be up to 1e9 + maybe? Actually if we schedule after many jobs, start could be large (e.g., after last occupied interval). But overall, start may be up to 1e9 + sum(d_i) maybe up to 1e9 + 1e9 = 2e9 roughly, which fits in 64-bit integer. But to be safe, we should use long long (64-bit). So use long long for all day values.
Now, complexities: n=200, each job we do O(n) for checking free, O(n) for find_earliest, O(n log n) for sorting and merging. Totally fine.
One more nuance: In the first step (checking if preferred interval is free), we must consider that the occupied intervals are merged, and if the preferred interval lies entirely within a gap but there is an interval that starts after? Our loop check works: iterate over intervals; if any overlaps, not free. Since intervals are sorted, we can break early when start > R. Good.
Now, one potential pitfall: The find_earliest function returns the smallest x based on the current occupied intervals. But what if the preferred interval is not free but the earliest x happens to be exactly s? But s is not free, so it cannot be free; but if we compute earliest x, we might get x = s if s is in a gap of sufficient length and no earlier gap. But if the preferred interval is not free, that means that [s, s+d-1] is not completely free; there is at least one day occupied. So it cannot be that the entire interval is free. So x cannot be s because that would mean [s, s+d-1] is free, contradicting not free. So we are safe.
But could there be a scenario where the preferred interval is partially overlapping but the earliest x is less than s but still overlapping? No, earliest x is determined by free intervals; it will be a location where it's free, so it's safe.
Now, is there any hidden complexity? The problem statement says: "If the car repair shop is idle for di days starting from si (si, si + 1, ..., si + di - 1), then these days are used to repair a car of the i-th client. Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." This implies that if the shop is not idle for the whole block starting at si, then he finds the earliest possible block anywhere (starting from day 1). So our interpretation is correct.
Now, we should test additional edge cases.
Edge case: n=1, s=5, d=1. Occupied empty, free, schedule at 5, output 5 5.
Edge case: n=1, s=1, d=1. Output 1 1.
Edge case: n=2. First: s=1,d=5 => schedule 1-5. Second: s=2,d=3. Preferred [2,4] overlaps, not free. Find earliest: occupied [1,5]. Before first: gap from 1 to 0 none. After last: gap_start = 6, returns 6 => schedule 6-8. Output 1-5, 6-8.
But what about if there is a gap earlier? Actually there is no earlier. So correct.
Now, what if the preferred interval is free but there is an earlier gap? That does not matter; we use preferred.
Now, consider the scenario where preferred interval is free, but later when we insert it, it might merge with previous intervals and cause future jobs to have different earliest x. That's fine.
Now, we must also consider that when checking free, the preferred interval might be exactly adjacent to an existing interval? That is free because there is no overlap. For example, existing [1,3], preferred [4,6] (d=3). That is free. So schedule at 4-6. After insertion, they will be merged into [1,6] if they are adjacent (3+1=4). So merging is important for future gap calculations.
Now, about implementation details: sorting by start, merging: we can use vector<pair<ll,ll>>. For merging, we use condition: if cur_start <= prev_end+1 then merge, else push new.
Now, find_earliest: We need to consider that the occupied list may not be merged? But we always merge after each insertion. So it's merged. But even if not, the function as described works if intervals are sorted but may overlap? Overlaps would be a bug. So we ensure merging.
One more subtle point: The find_earliest function as described does not consider that the earliest x could be inside a gap but not at the very start of the gap if the gap start is < 1? No, gaps start at >=1. So that's fine.
But is there a possibility that the earliest x is not at the start of a gap because the gap might start before 1? That's impossible.
Thus algorithm is correct.
Now we can implement.
But wait: The problem statement says: "Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." That's exactly scanning from day 1 upward. So our approach yields that.
But one more nuance: When we compute the gap before the first interval, we check if days 1..first_l-1 have length >= d. However, what if first_l is 1? Then no gap. If first_l > 1, the earliest possible x is 1. So we check that.
But what about the scenario where the gap before first has length >= d, but there is also a gap that starts earlier than 1? Not possible.
Now, is there any scenario where the earliest x is not exactly gap_start because the gap_start might be < 1? No.
Thus we are good.
Now, we should double-check with a few more random tests. We can also think about large values.
Edge case: after many jobs, the occupied intervals may be many, but n is small. So fine.
Now, let's think about potential bug: In the free check, we break when a > R. But we must ensure that the intervals are sorted. Since we merge after each insertion and sort, they are sorted. So fine.
Now, about the find_earliest function: we should also consider that the gap after last is infinite. But what if the last interval ends at some day, and we return last_r+1. That's correct.
But consider if the schedule is empty: we return 1. Good.
Now, a scenario: occupied = [5,10]. d=4. Before first: gap 1-4 length=4 >=4, so return 1. That's correct because [1,4] is free.
Now, scenario: occupied = [1,3] and [6,10], d=2. Before first: gap from 1 to 0 none. Between: gap_start=4, gap_end=5, length=2 (days 4,5) >=2 => return 4. That's earliest: [4,5] is free. Check earlier: day 1? [1,2] overlaps with [1,3] (day1 and 2 are occupied). Day 2? [2,3] overlaps. Day3? [3,4] overlaps because day3 occupied. So earliest is 4. Good.
Now, scenario: occupied = [1,3] and [5,7], d=1. Before first: none. Between: gap_start=4, length 1 day? Actually gap between is [4,4] length 1 => return 4. But is there any earlier day that is free? Day 4 is free. But what about day 8? after last. That's later. So 4 is earliest. Good.
Now, scenario: occupied = [2,2] (single day). d=1. Before first: gap 1-1 length 1 => return 1. That's earliest free day.
Now, scenario: occupied = [2,2], d=2. Before first: gap 1-1 length 1 <2. After last: return 3 => [3,4] is free. But is there a gap between? No. So earliest x=3. However, is there any possibility that [1,2]? No, because day2 occupied. [2,3]? day2 occupied. So correct.
Now, consider the possibility that after merging intervals, we may have intervals that are not sorted? We sort each time.
Now, about reading input: n up to 200, si up to 1e9, di up to 5e6. We'll use long long.
Now, let's code.
Pseudo-code:
typedef long long ll;
struct Interval { ll l, r; };
vector<pair<ll,ll>> occupied;
for each job:
ll s, d; cin >> s >> d;
ll L = s, R = s + d - 1;
bool free = true;
for (auto &iv : occupied) {
if (iv.second < L) continue;
else if (iv.first > R) break; // sorted, no further overlaps
else { free = false; break; }
}
ll start, end;
if (free) {
start = L; end = R;
} else {
// find earliest x
if (occupied.empty()) start = 1;
else {
// check gap before first
ll candidate = -1;
if (occupied[0].first - 1 >= d) { // gap length from 1 to first_l-1 is first_l-1
candidate = 1;
} else {
// check gaps between
for (int i = 0; i < occupied.size()-1; ++i) {
ll gap_start = occupied[i].second + 1;
ll gap_end = occupied[i+1].first - 1;
if (gap_end - gap_start + 1 >= d) {
candidate = gap_start;
break;
}
}
if (candidate == -1) {
candidate = occupied.back().second + 1;
}
}
start = candidate;
}
end = start + d - 1;
}
cout << start << " " << end << endl;
// insert and merge
occupied.push_back({start, end});
sort(occupied.begin(), occupied.end());
vector<pair<ll,ll>> merged;
for (auto &iv : occupied) {
if (merged.empty() || iv.first > merged.back().second + 1) {
merged.push_back(iv);
} else {
merged.back().second = max(merged.back().second, iv.second);
}
}
occupied = merged;
}
Now test on examples.
But wait, there is a subtle bug: In the find_earliest, we used condition occupied[0].first - 1 >= d to check gap before first. However, what if occupied[0].first = 2? Then gap from 1 to 1 length 1, so occupied[0].first - 1 = 1. So if d=1, 1>=1 => true, returns 1. Good. If occupied[0].first = 1, then occupied[0].first - 1 = 0, condition false. So fine.
Now, what if the schedule is empty, we handled separately.
Now, but what about the case where there is a gap before first but it is longer than d, but the earliest x might be 1? Yes.
But what about a scenario where there is a gap before first but its start is not 1? It is always 1. So correct.
Now, let's test with a random case to ensure merging does not cause find_earliest to miss gaps because we merged intervals before calling it. That's fine.
Now, we need to consider that the preferred interval might be free but later we merge it, but that's after we've printed.
Now, ensure that we use long long for everything.
Now, what about the case where d is huge, like 5e6, and the gap before first is also huge? We compute difference using long long, fine.
Now, is there any scenario where the earliest x falls inside a gap but the gap_start might be > 1e18? Unlikely.
Now, we should also consider that the schedule might have intervals with large numbers, but merging and sorting with long long works.
Now, test with some custom scenarios to be safe.
Case: n=2
1: s=10, d=5 => schedule 10-14.
2: s=1, d=6 => preferred [1,6] is free? Occupied: [10,14] which is after 6, so it's free (no overlap). So schedule at 1-6. But wait, check: preferred free because no overlap. So schedule at 1-6. That's allowed. Output: 10-14 and 1-6? But note we process jobs in order: first job got 10-14, second job checks preferred [1,6] and finds it's free (since [10,14] does not overlap). So schedule at 1-6. That's fine, but now the schedule has intervals that are not sorted by time of assignment, but that's okay. The machine can have jobs scheduled at any times as long as no overlap. So final occupied: [1,6] and [10,14] (merged? They are not touching, so separate). That is valid. So algorithm will correctly find it free.
Now, check: In free check, we need to consider that occupied intervals are sorted? At that point occupied contains [10,14] (only after first job, merged). It is sorted. For job2, we iterate: interval [10,14], check: since b < L? b=14 < L=1? No (14<1 false). else if a > R? a=10 > R=6 true => break. So free = true. Good.
Now, test scenario where preferred is not free but there is a gap that starts before s but still later than 1. That's fine.
Now, consider scenario where multiple jobs could cause merging that changes later gap detection. But since we compute each job sequentially based on current schedule, it's correct.
Now, complexity: merging with sorting each time O(n log n) with n <= number of intervals <= number of jobs = 200. So overall trivial.
Now, we might think of an alternative: we could maintain a set of free intervals, but not needed.
Now, check the condition in find_earliest when occupied list is not empty but there might be a gap before first that is exactly of length d? Condition uses >=, so works.
Now, we should test edge case: occupied = [2, 2], d=1. Condition before first: first_l=2 => first_l-1=1 >=1 => returns 1. Good.
Now, occupied = [1, 1], d=1: first_l-1=0 <1 => go to gaps: there is no gap between because only one interval. candidate = -1, then after last: last_r+1=2 => returns 2. But is day 2 free? Yes. So schedule at 2. But is there a possibility that day 1 is free? No, it's occupied. So earliest free day with 1 consecutive free day is day 2. So correct.
Now, what about d=2, occupied=[1,1]. Before first: gap length=0. After last: candidate=2. Check if [2,3] is free? Since only occupied is [1,1], yes. So start=2. That is earliest? Could start at 0? No days start from 1. So correct.
Now, what about occupied=[2,3], d=2. Before first: first_l=2 => gap 1-1 length=1 <2. After last: candidate=4. Check: [4,5] free. But is there any earlier? [1,2] includes day2 occupied; [3,4] includes day3 occupied. So 4 is earliest. Good.
Now, what about occupied=[1,1] and [3,3] (two separate intervals after merging? Actually they are not touching because 1+1=2 <3, so gap between 2 and 2? Actually gap is [2,2] length 1. So if d=2, before first: gap 0, between: gap_start=2, gap_end=2 length=1<2, after last: candidate=4. So start=4. Good.
Now, all seems fine.
Now, implement.
One more nuance: In find_earliest, we used occupied[0].first - 1 >= d. This calculates length as first_l - 1. That's correct because number of days from 1 to first_l-1 inclusive is first_l - 1 (since days 1,2,...,first_l-1). For example first_l=5, days 1-4 count 4 = 5-1.
But if first_l is huge, e.g., 1e9, and d=5e6, it's fine.
Now, after we insert a new interval, we must ensure that the merging step also merges if the new interval exactly touches existing intervals. We'll use condition new_start <= last_end+1 to merge. That merges touching intervals.
Now, possible oversight: In the find_earliest function, we assume that occupied intervals are non-overlapping and non-touching after merging. But after merging, if we have intervals like [1,3] and [4,6] (touching), they would have been merged into [1,6] because 3+1=4, so they are touching, so merge. So they won't exist as separate intervals. So in merged list, intervals are separated by at least one free day. So our gap detection works.
But if we did not merge touching, then the gap between them would be zero length, but our condition gap_start <= gap_end would skip it anyway. So even if we didn't merge, it would be fine. But merging is still beneficial for simplicity.
Now, about integer overflow: start + d - 1 might exceed 2e9 but still within 64-bit. Actually worst-case: si could be 1e9, d up to 5e6, so max ~1.005e9. But after many jobs, if we schedule after last, last_r could be up to about sum(d_i) + max(s_i) maybe 200*5e6 + 1e9 = 1e9 + 1e9 = 2e9. So fits in 32-bit signed? 2e9 fits within 2^31-1 (2.147e9) barely, but it's safer to use 64-bit.
Now, output format: two integers separated by space.
Now, we should write the code.
But before finalizing, we must test some more tricky cases.
Case: n=2
1: s=5, d=3 => schedule 5-7.
2: s=1, d=10 => preferred [1,10] overlaps with [5,7] => not free. find_earliest: occupied = [5,7]. Before first: gap from 1 to 4 length 4 <10. After last: candidate = 8. So start=8, end=17. Output: 5-7 and 8-17. That seems correct. Check: Is there any earlier block of 10 free days? Days 1-10: days 5-7 occupied, so no. Days 1-? earliest start that works is 8? Check [8,17] all free? After 7, from 8 onward free indefinitely, so yes.
But is there a possibility that we could start at 1 if we skip? No.
Now, case: n=2
1: s=1, d=5 => schedule 1-5.
2: s=6, d=5 => preferred [6,10] is free? It is adjacent, so free. So schedule at 6-10. After merging, they become [1,10]. So output 1-5, 6-10. That's fine.
Now, case: n=2
1: s=1, d=5 => schedule 1-5.
2: s=5, d=5 => preferred [5,9] overlaps at day5 => not free. find_earliest: occupied [1,5]. Before first: gap none because first_l=1. After last: candidate=6 => start=6, end=10. But is there a gap between? No. So schedule 6-10. After merging, we get [1,5] and [6,10]? Actually they are touching? 5+1=6, so they touch. After insertion, we would merge into [1,10]. So final occupied [1,10]. So output: 1-5, 6-10. That matches schedule.
Now, what about a scenario where the preferred interval is free but there is an earlier gap that could accommodate the job? That does not matter, rule says use preferred if free.
Now, we should also consider that the preferred interval might be free but later we need to ensure that we didn't accidentally schedule overlapping because we didn't check correctly due to multiple intervals? Our check is fine.
Now, let's think about potential issue with sorting and merging after each job: When we do occupied.push_back({start,end}) and then sort, we are mixing previous intervals and new one. However, the new interval might overlap with existing intervals? In the free case, we checked that it doesn't overlap, so no overlap. In the else case, we found a free gap, so it also does not overlap. So the new interval should not overlap with any existing interval. However, it might be adjacent (touching). After sorting and merging, we might merge adjacent intervals. That's intended.
But what about the scenario where the new interval is exactly between two existing intervals and touches both? For example, existing intervals [1,3] and [7,9], and we schedule new interval [4,6] (free). That would be separate; but after insertion, we have three intervals. Then merging: since [1,3] and [4,6] are touching? 3+1=4, so they merge into [1,6]. Then [1,6] and [7,9] are touching? 6+1=7, so they merge into [1,9]. So final becomes a single block. That's correct.
Now, potential issue: After merging, the list may become smaller, but that's fine.
Now, complexity: For each job we sort O(m log m) where m is number of intervals (<= job index). With n=200, it's fine.
Now, we can also implement using std::set for intervals to avoid sorting each time, but not necessary.
Now, think about potential bug in free check: Because occupied is sorted but we may break early when we see an interval with start > R. However, we need to ensure that we don't break too early if there are intervals that start after R but also start before? Actually if intervals are sorted by start, the first interval with start > R guarantees that all subsequent intervals have start > R, so they cannot overlap (since overlap requires start <= R). So break is correct.
Now, what about overlapping intervals? Since we merge, there is no overlap. So it's fine.
Now, find_earliest: When we compute between gaps, we assume that intervals are sorted and non-overlapping. But if there were overlapping intervals, the gap calculation might be wrong. But we merge, so no overlaps.
Now, confirm: In the else branch, we compute start = candidate. But what if candidate is -1? That should not happen because after last there is always a candidate. So we can set candidate directly without -1.
Simplify code:
if (occupied.empty()) start = 1;
else {
// check gap before first
if (occupied[0].first - 1 >= d) {
start = 1;
} else {
// check between
start = -1;
for (size_t i = 0; i+1 < occupied.size(); ++i) {
ll gap_start = occupied[i].second + 1;
ll gap_len = occupied[i+1].first - occupied[i].second - 1;
if (gap_len >= d) {
start = gap_start;
break;
}
}
if (start == -1) {
start = occupied.back().second + 1;
}
}
}
Now, note: In the condition before first, we used occupied[0].first - 1 >= d. But suppose occupied[0].first = 2, d=1 => 1>=1 true, returns 1. Good.
But what if occupied[0].first = 100, d=100 => 99>=100 false, so we go to between.
Now, verify with scenario: occupied = [100,200] (first_l=100). d=99 => gap before first length 99, so returns 1. That is correct: [1,99] free.
Now, what about d=100? gap length=99 <100, so not fit. Then between: no other intervals, so after last: returns 201. So start=201. That's earliest block of 100 free days? Actually days 1-99 free, but length 99 insufficient. Next free block after 200 starts at 201, length infinite. So yes.
Now, all good.
Now, handle large inputs and potential overflow: When computing gap_len, use (occupied[i+1].first - occupied[i].second - 1). Since values are up to ~2e9, subtraction safe in 64-bit.
Now, an important nuance: The problem statement says: "Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." This is exactly the earliest day where a block of d free days starts. However, we must also consider that the block could start before the end of an existing job? No, it must be free, so we check free days.
Our algorithm finds the earliest start of a gap that has length >= d. But what about the case where a gap length is exactly d? Then the earliest start is gap_start. Good.
Now, but what about the case where there is a gap that is longer than d, but the earliest x is not at the start of that gap? Could it be that the earliest x is earlier than gap_start? No, because gap_start is the first free day after previous occupied block. Before that, days are occupied. So x cannot be less than gap_start if we need d consecutive free days, because the days before gap_start are occupied at the previous block's end. For example, occupied = [1,3], gap_start = 4, but could x=3? No, because day3 is occupied, so [3,3+d-1] would include day3 which is occupied. So earliest x in that gap is gap_start. So correct.
Now, check if there is any scenario where the earliest x is not at the beginning of a gap because there might be a gap that starts at 1? That's covered.
Thus algorithm is sound.
Now, we should implement.
But wait: The rule says "Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." That is essentially scanning day by day from 1 upward, checking if the next d days are all free. Our algorithm with gaps will produce the same result because we check the earliest gap that can accommodate the block. However, consider a scenario where there is a gap of length 10, but the earliest x might be, say, day 5, even though day 4 is free? If the gap starts at 1 and length 10, then day 1 is free, so the earliest x is 1, not 5. So it's always the start of the gap. So no issue.
Now, I'll code in C++.
But one more nuance: In the first step, we check if the interval [s, s+d-1] is free. This check must be done considering the current schedule. But what if the interval is free but there is an earlier free block? That's irrelevant; we use s. So we schedule at s. However, after scheduling at s, we might create a situation where the interval merges with existing intervals, possibly causing a later job's preferred interval to be not free even if it would be free if we had chosen a different placement earlier. But that's the rule.
Now, ensure that we use correct data types: I'll use long long.
Now, final code.
Testing with given examples.
Now, I realize something: In the find_earliest function, we use the current occupied list, which is merged. But what if there is a gap that starts before the preferred s but is not the earliest? That's fine.
Now, also note that the process of checking free might be O(n) each time; we can improve by binary search but not needed.
Now, we should also consider that there might be large number of intervals but n small. So fine.
Now, I'm going to produce final solution.
But wait: There is a subtle bug: In the free check, we break when we find an interval with a > R. However, we need to ensure that the intervals are sorted by start. Since we sort after each merge, they are. But we also need to ensure that in the same loop, if we break early because a > R, that's correct only if we guarantee that there are no intervals that start before that but may overlap? Actually we iterate in sorted order; all intervals we have seen have start <= current start? They are sorted increasingly. So when we encounter an interval with start > R, all subsequent intervals have start >= that start > R, so they cannot overlap because overlap requires start <= R. So break is safe.
Now, what about intervals that start before R but end before L? They are skipped by the first condition (b < L). That's fine.
Now, implement.
After coding, test with provided examples.
Example1:
n=3
job1: s=9,d=2 -> L=9,R=10, occupied empty -> free true, schedule 9,10. Insert: occupied=[(9,10)] after merging.
job2: s=7,d=3 -> L=7,R=9. Check: occupied[0]=(9,10): b=10 < L=7? false; a=9 > R=9? a=9 > R=9? 9>9 false; so else (overlap) -> free false. else find_earliest: occupied[0].first=9 -> first_l-1=8 >=3? yes -> start=1, end=3. Insert: push (1,3); sort => [(1,3),(9,10)]; merge: (1,3) and (9,10) are not touching (3+1=4<9) => merged stays. Output: 1 3.
job3: s=2,d=4 -> L=2,R=5. Check free: iter intervals: (1,3): b=3 < L=2? 3<2 false; a=1 > R=5? false; else -> overlap => free false. else find_earliest: occupied = [(1,3),(9,10)]. Before first: first_l=1 => gap length0 <4. Between: i=0: gap_start=4, gap_end=8, len=5 >=4 => start=4, end=7. Insert: push (4,7); sort => (1,3),(4,7),(9,10); merge: (1,3) and (4,7) touching? 3+1=4 -> merge into (1,7); then (1,7) and (9,10) not touching -> merged list = [(1,7),(9,10)]. Output: 4 7. So matches.
Example2:
n=4
job1: s=1e9,d=1e6 -> schedule at s -> output 1000000000 1000999999. Insert.
job2: s=1e9,d=1e6 -> check free: occupied [(1000000000,1000999999)]; interval overlaps -> not free. find_earliest: before first: first_l=1e9 -> first_l-1 = 999999999 >= 1e6 -> start=1, end=1000000. Insert: push (1,1000000); sort => (1,1000000),(1e9,1000999999); merge not touching. Output 1 1000000.
job3: s=100000000,d=1e6 -> L=100000000,R=100999999. Check free: iter (1,1000000): b=1000000 < L=100000000? yes, continue; next (1e9,1000999999): a=1e9 > R=100999999? yes (1e9 > 100999999) -> break; free true. schedule at s: 100000000 100999999. Insert: push; sort => (1,1000000),(100000000,100999999),(1000000000,1000999999); merge: none touch. Output as given.
job4: s=1000000000,d=1000000 -> L=1e9,R=1000999999. Check free: intervals: (1,1000000): b=1000000 < L? yes; (100000000,100999999): b=100999999 < L? 100999999 < 1000000000? yes; (1e9,1000999999): a=1000000000 <= R=1000999999 and b=1000999999 >= L => overlap -> not free. else find_earliest: before first: first_l=1 => gap length0; between: gap1 between (1,1000000) and (100000000,100999999): gap_start=1000001, gap_end=99999999, len = 99999999 - 1000001 + 1 = 98999999? Actually compute: 99999999 - 1000001 = 98999998, +1 = 98999999, which >= 1e6. So start=1000001, end=2000000. Insert: push (1000001,2000000); sort: (1,1000000),(1000001,2000000),(100000000,100999999),(1000000000,1000999999). Now merging: (1,1000000) and (1000001,2000000) touch (1000000+1=1000001) -> merge into (1,2000000). Then (1,2000000) and (100000000,100999999) are not touching (2000000+1=2000001 < 100000000) -> separate. Then (100000000,100999999) and (1000000000,1000999999) not touching. So final occupied: (1,2000000), (100000000,100999999), (1000000000,1000999999). Output 1000001 2000000. Matches.
So algorithm passes examples.
Now, we need to consider potential pitfalls about the condition "first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." This means we need to check from day 1 upward, but our gap method yields the start of the first gap that can accommodate the block. However, consider a scenario where the earliest block might start not at the very beginning of a gap, but after some free days within the gap if the gap is longer than d? But the earliest x would still be at the start of the gap because those days are free, so x = gap_start is valid. But is it always valid? Suppose the gap starts at day 5, but day 5 is free, so [5,5+d-1] is within the gap? The gap is from 5 to, say, 20. So [5,5+d-1] is within gap as long as 5+d-1 <= gap_end. Since gap length >= d, this holds. So x=5 works. So it's indeed the earliest.
But what if the gap start is after day 1, but there might be an earlier block that starts at some day that is not at a gap boundary because there is a small occupied block, then a gap, but the earliest block could start before the gap start if it includes some days before? No, because any block that starts before gap_start would include at least one occupied day (the day just before gap_start is occupied by definition, because gap_start is defined as the first free day after an occupied block). So that would not be free. So indeed, the earliest possible start is exactly the start of a gap.
Thus algorithm is correct.
Now, we must ensure that we treat the case when the schedule is empty: find_earliest returns 1. And for free check, it's also fine.
Now, confirm that after merging, we correctly maintain that intervals are disjoint and not touching. For example, after inserting an interval that exactly touches an existing one, we merge, so future gaps will be correctly represented.
Now, what about scenario where the new interval is inside an existing gap but not touching any existing interval, so it remains separate; no merging needed.
Now, what about scenario where the new interval exactly fills a gap of length exactly d, then after insertion, the two surrounding intervals may become touching and should merge. Our merging algorithm will handle that because after sorting and merging, if we have intervals A, new, B, and A.end+1 = new.start and new.end+1 = B.start, after merging, they will all become one interval. The algorithm will process sorted intervals: first A, then new, then B. When encountering new, we check if new.start <= merged.back().second+1. Since A.end+1 = new.start, condition true => merge A and new into [A.start, max(A.end, new.end)]. Then when processing B, if B.start <= merged.back().second+1 (which is now new.end maybe) and since new.end+1 = B.start, condition true => merge further. So final becomes one block. Good.
Now, one more nuance: When we schedule a job, we insert it and then merge. But for the next job, we will compute free check and find_earliest using the merged list. That's correct.
Now, let's test some random small cases with brute force simulation to validate. We'll write a small brute for n up to maybe 5 and small day ranges, compare our algorithm with a naive day-by-day simulation for consistency. But here we can't run code, but we can reason.
Consider small case: n=3, s=[2,1,4], d=[2,1,1]. Process:
Job1: s=2,d=2 => L=2,R=3. occupied empty -> free, schedule 2-3. occupied: [2,3].
Job2: s=1,d=1 => L=1,R=1. Check free? occupied [2,3]: b=3<1? false; a=2>1? false? Actually a=2 > R=1? 2>1 true => break, no overlap? Wait condition: if a > R then break, but that means this interval starts after R, so it doesn't overlap. Since we break, we haven't marked free false. But we must ensure that we don't break prematurely before checking all intervals? In our loop, we have:
for (auto iv: occupied) {
if (iv.second < L) continue;
else if (iv.first > R) break;
else { free = false; break; }
}
For job2: L=1,R=1. The first (and only) interval: iv=(2,3). iv.second=3 < L=1? false. iv.first=2 > R=1? true => break. So we break out of loop, and free remains true. So we would schedule at s=1. But is day 1 free? Yes, because occupied is [2,3], day1 is free. So it's correct: job2 gets [1,1]. Good. So break is appropriate.
Now, after inserting job2: intervals: [1,1] and [2,3]. Sort: [1,1],[2,3]. Merging: first [1,1]; then next [2,3] start=2, which is <= 1+1=2? Actually condition iv.first <= merged.back().second+1: 2 <= 1+1=2 true => merge into [1, max(1,3)] => [1,3]. So occupied becomes [1,3] (continuous block).
Job3: s=4,d=1 => L=4,R=4. Check free: occupied [1,3] -> iv.second=3 < L=4? true, continue? Actually if iv.second < L, we continue; that's the case. Then loop ends, free remains true. So schedule at 4. Output: 4,4. That's fine.
Now, what about overlapping detection? For job2, we correctly determined free.
Now, consider scenario: overlapping detection with multiple intervals and break early might skip some intervals that start after R but also there might be an interval that starts before L but ends after L? Actually if interval ends after L but start is before L, then iv.second < L is false because iv.second >= L, so we go to else if iv.first > R? That could be false if iv.first <= R (since start before L, but start <= R? Since start < L, it's definitely <= R (maybe). Example: occupied [1,5], L=3,R=4. iv.second=5 < 3? false. iv.first=1 > R=4? false. So else -> overlap true. So that's correct.
Now, break when iv.first > R: that is safe because intervals are sorted by start. If we encounter an interval with start > R, all later intervals have start >= that start > R, so they can't overlap.
Thus free check is correct.
Now, find_earliest: we need to consider the possibility that the earliest block might start at a day that is not exactly at the start of a gap, if the gap starts after some free days but the block might start earlier if there is a gap that starts earlier but length insufficient? No.
Alright.
Now, we might consider an alternative: using a set to maintain free intervals? But not needed.
Now, let's think about performance with n=200, d up to 5e6, but the gap length before first could be up to 1e9. Our condition first_l - 1 >= d uses subtraction of large numbers, that's fine.
Now, one more test: When there is a gap that is exactly of length d, but the gap_start might be less than 1? No.
Now, also consider that after many jobs, the number of intervals may be up to n (if no merging). But merging reduces them.
Now, implement.
But there is a nuance: When we compute find_earliest, we use occupied list which may have been merged, but after insertion we also merge. That's fine.
Now, we should also consider that the new job's interval might be placed in a gap that is exactly at the beginning, but if the gap before first had length < d, we skip it. But what if the gap before first has length exactly d-1? Then not enough, we move on. Then we might find a gap between intervals that is exactly d. That's fine.
Now, we must also handle the case where the schedule is empty in find_earliest, we return 1.
Now, about input reading: n, then n lines of si di.
Now, let's write code.
One more thought: Could there be an issue with the condition "first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." This implies that we must check days sequentially until we find a block of d free days. Our gap method finds the earliest gap start that can fit. However, consider a scenario where there is a free gap from day 5 to day 10 (length 6), and d=4. The earliest x is 5. But what if day 1 is free? If day 1 is free, then we would have started at 1. So we already check before first gap. So it's fine.
Now, but what about a scenario where there is a free gap that is not contiguous because there is a small occupied day within it? But that's not a gap; that would be two gaps. So our gap representation captures it.
Now, final code.
I'll implement using vector<pair<long long, long long>>.
Implementation details:
- Use typedef long long ll;
- Use sort with default pair ordering (by first).
- For merging: after sorting, create new vector merged. For each interval in sorted list:
if merged.empty() or iv.first > merged.back().second + 1: push
else: merged.back().second = max(merged.back().second, iv.second)
Now, careful: The merging condition should be iv.first <= merged.back().second + 1 for merging (overlap or touching). Since we guarantee no overlap, but we can still use this.
Now, edge case: intervals might be overlapping due to bug, but we ensure they are not.
Now, after merging, occupied = merged.
Now, the find_earliest function:
ll find_earliest(const vector<pair<ll,ll>>& occ, ll d) {
if (occ.empty()) return 1;
// before first
if (occ[0].first - 1 >= d) return 1;
// between
for (size_t i = 0; i+1 < occ.size(); ++i) {
ll gap_start = occ[i].second + 1;
ll gap_len = occ[i+1].first - occ[i].second - 1;
if (gap_len >= d) return gap_start;
}
// after last
return occ.back().second + 1;
}
But note: In the before-first condition, we used occ[0].first - 1 >= d. However, we should check that the gap from 1 to first_l-1 actually exists (first_l > 1). If first_l == 1, then first_l-1 = 0, condition false. So it's fine.
Now, what about the case where occ[0].first is huge, say 100, and d=100, gap length = 99, so condition false, we go to between. But what if there are no other intervals? Then we go to after last. That returns occ.back().second+1 = 100+? Actually occ[0].second may be something, say [100,200], so returns 201. So start=201. That's correct: [201,300] works. But is there any earlier start that fits? Days 1-99 length 99 insufficient; days 101-200? Actually days 101-200 are occupied (since [100,200] occupied). So no.
Now, but is it possible that a block of d days could start before the first interval but not at day 1, if the first interval starts later but the block could start after day 1 but before first_l? For example, first_l=10, d=5. The block could start at day 5? But day 1 is also free. The earliest x is 1. So we return 1. That's correct.
But what if first_l=10, d=6? Gap length=9, so 1-9 free length 9 >=6, so earliest x =1 works. So fine.
Now, think about large d and empty schedule: occ empty => find_earliest returns 1. Good.
Now, all good.
Now, I will code.
But before finalizing, we must also consider that the schedule might become extremely large and the occupied intervals might become huge after merging, but that's fine.
Now, check for possible integer overflow in calculation of gap_len: occ[i+1].first - occ[i].second - 1. Both are up to about 2e9, subtraction yields up to 2e9, fits in 64-bit. For after last, occ.back().second + 1 could be up to about 2e9+1, fine.
Now, we can submit.
But wait: The problem's di can be up to 5*10^6, not huge relative to 1e9. But we also must consider that after many jobs, the occupied intervals may span up to 2e9, and gap lengths may be huge, but still within 64-bit.
Now, confirm that we used correct variable types: ll for start, end, s, d, L, R, etc.
Now, finalize.
We should also consider that the input might have si up to 1e9, di up to 5e6, but we also may compute end = s + d - 1, which could be up to 1e9 + 5e6 = 1.005e9, that's fine. But later start could be after last, which could be up to sum(d_i) + max(s_i) ~ 2e9.
Now, let's produce the final solution.
But before finalizing, let's do a quick test manually on some random small n=4 to see if any weirdness.
Suppose n=3:
1: s=5,d=2 => schedule 5-6. occupied [(5,6)]
2: s=1,d=3 => L=1,R=3. Check free: occ (5,6): b=6<1? false; a=5>3? true => break => free true? Actually break, and free remains true. That means we schedule at 1-3. But is [1,3] free? Yes, because occupied is [5,6], so days 1-3 are free. So schedule 1-3. After merging: (1,3) and (5,6) -> separate.
3: s=4,d=2 => L=4,R=5. Check free: intervals: (1,3): b=3<4? yes, continue; (5,6): a=5 > R=5? false? a=5, R=5, a > R? 5>5 false, so else -> overlap? Check if overlapping: Since a=5 <= R=5 and b=6 >= L=4? Actually overlap if not (bR). Here b=6 < L=4? false; a=5 > R=5? false; so else -> overlap true. So not free. So go to else. find_earliest: occ = [(1,3),(5,6)]. Before first: first_l=1 -> gap length 0. Between: gap_start = 3+1=4, gap_end=5-1=4, length=1 (day4 only) -> 1 <2 => not enough. After last: last_r=6 -> return 7. So schedule 7-8. Output: 5-6, 1-3, 7-8. Is that correct according to rule? Let's see: Job1 at 5-6, job2 at 1-3 (preferred free), job3 preferred 4-5 overlaps because day5 occupied, so find earliest free block of 2 days. Starting from day1: day1-2? day1-2 free? day1-3 are occupied after job2, so day1-2 not free. day2-3? occupied. day3-4? day3 occupied, so no. day4-5? day4 free, day5 occupied, so not. day5-6? day5-6 occupied. day6-7? day6 occupied, day7 free? but need both days free. day6 occupied, so no. day7-8? both free. So earliest is 7. So output correct. Our algorithm gave 7. Good.
Now, test scenario where there is a gap of length >= d but not at the start of the gap? Not relevant.
Now, I think it's ready.
But consider the possibility that after inserting a job, the new interval might overlap an existing interval due to a bug in free check or find_earliest? But we ensure they are free. However, there is a subtlety: In find_earliest, when we check gaps, we assume that the gaps are free. But what if there is a gap that is exactly of length d, but the gap_start is, say, 5. But we need to ensure that the days 5..5+d-1 are indeed free. Since it's a gap, they are. So it's correct.
Now, about the after-last case: we return occ.back().second + 1. This is the first day after the last occupied interval. That day is free (since last interval ends there). But we also need to check that the infinite tail has enough length, which it does. So fine.
Now, potential issue: What if the schedule has no occupied intervals? Then find_earliest returns 1 directly.
Now, another nuance: The rule says "If the car repair shop is idle for di days starting from si, then these days are used... Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." This implies that the second step ignores the preferred start entirely, just finds the earliest possible globally. That's what we did.
Now, one more test with large numbers: n=1, s=1000000000, d=5000000. free true -> schedule at s. So start=1e9, end=1,004,999,999? Actually 1e9 + 5e6 - 1 = 1,004,999,999. Should we output that? Yes. That's within 64-bit.
Now, we should also consider that output numbers can be large, so use long long and printf or cout with long long.
Now, I'll code in C++.
But wait: there is a nuance in the condition for free check when we break on a > R. In the loop, we have to be careful: The intervals are sorted by start, but it's possible that an interval starts after R but there might be an interval that starts before R but we haven't processed it because we break? But we process in order, so we have already processed all intervals with start <= current start. Since the list is sorted, when we encounter the first interval with start > R, all previous intervals (with start <= current start) have start <= current start, but they may have start <= R? Actually we process in ascending start order. So by the time we see an interval with start > R, all intervals with start less than or equal to that start have been considered. Since start of this interval > R, any previous interval had start <= this start, but they could have start > R? If the previous interval's start also > R, then they would have been after R as well. But the first interval with start > R means all previous intervals had start <= R? Not necessarily: because the list is sorted, if we have an interval with start > R, all subsequent intervals have start >= that > R. But what about earlier intervals? They have start less than or equal to this interval's start. Could there be an interval that we haven't processed yet that has start > R but we haven't reached it? No, we are processing sequentially. So by the time we encounter first interval with start > R, we have already processed all intervals with start less than that. But those intervals with start less than that could include intervals with start > R? No, because if an interval had start > R, then the first interval with start > R would be the smallest start > R. All intervals with smaller start would have start <= that start, but they could still be > R? Actually if start > R, then that interval qualifies as having start > R. The smallest such interval is the first we encounter that has start > R. So before that, all intervals had start <= R. So it's safe: all intervals with start <= R have been processed. Good.
Thus free check is correct.
Now, we can produce final solution.
But there is a potential bug: In the free check, we use 'break' when iv.first > R, but we should only break if we are sure that no later interval can overlap. That is true because later intervals have larger start, so start > R, so they can't overlap because overlap requires start <= R (since if start > R, then the interval is entirely after R). So safe.
Now, final code.
Edge Cases to consider:
-
d=0? Not possible because di >=1.
-
When merging, we need to be careful about overflow when computing max of ends; it's fine.
Now, what about using vector of pairs and sorting each time? Since n is small, fine.
But we can improve by not sorting the entire list each time; we could insert the new interval in the correct place using binary search and then merge neighbors. However, not necessary.
Now, we will implement using std::vector and sort.
Now, I'll write the code accordingly.
But I realize that we might have a mistake: In find_earliest, we compute gap_len = occ[i+1].first - occ[i].second - 1. This is correct if intervals are disjoint and not touching. If they are touching (which shouldn't happen due to merging), then gap_len would be -1? Actually if they are touching, occ[i].second + 1 = occ[i+1].first, so gap_len = occ[i+1].first - occ[i].second - 1 = (occ[i].second + 1) - occ[i].second - 1 = 0. So it's zero. That's fine; we check >= d, which will be false for positive d. So no problem. But our merging ensures no touching, but even if not, it's safe.
Now, also note: In the before first check, we used occ[0].first - 1 >= d. But if occ[0].first is 1, then 0 >= d false; good.
Now, a scenario: occ[0].first could be 0? No, days start from 1.
Now, I think we are done.
Let's also test a scenario where the preferred interval is free, but it is exactly at the start of a gap that we might have considered earlier for other jobs. That's fine.
Now, I'll write the final answer.
But wait, I just realized something: When we check if the preferred interval is free, we must also ensure that the days are not scheduled for repair. However, there is a nuance: what if the preferred interval is free, but the earliest x (if we were to go the else path) would be earlier than s? That doesn't matter; we still schedule at s.
Now, another nuance: The rule says "If the car repair shop is idle for di days starting from si, then these days are used... Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." So the second step does not consider si at all. So it's correct.
Now, we must ensure that when we schedule at s, we don't inadvertently cause overlap because we didn't check correctly. We check correctly.
Now, it's ready.
But consider this scenario: The schedule currently has intervals [1,3] and [5,7]. So free days: 4 and >=8. Now a new job with s=4, d=1. Preferred interval [4,4] is free? Day 4 is free, so yes, schedule at 4. That would create intervals [1,4] and [5,7]? Actually after insertion, we have [1,3] and [4,4] and [5,7]. Merging: [1,3] and [4,4] touch => merge to [1,4]; then [1,4] and [5,7] touch? 4+1=5, so merge to [1,7]. So becomes one block. That's fine.
Now, if job with s=4, d=2, preferred [4,5] would be partially overlapping because day5 is occupied by [5,7]? Actually day5 is occupied. So not free. So else: find earliest x: before first: first_l=1 -> gap none; between: after [1,3] gap_start=4, gap_end=4? Since next is [5,7], gap_len=1, insufficient. After last: candidate=8. So schedule at 8-9. That's correct.
Now, all good.
Now, we must produce final code.
Potential improvement: Use std::set for intervals to automatically keep sorted and merge with neighbor checks, but not needed.
Now, let's write the solution.
We'll include <bits/stdc++.h> using namespace std; typedef long long ll;
Then main:
int n; cin >> n;
vector<pair<ll,ll>> occupied;
for (int i=0;i<n;i++) {
ll s,d; cin >> s >> d;
ll L=s, R=s+d-1;
bool free=true;
for (auto &iv: occupied) {
if (iv.second < L) continue;
else if (iv.first > R) break;
else { free=false; break; }
}
ll start, end;
if (free) {
start = L; end = R;
} else {
// find earliest x
if (occupied.empty()) start = 1;
else {
// before first
if (occupied[0].first - 1 >= d) {
start = 1;
} else {
// between
bool found = false;
for (size_t j=0; j+1 < occupied.size(); j++) {
ll gap_start = occupied[j].second + 1;
ll gap_len = occupied[j+1].first - occupied[j].second - 1;
if (gap_len >= d) {
start = gap_start;
found = true;
break;
}
}
if (!found) {
start = occupied.back().second + 1;
}
}
}
end = start + d - 1;
}
cout << start << " " << end << "\n";
// insert and merge
occupied.push_back({start, end});
sort(occupied.begin(), occupied.end());
vector<pair<ll,ll>> merged;
for (auto &iv: occupied) {
if (merged.empty() || iv.first > merged.back().second + 1) {
merged.push_back(iv);
} else {
merged.back().second = max(merged.back().second, iv.second);
}
}
occupied = merged;
}
Now, check with examples: should produce correct.
But there is a subtlety: In the else branch, we set start = 1 if before first gap fits, else we compute between or after last. But we didn't consider the case where the schedule is empty: we handled separately earlier (occupied.empty() => start=1). Good.
Now, we need to ensure that after insertion, we merge correctly. In merging, we used condition iv.first > merged.back().second + 1 to push new; else merge. That merges when overlapping or touching. Good.
Now, any issue with the condition in find_earliest: occupied[0].first - 1 >= d. But we might have a scenario where the first interval starts at day 1, then first_l-1=0, condition false, we proceed to between/after. That's fine.
Now, test a scenario: occupied = [(1,1)]; d=1. before first: 1-1=0 <1, between none, after last: last_r+1=2 => start=2. That's correct.
Now, scenario: occupied = [(1,2)]; d=2. before first: 2-1? first_l=1 => first_l-1=0<2; after last: last_r+1=3 => start=3. But is there any gap? The gap after is infinite, so [3,4] works. But is there a possibility of [2,3]? day2 occupied, so not. So correct.
Now, scenario: occupied = [(1,1), (3,3)] after merging they would be separate? Actually they would not merge because 1+1=2 <3, so they are separate. So schedule: before first: gap none; between: gap_start=2, gap_end=2, length=1; after last: last_r=3 -> start=4. For d=2, after last =4 => [4,5] works. That's correct. However, is there a gap of length 2? No.
Now, check if find_earliest with occupied = [(1,1), (3,3)] and d=2 returns after last=4. Good.
Now, what about scenario: occupied = [(2,2)]; d=2. before first: first_l=2 => first_l-1=1 <2, after last: last_r=2 => start=3 => [3,4] works. But could we start at 1? [1,2] includes day2 occupied, so no. So correct.
Now, all good.
Now, we should also consider that the preferred interval might be free but we haven't merged it yet. That's fine.
Now, maybe we can simplify find_earliest by scanning gaps including an initial gap from 1 to first_l-1, then between, then after last. That's what we did.
Now, we must be careful about the type of d: it's integer up to 5e6, but we use ll for d as well.
Now, I'm ready to produce final answer.
However, I want to double-check that we correctly handle the case where the preferred interval is free but after insertion, we merge intervals, and then for the next job, our free check uses the merged intervals. That's correct.
Now, potential oversight: In free check, we might break too early if we have intervals like: occupied = [(1,3), (5,7)], L=4,R=4. We iterate: first interval (1,3): iv.second=3 < L=4 => continue. Next (5,7): iv.first=5 > R=4 => break. So free remains true. That's correct because day4 is free.
Now, what if L=5,R=5? Then first interval: iv.second=3<5 continue; second: iv.first=5 > R=5? false (5>5 false). So else? Let's evaluate: For second interval, iv.first=5 <= R=5, and iv.second=7 >= L=5, so else -> overlap true. So free false. Good.
Now, what if L=8,R=8? first interval: 3<8 continue; second: 7<8? iv.second=7<8 true? Actually check: iv.second=7 < L=8 => true, so continue. Then loop ends, free true. So day8 free. That's correct.
Now, our free check works.
Now, also need to consider that after merging, intervals could be like [(1,100)], L=50,R=60. Then iv.second=100<50? false; iv.first=1>60? false; else -> overlap true. So free false. Good.
Now, final code.
But I recall that sometimes in such problems, the "first day x" rule may be interpreted as: after the preferred start is not available, he finds the smallest x >= 1 that works. But what if the preferred interval is free, we use it. That's what we did.
Now, ensure output format: each line two integers separated by space, and newline.
Now, done.
I'll produce the final answer in the required format.
But before finalizing, let's also think about a scenario where the preferred interval is free, but the start day x computed by the second rule is actually earlier than s_i. That doesn't matter; we use s_i.
Now, everything seems fine.
Now, I'll write the final code with some comments for clarity.
But hold on: The problem statement says: "Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x." Could it be that he starts looking from day 1 even if the preferred start is later? Yes.
Now, what about the scenario where the preferred interval is not free, but the earliest x might be before s_i, which is allowed.
Now, check with an example: n=2, job1: s=10,d=1 -> schedule 10. occupied [10,10]. job2: s=5,d=1 -> preferred [5,5] free? Yes, because 5 is free. So schedule at 5. So output 10,10 and 5,5. That's allowed. So final schedule may have out-of-order start times. That's fine.
Now, all good.
Now, let's consider potential integer overflow in computing gap_len = occ[i+1].first - occ[i].second - 1. Since occ[i+1].first and occ[i].second could be up to ~2e9, difference could be up to 2e9, safe.
Now, a final nuance: The problem may have di up to 5·10^6, which is 5,000,000. And si up to 1e9. So sum of di could be up to 1e9. So we used long long, which is at least 64-bit. So safe.
Now, ready to code.
But I'll also test with a random small case manually with brute force in my head:
Let n=3:
Job1: s=5,d=2 -> schedule at 5-6.
Job2: s=3,d=2 -> L=3,R=4. Check free: occupied [5,6] => free? Since 5>4 => break, free true? Actually we break because a>R: a=5 > R=4 => break, free remains true. So schedule at 3-4. So now occupied: after merging: [3,4] and [5,6] touch? 4+1=5 => merge to [3,6]. So schedule becomes [3,6].
Job3: s=4,d=1 -> L=4,R=4. Check free: occupied [3,6] -> iv.second=6<4? false; iv.first=3>4? false; else overlap -> free false. find_earliest: occ = [(3,6)]; before first: first_l=3 => gap length 2 (days 1-2) => 2>=1? yes, so start=1. So schedule at 1-1. Output: 5-6, 3-4, 1-1. That seems correct by rules. The schedule ends up with days 1,3-6 occupied. That's fine.
Now, but is it correct that job3 gets day1? Let's simulate manually: After job1 (5-6) and job2 (3-4) (free because 3-4 is free), schedule becomes 3-6 occupied. Job3 wants start at 4 (duration 1). But day4 is occupied now, so not free. So he finds earliest x from day1: day1 is free, so he schedules at day1. So output day1. That's correct.
Thus algorithm works.
Now, we can finalize.
But I'm thinking: In the find_earliest function, we used condition occ[0].first - 1 >= d. However, if occ[0].first is very large, say 1e9, and d is 5e6, this is true. But we need to ensure that the gap from 1 to first_l-1 is indeed free. It is, because there are no intervals before first_l. So fine.
Now, one more test: occ = [(100,200)], d=150. before first: gap length = 99 <150, so fails. After last: start = 201, which works. So schedule at 201-350. That's correct.
Now, I think solution is ready.
Now, I'll produce final answer.
But before that, double-check for any off-by-one errors: In find_earliest after last, we return last_r+1. That's the earliest day after the last occupied interval. That day is free. But we also need to ensure that we have at least d consecutive free days starting from that day. Since there is no occupation after that (infinite), it's fine. So correct.
Now, in free check for preferred interval, we used L = s, R = s+d-1. That's correct.
Now, in merging condition, we used iv.first > merged.back().second + 1 to push new; else merge. That merges if overlapping or touching. But what if iv.first == merged.back().second + 1? That's touching, so we want to merge. Since condition is >, touching case yields false (since iv.first is not >), so we go to else and merge. Good.
Now, after merging, we guarantee intervals are disjoint and non-touching? Actually they become non-touching because we merge touching. So after merging, there is at least 1 free day between intervals. Good.
Now, final code.
But one more nuance: In the find_earliest, when we compute gap_len = occ[i+1].first - occ[i].second - 1, if the intervals are merged and thus non-touching, then occ[i+1].first - occ[i].second - 1 is the number of free days between them. Good.
Now, I think we are done.
Now, I'll produce the answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// returns the smallest x >= 1 such that a block of d days starting at x is free
ll earliest_free(const vector<pair<ll, ll>>& occ, ll d) {
if (occ.empty()) return 1;
// gap before the first occupied interval: days 1 .. occ[0].first-1
if (occ[0].first - 1 >= d) return 1;
// gaps between intervals
for (size_t i = 0; i + 1 < occ.size(); ++i) {
ll gap_start = occ[i].second + 1;
ll gap_len = occ[i+1].first - occ[i].second - 1;
if (gap_len >= d) return gap_start;
}
// after the last interval: infinite free tail
return occ.back().second + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<pair<ll, ll>> occupied; // merged, non-touching intervals
for (int i = 0; i < n; ++i) {
ll s, d;
cin >> s >> d;
ll L = s, R = s + d - 1;
// check if [L, R] is completely free
bool free = true;
for (auto &iv : occupied) {
if (iv.second < L) continue;
if (iv.first > R) break; // further intervals start later
free = false;
break;
}
ll start, finish;
if (free) {
start = L;
finish = R;
} else {
start = earliest_free(occupied, d);
finish = start + d - 1;
}
cout << start << " " << finish << "\n";
// insert the new interval and merge again
occupied.push_back({start, finish});
sort(occupied.begin(), occupied.end());
vector<pair<ll, ll>> merged;
for (auto &iv : occupied) {
if (merged.empty() || iv.first > merged.back().second + 1) {
merged.push_back(iv);
} else {
merged.back().second = max(merged.back().second, iv.second);
}
}
occupied = merged;
}
return 0;
}