We need to solve the problem: given sorted coordinates of all residents (both riders and drivers) with unique coordinates, and a binary array indicating which are drivers (t_i=1). For each driver (ordered by increasing coordinate), we need to compute the number of riders that are assigned to that driver under the rule: each rider is assigned to the closest driver (by absolute distance), with ties broken by smaller coordinate driver. We need to count for each driver, the number of riders whose closest driver (with tie-breaking) is that driver.
Constraints: n,m up to 1e5, total N = n+m up to 2e5. Coordinates sorted and distinct.
We need to output m numbers.
This is essentially a Voronoi diagram on a line with tie-breaking favoring left driver when distances equal.
One approach: sweep line. The riders are between drivers; each segment of the line between drivers will be divided at the midpoint, with tie-breaking affecting points exactly at midpoints (they go to left driver). Also riders to the left of the leftmost driver and to the right of the rightmost driver: all those riders assign to the nearest driver, which is the leftmost driver for left side, rightmost for right side, because no other driver is closer. But careful: For left side, the leftmost driver is the closest because all other drivers are to the right, so distance is larger. So all left-side riders go to leftmost driver. Similarly right side to rightmost driver.
Thus, the assignment is: For each rider at coordinate x, find the nearest driver. Since coordinates are sorted, we can do two-pointer or binary search. But to compute counts efficiently, we can determine intervals of riders assigned to each driver.
Let driver positions sorted: d_1 < d_2 < ... < d_m.
Consider riders between d_i and d_{i+1}. For a rider at x in (d_i, d_{i+1}), the closest driver is either i or i+1. The decision boundary is the midpoint m_i = (d_i + d_{i+1}) / 2. If x < m_i, then distance to d_i is less; if x > m_i, then distance to d_{i+1} is less. If x equals exactly m_i (which can happen only if sum is even and coordinate is integer), then tie-breaking: choose driver with smaller coordinate, i.e., d_i. So essentially, riders at positions <= m_i (strictly less if we consider real numbers, but since coordinates are integers, the midpoint could be .5, but there is no integer exactly at .5; so tie only occurs if (d_i + d_{i+1}) is even and there exists an integer exactly at that coordinate; but coordinates are integer and distinct, so it's possible that a rider lives exactly at the midpoint if d_i and d_{i+1} have integer coordinates and their average is integer. But the riders have integer coordinates, and they are distinct from drivers? Actually riders can have coordinates that coincide? The problem says each resident (including taxi drivers) lives in its unique location; so no two residents have same coordinate, regardless of being driver or rider. So drivers have unique coordinates and riders have unique coordinates and no rider shares a driver's coordinate. But a rider could be exactly at the midpoint if that coordinate is not occupied by a driver. However, midpoint is integer if sum even. Then a rider could exist there. In that case, we need to assign that rider to the left driver (since tie break chooses smaller coordinate). So it's consistent: for any rider at coordinate <= m_i (with equality only possible if m_i integer and rider there), they go to driver i, else to driver i+1.
But careful: m_i is the point where distances equal. Because coordinates are integers, the set of integer points that are closer to d_i than to d_{i+1} are those with x <= floor((d_i + d_{i+1}) / 2) ??? Let's derive.
Distance to d_i: |x - d_i|; to d_{i+1}: |x - d_{i+1}|. For x between d_i and d_{i+1} (inclusive ends? but there are no riders at driver positions), we have x - d_i >=0, d_{i+1} - x >=0. Condition to prefer d_i: (x - d_i) <= (d_{i+1} - x) i.e., 2x <= d_i + d_{i+1} => x <= (d_i + d_{i+1})/2. If x is integer, then we need to determine strictness. If (d_i + d_{i+1}) is even, then equality gives tie -> left driver (d_i). So x <= (d_i+d_{i+1})/2 includes equality for left driver. If (d_i+d_{i+1}) is odd, then (d_i+d_{i+1})/2 is a .5; integer x <= that .5 means x <= floor((d_i+d_{i+1})/2). So we can define boundary as floor((d_i+d_{i+1})/2) for left assignment. But note that the rider at exactly floor((d_i+d_{i+1})/2) will have distance to d_i less than to d_{i+1} if sum odd? Let's check: suppose d_i=2, d_{i+1}=5 => sum=7, half=3.5. integer x=3: distance to d_i=1, to d_{i+1}=2 -> indeed closer to d_i. So x=3 goes to left. x=4: distance 2 vs 1 -> goes to right. So boundary at floor(3.5)=3 is left; right starting from floor+1=4. If sum even, e.g., d_i=2, d_{i+1}=6 => sum=8, half=4. integer x=4: distances 2 vs 2 tie, left driver. So left includes x=4. Right starts from 5. So left includes x <= 4; right x>=5. So boundary is effectively: left goes up to floor((d_i+d_{i+1})/2) inclusive; right from floor((d_i+d_{i+1})/2)+1 inclusive.
Thus for each pair of consecutive drivers, we can split the integer coordinates between them. However, riders can also lie exactly at driver positions? No because unique.
Now also consider riders left of d_1: all go to d_1. Similarly riders right of d_m go to d_m.
Thus, we can compute for each driver i, the count of riders whose coordinates fall into the interval assigned to driver i.
Specifically:
- For driver 1: riders with x < d_1 (all) plus riders between d_1 and d_2 that are assigned to driver 1, i.e., x in [d_1+1? but careful: riders exactly at d_1? no, only driver there; riders at coordinates less than d_1? all those go to driver 1. Also riders in (d_1, d_2] that are assigned to driver 1: those with x <= floor((d_1+d_2)/2). But note that riders exactly at d_2? d_2 is driver, so no rider there. So we can consider interval: (-inf, floor((d_1+d_2)/2)] but exclude driver positions themselves? But we will just count riders that satisfy these conditions. However, we need to consider that d_1 itself is driver so no rider there. So left interval is x < d_1. For the segment between d_1 and d_2, the assignment is for x where d_1 < x < d_2 and x <= floor((d_1+d_2)/2) go to driver 1; those x > floor((d_1+d_2)/2) go to driver 2. But if there is a rider exactly at floor((d_1+d_2)/2) which is integer, it's included.
Similarly for driver i (2 <= i <= m-1): riders from the left side of the segment between d_{i-1} and d_i that are assigned to i: those with x >= floor((d_{i-1}+d_i)/2)+1 (since that's the start of right side from previous boundary) up to floor((d_i+d_{i+1})/2). Actually careful: The interval of riders assigned to driver i is the union of:
- from the segment between d_{i-1} and d_i, the right part: x in [ floor((d_{i-1}+d_i)/2)+1, d_i-1? but since x < d_i, but also x > floor((...)/2)+? Actually we need to combine with the left part from the segment between d_i and d_{i+1}. So effectively, driver i covers all x that are closer to d_i than to any other driver. On the left side, the boundary with driver i-1 is at b_{i-1} = floor((d_{i-1}+d_i)/2). So left side includes x > b_{i-1} (since those are assigned to i) and also includes x exactly at b_{i-1}+1? Actually if the boundary is b = floor((d_{i-1}+d_i)/2), then for x <= b, assign to i-1; for x >= b+1, assign to i, provided x < d_i? But what about x = d_i? not a rider. So the right part from left segment is x in (b, d_i) (but integer x so x >= b+1 and x <= d_i-1). On the right side, boundary with driver i+1 is b_i = floor((d_i+d_{i+1})/2). For x between d_i and d_{i+1}, assign to i if x <= b_i, assign to i+1 if x >= b_i+1. So the left part from right segment is x in (d_i, b_i] (i.e., d_i+1 <= x <= b_i). So total for driver i: x in (b_{i-1}, b_i]? But careful: b_{i-1} is the left boundary; b_i is the right boundary. However, note that b_{i-1} may be less than d_i, and b_i may be greater than d_i. So indeed the interval (b_{i-1}, b_i] (integer interval) contains all points that are closer to d_i than to i-1 or i+1, considering also points beyond the adjacent drivers? Actually points left of d_{i-1} are assigned to i-1 or earlier, points right of d_{i+1} assigned to i+1 or later. So for interior driver i, the region of riders assigned to i should be exactly the set of integer coordinates x such that the nearest driver is i. That region is a contiguous interval? On a line with points and tie-breaking to left for equal distances, the Voronoi cells are intervals (possibly with boundaries at midpoints). For drivers on a line, each driver's Voronoi cell is an interval [L_i, R_i] where L_i and R_i are boundaries between d_i and neighbors, with appropriate inclusive/exclusive. Indeed, for driver 1, cell = (-inf, B1] where B1 = floor((d1+d2)/2). For driver i (1 < i < m), cell = (B_{i-1}, B_i] where B_{i-1} = floor((d_{i-1}+d_i)/2), B_i = floor((d_i+d_{i+1})/2). For driver m, cell = (B_{m-1}, +inf). But note that driver positions themselves are excluded (they have no rider). However, the intervals include coordinates that are riders. Also note that for interior drivers, the left boundary is exclusive of B_{i-1} because those points go to driver i-1, and right boundary inclusive of B_i? Actually need to check inclusiveness based on the tie-breaking at boundaries. At boundary between i-1 and i, the point at exact midpoint (if integer) goes to left driver i-1. So the set of points assigned to i are those strictly greater than that midpoint, i.e., x >= floor((d_{i-1}+d_i)/2) + 1. So left boundary is exclusive of B_{i-1}? Actually B_{i-1} = floor((d_{i-1}+d_i)/2). Then points with x <= B_{i-1} go to i-1. So for driver i, left bound is B_{i-1}+1 inclusive. At boundary between i and i+1, points with x <= floor((d_i+d_{i+1})/2) go to i (including tie which goes to i). So right bound for i is B_i inclusive. So interval is [B_{i-1}+1, B_i] for interior i. For driver 1: leftmost, all points less than d1 go to 1, and points between d1 and d2 with x <= B1 go to 1, so interval = (-inf, B1] but excluding d1? Actually d1 itself is not a rider, but it's included in that interval? Since B1 could be >= d1? Let's compute: d1 < d2, B1 = floor((d1+d2)/2). Since d1 < d2, B1 >= d1? Actually if d1=2,d2=6 => B1=4 which is > d1. But d1 is driver coordinate, not a rider. So the interval for driver 1 can be defined as (-inf, B1] \ {drivers positions}. Since d1 is not a rider, it doesn't matter. However, to count riders, we need to include all rider coordinates that lie <= B1, plus those < d1. So effectively, the cell for driver 1 is (-inf, B1] (with the understanding that driver coordinate d1 is excluded from counting). Similarly for driver m: [B_{m-1}+1, +inf).
Thus, we can compute counts by determining for each driver the range of coordinates (in terms of integer coordinates) that are assigned to them, and then count how many riders' coordinates fall into that range.
Given that we have the sorted list of all residents with flags, we can extract rider coordinates and driver coordinates separately. Since we need answer for each driver in order of increasing coordinate, we can compute boundaries and then count riders in intervals.
Simplest: iterate through the combined sorted list, maintain current drivers and count riders between. Process intervals one by one.
Method: Let drivers positions be at indices where t=1. The riders are at indices where t=0. Since the coordinates are sorted, we can scan left to right, and for each rider, assign to the appropriate driver based on nearest neighbor among drivers. However, if we do this for each rider, it would be O(n log m) using binary search, or O(n+m) using two-pointer. But we need to output counts per driver. Since both n,m up to 1e5, O(n log m) is fine (1e5 * log 1e5 = 1e6). But we can also do linear.
But we need to be careful: each rider's nearest driver can be found by locating the driver just left and just right of the rider. Since the list is sorted, we can maintain pointers: i for riders, j for drivers. Or we can do a sweep that processes events.
However, the problem probably expects O(N) or O(N log N) solution.
I'll describe a linear solution:
- Extract driver coordinates into array D (size m) preserving order.
- Then iterate over all riders, for each rider coordinate x, find the driver to the left (largest D <= x) and to the right (smallest D > x). This can be done by merging sorted lists. Since both riders and drivers are sorted by coordinate, we can traverse both lists: keep pointer to the next driver. For each rider, we can compute the nearest driver by comparing distances to the current driver and the next driver. Actually we need left and right. We can have an array of drivers D. Then for each rider, while next driver <= x, move pointer. Then left driver = D[ptr] (maybe equal? riders never equal driver coordinates, so x != D[ptr] if ptr points to driver <= x, but if x > D[ptr], then left is D[ptr]; if x < D[ptr], then left is D[ptr-1] maybe). So we need to manage.
Simpler: We can compute for each driver the boundaries as described, then count number of riders in each interval using binary search on sorted rider coordinates. This is O(m log n). Since m up to 1e5, that's fine.
Steps:
- Read n,m.
- Read coordinates array X of length N = n+m.
- Read flags array T of length N.
- Collect rider coordinates: R = [X[i] for i in range(N) if T[i]==0] -> size n.
- Collect driver coordinates: D = [X[i] for i in range(N) if T[i]==1] -> size m, already sorted as X is sorted.
- Compute for each driver i (0-indexed) the range [L_i, R_i] (inclusive) for which riders are assigned.
- For i=0 (first driver):
L0 = -inf? Actually we need a lower bound. For counting, we will count riders < D[0] plus riders up to boundary B0 = floor((D[0]+D[1])/2) inclusive? But careful: Riders with coordinate < D[0] are all assigned to driver 0. But these coordinates are less than D[0]. Also riders between D[0] and D[1] that are <= floor((D0+D1)/2) are also assigned. So effectively the upper bound for driver 0 is B0 = floor((D0+D1)/2). The lower bound is effectively -infinity, but we can just count riders with x <= B0, and also include those < D0. But counting x <= B0 automatically includes those < D0, because D0 >? Actually B0 >= D0? Not necessarily. Let's check: D0=2, D1=3 => (2+3)/2=2.5 floor=2. So B0=2, which is equal to D0. But there are no riders at D0, but riders with x < D0 (x=1) are less than 2, so they are <= B0? 1 <=2, yes. So counting all riders with x <= B0 includes those left of D0. However, riders between D0 and D1 with x=2? Actually 2 is driver, not rider; x=3 driver; so only possible rider is maybe x=2? can't. So B0=2 includes no riders between D0 and D1 because x would have to be integer between 2 and 3 exclusive: none. So okay. So for driver 0, count = number of riders with x <= B0.
- For i = m-1 (last driver):
Let B_{m-2} = floor((D[m-2]+D[m-1])/2). Then driver m-1 gets riders with x >= B_{m-2}+1. Also riders > D[m-1] are automatically included. So lower bound = B_{m-2}+1, and no upper bound. So count = number of riders with x >= B_{m-2}+1.
- For interior i (1 <= i <= m-2):
Left boundary L = floor((D[i-1]+D[i])/2) + 1.
Right boundary R = floor((D[i]+D[i+1])/2).
Count = number of riders with x in [L, R] inclusive.
But we need to ensure that these intervals are disjoint and cover all riders exactly. Yes.
Edge cases: if m=1, then there is only one driver. Then all riders are assigned to that driver. For our formula: for i=0, we have no D[1] to compute B0. So we handle separately: count = n.
Now we need to compute counts efficiently: sort R already sorted (since X sorted, and we extracted in order, they will be sorted). So we can do binary search to count numbers <= value, >= value, between bounds.
We have to be careful with boundaries: The intervals as defined may overlap? They shouldn't if we use inclusive/exclusive correctly. For interior i, L = floor((D[i-1]+D[i])/2)+1, R = floor((D[i]+D[i+1])/2). Since floor((D[i-1]+D[i])/2) < floor((D[i]+D[i+1])/2) because D increasing. They are contiguous? Check: For i and i+1: the right bound of i is floor((D[i]+D[i+1])/2). The left bound of i+1 is floor((D[i]+D[i+1])/2)+1. So they partition seamlessly.
For driver 0, its right bound is floor((D0+D1)/2). Driver 1's left bound is floor((D0+D1)/2)+1, so no gap. Similarly for driver m-1.
Thus we can compute counts using binary search.
Complexities: O(N + m log n). But we can also compute counts by scanning riders and drivers simultaneously in linear time, which is simpler to implement and more efficient.
I'll consider implementing a sweep algorithm:
Let drivers array D (size m). Let riders array R (size n) sorted.
Initialize answer array ans of zeros length m.
We need to assign each rider to driver i.
We can do two-pointer: For each rider r in R:
Find the driver immediately to the left: i such that D[i] <= r and (i=m-1 or D[i+1] > r). That is, we can maintain index j where D[j] is the last driver <= r. Then compare distances to D[j] and D[j+1] (if exists). The nearest driver is chosen with tie to smaller coordinate if equal distance. Since if distances equal, r is exactly midpoint, choose left driver D[j]. That is exactly: if r - D[j] <= D[j+1] - r, choose j, else j+1. But careful: if j is -1 (no left driver), choose D[0] (rightmost? Actually left none, so nearest is D[0] because all drivers to the right). Similarly if j+1 out of range (no right driver), choose D[j].
So we can do binary search for each rider (O(n log m)) or linear by scanning: Since both lists sorted, we can merge them.
We can process riders and drivers in order:
- Keep a pointer to drivers: let i = 0 (current driver index considered as left?).
- For each rider r:
While i+1 < m and D[i+1] < r: increment i to get the largest driver < r? Actually we need driver <= r. So while i+1 < m and D[i+1] <= r: i++. This ensures that D[i] is the greatest driver coordinate <= r. After this, D[i] <= r and either i == m-1 or D[i+1] > r.
Then compute leftDist = r - D[i] if i valid (i>=0). But if no driver <= r, then i might be -1. So we need to handle the case when r < D[0]. So we can initialize i = -1. Then for each rider, we need to update i appropriately. Because riders are processed in increasing order, we can maintain i such that D[i] is the last driver with coordinate <= r. As r increases, we can increment i when D[i+1] <= r.
Thus algorithm:
Initialize i = -1 (meaning no left driver yet).
For each rider r in sorted order:
while i+1 < m and D[i+1] <= r: i += 1
// Now, if i == -1, left driver absent: assign to D[0]
// else if i == m-1, right driver absent: assign to D[m-1]
// else both exist: compare distances.
if i == -1: // r < D[0]
ans[0] += 1
else if i == m-1: // r > D[m-1] (since while loop ensures i = m-1 because D[m-1] <= r, but if r is > D[m-1]? Actually while loop increments i until D[i+1] <= r; if i reaches m-1, then no right driver. But r could be > D[m-1] definitely, but also there might be rider exactly at D[m-1]? not possible. So assign to D[m-1] (i)
ans[m-1] += 1
else:
leftDist = r - D[i]
rightDist = D[i+1] - r
if leftDist <= rightDist: // tie goes to left (smaller coordinate) which is D[i], so <= works.
ans[i] += 1
else:
ans[i+1] += 1
This will process each rider, and each rider triggers at most O(1) increments of i? Actually the while loop increments i potentially multiple times, but overall across all riders, i increases at most m times. So total O(n+m). Good.
We need to be careful about the case when r == D[i]? That can't happen because riders and drivers have distinct coordinates, and D[i] <= r but if r equals D[i], that would be a driver coordinate. Since drivers are not riders, they never coincide. So r != D[i].
Thus linear solution is simple.
Now we need to output ans for each driver in order of increasing coordinate, which is exactly the order we have D and ans indices correspond to drivers in that order. So print ans[i] for i=0..m-1.
Let's test with examples.
Example 1:
n=3,m=1. D=[3]. R sorted? X:1 2 3 10, T:0 0 1 0 => R=[1,2,10]. Process:
i=-1 initially. For r=1: while i+1<1 and D[0]<=1? D[0]=3, not <=1, so i remains -1. i==-1 => ans[0]++ => ans[0]=1.
r=2: similarly i==-1 => ans[0]=2.
r=10: while i+1<1 and D[0]<=10? D[0]=3 <=10 so i becomes 0. i=0, i==m-1=0 => ans[0]++ => ans[0]=3. Output 3. Good.
Example 2:
n=3,m=2. X:2 3 4 5 6, T:1 0 0 0 1 => D=[2,6]; R=[3,4,5].
Process:
i=-1.
r=3: while i+1<2 and D[0]=2 <=3 => i=0. Now i=0, not -1, i != m-1 (1) so both neighbors: leftDist=3-2=1, rightDist=6-3=3 -> leftDist <= rightDist => ans[0]++ => ans[0]=1.
r=4: while i+1<2 and D[1]=6 <=4? no. So i stays 0. leftDist=4-2=2, rightDist=6-4=2 -> leftDist <= rightDist (tie) => ans[0]++ => ans[0]=2.
r=5: while i+1<2 and D[1]=6 <=5? no. leftDist=5-2=3, rightDist=6-5=1 -> leftDist > rightDist => ans[1]++ => ans[1]=1.
Output ans[0]=2, ans[1]=1. Good.
Example 3:
n=1,m=4. X:2 4 6 10 15, T:1 1 1 1 0 => D=[2,4,6,10]; R=[15].
Process:
i=-1.
r=15: while i+1<4 and D[0]=2<=15 => i=0; then while i+1<4 and D[1]=4<=15 => i=1; while D[2]=6<=15 => i=2; while D[3]=10<=15 => i=3; now i=3, i==m-1=3 => ans[3]++ => ans[3]=1. Others 0. Output: 0 0 0 1. Good.
Thus algorithm works.
But we should also consider edge cases: many drivers, many riders, drivers at extremes. Also cases where distances equal tie-breaking correctly handled with <=.
Implementation details:
- Input: first line n,m.
- Second line: n+m integers.
- Third line: n+m integers t_i.
We can read all coordinates into an array coordinates.
We can read t into array.
Then build D and R.
But we can also build R and D on the fly while iterating over the input order (which is sorted). Since we need sorted order, we can just scan through the input and if t_i==1, add to D; else add to R. Since input is sorted, both D and R will be sorted.
Alternatively, we can process without storing both separate: we could process the input array in one pass, maintaining a list of drivers encountered so far? But riders may appear before drivers; we need to assign each rider based on nearest driver. Since we have to know drivers positions globally, not just those encountered so far because a rider that appears later might be closer to a driver that appears later? But since the input is sorted, drivers and riders are interleaved. However, when processing in order, we can assign each rider when we see it, but we may not have seen drivers to the right yet. For a rider at position x, the nearest driver could be to the right which we haven't read yet. So we cannot assign riders in a single forward pass without knowing future drivers. So we need to collect all drivers first or do a two-pass approach: store all coordinates and t, then compute.
Simplest: read all into arrays, then separate.
Complexities: O(N) time, O(N) memory.
Now we must be careful with large N: up to 200,000, fine.
Now answer output: print m integers separated by spaces.
Edge cases: m=0? but constraints say m>=1. n>=1? n can be 1? Actually 1 ≤ n,m ≤ 10^5, so both at least 1.
Also ensure that if m=1, the while loop for i+1<1? while condition fails; after processing riders < D[0], i remains -1 until a rider >= D[0]. For riders < D[0], i stays -1, we assign to driver 0. For riders >= D[0], while will increment i to 0, then i==m-1, assign to driver 0. So works.
Now let's double-check tie-breaking condition: if leftDist == rightDist, we assign to left driver (smaller coordinate). Our condition leftDist <= rightDist does that. Good.
What about when i==-1? That means rider is to left of first driver. The nearest driver is the first driver, which has coordinate D[0]. So we assign to driver 0. That's correct.
When i==m-1? Rider is to right of last driver (or equal? can't be equal) so assign to last driver. Good.
Thus algorithm is correct.
Now we should also consider the possibility of integer overflow? Coordinates up to 1e9, distances up to 2e9, but we only store differences in int (32-bit may overflow? 1e9 difference safe within 2^31? 2e9 < 2^31-1 = 2.147e9, actually 2e9 is 2*10^9 which is 2,000,000,000 < 2,147,483,647, so safe for signed 32-bit. In Python it's unlimited anyway.
Now I'll write Python code:
def solve():
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
total = n + m
coords = list(map(int, input().split()))
types = list(map(int, input().split()))
drivers = []
riders = []
for x, t in zip(coords, types):
if t == 1:
drivers.append(x)
else:
riders.append(x)
# drivers already sorted, riders sorted
ans = [0]*m
i = -1 # index of last driver with coordinate <= current rider
for r in riders:
# advance i while next driver <= r
while i + 1 < m and drivers[i+1] <= r:
i += 1
if i == -1:
# assign to first driver
ans[0] += 1
elif i == m-1:
ans[i] += 1
else:
leftDist = r - drivers[i]
rightDist = drivers[i+1] - r
if leftDist <= rightDist:
ans[i] += 1
else:
ans[i+1] += 1
# output
print(' '.join(map(str, ans)))
Test with examples.
We should also test some edge cases.
Edge case: m > n? That's fine.
Consider case: n=5, m=3, drivers at [10,20,30], riders at [5,15,25,35,45]. Expect: For riders left of 10: 5 -> driver 0; 15: between 10 and 20, midpoint 15 => tie? (15-10=5,20-15=5) -> left driver (10). So driver 0 gets 5 and 15? Actually 5 -> driver0, 15 -> driver0; 25: midpoint between 20 and 30 is 25 => tie -> left (20) driver1; 35: >30, but closer to 30? distance 5, while to 20 is 15, so driver2; 45: distance to 30 =15, to 20=25, driver2. So counts: driver0:2, driver1:1, driver2:2. Let's simulate: riders sorted:5,15,25,35,45. i=-1: r=5 -> while drivers[0]=10 <=5? no, i stays -1 -> ans[0]++ (1). r=15 -> while i+1<3 and drivers[0]=10 <=15 -> i=0; now i=0, not -1, not last; leftDist=15-10=5, rightDist=20-15=5 -> leftDist<=rightDist -> ans[0]++ (2). r=25 -> while i+1<3 and drivers[1]=20 <=25 -> i=1; leftDist=25-20=5, rightDist=30-25=5 -> leftDist<=rightDist -> ans[1]++ (1). r=35 -> while i+1<3 and drivers[2]=30 <=35 -> i=2; i=2 == m-1 -> ans[2]++ (1). r=45 -> while i+1<3? i+1=3 out of range -> i remains 2; i==m-1 -> ans[2]++ (2). So counts 2,1,2. Good.
Another edge: rider exactly at midpoint but driver left? Already covered.
Now consider drivers and riders interleaved in input, but we separated, works.
Time complexity O(N). Memory O(N). Should be fine.
But is there any tricky case regarding the condition drivers[i+1] <= r? We increment i while next driver coordinate is <= r. This ensures that after the while, drivers[i] <= r (if i>=0) and drivers[i+1] > r (if exists). This matches our needs.
But what about the case where r equals some driver coordinate? That shouldn't happen, but if it did (though problem guarantees uniqueness across all residents, so no rider shares coordinate with driver), then our while would increment i to include that driver because condition is <=. If a rider had same coordinate as driver, that would be a problem, but not in valid input.
Thus the solution is correct.
Now I will write final answer explanation and code.
However, the problem statement says: "Print m integers a_1, a_2, …, a_m, where a_i is the answer for the i-th taxi driver. The taxi driver has the number i if among all the taxi drivers he lives in the i-th smallest coordinate". So we are to output in order of driver coordinate.
Our ans array is indexed 0..m-1 corresponding to drivers in increasing order, so fine.
Now I'll produce final answer.
But before finalizing, double-check constraints: n,m up to 1e5, total up to 2e5. Our algorithm linear.
Potential pitfalls: The input lines could be large, using sys.stdin reading is necessary for speed in Python. We'll use .split() and map.
Now we also need to consider if riders list could be empty? n>=1, so at least one rider, but maybe n=0? According to constraints 1 ≤ n,m, so n≥1. So riders not empty.
But what if m=0? Not possible.
Now we should consider another approach: using two arrays of drivers and riders, but our algorithm is fine.
Edge: all riders to the left of first driver: then i never becomes >=0, so ans[0] gets all riders. Works.
All riders to the right: i will eventually become m-1 after processing first rider that is >= D[m-1], then for all later riders, i remains m-1 because while loop condition fails (i+1<m? false). So ans[m-1] gets those. Works.
Now ensure that while loop does not cause infinite loop: i increases only when condition holds, and condition eventually stops because drivers are finite.
Now code.
Now we should test with some random small cases by brute force to ensure correctness. But within reasoning, seems fine.
However, there is nuance: In the while condition, we check drivers[i+1] <= r. But suppose after incrementing i, we need to re-check the next driver? That's what while does. Good.
Implementation details: In Python, using while loop inside for loop is fine. Complexity linear.
Now confirm that we need to output m integers in one line separated by space. Example output has newline after each? Actually examples show one line with numbers separated by spaces. That's typical.
Thus final answer.
Wait: There is one more nuance: The problem says "the taxi driver can neither transport himself nor other taxi drivers." So we count only riders, not drivers. Our counting only includes riders, good.
Now I'll write explanation for solution: Using two pointers to assign each rider to nearest driver.
Now produce final message with code and explanation. The solution processes all residents in sorted order. The drivers' coordinates are extracted into a sorted list, and the riders' coordinates into another sorted list. Then, using a two-pointer sweep, each rider is assigned to the closest driver. Since the coordinates are already sorted, we can maintain the index of the last driver that is not greater than the current rider. The distances to the driver on the left and to the next driver on the right are compared; ties are broken in favor of the driver with the smaller coordinate (i.e., the left one). Riders to the left of the first driver or to the right of the last driver are automatically assigned to the nearest extreme driver. This works in O(n+m) time and O(n+m) memory.
import sys
def solve() -> None:
input = sys.stdin.readline
n, m = map(int, input().split())
total = n + m
x = list(map(int, input().split()))
t = list(map(int, input().split()))
drivers = []
riders = []
for xi, ti in zip(x, t):
if ti == 1:
drivers.append(xi)
else:
riders.append(xi)
ans = [0] * m
i = -1 # index of rightmost driver with coordinate <= current rider
for r in riders:
# advance i while the next driver is not greater than the rider
while i + 1 < m and drivers[i + 1] <= r:
i += 1
if i == -1: # no driver to the left
ans[0] += 1
elif i == m - 1: # no driver to the right
ans[i] += 1
else:
left_dist = r - drivers[i]
right_dist = drivers[i + 1] - r
if left_dist <= right_dist: # tie goes to left driver
ans[i] += 1
else:
ans[i + 1] += 1
print(' '.join(map(str, ans)))
if __name__ == "__main__":
solve()