We need to compute for each target (circle) the earliest shot (by index, 1-indexed) that lies inside or on the boundary of that circle. We need to output the count of targets that were hit at least once, and for each target the index of first hit (or -1). Input constraints: n up to 10^4, m up to 2*10^5. Coordinates are integers; circles have centers on x-axis, radii integer (1..1000). Circles do not intersect, are not nested, but can touch. They may be in any order. Shots are points (x,y) with integer coordinates in range [-20000,20000].
We need efficient solution. Direct O(n*m) is 2e9 - too large.
We need to leverage geometry: circles with centers on x-axis, non-intersecting (except possibly at boundaries). Since they don't intersect or nest, they are disjoint interiors? Actually condition: "no two targets coincide, intersect or are nested into each other, but they can touch each other." So circles do not overlap except possibly at a point (touching). They are all on x-axis, so they are intervals along the x-axis? Actually circles: center (x_i,0). Radius r_i. They are circles. Since centers on x-axis, they are symmetric. The condition that they don't intersect nor be nested means that for any two circles i and j, the distance between centers > r_i + r_j if not touching; or = r_i + r_j if touching. Also not nested: distance + min(r_i, r_j) > max(r_i, r_j) (i.e., one is not inside the other). Since they all are on x-axis, their x-coordinate differences d = |x_i - x_j|. So condition: either d >= r_i + r_j (if no touch) or d > |r_i - r_j| (to avoid nesting). Actually for circles not to intersect (i.e., interiors disjoint) and not nested, we require that either d >= r_i + r_j (disjoint, maybe touching) or d <= |r_i - r_j| (one inside another, nested). But the problem says they are not nested, so we must have d > |r_i - r_j|. And also they do not intersect (i.e., interiors disjoint) so we must have d >= r_i + r_j or d <= |r_i - r_j|? Actually circles intersect if their interiors have common points. They are not allowed to intersect, but can touch. So the condition is: either d >= r_i + r_j (disjoint or touching) or d <= |r_i - r_j| (one inside another, possibly touching). But they also say they are not nested, so they cannot have one inside another. Therefore we must have d >= r_i + r_j for all i!=j? Possibly with equality allowed (touching). So basically the intervals on the x-axis of projection: circle i covers x from x_i - r_i to x_i + r_i. Since circles don't intersect or nest, these intervals are also non-overlapping except possibly at endpoints (touching). Let's verify: Suppose circles are on x-axis. Their intervals (the set of x such that point (x,0) lies inside the circle) is exactly [x_i - r_i, x_i + r_i] because on the axis, the distance to center is |x - x_i|. If circles don't intersect and are not nested, then indeed the intervals must be disjoint (except maybe touching at endpoints). Could there be a scenario where intervals overlap but circles don't intersect? For circles on x-axis, if intervals overlap, then there is some x such that both |x - x_i| <= r_i and |x - x_j| <= r_j. That means the point (x,0) is inside both circles, so circles intersect. So that's not allowed. So intervals are disjoint (except possibly at endpoints). Similarly, nesting would mean one interval completely inside another, which is disallowed. So indeed the intervals [x_i - r_i, x_i + r_i] are pairwise disjoint (except endpoints when touching). So they form a set of non-overlapping intervals on the real line.
This is crucial: the circles are essentially separated along the x-axis. So for a given shot point (x,y), we need to determine which circles (if any) contain that point. Since circles are disjoint, a point can be inside at most one circle? Not necessarily; could it be inside two circles if the point is not on the x-axis? Even though intervals on x-axis are disjoint, the circles might still intersect off the x-axis? Let's analyze. Two circles with centers on x-axis, radii r1, r2, centers at x1, x2, with disjoint intervals on x-axis (d >= r1 + r2 maybe). For a point (x,y) with y != 0, could it be inside both circles? The condition for being inside both is (x - x1)^2 + y^2 <= r1^2 and (x - x2)^2 + y^2 <= r2^2. This would imply both inequalities. Is it possible if the intervals on x-axis are disjoint? For y=0 it's impossible. For y !=0, maybe. Let's consider two circles that are far apart; might a point be above both? Actually think of two circles far apart, the point would have to be near both centers to be inside both radii. The distance between centers d. For a point to be in both circles, the distances to centers must satisfy <= r1 and <= r2. The maximum possible distance between two points that are each within distances r1 and r2 from the respective centers is at most r1 + r2 (by triangle inequality). So if d > r1 + r2, then no point can be inside both circles because the distance between centers is too large: for any point P, |P-C1| + |P-C2| >= d. If both distances are ≤ r1 and r2, then r1 + r2 ≥ d. So the condition for a point to be possibly inside both is d ≤ r1 + r2. But the problem states circles do not intersect (i.e., interiors disjoint). Intersection would mean there exists a point (anywhere) that lies in both circles. So they guarantee that no point (including off-axis) lies in both circles. Because if there were such a point, circles intersect. So the condition is indeed that for all i != j, d >= r_i + r_j (with equality allowed only if they touch at exactly one point, but that point would be on the line connecting centers? Actually when d = r1 + r2, the circles are externally tangent, and they meet at exactly one point, which lies on the line connecting centers. That point would be on the x-axis? Since both centers on x-axis, the line is the x-axis, so tangent point is on the x-axis. So off-axis they are disjoint. So indeed for any y != 0, point cannot be in both circles because that would imply a point with interior intersection. So circles are pairwise disjoint (interiors do not overlap). So a shot can hit at most one target. Because if a point is inside two circles, that would mean the interiors intersect (since point belongs to both). So yes, each shot can hit at most one target.
Thus the problem reduces to: given n disjoint circles on x-axis, each defined by center x_i and radius r_i, intervals [x_i - r_i, x_i + r_i] are non-overlapping. For each shot (x,y), determine which circle (if any) contains it. Then for each circle, we want the earliest shot index that hits it. Since each shot can hit at most one circle, we can process shots in order, and for each shot, quickly find the potential circle it might belong to, test the condition (distance^2 <= r^2), and if it hits and the circle hasn't been hit yet, assign the current shot index as first hit.
So the main subproblem: for a given point (x,y), find which circle's x-interval potentially contains the point? Actually even if the point's x coordinate falls outside the interval [xi - ri, xi + ri], it cannot be inside the circle because the horizontal distance alone would be > ri. But if it falls inside the interval, it's not sufficient because the vertical distance matters. So we need to check all circles whose x-interval contains x. Since intervals are disjoint, at most one such circle can exist? Actually intervals are disjoint, so there is at most one interval that contains a given x (if x not exactly at endpoint of two intervals? At endpoints, circles touch, but the point at the endpoint (x,0) is on the boundary of both circles. However, if the point is exactly at the common boundary, it belongs to both circles? For circles that touch externally, the tangent point is common to both circles. The problem says circles can touch each other. So if a shot lands exactly at that point, it would be inside (or on the boundary) of both circles. But they said no two targets coincide, intersect or are nested, but they can touch each other. That means they can have exactly one common point. In that case, a shot exactly at that point would be considered hitting both targets? The problem statement: "The target is considered hit if the shot is inside the circle or on its boundary." If a point lies on the boundary of two circles that touch at that point, then it's on the boundary of both. So it would be considered hitting both. But earlier we argued a shot can hit at most one circle because circles interiors are disjoint. But boundaries touching at a point may be shared. So a shot exactly at the tangency point would be considered hitting both circles. However, is that possible? Coordinates are integers. Since centers and radii are integers, the tangency point's x-coordinate would be something like? For two circles with centers x1, x2 and radii r1, r2 touching externally, we have d = x2 - x1 = r1 + r2 (assuming x2 > x1). The tangency point is at (x1 + r1, 0) = (x2 - r2, 0). So its coordinates are (x1+r1, 0). Since x1 and r1 are integers, it's integer. So yes, a shot could be (x,0) with x exactly that integer. So a shot could be on the boundary of two circles simultaneously. But the problem states circles can touch, but they didn't address whether a shot hitting exactly the tangency point should count as hitting both targets. Typically in such geometry problems, if a point lies on the boundary of multiple shapes, it belongs to all of them. The problem says: "The target is considered hit if the shot is inside the circle or on its boundary." So if it's on the boundary of two circles, both are hit. Does that conflict with the guarantee that circles do not intersect? They can only touch, so the overlapping boundary is a single point; it's not an intersection of interiors, but it's an intersection of boundaries. So it's allowed. So a shot could hit two targets if it lands exactly at a tangency point. But is that possible? We need to consider if the input could produce such a case. Since circles may touch, and shots are integer points, it's possible. However, note that the circles are disjoint except possibly at that one point. So each shot can hit at most two circles (if it lands exactly at a tangency point where two circles meet). Could it hit more? Since intervals are disjoint and each circle is defined, tangency points can only be between two circles; it's impossible for three circles to share the same point because intervals are disjoint and each circle is separate; for three to share a point, that would require overlapping intervals or something else. So at most two.
Thus we need to handle this edge case. However, we can still approach by: For each shot, find the circle(s) whose interval contains the x coordinate of the shot. Since intervals are disjoint, at most two intervals can have the point as endpoint if it is exactly at a boundary between two intervals. Actually if intervals are like [a,b] and [b,c] (touching at b), then a point with x = b lies in both intervals (if intervals are considered closed). In typical interval definition, they are closed [xi-ri, xi+ri]. So at the common endpoint, it belongs to both. So at most two circles.
So we can find the interval(s) that contain x. Then test each candidate circle for containment: (x - xi)^2 + y^2 <= ri^2. If yes, and circle not yet hit, record the shot index.
Now the challenge: we need to answer many queries (m up to 200k) quickly. n up to 10k. We can preprocess the circles: sort them by their left endpoint (or center) and build an interval tree. Because intervals are non-overlapping, we can simply map each x to the interval that covers it using binary search on the sorted list of intervals.
Specifically, sort circles by their left endpoint (xi - ri). Since intervals are disjoint and sorted by left, they will also be sorted by right? If intervals are disjoint, then sorting by left also sorts by right (monotonically increasing). So we have an array of intervals [L_i, R_i] with L_i < L_{i+1} and also R_i < R_{i+1} (since they don't overlap, each interval ends before the next starts except maybe touching: R_i <= L_{i+1}. Actually if they touch, R_i = L_{i+1}. So we have non-decreasing L, and each R_i <= L_{i+1}. So we can for a given x, find the interval i such that L_i <= x <= R_i (if any), or if x equals L_{i+1} also could be in interval i+1? Actually if R_i = L_{i+1} and x = that value, it's in both. So we can do binary search for the greatest L_i <= x, then check if x <= R_i. If yes, candidate i. Also, if x == L_{i+1}? Then the greatest L_i <= x is i (since L_i <= x, and L_{i+1} = x, it's also <= x, but greatest is i+1? Let's do careful: we have sorted L array. We want to find the interval(s) that contain x. Binary search to find the rightmost L_i <= x. Let idx be that index. Then check if x <= R_idx. If yes, then circle idx is candidate. Additionally, if x == L_{idx+1}? Actually if idx is not the last, we have L_{idx+1} > L_idx. If x == L_{idx+1}, then the binary search (rightmost L <= x) would return idx+1 because L_{idx+1} <= x (since equality). But wait, we need to ensure we get the rightmost L_i <= x. So we can find index i such that L_i <= x and L_{i+1} > x (or i=n). That is typical binary search for "upper_bound" of x in L: first index > x, then i = index-1. So if L_i == x, then it's included. So suppose intervals touch: Interval A: [L1,R1] with R1 = L2. For x = R1 = L2, the L array: L1, L2, ... The upper_bound for x (first L > x) would be index of L3? Actually if L2 == x, then L2 <= x, so upper_bound (first > x) would be the first L > x, which might be L3 if exists, else n (past end). So the index before that (i = upper_bound - 1) would be the largest L <= x, which is L2. So i would be the second interval. Then we check if x <= R_i? For i=2, R2 >= x? Since L2 = x, and interval2 is [L2,R2], and L2 <= x <= R2, so yes. Also, what about interval1? It also contains x because R1 = x. But our binary search only returned interval2. However, interval1 also has L1 <= x, but it's not the rightmost. To capture both, we need to also consider the case where x equals R_i for i and also L_{i+1}. But note that intervals are closed, so both contain x. So we need to check both interval i (from binary search) and interval i-1 if x equals R_{i-1}? More systematically, we can find the interval(s) by checking the interval found (i) and also the previous interval if x equals its right endpoint. But we need to be careful: after finding i such that L_i <= x <= R_i (from binary search), we also consider i-1 if x == R_{i-1} (which equals L_i if touching). That seems fine. However, we need to check also if x is exactly at L_i and there is a previous interval that ends at L_i? That is the same condition: previous interval's R_{i-1} = L_i. So we can check both i and i-1 if x == L_i and i>1 and R_{i-1} == x. But more uniform: The intervals are disjoint except at touching points, so any x can belong to at most two intervals, and if it belongs to two, then it must be exactly the touching point. So we can just: find i = the interval with L_i <= x (largest such). Then check if x <= R_i. If yes, then candidate = i. Additionally, if i>1 and x == R_{i-1} (i.e., the previous interval's right endpoint equals x), then also candidate i-1. But we need to know R_{i-1} quickly. We have sorted intervals, so we can compute that.
But careful: what if x is less than L_1? Then no candidate. If x > R_n, none.
Alternatively, we could use a map from each possible integer x coordinate to the circle(s) that cover that x? But x range -20000 to 20000, but circles radii up to 1000, so total interval length sum maybe up to n*2000 = 2e7, which might be borderline but okay? However, m is 2e5, so we could even compute for each shot directly without precomputing map? Actually we could do binary search for each shot O(log n). That's fine: O(m log n) = 2e5 * log(1e4) ~ 2e5 * 14 = 2.8e6, trivial.
So algorithm:
- Read n.
- For each target i (1-indexed), read x_i, r_i. Compute L_i = x_i - r_i, R_i = x_i + r_i. Store also center x_i and radius r_i. We also need to keep original order for output. So we will store arrays.
- Sort circles by L_i (or perhaps by center). But note: we need to output results in original order. So we can create an array of indices, sort them, and maintain separate sorted arrays. Or we can keep original array and also have a sorted order mapping.
Simplify: create vector of struct Circle { int id; int x; int r; int L; int R; }. Then sort by L. Also we might need to access original order for output: we'll have ans array of size n+1 initialized to -1. When we process shots, we find the circle(s) in sorted order, then use the id to update ans if not already set.
But careful: The circles are guaranteed to be non-overlapping. However, the input order might not be sorted. Sorting by L will give disjoint intervals. Good.
But we also must consider that circles may touch, so L_i could equal previous R_{prev}. That's fine.
Now for each shot (x_s, y_s):
- If y_s == 0? That is not needed for algorithm.
- Perform binary search on sorted circles to find candidate index(s):
Let sortedCircles be array sc[0..n-1].
Find idx = upper_bound(L values <= x_s?) Actually we need the largest L_i <= x_s. We can do: use std::upper_bound on L array to get first index with L > x_s. Let pos = upper_bound(Ls.begin(), Ls.end(), x_s) - Ls.begin(); Then candidate index = pos - 1. If pos == 0, then no candidate (x_s < all L). else we have i = pos-1.
Then we need to consider i and possibly i-1 if x_s == L_i? Wait: Let's analyze.
Case 1: x_s lies strictly inside an interval (L_i < x_s < R_i). Then candidate only i. Also if x_s equals L_i and also equals R_{i-1}? That's the touching case. But if x_s == L_i and i>0 and R_{i-1} == L_i, then it's also on previous interval's right endpoint. However, our binary search: Since L_i == x_s, upper_bound (first L > x_s) returns i+1 (since L_i == x_s, not >). So pos = i+1, then i = pos-1 = i. So we get i, which is the interval whose left endpoint equals x_s. The previous interval i-1 is not automatically retrieved. But we need to check if x_s <= R_i? Since L_i <= x_s <= R_i? If x_s == L_i, yes. So i is candidate. But we also need to check previous if x_s == R_{i-1}. Since R_{i-1} = L_i (touching), x_s == R_{i-1} holds. So we should also check i-1 if i>0 and x_s == R_{i-1}. However, we can't rely solely on i; we need to check both.
Alternatively, we could use binary search to find the interval with R_i >= x_s? Another approach: since intervals are disjoint, we can also find the interval that contains x_s by checking both boundaries. Use binary search on L to get the interval with L_i <= x_s, then test if x_s <= R_i. If yes, then it's inside or on boundary. But if x_s is at a touching point, it will satisfy L_i <= x_s <= R_i for both intervals? For the left interval (i-1), we have L_{i-1} <= x_s? Yes, because L_{i-1} <= R_{i-1} = x_s. Also x_s <= R_{i-1}? Since x_s = R_{i-1}, yes. So the left interval also satisfies L_{i-1} <= x_s <= R_{i-1}. However, our binary search that gave i as the largest L_i <= x_s would also consider i-1? Actually largest L_i <= x_s: Since L_{i-1} <= x_s, and L_i == x_s, the largest L_i <= x_s is i (because L_i == x_s, which is allowed). So it does not return i-1. But we can still test i-1 explicitly if x_s <= R_{i-1}? But we'd need to know that x_s lies in that interval as well. However, we could also find the interval by using a segment tree or something. But given the special structure, we can simply do two checks: Check the interval i from the binary search, and also check i-1 if i>0 and x_s <= R_{i-1}. Because if x_s is within the right interval i, it's automatically satisfied. But if it is exactly at the touching point, then x_s <= R_{i-1} is true. But could it be that x_s is not inside i, but inside i-1? That would happen if x_s is less than L_i? But then binary search would give i-1 as the largest L <= x_s (since L_i > x_s). So that case is handled already: if x_s < L_i, then i (from binary search) would be i-1. Actually we need to consider the scenario: Suppose we have intervals A: [0,2], B: [4,6] (not touching). For x_s = 3, binary search on L: L values [0,4]. upper_bound(3) returns index of 4? Actually first L > 3 is 4 at index 1, so pos=1, i=0 (interval A). Check if 3 <= R_0? R_0=2, so no. So no candidate. That's correct. Now if we have intervals touching: A: [0,2], B: [2,4]. x_s=2. L array [0,2]. upper_bound(2) returns index of first L > 2, which is end (since both 0 and 2 are <=2). pos=2 (n), i=1 (last index, B). Check x_s <= R_1? 2 <=4 yes, so candidate B. Then we also need to check A: because x_s <= R_0? 2 <=2 yes. So we should also consider i-1 if i>0 and x_s <= R_{i-1}. But careful: In this case, i=1. i-1=0, x_s <= R_0 yes. So we check both. However, what about scenario where x_s falls exactly on the left boundary of the first interval? i.e., x_s = L_0. Then binary search: upper_bound(L0) returns first L > L0? Since L0 == x_s, first L > x_s might be L1 if exists. So pos could be 1, i=0. Then we check i candidate 0. Then we check i-1? i-1 doesn't exist. So fine.
But we must also consider scenario where x_s equals R_i for some i but not equal to L_{i+1}? That can't happen because intervals are disjoint except possibly touching; if R_i is not equal to L_{i+1}, then there's a gap. So x_s = R_i but x_s < L_{i+1}. Then x_s lies only in interval i. In that case, binary search: largest L <= x_s is i (since L_i <= x_s, and L_{i+1} > x_s). So i candidate. Then we also check i-1? Only if i>0 and x_s <= R_{i-1}. But x_s = R_i, which is > R_{i-1} typically? Not relevant. But if we check i-1, x_s <= R_{i-1} would be false unless x_s <= R_{i-1}. Since intervals are disjoint, R_{i-1} < L_i <= x_s, so false. So no extra.
Thus algorithm for each shot:
- Use sorted L array to find idx = upper_bound(L, x_s) - 1. (If idx < 0, then no candidate from that side).
- Initialize list of candidate indices = [].
- If idx >= 0:
- if x_s <= R[idx], add idx.
- If idx+1 < n (i.e., there is a next interval) and L[idx+1] == x_s? Actually we might also need to consider the next interval if x_s is exactly at its left boundary and we didn't get it from the binary search because idx was the previous? Wait, let's derive systematically.
From our earlier reasoning, the two possible intervals containing x_s are:
- The one where L_i <= x_s <= R_i. This can be found by binary search to get the interval with largest L_i <= x_s, but that interval may not satisfy x_s <= R_i. If it doesn't, then no interval contains x_s? Actually if the interval found has L_i <= x_s but x_s > R_i, then x_s lies in a gap. So no interval. However, could the next interval (i+1) contain x_s? If x_s > R_i, then since intervals are sorted, L_{i+1} >= R_i (maybe >). If L_{i+1} <= x_s, then that would contradict i being the largest L <= x_s because L_{i+1} <= x_s would be true if L_{i+1} <= x_s. But if x_s > R_i, we know L_{i+1} >? Actually we have L_{i+1} >= R_i (since no overlap). So if x_s > R_i, it's possible that L_{i+1} <= x_s? If L_{i+1} could be <= x_s? For L_{i+1} to be <= x_s, we need L_{i+1} <= x_s. Since L_{i+1} >= R_i, and x_s > R_i, it is possible that L_{i+1} <= x_s if L_{i+1} is not too large. But if L_{i+1} <= x_s, then i would not be the largest L <= x_s because L_{i+1} is larger and also <= x_s. So our binary search would have returned i+1 instead. Let's test: Suppose intervals: A: [0,2], B: [3,5]. x_s = 3. L array: [0,3]. upper_bound(3) returns index first L > 3. Since L0=0 <=3, L1=3 <=3, L2? none. So first >3 is n. So pos=n, idx=n-1=1. So we get idx=1 (B). Indeed, largest L <= 3 is 3 (B's left). So it returns B, not A. So if x_s equals L_{i+1}, it returns the later interval. So in the case where x_s > R_i and equals L_{i+1}, we get idx = i+1. So that's covered. So our earlier approach of only considering idx and possibly idx-1 when x_s <= R_{idx-1} seems sufficient: if x_s is exactly at L_{idx} and also equals R_{idx-1}, we will have idx being the right interval; then we also check idx-1 because x_s <= R_{idx-1} is true (since equals). If x_s is exactly at L_{idx} but not equal to R_{idx-1} (i.e., not a touching point), then there is no left interval containing it. That case is fine.
But what if x_s is inside left interval and also equals L_{i+1}? That cannot happen because if x_s is inside left interval, then x_s < R_i. If it also equals L_{i+1}, then L_{i+1} = x_s <= R_i? That would mean intervals overlap (since L_{i+1} <= R_i) or touch (if equals). If it equals, then it's a touching point, and x_s is on boundary of both; but then x_s is not inside left interval (strictly), it's exactly at its right endpoint. So it's the case above.
Thus candidate set = {idx} if idx >=0 and x_s <= R[idx]. Additionally, if idx-1 >=0 and x_s <= R[idx-1], add idx-1. That would cover the touching case where both contain x_s? But careful: In the touching case, we have idx as the right interval, and idx-1 also contains x_s. For the right interval, we check x_s <= R[idx] and it's true. For the left interval, we check idx-1 with condition x_s <= R[idx-1] (true). So both added. Good.
But is there any case where x_s is contained in an interval that is not the idx or idx-1? Suppose x_s is less than L_0. Then idx = -1 (no candidate). So none. Suppose x_s is greater than R_n-1. Then idx = n-1 (largest L <= x_s), but x_s <= R[n-1]? If not, then no candidate; but also idx+1 doesn't exist. So fine.
Thus we can compute two candidate indices at most.
Now for each candidate index, we get the circle from sorted array: circle = sortedCircles[candidate]. Compute distance squared: (x_s - circle.x)^2 + y_s^2. If <= circle.r^2 (use 64-bit to avoid overflow), then it's a hit. If the circle hasn't been hit yet (ans[circle.id] == -1), then set ans[circle.id] = shot_index (1-indexed). Also, we might break early? But we need to consider that if a shot hits two circles simultaneously (touching point), then both should be potentially recorded. However, the order: the shot is a single shot; it occurs at index j. For both circles, the first hit would be that same shot index, unless one was already hit earlier. Since we process shots in order, if we haven't recorded either, we set both to j. That's correct.
Now after processing all shots, we count number of circles with ans != -1. Print that number, then print ans[1..n] separated by spaces.
Edge Cases:
- Shots may be repeated? Yes, but we process in order; earliest matters.
- Shots may hit a circle multiple times; we only record first.
- Need to ensure we use 64-bit for distance squared because coordinates up to 20000, difference up to 40000, square up to 1.6e9, plus y^2 similar, sum up to ~3.2e9 fits in 32-bit signed? 2^31-1 = 2.147e9, actually 40000^2 = 1.6e9, so sum up to 3.2e9 > 2.147e9, so may overflow 32-bit signed int. So use long long (64-bit). Since r up to 1000, r^2 up to 1e6, so fine.
Complexities: O(n log n + m log n). n=1e4, m=2e5, fine.
But we need to be careful about binary search: we have sorted L array. For each shot, we need to compute idx. However, we also need to check R[idx] and R[idx-1]. We should store arrays: L[i], R[i], x[i], r[i], id[i] in sorted order.
Implementation Steps:
-
Read n.
-
Create vector of struct Circle { int id; int x; int r; int L; int R; } circles(n).
-
For i from 0 to n-1: read x, r; compute L=x-r; R=x+r; store.
-
Sort circles by L (and if L ties? Could there be ties? If two circles have same L? That would imply they start at same left coordinate. But if they have same L, then intervals would overlap at least at that point? Since L = x - r. If two circles have same L, then either they are exactly the same (forbidden) or they might intersect? Let's see: If L1 = L2, then both intervals start at same left point. Could they be non-overlapping? For non-overlap, we need R1 <= L2 or R2 <= L1. If L1 = L2, then the only way to not overlap is if both intervals are zero-length? But radius >=1, so intervals have positive length. If they start at same left, then they overlap for at least a small region because both include points greater than L. So they would intersect. Since problem says no intersection, this cannot happen. So L_i are strictly increasing. Also because circles can touch, L_i could equal R_{i-1}. So L_i > L_{i-1}. So sorting yields unique L.
-
After sorting, we might also want to verify that intervals are indeed non-overlapping (given guarantee), but we can trust.
-
Create separate arrays for L_sorted, R_sorted, X_sorted, R2_sorted (r^2 maybe), id_sorted.
-
Read m.
-
Initialize ans array of size n+1 with -1.
-
Initialize hit_count = 0 later.
-
For each shot (from 1 to m):
read x_s, y_s.
// binary search on L_sorted
auto it = upper_bound(L_sorted.begin(), L_sorted.end(), x_s); // first > x_s
int idx = it - L_sorted.begin() - 1;
// candidates set
vector cand;
if (idx >= 0) {
if (x_s <= R_sorted[idx]) {
cand.push_back(idx);
}
}
// also check the previous interval? Actually we need to check idx-1 only if x_s <= R_sorted[idx-1]? But we should also consider the next interval? What if idx is such that x_s > R_sorted[idx] and the next interval could still contain x_s? In that case, idx would not be the rightmost L <= x_s? Actually if x_s > R_sorted[idx], but L_{idx+1} > x_s? Could it be that L_{idx+1} <= x_s? Let's think: Since idx is the largest index with L <= x_s, we have L_{idx+1} > x_s (if idx+1 exists). So x_s < L_{idx+1}. Since intervals are disjoint, R_{idx} < L_{idx+1} (or = if touching). So if x_s > R_{idx}, then x_s lies in gap between R_{idx} and L_{idx+1}. So no interval contains x_s. So no candidate from right side. However, there could be an interval with L > x_s that contains x_s? That would require L_{i} > x_s but also x_s >= L_i? impossible. So no.
// But we also need to consider the possibility that x_s equals L_{idx+1}? That would make L_{idx+1} <= x_s? Actually if x_s equals L_{idx+1}, then L_{idx+1} <= x_s is true. So then idx wouldn't be the largest L <= x_s because L_{idx+1} is also <= x_s. So our binary search would have returned idx+1. So we wouldn't be in this case. So it's fine.
But what about scenario where x_s is exactly at L_{idx} but intervals are not touching? Then R_{idx-1} < L_{idx}. So x_s > R_{idx-1}, so x_s <= R_{idx-1} false, so we don't add left interval. Good.
Thus our candidate generation: after computing idx (largest L <= x_s), we have:
- candidates = empty.
- if idx >= 0 and x_s <= R[idx]: add idx.
- if idx >= 1 and x_s <= R[idx-1]: add idx-1.
But is there any scenario where x_s belongs to an interval that is not idx nor idx-1? Could it belong to idx+1? Only if L_{idx+1} <= x_s, but then idx would not be the largest L <= x_s because L_{idx+1} is larger and <= x_s, so idx+1 would be the largest. So it's covered.
Thus that's good.
But need to double-check for idx = -1: no candidate.
Now, for each candidate, check the circle's containment. But note: The candidate indices refer to sorted order. But we also need to ensure that the point is actually inside the circle, not just within x-interval. So we compute distance squared.
Potential optimization: Because circles are on x-axis, the condition (x - xi)^2 + y^2 <= ri^2 is equivalent to |x - xi| <= sqrt(ri^2 - y^2). But that's not needed.
Now about large numbers: m up to 200k, n up to 10k, so algorithm fine.
But we must be careful about multiple hits on same target: we only record first. So after setting ans for a target, we can skip further checks for that target? But we still need to check candidate circles because we need to test containment for the shot; but if a target already has ans set, we don't need to update. However, we might still need to check other circles that are not hit yet. So we should still evaluate containment for all candidates, but skip if already hit.
But we could also early stop once all circles are hit? But not necessary.
Now consider performance of binary search: we use std::upper_bound on vector L_sorted. That's O(log n). Then check two candidates.
Now we must ensure that the intervals are truly non-overlapping. The problem guarantee: no two targets coincide, intersect or are nested into each other, but they can touch. So indeed intervals are disjoint (except possibly at endpoints). However, we must be careful: The circles are on x-axis; but does the condition "intersect" include interior intersection? Typically "intersect" means they have at least one common point. Since they can touch, they are allowed to have exactly one common point. So indeed intervals can share endpoints. So our approach works.
Now, what about the scenario where a circle is entirely to the left of another, but not sorted by left? We sort.
Now, we must also handle negative coordinates. That's fine.
Now, test with examples.
Example 1:
3
2 1 -> L=1,R=3
5 2 -> L=3,R=7
10 1 -> L=9,R=11
Sorted: [1,3], [3,7], [9,11] (touching at 3 between first and second)
Shots:
0 1 -> x=0, idx? L sorted [1,3,9]. upper_bound(0)=first >0 -> index0 (L=1) -> idx=-1, no candidate. So no hit.
1 3 -> x=1, L: upper_bound(1) returns first >1 -> index0? Actually L0=1 is not >1, so first >1 is L1=3, so pos=1, idx=0. Check R[0]=3, x=1 <=3 -> candidate0 (circle 1). Also idx-1? none. Compute distance: (1-2)^2 + 3^2 =1+9=10 > r^2=1 => not hit.
3 0 -> x=3, L: upper_bound(3) returns first >3 -> L1=3? Actually 3 not >3, so L2=9 >3? Wait L array [1,3,9] => first >3 is index2 (9) because 3 is not >3. So pos=2, idx=1 (circle 2). Check x<=R[1]=7 -> yes, candidate1. Also check idx-1: idx-1=0, x<=R[0]=3? yes (3<=3) -> candidate0 also. So two candidates: circle1 (id1) and circle2 (id2). For candidate0: (3-2)^2+0 =1 <=1? Actually r1=1, r1^2=1, so 1<=1 -> hit. circle1 not yet hit -> set ans[1]=3 (since third shot). For candidate1: (3-5)^2+0=4 <=4? r2=2, r2^2=4 -> hit. circle2 not yet hit -> set ans[2]=3. Both get hit by shot 3.
4 0 -> x=4, L: upper_bound(4) first >4 -> index2 (9) pos=2 idx=1. Check x<=R[1]=7 yes -> candidate1. idx-1? x<=R[0]=3? 4<=3 false. So only candidate1. Distance: (4-5)^2+0=1 <=4? yes -> hit, but circle2 already hit, skip.
4 0 -> same as above; circle2 already hit. So final ans: circle1:3, circle2:3, circle3:-1. Output: 2 hits, and "3 3 -1". matches.
Example 2:
3
3 2 -> L=1,R=5
7 1 -> L=6,R=8
11 2 -> L=9,R=13
Sorted: [1,5], [6,8], [9,13] (note gap between 5 and 6? Actually 5<6, so gap; circles not touching).
Shots:
2 1 -> x=2, L: first >2 -> index1? L0=1<=2, L1=6>2, so pos=1, idx=0. x<=R0=5? yes candidate0. distance: (2-3)^2+1=1+1=2 <=4? yes -> circle1 hit, ans[1]=1.
6 0 -> x=6, L: first >6 -> L1=6? not >6, L2=9 >6 -> pos=2 idx=1. x<=R1=8? yes candidate1. idx-1: x<=R0=5? 6<=5 false. So only candidate1. distance: (6-7)^2+0=1 <=1? r2=1^2=1, yes -> circle2 hit, ans[2]=2.
6 4 -> x=6, candidate1 as before. distance: (6-7)^2+16=1+16=17 >1 -> no hit.
11 2 -> x=11, L: first >11 -> L2=9? Actually L sorted [1,6,9]. upper_bound(11) returns end, pos=3 idx=2. x<=R2=13? yes candidate2. idx-1? x<=R1=8? false. distance: (11-11)^2+4=4 <=4? yes -> circle3 hit, ans[3]=4. So output: 3 hits, "1 2 4". matches.
So algorithm works.
Now potential pitfalls:
-
The binary search must be correct when L array contains values and x_s may be exactly equal to some L. Use upper_bound as described. Ensure using vector L_sorted of size n.
-
Use long long for squared distances.
-
Sorting: Use sort on vector of Circle by L. Guarantee strict increasing L, but if not, we might need to handle ties, but guarantee says no intersection, so should be strict. But for safety we can sort by L and if ties, by R? Not needed.
-
When computing R_sorted array, we should store as int but may need to compare x_s <= R_sorted[idx]. That's fine.
-
For candidate idx-1, we need to check condition x_s <= R_sorted[idx-1]. But note that if x_s is exactly at the left boundary of idx and there is a gap (R_{idx-1} < L_idx), then condition fails. So only touches.
-
But consider the case where x_s is exactly at the left boundary of the first circle (idx=0). Then idx-1 doesn't exist. So fine.
-
Also, if idx is such that x_s <= R[idx] is false (i.e., point lies in a gap to the left of next interval?), then we have no candidate from idx. But could it still be contained in idx-1? If x_s <= R[idx] false, that means x_s > R[idx]. Then x_s > R[idx] >=? Since idx is largest L <= x_s, but if x_s > R[idx], then it lies to the right of interval idx. Could it be contained in idx+1? Then L_{idx+1} <= x_s would need to hold. But we said idx is largest L <= x_s, so L_{idx+1} > x_s. So cannot. Could it be contained in idx-1? That would require x_s <= R_{idx-1}? But we have x_s > R[idx] >= L_{idx} > R_{idx-1} (since intervals are disjoint and non-nested). So x_s > R_{idx-1}. So no. So no candidate at all. So we don't need to add idx-1 when idx's condition fails. So we should only add idx-1 if we added idx? Actually not necessarily; consider case where x_s is exactly at the touching point but we got idx = right interval? Already both added. If we got idx = left interval? Could x_s be at touching point and idx be left interval? That would happen if we use binary search that returns the largest L <= x_s. For touching point x_s = R_i = L_{i+1}. Since L_i <= x_s and L_{i+1}=x_s, the largest L <= x_s is L_{i+1}. So idx = i+1 (right). So left interval is i. So in that scenario we always get idx as right interval. So we will add idx and idx-1. If we had used lower_bound instead, we might get left interval. But using upper_bound as described gives right. So it's consistent.
Thus final candidate generation as above.
Now, we must store ans for original order. We'll have ans[original_id] = shot index. Initialize with 0 or -1; we'll use -1.
Now, output: first line: number of targets with ans != -1. Second line: ans[1] ... ans[n] separated by spaces. Print -1 for those not hit.
Edge Cases:
Now, complexity: O((n+m) log n) okay.
But we can also try to reduce log factor by using direct mapping due to limited x range? x from -20000 to 20000, circles intervals may cover up to 2e4+1000? Actually max x range: circles centers in [-20000,20000], radius up to 1000, so L from -21000 to 21000. Shots x also in [-20000,20000]. So overall coordinate range about 42001 possible x values. We could create an array of size ~42001 mapping each integer x to the index of the circle covering that x (or multiple if touching). Since intervals are disjoint, each x (except touching point) belongs to at most one circle. For touching point, there are two circles. So we could precompute for each integer x from -21000 to 21000 which circle(s) cover it. That would be about 42k entries, which is fine. Then for each shot we can directly get candidates without binary search. However, we need to be careful about boundaries: the circle includes all points with real coordinates; but our mapping based on integer x may miss hits where x is integer but the circle might include the point even if x is not in the integer interval? Actually if we only consider x coordinate, for a given shot (x_s,y_s), we need to check the circle whose x-interval contains x_s. If we have a direct map from x_s to candidate circle(s), that would be O(1) per shot. But we still need to compute distance to check if within circle because y may affect. But that's fine.
Given n=1e4, m=2e5, the binary search approach is simple and efficient enough. But the direct mapping may be even simpler to implement and faster? But we have to compute for all x from min L to max R (maybe up to 42000) and assign circle indices. Since circles are disjoint, we can just loop over each circle and fill an array for its integer x range. But each circle radius up to 1000, so each circle covers up to 2*1000+1 = 2001 integer x positions. n=1e4, total fill operations ~20 million, which is okay. But we need to handle touching points (two circles share same x). We could have two circles at same x only if that x is exactly the touching point. In that case, the x coordinate belongs to both circles. So we need to support multiple candidates (max 2). We could store for each x a vector of circle indices? But that would use more memory, but with 42000 entries, each having at most 2, it's fine. Or we could store primary and secondary. But binary search is simpler and avoids precomputation.
Given constraints, both are okay. I'll stick with binary search.
But we need to consider that the circles are sorted by L. Since L and R are integers, we can use integer binary search.
Now, confirm that the intervals are indeed non-overlapping and sorted by L after sorting. Because the input guarantees no circles intersect or are nested, but does it guarantee that after sorting by L, they are in order? Yes.
Now, think about potential issues: The circles can touch, so L_i could equal R_{i-1}. That's okay.
Now, what if circles are given in order of decreasing L? Sorting will fix.
Now, we need to output the number of targets hit, then the first hit numbers. The example shows output: "2\n3 3 -1 " note trailing space? Probably we can print without trailing space.
Now, let's think about memory: store arrays of size n.
Now, implementation details:
- Use vector circles_unsorted? Or we can store circles and then create sorted order.
Define:
struct Circle {
int id;
int x, r;
int L, R;
};
vector circles(n);
for i 0..n-1:
cin >> x >> r;
circles[i] = {i+1, x, r, x - r, x + r};
vector sorted = circles;
sort(sorted.begin(), sorted.end(), [](const Circle &a, const Circle &b) { return a.L < b.L; });
Now extract L_sorted, R_sorted, X_sorted, R2_sorted, id_sorted.
Alternatively, we can keep sorted vector and access fields directly.
Now process shots:
vector ans(n+1, -1);
int shot_idx = 1;
for each shot:
int x, y; cin >> x >> y;
// binary search
int pos = upper_bound(L_sorted.begin(), L_sorted.end(), x) - L_sorted.begin();
int idx = pos - 1;
// candidate indices
vector cand_indices;
if (idx >= 0 && x <= R_sorted[idx]) {
cand_indices.push_back(idx);
}
// Check previous circle
if (idx-1 >= 0 && x <= R_sorted[idx-1]) {
cand_indices.push_back(idx-1);
}
// for each candidate:
for(int ci : cand_indices) {
const Circle &c = sorted[ci];
if (ans[c.id] != -1) continue; // already hit
long long dx = x - c.x;
long long dy = y;
if (dxdx + dydy <= (long long)c.r * c.r) {
ans[c.id] = shot_idx;
// we could break? But we still need to check other candidates because they might be different circles. So continue.
}
}
shot_idx++;
After processing all shots, compute hit_count = count_if(ans.begin()+1, ans.end(), [](int v) { return v != -1; });
Print hit_count, then for i=1..n print ans[i] (with spaces).
Potential issues: The candidate list may contain duplicates if idx-1 == idx? That cannot happen. It may contain the same candidate twice if we add both conditions? For example, if idx >=0 and x <= R[idx] and also idx-1 >=0 and x <= R[idx-1] are distinct indices, no duplication. Also if idx-1 == idx? Impossible. So fine.
But what about case where idx is -1? Then we skip.
Now, we also need to consider that the candidate idx may be added but then we also need to consider idx+1? As argued, not needed.
Edge Cases to test:
-
Two circles touching at point (5,0). Circle A: center 2 radius 3 => L=-1,R=5. Circle B: center 8 radius 3 => L=5,R=11. Sorted L: -1,5. For shot at (5,0): upper_bound(5) returns first >5: L1=5 not >, L2? end, so pos=2, idx=1 (B). Check x<=R[1]=11 true -> cand add B. Check idx-1=0, x<=R[0]=5 true -> add A. So both. Good.
-
Shot at (5,5): same x. But distance to A: (5-2)^2+25=9+25=34 >9; to B: (5-8)^2+25=9+25=34 >9. So none. But candidates list includes both, but after distance check, none hit. Good.
-
Shot at x=4.9? Not integer; but shots are integer coordinates, so x integer. So fine.
-
Shot at x exactly at left boundary of a circle that is not touching any other: e.g., A: [1,5], B: [6,10] (gap). Shot at x=6: L sorted [1,6]; upper_bound(6) returns first >6 -> L1=6 not >, L2? end, pos=2 idx=1 (B). Check x<=R[1]=10 true -> B added. Check idx-1=0, x<=R[0]=5? 6<=5 false -> only B. That's correct because x=6 is inside B? Actually B left is 6, so on boundary. Yes B includes point (6,0). So it's correct. A does not contain x=6.
-
Shot at x=5: upper_bound(5) -> first >5? L0=1<=5, L1=6>5 -> pos=1 idx=0. Check x<=R[0]=5 true -> add A. idx-1? none. So only A, correct. So works.
Now, what about circles that are far apart, but shot's x lies exactly at a gap: e.g., A: [1,5], B: [7,10]. x=6: L sorted [1,7]; upper_bound(6) -> first >6 is L1=7, pos=1 idx=0. Check x<=R[0]=5? false. So no candidate. So no hit. Good.
Now, what about circles that are not sorted by L due to floating? Already sorted.
Now, what about potential integer overflow when computing L and R? x up to 20000, r up to 1000, so L down to -21000, within int range (we can use int). Shots x up to 20000, so within range.
Now, the distance squared: dx up to 40000? Actually difference could be as high as? center x up to 20000, shot x up to 20000, so difference up to 40000. dx^2 up to 1.6e9, dy up to 20000, dy^2 up to 4e8. Sum up to 2e9, still within 2^31? 2e9 < 2^31-1 (~2.147e9). Actually 40000^2 = 1,600,000,000 (1.6e9). dy up to 20000, dy^2 = 400,000,000. Sum = 2,000,000,000 exactly? 1.6e9+4e8 = 2.0e9. That's less than 2,147,483,647, so fits in signed 32-bit. But safe to use long long to avoid any risk. Also, r^2 up to 1e6, safe.
Thus int would be okay, but better to use long long to be safe.
Now, we need to be careful about reading input: first line n, then n lines each x,r, then m, then m lines x,y. Use cin.
Now, potential performance: For each shot, we do binary search (log n) and up to two distance checks. That's fine.
Now, we must ensure that we handle the case where a shot hits both circles at touching point, and both circles might be already hit; but we still need to check containment to possibly record first? Actually if one circle already hit, we skip. The other might not. So we check both.
Now, consider a shot hitting the same circle twice? Not possible.
Now, output: Print count on first line. On second line, print ans[1]..ans[n] separated by spaces. The example has trailing space, but we can avoid.
Now, are there any tricky cases where a shot could be inside a circle even though its x is outside the interval? That cannot happen because for a point to be inside the circle, we need |x - xi| <= sqrt(ri^2 - y^2) <= ri, so |x - xi| <= ri, so L_i <= x <= R_i. So if x is outside [L,R], it's definitely outside. So candidate generation using x-interval is necessary and sufficient condition for potential containment; we still need to check y.
Thus algorithm correct.
Now, verify with more custom tests.
Case: n=1, circle center 0 radius 5, L=-5,R=5.
Shots: (-5,0) -> hit boundary; (0,5) hit top; (0,6) miss.
Our binary search: L_sorted = [-5]. Upper_bound(-5): first > -5? L0=-5 not >, so pos=1 idx=0. x<=R0=5? yes candidate0. Distance check: works for first two, fails for third.
Case: n=2 touching: A: center 0 radius 2 => L=-2,R=2. B: center 5 radius 3 => L=2,R=8. Touching at (2,0). Shots: (2,0) hits both. (2,1): distance to A: (2-0)^2+1=4+1=5 >4; to B: (2-5)^2+1=9+1=10>9. So none. Our candidate generation: For (2,0): x=2, L sorted [-2,2]. upper_bound(2) -> first >2? pos=2 idx=1 (B). Check R[1]=8, x<=8 true -> add B. Check idx-1=0, x<=R[0]=2 true -> add A. So both candidates, distance checks succeed, both recorded if not hit before. Good.
Case: shot exactly at left boundary of first circle and not touching: A: [0,10], no other circles. x=0. L sorted [0]. upper_bound(0) -> first >0? pos=1 idx=0. Check x<=R0=10 true -> candidate0. distance check: (0-x_center)^2+y^2. Works.
Now, what about scenario where circles are not sorted by L because they might be out of order? Already sort.
Now, ensure that after sorting we still can map back to original id.
Now, what about the possibility that circles are not strictly non-overlapping due to input guarantee, but we assume they are. But we might want to verify: if they accidentally overlapped, our candidate generation might yield multiple candidates incorrectly? However, given guarantee, we trust.
Now, performance: upper_bound on vector is O(log n). For each shot we also do up to 2 distance checks. So total ~ 200k * (log(1e4) + 2) ~ 200k*15 = 3M operations, fine.
Now, memory: ans array of 10001 ints, circles vectors etc.
Now, Implementation details:
We'll use:
#include <bits/stdc++.h>
using namespace std;
struct Circle {
int id;
int x, r;
int L, R;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector circles(n);
for (int i=0; i<n; ++i) {
int x, r;
cin >> x >> r;
circles[i] = {i+1, x, r, x - r, x + r};
}
// sort by L
vector sorted = circles;
sort(sorted.begin(), sorted.end(), [](const Circle &a, const Circle &b) { return a.L < b.L; });
// Extract arrays for binary search
vector Ls(n), Rs(n), Xs(n);
vector Rsq(n); // r^2
vector ids(n);
for (int i=0; i<n; ++i) {
Ls[i] = sorted[i].L;
Rs[i] = sorted[i].R;
Xs[i] = sorted[i].x;
Rsq[i] = 1LL * sorted[i].r * sorted[i].r;
ids[i] = sorted[i].id;
}
int m;
cin >> m;
vector ans(n+1, -1);
int shot_idx = 1;
for (int i=0; i<m; ++i) {
int x, y;
cin >> x >> y;
// find candidate interval(s)
int pos = upper_bound(Ls.begin(), Ls.end(), x) - Ls.begin();
int idx = pos - 1;
// we will collect indices in sorted order to check
// use small array for candidates
int cand[2];
int cnt = 0;
if (idx >= 0 && x <= Rs[idx]) {
cand[cnt++] = idx;
}
// check previous interval
if (idx-1 >= 0 && x <= Rs[idx-1]) {
cand[cnt++] = idx-1;
}
// process candidates
for (int k=0; k<cnt; ++k) {
int ci = cand[k];
// if already hit, skip
int cid = ids[ci];
if (ans[cid] != -1) continue;
long long dx = x - Xs[ci];
long long dy = y;
if (dxdx + dydy <= Rsq[ci]) {
ans[cid] = shot_idx;
}
}
++shot_idx;
}
// count hits
int hitCount = 0;
for (int i=1; i<=n; ++i) {
if (ans[i] != -1) ++hitCount;
}
cout << hitCount << '\n';
for (int i=1; i<=n; ++i) {
cout << ans[i] << (i==n ? "\n" : " ");
}
return 0;
}
But careful: In the candidate generation, if idx >=0 but x <= R[idx] is false, we might still have idx-1 candidate? As argued, if x > R[idx] then it's in gap, and since idx is the largest L <= x, we know L_{idx+1} > x, so x is between R[idx] and L_{idx+1}. Could it be that idx-1 contains x? That would require x <= R[idx-1], but x > R[idx] >= L_{idx} > R_{idx-1} (since intervals are disjoint). So x > R[idx-1]. So no. So we can keep condition as above.
Now, we also need to consider the scenario where pos = 0 (idx = -1). Then no candidate.
Now, also consider scenario where pos = n (idx = n-1) and x <= R[idx] true, candidate added; we also check idx-1 if applicable. That's fine.
Now, what about the case where x is exactly equal to L of some interval and also equal to R of previous (touching), we will have both candidates. Good.
Now, what about the case where x is equal to L of some interval but there is no previous interval (idx=0). Then we only add idx.
Now, verify with an example where intervals are touching but the shot's x is exactly at that touching point, but y non-zero such that it lies inside both circles? Could a point with y != 0 be on the boundaries of both circles at the same time? For two circles externally tangent at a point on the x-axis, the only common point is that tangent point on the x-axis. Any point with y != 0 cannot be on both circles because the circles only meet at that one point. So only when y=0 and x is exactly the tangency point does the shot hit both. So our candidate generation will add both circles; distance check will verify if the point is actually inside each circle. For y != 0, only at most one might contain it, but we still add both and then distance check will fail for the other. That's okay.
Now, regarding performance: we do up to 2 distance checks per shot. That's fine.
Now, there might be a scenario where a shot falls exactly on the boundary of a circle but its x is not inside the interval due to rounding? Actually if it's on boundary, we have (x - xi)^2 + y^2 = ri^2. This implies |x - xi| <= ri, so x in [L,R]. So yes.
Now, ensure that our sorted intervals indeed cover the circles. Since we sorted by L, the order is increasing. There's no guarantee that the circles are given in increasing order of center or L. Sorting ensures.
Now, what about negative L? That's fine.
Now, one more nuance: The condition that no two targets intersect or are nested ensures that the intervals are disjoint. But what about circles that are completely separated but one may be on left and other on right. Sorting works.
Now, there's a possibility that circles are arranged such that some L values are equal? The condition prohibits coinciding circles; could they have same L but different R? Consider circle1: center 0 radius 2 => L=-2,R=2. Circle2: center 0 radius 3 => L=-3,R=3. These are nested (one inside another) because center same? Actually both centers on x-axis, but if centers coincide, then one is completely inside the other (nested) - not allowed. So that's prohibited. Could there be two circles with different centers but same L? For same L, we have x1 - r1 = x2 - r2. Could they be non-intersecting? Suppose circle A: center 0 radius 5 => L=-5,R=5. Circle B: center 10 radius 15 => L=-5,R=25. That would intersect heavily. Not allowed. In fact, if L are equal, then one interval starts at same point; if they are not nested, they would overlap at least at the start. Overlap would mean intersection, so not allowed. So L must be strictly increasing. Good.
Now, we need to pay attention to the case where the input circles are already sorted by L, but we don't rely on that.
Now, we may also consider using a segment tree or interval tree, but not necessary.
Now, test on random small cases with brute force to ensure correctness. We could simulate mentally.
But one more nuance: The problem statement: "no two targets coincide, intersect or are nested into each other, but they can touch each other." So if they touch, they share a boundary point. That point belongs to both. As per condition, that's allowed. So our algorithm correctly handles that.
Now, about the output: The number of targets hit, then the list. In examples, there is a space after last number? It prints "3 3 -1 " with trailing space. But we can output without trailing space.
Now, confirm that we output the first shot number, not the shot index? Yes.
Now, check corner case: n=1, m=1, shot hits, output "1\n1". Good.
Now, potential bug: When idx >=0 and we check x <= R[idx], but what if x is exactly equal to L of next interval? For example, intervals: [1,5], [6,10] (gap). x=6: upper_bound(6) returns first >6 -> L1=6 not >6, so pos=2 idx=1. Then x<=R[1]=10 true, candidate idx=1 (right). Also we check idx-1: idx-1=0, x<=R[0]=5? false. So only right. Good.
Now, consider x=5: upper_bound(5) returns first >5 -> L1=6, pos=1 idx=0. x<=R[0]=5 true, candidate idx=0. Good.
Now, consider x=1: upper_bound(1) returns first >1 -> L0=1 not >1, L1=6>1, pos=1 idx=0. x<=R0=5 true candidate0. idx-1 none.
Now, consider x=0: upper_bound(0) returns first >0 -> L0=1 >0, so pos=0 idx=-1, no candidate.
All good.
Now, confirm that using upper_bound on vector is correct. We need to include #include .
Now, complexity wise, it's fine.
Now, edge case: large m (200k) but we do per shot operations that are cheap.
Now, consider memory: storing sorted arrays of n elements. OK.
Now, we might want to avoid storing both original and sorted circles separately. But we need original order for output. So we store ans mapping by original id.
Now, let's test more thoroughly with some random data. We'll mentally generate small random circles that satisfy non-overlap condition. To be sure, we could write a quick script but not here.
Now, think about potential pitfalls: The guarantee says circles do not intersect or are nested, but they can touch. However, what about the scenario where circles are given such that one circle's interval is completely inside another's interval? That would be nested, not allowed. So no.
Now, what about circles with same center? Not allowed due to nested.
Now, what about circles that are not on x-axis? They are all on x-axis because center on Ox axis, so y=0. So fine.
Now, we might need to consider precision? All integers, so exact.
Now, confirm that we haven't missed any candidate: Could a shot be inside a circle even though its x is not in [L,R]? No, as argued, because |x - xi| <= sqrt(ri^2 - y^2) <= ri, so |x - xi| <= ri, so L <= x <= R.
Thus candidate generation is correct.
Now, about the binary search: We are using L array for upper_bound. What if x is less than all L? Then pos=0, idx=-1, skip. If x is greater than all L? Then pos=n, idx=n-1, we check R[idx]. If x > R[idx], then candidate not added. But we also need to consider possibility that x is > L_n-1 but maybe x is within last interval? If x <= R[n-1], candidate added. So fine.
Now, additional candidate: What about idx+1? Could x be inside idx+1 even though we have idx from binary search? If x is exactly at L_{idx+1} and we have idx from binary search? Then L_{idx+1} <= x, so idx+1 is also <= x. So the largest L <= x would be idx+1, not idx. So our idx would be idx+1. So it's covered.
Thus correct.
Now, about multiple candidates: We only check idx and idx-1. That covers at most two. No other.
Now, I think solution is good.
Now, we must also consider that shots may be up to 200k, and we need to compute square of dy only once? But fine.
Now, let's consider a degenerate case where circles are extremely narrow? radii at least 1, so intervals length at least 2.
Now, any potential integer overflow when computing L and R? x could be -20000, r=1000 => L=-21000, that's within int range (-32768). Good.
Now, we might consider using arrays of size n, but vector is fine.
Now, I'll produce final solution.
But I realize something: In the candidate generation, we are adding idx if x <= R[idx]. But suppose idx is from binary search and x is exactly at L_{idx+1}? As argued, then idx+1 would be largest L <= x, so idx would be idx+1, not current idx. So no issue.
But I'm going to test a specific case: circles: A: L=0,R=5; B: L=5,R=10. Touching at 5. x=5. L_sorted = [0,5]. upper_bound(5) returns index of first element >5. Both 0 and 5 are <=5. So first >5 is end, pos=2, idx=1. Good. Then we add idx=1 (B) because x<=R[1]=10 true. Then we add idx-1=0 because x<=R[0]=5 true. So both added.
Now, if we had used lower_bound: lower_bound returns first element not less than x, so for x=5 it would return index0? Actually lower_bound(5) returns first element >=5, so that would be index1 (5). So idx=1 again. So same.
Now, we need to ensure that we don't miss the left interval when x is exactly at the touching point but binary search (upper_bound) returns the right interval. That's fine because we also add left via idx-1.
Now, consider a scenario where circles touch and the shot's x is exactly at the touching point, but we have a third circle whose interval starts later. Only two.
Now, all good.
Now, we might think about a scenario where circles are not given in increasing order, but after sorting, the intervals become [L1, R1], [L2, R2], ... with L1 < L2, etc. Since they are disjoint, R1 < L2 or R1 = L2. So the property holds.
Now, regarding the output: We need to print the number of targets hit (i.e., those with at least one hit), then the first hit numbers. In the example output, they printed "2\n3 3 -1 ". There is a trailing space after -1? But it's fine.
Now, let's write the final code.
But before finalizing, double-check candidate generation in code:
int pos = upper_bound(Ls.begin(), Ls.end(), x) - Ls.begin();
int idx = pos - 1;
Now if pos == 0, idx = -1; else idx >=0.
Now we do:
if (idx >= 0 && x <= Rs[idx]) cand[cnt++] = idx;
if (idx-1 >= 0 && x <= Rs[idx-1]) cand[cnt++] = idx-1;
But what about scenario where idx = -1? Then first condition false, second condition idx-1 >=0? idx-1 = -2, false.
Now scenario where idx = 0, then first condition may add idx=0, second condition idx-1>=0 false.
Now scenario where idx = n-1, first condition maybe true, second may add idx-1 if condition holds.
Now scenario where idx from binary search, but x <= Rs[idx] is false. Then no candidate from idx. But we still consider idx-1? According to our code, we would check second condition regardless of first. That might add idx-1 even if x > Rs[idx] but x <= Rs[idx-1]? Is that possible? Let's test: Could we have x such that idx is the largest L <= x, but x <= Rs[idx-1]? That would mean x <= Rs[idx-1] but also L_idx <= x? Since intervals are disjoint, if x <= Rs[idx-1], then x is <= right endpoint of previous interval. But since L_idx is the next interval's left, we have L_idx > Rs[idx-1] unless they touch. If they touch, Rs[idx-1] = L_idx. Then x <= Rs[idx-1] implies x <= L_idx. But we also have L_idx <= x because idx is largest L <= x, meaning L_idx <= x. So together we have x = L_idx = Rs[idx-1]. So that is the touching case. But in that case, x <= Rs[idx] is also true? For touching case, x = L_idx = Rs[idx-1]. Also, x <= Rs[idx]? Since Rs[idx] is the right endpoint of the right circle, which is >= x, so yes true. So both conditions would hold: x <= Rs[idx] true, and x <= Rs[idx-1] true. So idx candidate added anyway. So if x <= Rs[idx] false, then x > Rs[idx]. But if x > Rs[idx], can x <= Rs[idx-1] be true? That would require Rs[idx-1] >= x > Rs[idx]. Since intervals are disjoint, we have Rs[idx-1] < L_idx <= x? Actually if x > Rs[idx], then x is to the right of interval idx. Since idx is largest L <= x, we have L_idx <= x. For x > Rs[idx], we have L_idx <= x and also, because intervals are sorted, Rs[idx] < L_{idx+1} if exists. But could Rs[idx-1] be greater than Rs[idx]? Since intervals are sorted by L, and they are disjoint, the right endpoints are also increasing: R_i < R_{i+1}? Not necessarily strictly? Since intervals are disjoint and left increasing, right endpoints are also increasing: Because if L_i < L_{i+1} and intervals don't overlap, then R_i < L_{i+1} <= R_{i+1}? Actually L_{i+1} > R_i (or equal if touching). So R_i < L_{i+1} <= R_{i+1}. So R_i < R_{i+1} (strictly) because L_{i+1} <= R_{i+1} and R_i < L_{i+1}, so R_i < R_{i+1}. So right endpoints are strictly increasing. So Rs[idx-1] < Rs[idx]. So if x > Rs[idx], then x > Rs[idx-1] automatically. So condition x <= Rs[idx-1] would be false. Therefore, in the case x <= Rs[idx] false, the second condition will also be false. So we could skip adding idx-1 in that scenario. However, we still check it; it's safe because it will be false. So we can keep the code as is.
Thus candidate generation is correct.
Now, we might consider also checking the next interval (idx+1) if x is exactly at its left boundary and we missed? But as argued, if x = L_{idx+1}, then largest L <= x is idx+1, not idx. So we would have idx = idx+1, and we then check idx-1 = idx. So covered.
Thus final code is ready.
Now, let's test with a custom case where n=2, circles not touching and shot hits boundary of first.
Input:
2
0 5
20 5
3
5 0
15 0
25 0
Expected: first shot hits circle1 (since (5-0)^2+0 =25 <=25), second shot hits none (since 15 not inside either: distance to circle1: 225 >25; to circle2: (15-20)^2=25 <=25? Actually (15-20)^2=25, so yes! Wait, circle2: center 20 radius 5, so interval [15,25]. x=15 is on left boundary, so hit. So second shot also hits circle2. So both circles get hit. Third shot x=25 hits circle2 boundary again but already hit. So output: 2 hits, first hits: shot1 for circle1, shot2 for circle2. So ans = "1 2". Let's test our algorithm:
Sorted circles: A: L=-5,R=5; B: L=15,R=25? Actually B center 20 r=5 -> L=15,R=25. Sorted by L: A (L=-5) then B (L=15). Ls = [-5,15].
Shot1 x=5: upper_bound(5) -> first >5? L0=-5<=5, L1=15>5, pos=1 idx=0. Check x<=R[0]=5 true -> candidate0 (A). idx-1? none. Check distance -> hit. ans[1]=1.
Shot2 x=15: upper_bound(15) -> first >15? L0=-5<=15, L1=15 not >15, so pos=2 idx=1. Check x<=R[1]=25 true -> candidate1 (B). idx-1=0, check x<=R[0]=5? 15<=5 false. So only candidate1. Distance: (15-20)^2=25 <=25 -> hit. ans[2]=2.
Shot3 x=25: upper_bound(25) -> first >25? L0=-5<=25, L1=15<=25, so pos=2? Actually both <=25, so upper_bound returns end, pos=2 idx=1. Check x<=R[1]=25 true -> candidate1. Distance: (25-20)^2=25 <=25 -> hit but already hit, skip. So output: 2\n1 2. Works.
Now, test case with touching and both hit same shot:
Input:
2
0 2
4 2
m=1
2 0
Expected: circles: A: L=-2,R=2; B: L=2,R=6. x=2. upper_bound(2) returns pos=2 idx=1 (B). candidate1 added, candidate0 added. Both distances: (2-0)^2=4 <=4, (2-4)^2=4 <=4. Both hit. ans[1]=1, ans[2]=1. Output: 2\n1 1. Good.
Now, consider scenario where a shot hits both, but one circle already hit earlier: e.g., shot1 at (2,0) hits both, both recorded as first hit 1. If later shot hits one again, no change. That's fine.
Now, consider scenario where earlier shot hits only one, later shot hits the other and also the first again? Not relevant.
Now, check scenario with negative coordinates: Circles center -10 radius 5 => L=-15,R=-5. Shot at (-15,0) on boundary. Sorted: maybe other circles positive. Our binary search works with negative values.
Now, we must be careful: In binary search, we use upper_bound on Ls. Ls sorted ascending. Works.
Now, any potential pitfalls regarding integer overflow when computing L and R? x could be -20000, r=1000 => L=-21000. That's fine for int.
Now, I think solution is ready.
But I realize one more subtlety: The condition "no two targets coincide, intersect or are nested into each other, but they can touch each other." This is for the targets. However, does it also guarantee that the intervals are disjoint? As argued, yes. But what about circles with the same center? That would be nested, not allowed. Good.
Now, one more nuance: Could a circle be completely to the left of another but have a larger L because of radius? Actually L = x - r. If circles are not overlapping, the order by L should correspond to order by x? Not necessarily, but it's still an increasing sequence.
Now, final code as described.
Now, double-check that we output the count of hit targets correctly: count of ans[i] != -1.
Now, check example: first output: "2\n3 3 -1". In our code, we will output "2\n3 3 -1" (without trailing space). That's acceptable.
Now, might there be any leading/trailing spaces issues? Not critical.
Now, final code.
But before finalizing, consider alternative approach: using sweepline. Not needed.
Now, we will produce final answer.
I'll now write final explanation for solution. We are given n circles with centers on the x‑axis, pairwise disjoint except possibly at a single common point (touching). For each of the m shots (points with integer coordinates) we must determine the earliest shot that hits a given circle (a point hits a circle if it lies inside or on its boundary). The answer should contain the number of circles that were hit at least once and, for each circle, the index of its first hit (or -1).
Because the circles are disjoint and lie on the x‑axis, their orthogonal projections onto the x‑axis are intervals that do not overlap (they may only touch at endpoints). Consequently, a point can belong to at most two circles – only when it lies exactly at a touching point. This allows us to process the shots sequentially and, for each shot, quickly locate the circle(s) that might contain it by checking the x‑coordinate.
Preprocessing:
For every circle we compute its left and right boundaries (L = x - r) and (R = x + r). We sort the circles by (L) (these values are strictly increasing because the circles do not intersect). After sorting we keep arrays of (L), (R), the center (x), the squared radius (r^2), and the original id.
Processing a shot ((x_s, y_s)):
- Use binary search (
upper_bound) on the sorted (L) array to find the largest index (i) with (L_i \le x_s).
- Consider circle (i) (if (i \ge 0) and (x_s \le R_i)) – it is the candidate whose interval contains (x_s).
- Also consider circle (i-1) (if (i-1 \ge 0) and (x_s \le R_{i-1})) – this is needed only when the shot lies exactly at a touching point and therefore belongs to two circles.
- For each candidate circle we test the exact containment condition
((x_s - x_i)^2 + y_s^2 \le r_i^2).
If the circle has not been hit before, we record the current shot index as its first hit.
Complexities:
Sorting the circles costs (O(n \log n)); each shot requires (O(\log n)) for the binary search and at most two distance checks. With (n \le 10^4) and (m \le 2 \cdot 10^5) this is easily fast enough. Memory usage is linear.
The solution correctly handles the special case of a shot hitting two circles at a touching point because both candidates are examined.