We need to solve: given n dominoes with coordinates x_i and heights h_i, push the i-th domino to the right. It falls to the right, covering interval [x_i + 1, x_i + h_i - 1]? Actually problem statement: "the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1]." It says strictly above the base: touching strictly above the base. So the domino itself is at x, and its height h means it extends to the right? Since it's perpendicular to axis, it falls to the right? Usually domino falls in direction of push. He pushes it to the right. So when it falls, it may knock down dominoes to its right if they are within its falling range. The falling domino itself covers from its base at x to the point x + h? Actually, a domino of height h, when it falls to the right, its top will land at coordinate x + h (if we consider the base at x and height along axis). However, the problem defines that it causes fall of dominoes on segment [x+1, x+h-1]. That suggests that the domino occupies the point x (its base) and its body extends to the right, but the falling effect is that any domino whose coordinate lies strictly between x and x+h (excluding x and x+h? Actually [x+1, x+h-1] includes positions strictly greater than x and strictly less than x+h. So it does not affect a domino at exactly x+h? But if a domino's base coordinate is x+h, would it be knocked? Typically if a domino falls, its top would land at x+h and might hit the next domino's base? But the problem says "touched strictly above the base", meaning if the falling domino touches another domino's body above its base, i.e., at height >0. The base of the other domino is at its coordinate. Its body extends upward? Actually need to interpret: Dominoes are placed standing on the table along a line; each domino has a base at coordinate x_i, and it stands upright, so its top is at x_i + h_i? Actually if it's perpendicular to the axis, the axis passes through the center of its base. So imagine the axis as a number line. The domino stands upright, with its base centered at x_i. The domino has height h_i, so its top is at x_i + h_i? But that would be along the axis? Since it's perpendicular to the axis, the axis passes through base center; so the domino extends perpendicular to axis? Wait: "He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base." This is a bit confusing. Typically in domino falling problems, dominoes are placed along a line, and when one falls, it topples and knocks over those to its right. The usual model: domino i at position x_i, with height h_i. When it falls to the right, it will cover the interval [x_i, x_i + h_i] (or something). But here they say it leads to fall of all dominoes on the segment [x+1, x+h-1]. So they exclude the endpoints. This is unusual.
Let's read carefully: "Consider that a domino falls if it is touched strictly above the base. In other words, the fall of the domino with the initial coordinate x and height h leads to the fall of all dominoes on the segment [x + 1, x + h - 1]."
Interpretation: A domino is a vertical segment standing on its base. The base is a point on the axis? Actually the base is a segment? But they say axis passes through center of its base. Probably the base is a small segment but we can consider it as a point. The domino stands upright, its body extends vertically? Actually perpendicular to the axis: if the axis is horizontal, and domino stands perpendicular to that axis, that means it's vertical? Wait: "He put n dominoes on the table along one axis, going from left to right. Every domino stands perpendicular to that axis so that the axis passes through the center of its base." So there is a line (axis) along the table. The dominoes are placed such that they are standing perpendicular to that axis. So the axis is like a line on the table; each domino is like a vertical block (its height is vertical), but the axis is horizontal and passes through the center of its base. So when the domino falls, it rotates around its base and topples onto the axis? Hmm.
Maybe it's like typical falling dominoes: they are placed along a line, and each domino has a certain "range" to the right when it falls. In many problems, if domino i falls, it will knock down domino j if x_j is in (x_i, x_i + h_i]. However, here they say [x+1, x+h-1]. So they exclude the point exactly at x+h. So a domino at coordinate x+h would not be knocked? Possibly because at x+h the falling domino touches exactly at the base? But that's speculation.
Nevertheless, the problem is defined: pushing domino i to the right will cause all dominoes with coordinates in [x_i+1, x_i+h_i-1] to fall. Moreover, those dominoes, when they fall, will also cause further dominoes to fall (to the right) based on their own heights. So it's a chain reaction. We need for each i, the total number of dominoes that fall, including i itself.
Thus we have n dominoes with positions x_i (unique) and heights h_i. When we push i, domino i falls. That triggers a chain: any domino j with x_j in [x_i+1, x_i+h_i-1] falls; each fallen domino k then triggers its own interval [x_k+1, x_k+h_k-1]; etc. This is essentially a process of covering the right side interval starting from i, with dominoes that are within the current covered range. Since dominoes only affect to the right (positive direction), it's a one-dimensional process.
We need for each i, the count of dominoes that eventually fall, i.e., the size of the set of dominoes reachable via a directed graph where i points to all j > i such that x_j <= x_i + h_i - 1? Actually the condition: domino i knocks domino j if x_j is in [x_i+1, x_i+h_i-1]. But note that dominoes are not necessarily sorted by x; they can be in any order but coordinates are unique. Since we only care about knocking to the right, we should consider dominoes sorted by increasing x. Because if a domino i has coordinate x_i and it falls, it can only affect dominoes with larger x coordinates (since interval start at x_i+1). So it's a directed acyclic graph (DAG) if we order by x. Actually edges only go from left to right (if x_j > x_i). So it's a DAG.
Thus we can sort dominoes by x. Let indices after sorting be 1..n with x[1] < x[2] < ... < x[n]. For each i, we need to compute how many dominoes to the right are reachable via the falling process.
Define a function f(i) = the furthest index (or furthest coordinate) that eventually gets covered when starting from i. Because once we know the furthest coordinate that is knocked down, all dominoes with indices between i and that furthest index (in terms of position) will fall? Not necessarily: if there are gaps? But dominoes are discrete points; if the falling process covers a continuous interval? Because when dominoes fall, they cover intervals; and if a domino falls, its interval is continuous. The union of intervals from all fallen dominoes will be a contiguous segment from x_i to some max coordinate, because intervals are continuous and they start from positions of fallen dominoes. Since we start at x_i, and we add intervals of dominoes that are within the current covered range, the final covered set of coordinates (on the axis) will be a continuous interval from x_i to some R_i. However, is it guaranteed that all dominoes whose x lies in [x_i, R_i] will fall? Possibly not if some domino's coordinate is within [x_i, R_i] but its interval does not get triggered because it's not directly hit? Actually if the coverage becomes a continuous interval, any domino with base inside that interval should be knocked down because the falling dominoes collectively cover that interval. But consider: the fall of domino i covers [x_i+1, x_i+h_i-1]. Within that, if there is a domino j with coordinate in that interval, it falls. Then its interval covers [x_j+1, x_j+h_j-1]. The union of these intervals, along with the initial [x_i+1, x_i+h_i-1], may extend beyond. Since intervals are contiguous and they start at points that are covered, the union will be a contiguous interval from x_i (or x_i+1?) to some maximum. However, what about gaps? Suppose there is a domino k with coordinate between x_i and some other domino j, but not within any interval? But if the intervals are continuous and we start at x_i, we cover from x_i+1 onward up to some point. Any domino with coordinate within that covered range will be intersected by the falling "wave"? But the problem says a domino falls if it is touched strictly above the base. The falling domino covers an interval; any domino whose base lies inside that interval (i.e., its coordinate) is touched above the base? Actually the falling domino's body will sweep across the axis? Let's interpret physically: Domino stands upright; when it falls to the right, it rotates and its top describes an arc. It will hit any domino whose base is within some distance but also might hit the domino's body if it's close enough. The typical simplification: if a domino falls, it will knock over any domino whose position is less than or equal to its position + its height. However, this problem says "strictly above the base" meaning if the falling domino touches the other domino above its base, i.e., at some positive height, then the other domino falls. So if a domino's base is within the interval (x_i, x_i+h_i), then the falling domino's body will hit it above its base? Possibly yes if we consider the falling domino as a segment rotating. But more importantly, the problem explicitly defines the effect: the fall of domino (x,h) leads to the fall of all dominoes on segment [x+1, x+h-1]. So that is the rule: any domino whose coordinate is in that closed interval will fall. So it's deterministic: if a domino's coordinate is in that interval, it falls. So the chain reaction: we start with set S = {i}. For any domino in S, we add all dominoes with coordinates in its interval. So the final set is all dominoes whose coordinates are reachable via intervals. Since intervals are contiguous and only go rightwards, the final set will consist of all dominoes whose coordinates lie in the minimal interval covering all reached coordinates, but is it possible that some domino inside that interval is not reached? Suppose we have dominoes at positions 0, 2, 4. Heights: domino0 h=3 covers [1,2]? Actually [0+1,0+3-1]=[1,2] so it covers up to 2. Domino2 at pos2 falls because it's in [1,2]. Domino2 height maybe 1? But h>=2 so minimum 2: height 2 gives interval [3,1]? Actually [2+1,2+2-1]=[3,3]? That's just point 3? If h=2, interval [3,3] covers only coordinate 3. So domino2 falls, covers [3,3]. Domino4 at pos4 is not covered. So final fallen: 0 and 2. The covered coordinates: from 0 to 2? Actually intervals: initial covers [1,2]; domino2 covers [3,3]; union is [1,3]. But domino at pos3? Not present. So all dominoes with coordinates in [0,2]? Actually domino0 at 0 is starting; domino2 at 2 is covered; there is no domino at 1; so all existing dominoes in the range from 0 to 2? That's just 0 and 2. But if there was a domino at 1, would it fall? Let's test: pos1 domino, not in interval of domino0? interval [1,2] includes 1, so yes it would fall. So if a domino exists at coordinate 1, it would be hit. So essentially, the final set of fallen dominoes is exactly those dominoes whose coordinates lie within the closure of the union of intervals starting from i? But is there a scenario where a domino lies within the union of intervals but is not hit because the union is not covered in a contiguous way? For it to be hit, there must be some domino that falls whose interval contains its coordinate. But if its coordinate lies in the union, it means there is some domino j that fell and whose interval contains that coordinate. Because the union is defined as the union of intervals of fallen dominoes. So by definition, if a coordinate is in the union, then some fallen domino's interval covers it, and thus the domino at that coordinate would fall (if exists). So the set of fallen dominoes is exactly those whose coordinates belong to the union of intervals of fallen dominoes, plus the starting domino. And this union will be a contiguous interval from x_i (maybe plus something) to some R, because intervals are attached to positions that are within the union. Let's reason: Start with interval I0 = [x_i+1, x_i+h_i-1]. As we add dominoes, each new domino j contributes interval [x_j+1, x_j+h_j-1] provided x_j is already within the current union (or more precisely, j falls, so its interval is added). The union of intervals will be contiguous from x_i+1 to some R. Proof by induction: Initially the union is I0, which is contiguous from x_i+1 to x_i+h_i-1. Suppose at some stage we have a contiguous interval [L, R] (with L = x_i+1) covering all coordinates that are covered so far. Then any domino j that falls has x_j in that interval, and its interval extends to x_j+h_j-1, which is >= x_j >= L. So the union of [L,R] and [x_j+1, x_j+h_j-1] will be contiguous because [x_j+1, x_j+h_j-1] starts at x_j+1 which is <= R+? Actually x_j <= R, so x_j+1 <= R+1. If x_j+1 <= R, then the interval overlaps or touches? It could be that x_j+1 > R? Since x_j <= R, x_j+1 could be R+1 if x_j = R. Then the intervals are [L,R] and [R+1, x_j+h_j-1] which are disjoint but adjacent? Actually [R+1, ...]. Are they contiguous? The union would be [L, x_j+h_j-1] if there is no gap. But is there a gap? The gap would be if x_j+1 > R+1? Since x_j+1 <= R+1 exactly when x_j <= R. The worst case: x_j = R, then x_j+1 = R+1, so the new interval starts at R+1, which is exactly one unit after R. The union would then be [L,R] ∪ [R+1, ...] = [L, ...]. Since the intervals are closed, is R+1 contiguous? There is no integer point between R and R+1? But the axis is continuous? Coordinates are real numbers (since x_i can be any integer, but actually they are integers, but the interval is continuous? The coordinates are integers, but the interval [x+1, x+h-1] includes all real numbers in that range? Since coordinates are integers, but the falling domino can hit any domino whose coordinate is in that interval. The interval is continuous; if a domino's coordinate is exactly at R+1, it will be hit because the interval includes that point. So the union becomes contiguous. If x_j < R, then x_j+1 <= R, so the new interval overlaps or is within the existing interval, so union remains contiguous. So indeed, the covered set becomes a contiguous interval from x_i+1 to some maximum R_i. And the starting domino at x_i is not covered by any interval (since intervals start at x+1), but we count it as fallen. So all dominoes with coordinates in [x_i, R_i] will fall? Actually dominoes with coordinates in (x_i, R_i] (i.e., greater than x_i) will fall if their coordinate is ≤ R_i, because they are covered by some interval. But what about dominoes exactly at R_i+something? The furthest coordinate that gets covered is R_i, meaning that there is some fallen domino whose interval reaches up to R_i. But there could be dominoes with coordinates between x_i and R_i that are not hit? For them to be hit, there must be some interval covering their coordinate. Since the union is [x_i+1, R_i], any point in that interval is covered by some interval (by definition of union). So yes, if a domino exists at coordinate y where x_i < y ≤ R_i, then y is in the union, so there exists some fallen domino j such that y ∈ [x_j+1, x_j+h_j-1]; thus that domino falls. Therefore, all dominoes with coordinates in [x_i+1, R_i] fall. Also, the domino at x_i itself falls (starting). So total fallen are those dominoes with coordinates in [x_i, R_i] (including left endpoint) that exist. But is it guaranteed that R_i >= x_i? Yes, since h_i >=2, interval [x_i+1, x_i+h_i-1] has at least length 1, so R_i >= x_i+1 > x_i. So the fallen dominoes are exactly the dominoes whose x-coordinate lies between x_i (inclusive) and R_i (inclusive), where R_i is the maximum coordinate that is covered by the chain reaction.
But wait: could there be a scenario where a domino at some coordinate y within [x_i, R_i] is not hit because the chain reaction didn't "reach" it? For it to be hit, there must be a domino that falls whose interval includes y. Since y is in the union, by definition there exists some j that fell with y in [x_j+1, x_j+h_j-1]. So that j fell, meaning that its own coordinate was covered by some earlier interval, etc. So it's consistent.
Thus the problem reduces to: given sorted dominoes by x, for each i, compute the furthest coordinate R_i (a real number, but we can treat as integer since coordinates and heights are integers, and intervals are inclusive of endpoints) such that the chain reaction starting from i will cover exactly the interval [x_i, R_i] (in terms of domino positions). Then the answer for i is the number of dominoes with coordinates in [x_i, R_i] = the count of indices from i to j where x_j ≤ R_i.
So we need to compute for each i the rightmost coordinate reachable via intervals from i.
This is reminiscent of a standard problem "falling dominoes" where we need to compute the farthest point each domino can knock over to the right, considering cascading effects. It can be solved with a stack or DP from right to left.
Let positions sorted: p[1..n] with p[i] = x_i (I'll denote as x[i]), and h[i] as height.
Define f[i] = maximum coordinate that can be reached if domino i falls (including its own effect and subsequent). More precisely, f[i] = max( x[i] + h[i] - 1, max_{j: i < j and x[j] ≤ x[i] + h[i] - 1} f[j] )? But careful: the interval from domino i directly covers up to x[i] + h[i] - 1. However, if there are dominoes within that interval that themselves can reach further, the overall reach extends to the maximum of their f values. But note: f[j] might be defined as the furthest coordinate reached starting from j alone. But when starting from i, after j falls, j's effect will propagate, so the overall reachable coordinate is the max over all j that fall of f[j]. Since the process is deterministic and monotonic (only increasing), we can define:
Let R[i] = the furthest coordinate that will be covered when i falls (including cascades). Then R[i] = max( x[i] + h[i] - 1, max{ R[j] : i < j and x[j] ≤ x[i] + h[i] - 1 } ).
But is that correct? Consider domino j that is within the direct range of i; when i falls, j falls. Then j triggers its own cascade, which may bring down further dominoes, ultimately covering up to R[j]. However, it's possible that some domino k that is not within direct range of i, but within R[j] of j, gets knocked, and then further extends. So yes, R[i] is the maximum of all R values of dominoes that are reachable from i. Since the reachability is transitive, we can think of a directed graph: i -> j if x[j] ≤ x[i] + h[i] - 1 (and j > i). Then the set of reachable nodes from i is all nodes in the interval from i to some furthest index, but not exactly: because if there are gaps where some domino is not directly reachable from i but reachable via others, then eventually all dominoes from i up to some index will be reachable. Actually the property that if we have sorted positions, then the reachable set from i is a contiguous suffix of indices starting from i (i.e., all indices i, i+1, ..., r[i])? Is it always contiguous? Let's test: Suppose positions: i=1 at x=0, h=10 (covers up to 9). Domino 2 at x=5, h=2 (covers up to 6). Domino 3 at x=7, h=2 (covers up to 8). Domino 4 at x=9, h=2 (covers up to 10). Domino 5 at x=11, h=1? But h>=2 so min 2, covers up to 12? Actually 11+2-1=12. Then from i=1, direct range covers up to 9, so dominoes 2,3,4 are directly covered? x2=5 ≤9, x3=7 ≤9, x4=9 ≤9 (since interval [1,9] includes 9? x+1 to x+h-1 = [1,9], inclusive, so x=9 is covered, yes). So all 2,3,4 fall. Then from 4, R[4] = max(9+2-1=10, maybe further). 4 covers up to 10, which includes domino 5? x5=11 >10, not covered directly. But 3 covers up to 8, 2 covers up to 6. So none directly cover 5. But domino 4 covers up to 10, not 11. So domino 5 does not fall. So reachable indices are 1,2,3,4. That's contiguous.
But is it always contiguous? Consider a scenario where there is a domino that is not directly reachable from i but reachable via others, yet there is a domino with a smaller index that is not reachable? Since indices are sorted by x, if we have indices i < j < k, and i can reach j (directly or indirectly) and j can reach k, then i can reach k. So the set of reachable indices is upward closed: if i can reach k, then for any j between i and k, does i necessarily reach j? Not automatically: j might be positioned between i and k but maybe its coordinate is such that it is not knocked by any domino that falls. But if i reaches k, that means there is a chain i -> ... -> k. The chain consists of dominoes with increasing positions. All dominoes in between i and k, if they exist, are they necessarily knocked? Suppose we have positions: i at 0, h large enough to cover up to 100. But there is a domino at 50 with height 1? Actually h>=2. Suppose domino i covers [1,99] (h=100? Actually h=100 gives up to 99). Domino at 50 (j) with height 2 (covers up to 51). Domino at 60 (k) with height 2 (covers up to 61). If we start from i, it directly knocks both j and k because they are within [1,99]. So both fall. That's contiguous.
What about a gap: i covers [1,10]; there is a domino at 5 (j) with height 2 covering up to 6; and a domino at 8 (k) with height 2 covering up to 9; and also a domino at 7 (l) but suppose l is present. Actually l at 7 would be covered by i directly as well (since 7 in [1,10]) so it falls. So no gap.
Could there be a scenario where a domino in between is not knocked because it's not in any interval? Suppose i covers [1,10], there is a domino at 6 with height 2 (covers up to 7). Another domino at 8 with height 2 (covers up to 9). So both fall. What about a domino at 7? Its coordinate is 7. Is it covered? i covers [1,10] includes 7, so it falls directly, regardless of j and k. In fact, any domino with coordinate in the initial direct range will be directly knocked by i. So there's no way for i to skip a domino that lies in its direct range. So the only chance to have a gap is if there is a domino at a coordinate y such that y > x_i + h_i - 1 (outside direct range), but becomes reachable via chain, and there is another domino with coordinate between x_i + h_i - 1 and y that is not reachable. But if y is reachable via chain, there must be a sequence of dominoes that connect from i to y. That sequence will involve dominoes with positions increasing, each within the previous domino's interval. Since intervals are continuous, and positions are increasing, the union of intervals from the chain will cover a continuous segment from some point. Specifically, starting from i, the covered segment expands to the right. When the chain reaches y, it means that at each step, the intervals cover a contiguous region. So the entire interval from x_i to y is covered? Let's prove: Suppose we have a chain i = i0 -> i1 -> i2 -> ... -> im, where x_{i0} < x_{i1} < ... < x_{im} = y, and for each t, x_{i_{t+1}} ∈ [x_{i_t}+1, x_{i_t}+h_{i_t}-1]. Then we claim that for any position z between x_i and y, there is some domino in the chain (or maybe i itself) whose interval covers z. Is that necessarily true? Not obviously. Consider i0 at 0, h=5 => covers [1,4]. i1 at 4, h=5 => covers [5,8]. So chain: i0 covers up to 4, i1 covers from 5 to 8. The positions between 0 and 8 are all covered? What about point 4.5? It's covered by i0? i0 covers [1,4], so 4.5 not covered. i1 covers [5,8], so 4.5 not covered. So the coordinate 4.5 is not covered, but there might be no domino there. But if there was a domino at 4.5, would it fall? According to rule, a domino falls if its coordinate is in the interval of a falling domino. 4.5 is not in [1,4] nor in [5,8] because intervals are integer endpoints? Are intervals integer? x and h are integers, so intervals are [x+1, x+h-1] which are integer intervals? Actually they are closed intervals on the real line, but x+1 and x+h-1 are integers, so the interval includes all real numbers between those integers as well. For integer x, [x+1, x+h-1] includes all reals from x+1 to x+h-1 inclusive. So 4.5 is in [1,4]? 1 to 4 includes numbers up to 4, so 4.5 >4, so not included. In [5,8]? starts at 5, so 4.5 <5. So indeed 4.5 is not covered. So a domino at 4.5 would not fall. But can there be a domino at 4.5? Coordinates are integers per input: xi are integers. So all domino positions are integers. So 4.5 is not possible. Therefore gaps at non-integer coordinates are irrelevant. But could there be an integer gap? For example, i0 at 0 h=5 covers [1,4]; i1 at 4 h=5 covers [5,8]. Then integer points: 1,2,3,4 are covered by i0; 5,6,7,8 covered by i1. There is no integer between 4 and 5, so all integer points from 1 to 8 are covered. So the union is contiguous in integer domain. More generally, since intervals are inclusive of integer boundaries, and domino positions are integers, the union of intervals from a chain will cover all integer points from x_i+1 to some max integer R. Because each interval covers a set of real numbers, but we only care about integer positions of dominoes. However, consider intervals like [x+1, x+h-1], where h can be any integer >=2. So the length is h-1 (which is at least 1). So these intervals cover contiguous integer points from x+1 to x+h-1 inclusive. So they cover integer segments. The union of such integer segments that are "connected" (i.e., overlapping or touching at boundaries) will be a contiguous integer segment if there is no gap of integer points. For union to have a gap at integer point k, there must be two intervals covering up to some m and from some n>m+1, such that m < k < n and no interval covers k. But if we have a chain where each successive domino is at position x_{t+1} and it is in the interval of previous domino, then x_{t+1} ≤ x_t + h_t -1. The interval of domino t covers all integers from x_t+1 to x_t+h_t-1. So it covers every integer up to x_t + h_t -1. Since x_{t+1} is at most x_t + h_t -1, the interval of domino t covers all integers from x_t+1 to x_{t+1}? Not necessarily: if x_{t+1} = x_t + h_t -1, then the interval covers up to that point, and the next interval starts at x_{t+1}+1. There is no integer between x_t + h_t -1 and x_{t+1}+1? Since x_{t+1}= x_t + h_t -1, then x_{t+1}+1 = x_t + h_t. That's one integer larger. Is there an integer point at x_t + h_t? Possibly. But is it covered? It is covered by domino t+1's interval if its interval starts at x_{t+1}+1 = x_t+h_t and goes to x_{t+1}+h_{t+1}-1. So the point x_t + h_t is covered by domino t+1 if x_{t+1}+1 ≤ x_t+h_t ≤ x_{t+1}+h_{t+1}-1. Since x_{t+1}+1 = x_t+h_t, that is exactly the left endpoint, so it is covered. So the union covers all integer points from x_i+1 up to x_{t+1}+h_{t+1}-1, with no gaps. More formally, the union of intervals is contiguous in integers if for each consecutive domino in the chain, the left endpoint of the next interval is <= previous right endpoint +1? Actually intervals are [a,b] where a and b are integers. The union of such intervals is contiguous if there is no gap of at least 1 between b_k and a_{k+1} i.e., b_k + 1 < a_{k+1}. In our chain, we have a_{t+1} = x_{t+1}+1. And b_t = x_t + h_t -1. Since x_{t+1} ≤ b_t, we have a_{t+1} = x_{t+1}+1 ≤ b_t +1. So a_{t+1} ≤ b_t +1. That means the intervals either overlap or touch at the boundary (a_{t+1} = b_t +1). If a_{t+1} = b_t +1, then the point b_t +1 = a_{t+1} is the start of next interval, so it is covered by the next interval. And there is no integer between b_t and a_{t+1} because the next integer after b_t is b_t+1 = a_{t+1}. So all integers are covered. Therefore the union covers a contiguous set of integers from x_i+1 to some max.
Hence, the set of domino positions that fall is exactly those with coordinates in the integer interval [x_i, R_i] where R_i is the maximum coordinate covered (i.e., the rightmost integer such that all integers up to that are covered). So it's contiguous.
Thus we can define for each domino i (in sorted order) the furthest coordinate reachable R[i] (which could be a real number, but we can think of it as integer because the coverage ends at an integer from some domino's interval endpoint). Actually the coverage might end at some value that is not an integer coordinate of a domino, but the last covered integer coordinate is some value, and the next integer is not covered. Since domino positions are integers, we care about whether domino at coordinate y falls if y ≤ R[i]. So we can define R[i] as the maximum integer coordinate that is covered (or a real bound). Typically in such problems, we compute the farthest index (the index of the rightmost domino that falls). Because domino positions are distinct and sorted, we can define rightmost index reachable, call it farthest[i] = the maximum index j such that x_j ≤ R[i]. So answer for i is farthest[i] - i + 1.
Therefore we need to compute for each i (in sorted order) the maximum index that falls.
This is similar to computing the "rightmost reach" via intervals that can extend further due to cascading.
Let pos[i] = x_i sorted, height[i] = h_i.
Define direct_right[i] = the furthest coordinate that i can directly knock: pos[i] + height[i] - 1.
But due to cascading, the actual reach R[i] may be larger.
We can process from right to left, maintaining a stack or segment tree to compute R[i] = max(direct_right[i], max{ R[j] | i < j and pos[j] ≤ pos[i] + height[i] - 1 }).
But careful: The condition for j to be knocked by i is that pos[j] is in [pos[i]+1, pos[i]+height[i]-1]. So we need to consider all j > i such that pos[j] ≤ pos[i] + height[i] - 1. Among those, we take the maximum of R[j] (or the farthest index they can knock). But R[j] is a coordinate, not an index. However, if we want to compute the farthest index, we could compute an index-based DP. Let's denote dp[i] = the maximum index reachable from i (i.e., the largest index k such that domino k falls when starting from i). Alternatively, define reach[i] = the maximum coordinate reachable. We can convert between them using binary search (since positions sorted). I think it's easier to compute reach_coord[i] = maximum coordinate that gets covered.
Then we can binary search for the largest index j such that pos[j] ≤ reach_coord[i] to get count.
But we also need to consider that while we are computing reach_coord[i], it depends on reach_coord of later indices that are within its direct range. So we can compute from right to left.
Standard approach: Use a segment tree or monotonic stack to find, for each i, the furthest index it can affect via cascading. Another way: treat intervals as [pos[i], pos[i] + height[i] - 1] (or [pos[i], pos[i] + height[i] - 1]? Actually direct coverage interval is [pos[i]+1, pos[i]+height[i]-1] but the starting domino itself at pos[i] is also fallen, but for propagation we only care about right side. So we can define effective rightmost point that i can directly affect as max_right[i] = pos[i] + height[i] - 1.
Then, if we have later dominoes j, the cascaded effect can extend to R[j] if j is within [pos[i]+1, max_right[i]]. But note: if j is exactly at pos[i] + height[i] - 1, it is within the direct range because interval is inclusive. So condition: if pos[j] ≤ max_right[i] then i can knock j.
Thus, R[i] = max( max_right[i], max{ R[j] : i < j and pos[j] ≤ max_right[i] } ).
But this is not sufficient if some j that is not directly knocked by i but is knocked by a domino that i knocks, and that domino's R[j] extends beyond max_right[i]. That's captured because if j is within direct range, we consider R[j] which may be larger. However, what about j that are not directly within range but become reachable because some k within range knocks j, and then j knocks further? That's also captured: if we take max over all j directly reachable, and those j themselves may have R[j] that include even further dominoes. So the recurrence seems correct.
Thus we need to compute R[i] for i from n down to 1.
A naive O(n^2) would be to for each i, scan forward until we exceed max_right[i] while updating R[i] = max(R[i], R[j]) and then break? But we need to consider all j with pos[j] ≤ max_right[i]; however, if we process from right to left and maintain some data structure, we can query the maximum R among indices j > i with pos[j] ≤ max_right[i]. Because positions are sorted, the set of j > i with pos[j] ≤ max_right[i] is a contiguous segment in terms of index: from i+1 to some idx r = upper_bound(pos, max_right[i]) - 1. So we need to get the maximum R among indices in (i, r]. And also, we need to consider that after we compute R[i], it might be that R[i] extends beyond max_right[i], but that's fine: we compute R[i] as max of max_right[i] and the max R[j] in that range. However, note that the max R[j] may be greater than max_right[i], and then R[i] becomes that larger value. That's correct.
Thus we can compute R[i] by:
Let r_idx = the greatest index j such that pos[j] ≤ max_right[i]. This can be found via binary search on the sorted pos array.
Then R[i] = max( max_right[i], max_{j in (i, r_idx]} R[j] ).
But careful: If r_idx = i (i.e., no j with pos[j] ≤ max_right[i]), then R[i] = max_right[i].
We need to compute max_{j in (i, r_idx]} R[j] efficiently as we process from right to left. Since we process i decreasing, we can maintain a segment tree or fenwick tree for range maximum query over R values for indices from i+1 to n. But note that r_idx can be > i, but R[j] for j > i may not be final yet? Since we process from right to left, when we are at i, we have already computed R for all j > i. So we can query max over those indices.
Thus algorithm:
- Read n, arrays x, h.
- Create list of pairs (x, h).
- Sort by x.
- Create array pos[1..n], height[1..n].
- Precompute max_right[i] = pos[i] + height[i] - 1.
- Initialize array R[n+1] (maybe as double for coordinate, but can be int: up to 1e8 + 1e8 = 2e8, safe in 32-bit? Actually x up to 1e8, h up to 1e8, sum up to 2e8, but with cascading R can be larger than that? Possibly through chain, could exceed sum of all? But maximum coordinate reachable could be up to max x + max h, but chain could combine multiple, so the furthest could be as far as the furthest domino's max_right. Since we take max over R[j] which could be larger than max_right[i], but R[j] itself is bounded by the maximum among all max_right values to the right. So overall R[i] is at most max over all max_right[j] for j>=i. So it's within about 2e8, but could be up to 1e8 + 1e8 = 2e8. However, consider dominoes placed at increasing positions, each with large height, the chain could propagate far beyond any single max_right? Let's think: domino i at x=0, h=1e8, max_right=1e8-1. Domino j at x=1e8-2, h=1e8, max_right ~2e8-3. So R[i] would be max(max_right[i], R[j]) = max(1e8-1, 2e8-3) = 2e8-3. So indeed it's still within sum of two heights. In worst case, if dominoes are spaced closely, the R can be as large as the farthest max_right among those reachable. That could be up to max x + max h, maybe around 2e8. But x and h can be up to 1e8, so sum up to 2e8. However, coordinates can be negative as well, but we can shift? Not needed.
But is it possible that R[i] exceeds 2e8? Suppose dominoes: x1=0, h1=1e8 => max_right=1e8-1. x2 at 1e8-1, h2=1e8 => max_right= (1e8-1)+1e8-1 = 2e8-2. x3 at 2e8-2, h3=1e8 => max_right=3e8-3. But can domino 1 reach domino 2? Condition: x2 ≤ x1 + h1 -1 = 1e8-1, yes if x2 = 1e8-1 exactly, it's within. So domino 1 knocks domino 2, and domino 2 knocks domino 3, so R[1] could be max_right[3] = 3e8-3. So indeed R[i] can be up to max coordinate reachable by chain, potentially up to max initial x + sum of some heights? Actually if dominoes are placed such that each domino's max_right extends to the next, the chain can propagate arbitrarily far, but limited by the number of dominoes (n up to 1e5). Since each height can be up to 1e8, the maximum R could be as large as x_max + n * h_max? But wait: The positions are fixed. For domino i to knock domino j, we require x_j ≤ x_i + h_i - 1. That means that the chain's reach is bounded by the maximum of x_k + h_k -1 among k in the reachable set. Since positions are increasing, the maximum x_k + h_k -1 among those k is at most the maximum of (pos + height) among all dominoes to the right of i that are reachable. However, could the reach extend beyond the maximum of those because of overlapping? Actually R[i] is defined as the maximum coordinate covered, which is the supremum of all intervals. The intervals are [pos+1, pos+height-1] from each fallen domino. The rightmost point is max over all (pos_k + height_k - 1) for k in fallen set. So R[i] = max_{k in fallen set} (pos_k + height_k - 1). So it is indeed the maximum of max_right among the fallen set. So R[i] cannot exceed the maximum max_right among all dominoes that are reachable. Since there are at most n dominoes, the maximum R is at most max over all (pos_j + h_j - 1). That could be as high as (1e8 + 1e8 - 1) = 2e8 - 1? Wait, that's only for a single domino. But consider domino at x = 1e8, h = 1e8 => max_right = 2e8 - 1. But there could be a domino with x = 2e8, h = 1e8? But x up to 1e8? Actually x_i are bounded by 1e8 in absolute value, so max x is 1e8. So x_i can be at most 1e8. So pos_max = 1e8. Then max_right = 1e8 + 1e8 - 1 = 2e8 - 1. That's the maximum possible for a single domino. So indeed, because x_i ≤ 1e8, the sum x_i + h_i is at most 2e8. So R[i] ≤ 2e8. But note: x_i can be negative, but negative plus height could be less than 2e8, but maximum still bounded by 2e8. So R fits in 32-bit int. Good.
Thus we can use integers.
Now the recurrence:
For i from n down to 1:
let r = upper_bound(pos, max_right[i]) - 1 (the greatest index j such that pos[j] ≤ max_right[i]).
if r > i:
R[i] = max(max_right[i], query_max(i+1, r))
else:
R[i] = max_right[i]
But careful: Is it enough to just take max over R[j] for j in (i, r]? Consider the possibility that some domino j with pos[j] ≤ max_right[i] has R[j] that is greater than max_right[i], but also there might be some domino k with pos[k] > max_right[i] that is reachable via j, i.e., k > r but pos[k] ≤ R[j]. Since R[j] may be > max_right[i], then after i knocks j, j will knock k. That is captured because we take max over R[j] which already includes k. So R[i] becomes at least R[j] which may be > max_right[i] and potentially includes dominoes beyond r. However, when we compute R[i] as max(max_right[i], max_{j in (i, r]} R[j]), we are not directly including R of dominoes beyond r that are not directly reachable, but those beyond r could be included if some j's R covers them. That's correct because that max includes R[j] for j in (i, r]. So R[i] will be the maximum coordinate among all dominoes that are reachable.
But is it sufficient to only consider j in (i, r] for the max? What if there is a domino j with pos[j] ≤ max_right[i] but its R[j] is not the maximum among those; but some other domino that is not directly reachable (pos > max_right[i]) could be reachable via a chain that goes through a domino j with pos[j] ≤ max_right[i] but then that j's R[j] covers that far domino. That's included as R[j] is already the maximum coordinate reachable from j. So taking max over R[j] for j directly reachable captures all extended reach.
Thus recurrence works.
Now we need to answer for each original domino (in input order) the number of dominoes that fall. We have sorted order and computed R[i] for sorted indices. Then for each sorted index i, the farthest index fallen is the largest index k such that pos[k] ≤ R[i]. Since R[i] is the maximum coordinate covered, all dominoes with pos ≤ R[i] and index >= i will fall? Wait, need to ensure that all dominoes with index between i and that k actually fall. As argued, the set of fallen dominoes is exactly those with indices from i up to some index f[i], where f[i] = upper_bound(pos, R[i]) - 1. But is it guaranteed that all those dominoes indeed fall? We argued that the covered coordinates are a contiguous interval from pos[i] (or pos[i]+1?) up to R[i], but does it necessarily include every domino whose coordinate lies in that interval? Let's check: Suppose we have dominoes at positions: i at 0, height 3 => max_right=2. Next domino at 1, height 2 => max_right=2. Next domino at 2, height 2 => max_right=3? Actually 2+2-1=3. Next domino at 3, height 2 => max_right=4. Sorted indices: 1: pos=0, h=3, max_right=2; 2: pos=1, h=2, max_right=2; 3: pos=2, h=2, max_right=3; 4: pos=3, h=2, max_right=4.
Now compute from right:
i=4: r = upper_bound(pos ≤ 4) => all ≤4, so r=4, but i=4, r=4, so r>i? no, r=i. So R[4]=max_right[4]=4.
i=3: max_right[3]=3. r = upper_bound(pos ≤3)-1 = index of last pos ≤3, that's index 4? Actually pos[4]=3? Wait we have pos[4]=3. So pos[4]=3 ≤3, so r=4. Since r>i (3<4). Then query max over R[j] for j in (3,4] = j=4: R[4]=4. So R[3]=max(3,4)=4.
i=2: max_right[2]=2. r = last index with pos ≤2: pos[3]=2, so r=3. query over j=3,4? Actually j in (2,3] = j=3 only. Also check j=4? pos[4]=3 >2 so not included. So max R[3]=4. So R[2]=max(2,4)=4.
i=1: max_right[1]=2. r = last index with pos ≤2: index 3? pos[3]=2, so r=3. query j in (1,3] = j=2,3. R[2]=4, R[3]=4 => max=4. So R[1]=max(2,4)=4.
Now for i=1, R[1]=4. The largest index with pos ≤4 is index 4 (pos=3). So f[1]=4. So answer count = 4-1+1=4. Indeed, all dominoes fall? Let's simulate: push domino at 0 (h=3). Direct range [1,2] covers dominoes at 1 and 2. Domino at 1 (h=2) falls, covers [2,2] (since 1+2-1=2). Domino at 2 (h=2) falls, covers [3,3]. Domino at 3 falls, covers [4,4]. So all fall. Yes.
Now test a case where there might be a domino within [pos[i], R[i]] that does not fall due to some gap? Suppose positions: 0 (h=2) => covers [1,1]. Next domino at 2 (h=2) => covers [3,3]. Next domino at 1? But coordinates must be unique and sorted. Let's try: pos: 0 (h=2), pos: 1 (h=1? but h>=2, so min 2, but 1+2-1=2, covers [2,2]), pos: 3 (h=2) covers [4,4]. Actually we need a scenario where a domino is within the interval but not hit because the chain jumps over it? For a domino to be not hit, it must not be in any interval of fallen dominoes. But if its coordinate is between pos[i] and R[i], it might still be not hit if there is a gap in coverage at that integer. But we argued coverage is contiguous in integer points if we have intervals that are inclusive and start at integer+1 and end at integer+h-1. Let's test a potential gap: start i at 0, h=3 (covers [1,2]). Next domino at 3 (h=2) covers [4,5]. But domino at 2? Actually pos=2 not present. So we don't care. Suppose we have domino at 2? That would be hit by i because [1,2] includes 2. So it would fall. To have a domino at 2 not fall, we need i's interval not cover 2, but then 2 wouldn't be within R[i]? Possibly R[i] could be >2 due to later domino, but if 2 is not covered by any interval, then it wouldn't fall. But can that happen while some domino with larger coordinate falls? Let's try: i at 0, h=2 (covers [1,1]). Next domino at 1, h=3 (covers [2,3]). Next domino at 2 (h=2) (covers [3,3]). Starting from i=0: i covers [1,1] so domino at 1 falls. Then domino at 1 covers [2,3], so domino at 2 falls (since its coordinate 2 is in [2,3]). So all fall. So no gap.
What about i at 0, h=2 (covers [1,1]). Domino at 2 (h=2) covers [3,3]. Domino at 1? Not present. So R[0] would be? Starting from 0: it directly knocks domino at 2? No, because 2 is not in [1,1]. So domino at 2 does not fall. So R[0]=1 (max_right=1). So not an issue.
To find a counterexample, we need a situation where the union of intervals covers up to some coordinate R, but there is an integer point y in (pos[i], R] that is not covered by any interval, yet the intervals from fallen dominoes collectively cover up to R. For that to happen, the intervals must be such that they skip over y. But intervals are contiguous segments of integers. If they collectively cover up to R, then the union must be a set of intervals. For the union to have a gap at y, there would need to be two intervals [a,b] and [c,d] with b < y < c, and b+1 < c, leaving a gap at y. But can such gap occur while still having the later interval start beyond y? If the later interval starts at c > y, then for the later domino to fall, its coordinate c-1? Actually the later domino's coordinate is x = c-1? Because interval for domino with coordinate x is [x+1, x+h-1]. So if we have an interval [c,d], its left endpoint is c = x+1, so the domino's coordinate is x = c-1. For this domino to fall, its coordinate must be within some earlier interval. So x = c-1 must be ≤ b (since earlier interval ends at b). But if b < y < c, and c = x+1, then x = c-1, and we need x ≤ b. So c-1 ≤ b => c ≤ b+1. But if b < y < c and c ≤ b+1, then the only integer y that could be between b and c is if b+1 = c and y = b+1 = c, but then y = c, which is the left endpoint of the later interval and thus covered. So there is no integer gap: if b and c are consecutive integers (b+1 = c), then the integer b+1 is covered by the later interval (since it includes its left endpoint). If b+1 < c, then there is an integer gap, but then c ≥ b+2 => c-1 ≥ b+1, so x = c-1 ≥ b+1. Since x must be ≤ b to be knocked, we have contradiction. Therefore, such gap cannot occur if the later domino falls. So the union of intervals from fallen dominoes is indeed a contiguous integer interval from the starting domino's coordinate+1 to R, covering every integer in between. Since domino positions are integers, any domino with coordinate in that integer interval will be covered. Hence, the set of fallen dominoes is exactly those with coordinates in [pos[i], R[i]] (since the starting domino itself is at pos[i]). Actually does the interval [pos[i]+1, R[i]] cover all integers from pos[i]+1 to R[i]? If R[i] is integer, yes. And pos[i] is included as the starting domino. So all dominoes with pos in [pos[i], R[i]] fall.
Thus the answer count for sorted i is: let j = upper_bound(pos, R[i]) - 1; then count = j - i + 1.
Now we need to output for each domino in original input order. So we'll store original index and map back