N persons visited a restaurant. The restaurant is open from 0 to T. The i-th person entered the restaurant at li and left at . Find the maximum number of persons during the business hours.
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 parse the problem: "Find the maximum number of persons during the business hours." That is: given N intervals [l_i, r_i] within [0, T] (inclusive? Actually, persons entered at l_i and left at r_i. The restaurant is open from 0 to T. We need to find the maximum number of persons present at any time t in [0,T] (presumably inclusive-exclusive? Usually, if a person leaves at r_i, they are not present at time r_i? Need to clarify based on examples.)
We need max number of persons present at any moment.
Let's simulate:
Time 0: person1 present (0-2). So count=1.
Time 1: persons 1,2 -> 2.
Time 2: At time 2, what's the status? Person1 left at 2, person2 leaves at 3, person3 enters at 2. Usually, if intervals are [l_i, r_i) (i.e., inclusive of l, exclusive of r), then at t=2, person1 is not present, person3 is present. So count: persons: person2 (1-3), person3 (2-6) =>2? But also possibly person1? If inclusive on both ends, then maybe person1 present at 2, but then count would be 3? Let's compute more systematically.
Let's consider typical sweep line: For each person, add +1 at l_i, and -1 at r_i (if they leave at r_i, they are not counted after r_i). Then the maximum prefix sum over sorted event times gives max concurrent.
Now for given example, compute events:
(0, +1)
(2, -1) for person1
(1, +1)
(3, -1) for person2
(2, +1)
(6, -1) for person3
(3, +1)
(8, -1) for person4
(4, +1)
(10, -1) for person5
(5, +1)
(10, -1) for person6
Sort events by time, and if same time, we need to process -1 before +1? Actually to correctly compute number at that exact time, we need to define ordering: At time t, people who leave at t should be gone before those who enter at t. So typical approach: process -1 before +1 at same time for max count. Alternatively, if we want the number just after events at time t (excluding leaving at t), we could process +1 before -1? Let's test both.
We can check what answer is: Output says 4. So maximum number should be 4.
Let's compute timeline with exclusive right.
Times:
At t=0, after arrival of person1: count=1.
At t=1, arrival of person2: count=2.
At t=2: departure of person1 and arrival of person3. If we process departures first: at t=2, remove person1 => count becomes 1, then add person3 => count becomes 2. So after t=2, count=2. If we process arrivals first: add person3 => count=3, then remove person1 => count=2. So same final count=2. But the instantaneous count at exactly t=2 might be ambiguous. Usually we care about maximum over continuous time. The count is constant on intervals between events. So at t just after 2, count=2. So not max.
Continue:
At t=3: departure of person2 and arrival of person4. If depart first: count becomes (from previous count 2) remove person2 => 1, add person4 => 2. So after 3, count=2.
But maybe we need to also consider person3 still there. So at t between 2 and 3, we have persons 2 and 3? Actually after t=2 but before t=3, we have persons: person2 (until 3), person3 (until 6). So count=2. At t=3, after processing, we have person3 and person4? Actually if depart first: after removing person2, we have person3 only (count=1), then add person4 => count=2: persons 3 and 4. If arrivals first: add person4 => persons 2,3,4 => count=3, then remove person2 => count=2. So same final count=2. So still 2.
At t=4: arrival of person5. So after t=4, persons: 3,4,5 => count=3.
At t=5: arrival of person6. After t=5, persons: 3,4,5,6 => count=4 (since persons 3 (until 6), 4 (until 8), 5 (until 10), 6 (until 10)). So count=4.
At t=6: departure of person3. At t=6, remove person3: count becomes 3 (persons 4,5,6). No arrivals. So after 6, count=3.
At t=8: departure of person4: count becomes 2.
At t=10: departures of person5 and person6 (two departures): count becomes 0.
So maximum count = 4, matches output. So exclusive right works.
But note: Some problems treat intervals inclusive-inclusive. Let's test if that would yield max 4? If intervals inclusive of both ends, then at time 2, persons 1,2,3 all present -> count 3; at time 3, persons 2,3,4 -> count 3; at time 5, persons 3,4,5,6 -> count 4; at time 6, persons 3,4,5,6? Actually if inclusive, person3 present at 6, person4 at 6, person5 at 6, person6 at 6 -> count 4; at time 8, persons 4,5,6 -> count 3; so max 4 as well. But also maybe at time 10, persons 5,6? If inclusive of T, but restaurant closes at T, maybe not counted. Anyway 4 still matches. But need to ensure typical interpretation.
Second example:
Input:
2 2
0 1
1 2
Output: 1
If exclusive right: At time 0: count=1 (person1). At time 1: arrival of person2 and departure of person1. After processing, count=1 (person2). So max count=1. If inclusive right: At time 1, both persons present (since person1 leaves at 1 inclusive, person2 arrives at 1 inclusive), count=2. That would output 2, but expected output is 1. So the correct interpretation is exclusive at right: intervals are [l_i, r_i) with r_i being exit time, so they are not present at time r_i. At time l_i they become present. So we need to count +1 at l_i, and -1 at r_i.
Thus the problem reduces to: given N intervals within [0, T] (where times are integer? Actually constraints say T up to 1e5, and l_i, r_i are integers presumably; but they can be any numbers? Input gives integers. So we can treat them as integer points. But note: the maximum may occur at non-integer times? Since events happen at integer times, but between integers count is constant, so maximum can be found at integer times just after events. So we can use event sweep.
Simple approach: Create an array of size T+2 (since times up to T inclusive). Since T <= 1e5, we can use difference array. For each interval, add +1 at l_i, and -1 at r_i. Then compute prefix sum over time indices 0..T (or up to T-1?) and find max.
But careful: Because r_i can be T (e.g., 10), and we should not include beyond T? The restaurant is open from 0 to T. At time T, does it close? Probably at time T, the restaurant is closed, so we care about times strictly less than T? However, if someone leaves at T, they are not present at T (since exclusive right). So we only care about times t where 0 <= t < T (or maybe t in [0,T)?). The problem says "during the business hours" which is from 0 to T. Usually that includes 0 but excludes T? Possibly inclusive at T? But the second example suggests exclusive at right because at t=2, person2 leaves at 2, so at time 2, should we count? Restaurant closes at T=2, so maybe at exactly T it's closed, so we don't consider that moment? The second example: if intervals [0,1] and [1,2], then max is 1. If we consider t in [0,2) inclusive of 0, exclusive of 2, then at t=1, we have to decide: person1 left at 1, person2 arrives at 1. Since exclusive at right, at t=1 exactly, person1 is gone if we process departures first. If we consider continuous time, just after 1, only person2 is present; just before 1, only person1. So maximum is 1. So indeed output 1.
Thus our event sweep: add +1 at l_i, add -1 at r_i. Then compute prefix sum over times sorted by event points. Since T is up to 1e5, but l_i and r_i are within [0,T] integer. So we can use an array diff[0..T+1] (maybe size T+2) and then prefix sum. However, note: r_i can be T, and we should apply -1 at r_i, but the prefix sum for times >= r_i should not count that person. For times less than r_i, they are counted. So the array approach: we can do diff[l_i]++, diff[r_i]--, then compute prefix sum for i from 0 to T-1 (or 0 to T) and take max. But careful: If we compute prefix sum up to T inclusive, at index T we might include contributions from intervals that ended at T (since diff[T]--). That would reduce count appropriately. But we don't care about time T (since restaurant closed). However, the prefix sum at time i could be interpreted as count at time i after processing events up to i? Let's define: Let an array cnt[t] denote number of persons at time t, where t is integer time point? But the actual occupancy is constant on intervals [t, t+1) if events occur at integer times. Typically, we can compute occupancy for "moments" just after integer times. To avoid confusion, we can process events sorted, as we did before, and keep a running count.
Given that T <= 1e5 and N <= 1e5, we can use event sweep without needing to allocate an array of size T if we sort events, but using an array is simpler because T is only 1e5.
But we must be cautious: times are integers, and maximum count may occur at non-integer times? Actually since all event times are integers, the count changes only at integer times. So the maximum count is constant on intervals (t, t+1) for integer t from 0 to T-1. So we can compute the count on each integer segment. This can be done by prefix sum: if we define an array diff of length T+1 (or T+2) and then prefix sum for i from 0 to T-1 yields the count on interval (i, i+1) after processing events at i? Let's formalize:
We have events: +1 at l_i (inclusive start), -1 at r_i (exclusive end). The number of people present at any time t (real) is sum over i of indicator(l_i <= t < r_i). This is a step function. For integer t_0, the value just after t_0 (or at any non-integer point) can be derived by considering t in (t_0, t_0+1). The count on interval (t, t+1) for integer t is: sum over i of indicator(l_i <= t and r_i > t+1? Actually need to be precise: For any real t in (a, b) where a,b are consecutive event times, the count is constant. But if we only have integer bounds, the intervals between integer times may contain event times at boundaries. However, we can compute the count at any real time t as the cumulative sum of events up to time t (with appropriate handling at exact event times). The count is right-continuous if we define that at the moment of entry, person becomes present immediately, and at moment of exit, they leave immediately. That is typical: count(t) = number of i such that l_i <= t < r_i. This is left-continuous? Actually l_i <= t < r_i means at t exactly equal to l_i, person is present; at t exactly equal to r_i, person is not present. So the function is right-continuous? At t = l_i, left limit: just before l_i, count does not include i; at t = l_i, count jumps +1, so it is right-continuous? Actually right-continuous means limit from right equals value. At a jump upward, the value at the point is the higher value, limit from right is same as value, limit from left is lower. So it is right-continuous. At t = r_i, count jumps -1; at t = r_i, person not present; limit from left includes person; limit from right excludes; so value = right limit, so also right-continuous. So the function is right-continuous. So count(t) = sum_{i} (1 if l_i <= t < r_i else 0).
Now to find max over t in [0,T). We can consider all times t, but because it's step function with changes at l_i and r_i, the maximum occurs just after an event (or at the start). So we can compute by sweeping events sorted by time, with ordering: for same time, we should process departures (r_i) before arrivals (l_i)? Actually need to get the correct count at time t after processing all events at that time. Since count(t) is defined as l_i <= t < r_i, for a given time t0 which equals some l_i or r_i, we need to decide inclusion. Let's check: For t = l_i, condition is l_i <= t, so true, so person is included. For t = r_i, condition is t < r_i, so false, so person is excluded. So at time exactly equal to an arrival, the person is present; at time exactly equal to a departure, the person is absent. Therefore, when processing events at a given time, we should add +1 for arrivals before we compute count for that time? Actually count at that exact time should include arrivals but exclude departures. So to compute count at the exact time after events at that time have been accounted, we could process all -1 (departures) first, then all +1 (arrivals), then record count. That would give count(t) including arrivals at t, excluding departures at t. But note: if an arrival and departure happen at same time, they could be for different persons. In that case, we want arrivals to count, departures to not count. So process -1 before +1 yields correct count at that exact time. If we process +1 then -1, then at the moment we would have included the departing person still? Actually if we process +1 then -1, then after processing both, the net effect is the same because we subtract that person. But the intermediate count may be off if we record after each, but we only record after processing all events at that time. Since both arrivals and departures at same time are independent, the order does not affect final count after processing all events? Let's check: Suppose at time t, we have one arrival and one departure (different persons). Starting with count C before t. If we process -1 first: C' = C - 1 (departure), then +1: C'' = C' + 1 = C. So final count = C. If we process +1 first: C' = C + 1, then -1: C'' = C' - 1 = C. So final count same. So for final count after processing all events at that time, order doesn't matter. However, the count at the exact time t should be after arrivals and after departures? According to definition, at time t equal to an arrival, that person is present; at time t equal to a departure, that person is absent. So the final count after processing both is correct (the arriving person present, departing person absent). So as long as we apply both updates, final count will be correct regardless of order. But if we wanted to capture the maximum possible count at any instant, we might consider the count during the moment when arrivals have occurred but departures haven't? But such a moment does not exist because events happen at the same time instant. So we consider the function right-continuous, so at the exact time t, the value is defined as above (arrivals included, departures excluded). So that's fine.
Thus we can sort events (time, delta) and process in any order, as long as we apply all deltas for that time before recording count for that time (or after). But we need to be careful about intervals between times. Actually, we want the maximum over all real t. Since the count is constant on intervals between event times, we can record the count after processing all events at a time, and that count will hold for (t, next_event_time). Also we need to consider the initial interval from 0 to first event. At time 0, if there are arrivals at 0, they should be present. So we can start count = 0, then process events sorted. But we should also consider times before any event? Since start is at 0, we might have arrivals at 0, so we need to include them. So processing from time 0: initially count = 0. Then we process events in increasing order. For each distinct event time, we apply all deltas, then after updating, the count is the number of people present for times just after that time (i.e., (t, next_time)). But is that correct for right-continuous? Let's test: Suppose first event at time 0: arrivals. We start count = 0 before processing any. Then process time 0: we apply +1's. After applying, count becomes number of people present for times t>0 up to next event. But what about t=0 exactly? According to definition, at t=0, persons with l_i=0 are present. So the count at t=0 equals the count after applying arrivals at time 0 (since they are present). So if we record after processing events at time 0, that count is valid for t in [0, next_event_time) if we consider the right-continuous definition? Actually at t=0 exactly, the function value is count after arrivals (since arrivals are included at t=0). So if we treat intervals as half-open [t, next_time) with left-closed, then after processing events at time t, that count holds from t inclusive to next event exclusive. Because at time t, the value is that count. So we can safely take the count after processing all events at time t as the value for the interval [t, next_time). Then we consider maximum over these intervals.
But is there any scenario where count can be higher in an interval that starts before an arrival? For example, before time 0, we don't care. So approach: sort events, sweep, keep current count, after processing all events at a time t, update max with current count. Also we need to consider the initial state before any events? At time 0, if there are no events at 0, then the count is 0 for t in [0, first_event_time). But if first event is at time >0, then for t in [0, first_event_time), count = 0. So we need to initialize max=0, and if we process events starting from the smallest time, after processing events at that time we set max. But we should also consider the period before the first event. So before starting sweep, we can set max = 0 (or maybe count initial 0). Then process events: for each distinct time, apply deltas, then update max with the new count. However, this will miss the period between events? Actually after processing events at time t, the count applies from t onward until the next event. So we have captured the count for the interval starting at t. But what about the interval from 0 to first event? That's captured by initial count (0) before any events. So we should set max = initial count (0) before sweep. But we also need to consider if there are events at time 0: after processing them, we have count >0, which applies from 0 onward. So max will be updated to that. So that works.
Now for times equal to T: The restaurant is open until T, but do we consider times up to T? At time T, business hours end. The definition says restaurant is open from 0 to T. Usually "during business hours" means time within that interval, possibly inclusive of 0 and exclusive of T? Or inclusive of both? The second example indicates that at t=T (2) we should not count the person leaving at 2? Actually if restaurant closes at T, we might not consider t=T because it's the closing time, and persons must leave by then. So the occupancy at exactly T is 0 (or not considered). In our definition, persons are present for t < r_i, and r_i can be T. So at t=T, they are not present. So the occupancy function for t in [0,T) is defined. So we only care up to T (exclusive). So when we process events, we have events at times up to T. At time T, we may have departures (r_i = T). They should be processed, but after processing them, the count will be for times >= T? Actually after time T, we don't care. But we need to consider interval [last_event_before_T, T). For example, if the last event is at time 5, and T=10, then after processing events at time 5, the count holds from 5 to next event. If there are no events after 5 but before T, the count holds from 5 to T. So we don't need to process an event at T unless there are departures/arrivals at T. But note arrivals at T? l_i can be T? Constraint says l_i < r_i, and r_i <= T. So l_i could be equal to T? But if l_i = T, then r_i > T? No, r_i <= T, but l_i < r_i, so if l_i = T, then r_i > T, impossible because r_i <= T. So l_i cannot be T. So only possible events at T are departures (r_i = T). At time T, these departures should be processed: before T, those persons were present (since r_i=T, they are present for t < T). At t=T, they are not present. So the occupancy for t in [some previous time, T) includes those persons. After processing the departures at time T, the count becomes lower, but that applies for t >= T, which we don't care. So we should not update max after processing events at time T, because that count is for t >= T, not during business hours. However, what about the interval from the last event before T up to T? The count just before T is the count before processing departures at T. That count includes persons with r_i=T. So that count is valid for t in (last_event_time, T). So we need to consider that count as well. In our sweep approach, after processing events at time t (where t < T), we set max with the count, and that count holds for [t, next_event_time). If the next event time is > T? All events are <= T. Actually the next event could be at T. For t < T, after processing events at that time, the count holds until the next event. If the next event is at T, then the count holds for the interval [t, T). So we will have recorded that count as max candidate. Then when we process events at time T, we would update count to something else, but we don't need to consider that for max. So we can simply sweep all events sorted, and after processing each time point (including possibly T), we could update max only if the time is < T? But careful: if we process events at time T, the count after processing will be for t >= T, and we don't care. So we should not update max there. But if we update max after processing each time, and we also have initial max 0, the max will not be affected by the T events because they only decrease count (or maybe if there were arrivals at T? not possible). So updating after T would possibly lower max, but max already captured the higher earlier count. So it's safe to just update after every event time (including T). However, to be precise, we can simply ignore after T. But the simplest: sort events, initialize count=0, max=0, iterate through sorted event times; for each time, apply all deltas; after applying, if time < T, update max? But what about the interval from the last event before T to T? Suppose the last event time is t_last < T, after processing events at t_last, count holds for [t_last, T). That is included because we update max after processing at t_last. So that's fine. But what if there are no events at all? Then we never update max, but max=0 initially; that's correct because there are no persons.
Thus solution: Use an array of length T+2 to do difference, then compute prefix sum up to T-1 (or up to T, but careful). Let's explore array method:
Let diff[0..T] initialized to 0. For each interval [l, r):
diff[l] += 1
diff[r] -= 1 (since r is exclusive)
Then compute prefix sum: cur = 0; for i from 0 to T-1: cur += diff[i]; update max with cur. Because at i we want the count for times in (i, i+1) or [i, i+1)? Let's test with example: diff[0]++ (1), diff[2]--; diff[1]++ (1), diff[3]--; diff[2]++ (1), diff[6]--; diff[3]++ (1), diff[8]--; diff[4]++ (1), diff[10]--; diff[5]++ (1), diff[10]--.
Now compute prefix sum:
i=0: cur += diff[0]=1 => cur=1, max=1 (count for t in [0,1) ) correct.
i=1: cur += diff[1]=1 => cur=2, max=2 (t in [1,2)) correct.
i=2: cur += diff[2]=? diff[2] currently: from person1: -1 at 2, from person3: +1 at 2, so diff[2]=0? Actually we added +1 for person3 at 2, and -1 for person1 at 2, so net diff[2] = 0. So cur = 2 + 0 = 2, max stays 2. But we expected count for t in [2,3) to be 2 (persons 2 and 3). That's correct. However, wait: after i=2, cur=2. But earlier we computed that after processing events at time 2, count=2. So okay. But is this count for t in [2,3)? Yes.
i=3: diff[3] has from person2: -1, from person4: +1 => net 0. cur remains 2, max still 2 (but we expected count 2 for t in [3,4)? Actually earlier we had after processing events at time 3, count=2 (persons 3 and 4). So cur=2, matches.
i=4: diff[4] = +1 (person5). cur=3, max=3.
i=5: diff[5] = +1 (person6). cur=4, max=4.
i=6: diff[6] = -1 (person3). cur=3, max stays 4.
i=7: diff[7] = 0. cur=3.
i=8: diff[8] = -1 (person4). cur=2.
i=9: diff[9] = 0. cur=2.
i=10: diff[10] = -1 -1 = -2. But we stop at i = T-1 = 9? Actually if we go up to T-1, we don't include i=10. Since T=10, we should only consider times up to 9. So we do i from 0 to 9 inclusive? Actually we need to consider intervals up to T: for t in [9,10). That's the last interval. So we should loop i=0..T-1. That gives correct max=4. If we include i=10, cur would become 1? Actually after i=10, cur becomes 1? Let's compute: after i=9, cur=2. Then i=10: cur += -2 => cur=0. That would correspond to time interval [10,11) but we don't care. So not needed. So array method works by iterating i from 0 to T-1.
But note: What if there is an event at time 0? diff[0] increments, and we include i=0, which adds that to cur, giving count for interval starting at 0. Good.
What if there is an event at time T? diff[T] has negative values, but we never use diff[T] in our loop (since we stop before T). That's correct because persons leaving at T are not counted after T; they were counted up to T, and the loop up to T-1 included them. Good.
Thus array method is straightforward.
Edge Cases: If T is large (up to 1e5), array is fine. N up to 1e5, so O(N+T) time is fine.
Now we need to implement reading input and output.
Input format: first line N T, then N lines each with l_i r_i.
Constraints: 0 ≤ l_i < r_i ≤ T.
Thus we can proceed.
But wait: The problem statement: "Find the maximum number of persons during the business hours." It might be simply the maximum overlap of intervals. So our approach is correct.
Potential Pitfalls: Could there be intervals where r_i = 0? No, since l_i < r_i, r_i > l_i >= 0, so r_i >= 1? Not necessarily if l_i can be 0 and r_i >0, so r_i could be 0? Not possible. So okay.
Now, we must ensure that we correctly handle times that are not integers? Input l_i, r_i are integers. So we can treat them as integer.
Thus solution: create diff array of size T+2 (since we need index up to T, inclusive for diff). Initialize with 0. For each interval, diff[l_i] += 1; diff[r_i] -= 1. Then cur = 0; max_persons = 0; for i in 0..T-1: cur += diff[i]; if cur > max_persons: max_persons = cur. Print max_persons.
But is there any possibility that the maximum occurs at some non-integer point that is not captured by integer indices? Since all events are at integer times, the count only changes at integer times. So the count on each integer segment [i, i+1) is constant. So we cover all. So it's fine.
Now we can implement.
But wait: what about if there are arrivals at time T? Not possible because l_i < r_i ≤ T, if l_i = T, then r_i > T, contradiction. So none.
Thus solution seems straightforward.
But I'm thinking: maybe the problem expects output 4 for first example, and 1 for second. So array method yields that.
Now we can produce code.
But we must consider memory: T up to 1e5, so array of size 1e5+2 is fine.
Complexities: O(N+T).
Edge Cases: N=0? But constraints: N≥1? Actually says 1≤N, so at least one.
Now there might be nuance: Restaurant open from 0 to T inclusive? The second example suggests exclusive at T. So we are good.
Let's test with a few cases:
Case: N=1, T=5, interval [0,5]. diff[0]++, diff[5]--. Loop i=0..4: i=0 cur=1, max=1; i=1 cur=1; ... i=4 cur=1 => max=1. So output 1. That seems correct: only one person present.
Case: Two intervals: [0,2], [1,3] with T=3. diff: +1 at 0, -1 at 2; +1 at 1, -1 at 3. Loop i=0..2: i=0 cur=1; i=1 cur=2; i=2 cur = after adding diff[2]? diff[2] = -1, so cur=1. So max=2. That's correct: at time 1 to 2, two persons.
Now consider if intervals have same l and r? e.g., [1,2] and [1,2]. diff[1]+=2, diff[2]-=2. Loop: i=0 cur=0; i=1 cur=2; i=2? T=2, we only go to i=1? Actually T=2, loop i=0 to 1. i=0: cur=0; i=1: cur=2; So max=2. That's correct: both present at time interval [1,2). At time exactly 2, they left.
Now test example 2: N=2, T=2, [0,1], [1,2]. diff[0]++, diff[1]--; diff[1]++, diff[2]--. diff[1] net = 0. Loop i=0 to 1: i=0 cur=1; i=1 cur = cur+diff[1] = 1+0=1. So max=1. Works.
Thus solution is correct.
But there is a nuance: The problem might be from some contest where T can be up to 10^9? But constraints say T <= 10^5, so it's fine.
Alternative approach: sort all events and sweep to avoid O(T) if T was larger, but not needed.
Now implement.
We need to read input. Use standard input reading.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
N, T = map(int, input().split())
diff = [0]*(T+2) # extra to be safe
for _ in range(N):
l, r = map(int, input().split())
diff[l] += 1
diff[r] -= 1
cur = 0
max_people = 0
for i in range(T): # 0 to T-1 inclusive
cur += diff[i]
if cur > max_people:
max_people = cur
print(max_people)
We should ensure that diff array is large enough for index r, where r can be T. So size T+1 is enough (indices 0..T). diff[T] used only for decrement; we never access diff[T] in loop because loop goes to T-1. But we need diff[T] when we compute cur? Actually we only add diff[i] for i up to T-1, so diff[T] not used. So size T+1 is enough. But we could allocate T+2 just to be safe.
Complexity O(N+T).
Now test with examples.
Example 1:
N=6,T=10; diff after processing as above; loop i=0..9 yields max=4. Output 4.
Example 2: output 1.
Works.
Thus answer.
But I notice: The problem statement says "The i-th person entered the restaurant at l_i and left at r_i." And we need "maximum number of persons during the business hours." Possibly they consider inclusive intervals [l_i, r_i] (including both ends). Then the second example would be 2, but output is 1. So exclusive right is correct.
Now confirm with another test: Suppose N=1, T=1, interval [0,1]. According to our method, diff[0]++, diff[1]--, loop i=0 -> cur=1, max=1. So output 1. If inclusive, would have one person present at time 0 and at time 1? At time 1 inclusive? But business hours until 1, maybe they close at 1, so they might not be there at 1. So 1 is plausible.
Thus solution final.
Now I need to produce the answer. But as ChatGPT, I need to respond with the final code and explanation? The problem statement likely expects just code. But in typical usage, I must provide solution. Since this is a coding problem, I'll write a Python solution.
But note: Some judges might have large input, so we should use fast I/O.
Edge Cases: T could be 0? But constraints say 1 ≤ T, so T >=1.
Now confirm that diff array may become large if T=100000, which is fine.
Now one more test: Overlap at same time many persons.
Now finalize.
But wait: Is there any scenario where the maximum number is not captured by integer points because the events are not integer? But input l_i and r_i are integers; so changes only at integers.
Thus answer is ready.
But I'm thinking: The problem could be from AtCoder? Actually I recall AtCoder beginner problem "Maximum Number of Overlaps" or something like that. Usually they do sweep line.
But let's double-check if there is any trick: The restaurant is open from 0 to T. That means we only consider times t in [0, T]. But if we consider inclusive right at T, then we might need to consider t=T as well. But the second example output 1 indicates we shouldn't. So fine.
Now implement.
Let's produce final answer in Python.
But for thoroughness, consider if we can do with sorting events to avoid O(T) memory? But T is 1e5, so fine.
I'll include explanation in comments.
Now final code:
python
import sys
def main():
input = sys.stdin.readline
N, T = map(int, input().split())
diff = [0] * (T + 1) # indices 0..T
for _ in range(N):
l, r = map(int, input().split())
diff[l] += 1
diff[r] -= 1
cur = 0
ans = 0
for i in range(T): # up to T-1
cur += diff[i]
if cur > ans:
ans = cur
print(ans)
if __name__ == "__main__":
main()
Check: diff allocated size T+1; when r = T, diff[T] accessed; that's fine as long as T+1 elements: indices 0..T. Then loop i from 0 to T-1 inclusive (range(T) yields 0..T-1). Works.
Now if T=0, range(T) would be empty, but T>=1.
Thus done.
But could there be negative indices? No.
Now final answer.
However, I'm conscious of potential off-by-one if T is extremely large but still within constraints. But fine.
So final. ```python
import sys
def main() -> None:
input = sys.stdin.readline
N, T = map(int, input().split())
# difference array, indices 0 .. T
diff = [0] * (T + 1)
code
for _ in range(N):
l, r = map(int, input().split())
diff[l] += 1
diff[r] -= 1
cur = 0
ans = 0
# The restaurant is open for t in [0, T); we only need i = 0 .. T-1
for i in range(T):
cur += diff[i]
if cur > ans:
ans = cur
print(ans)