We need to solve the problem: given n (up to 1000) people, each with start time ti, start position si, end position fi. They move at speed 1 (so time = distance) either left or right. So direction is sign of (fi - si): if fi > si, direction = +1 (right); else direction = -1 (left). They start at time ti at si, then move continuously toward fi, arriving at time ti + |fi - si| at fi, then exit.
When two persons are at the same point at the same time, they greet each other (each pair greets at most once, even if they meet multiple times? Actually the problem says: "every pair of people greet each other at most once." So if they meet at multiple times, they only greet once. But we need to count for each person how many distinct other people they greet.
We need to compute for each i, the number of j != i such that i and j meet at some time (including possibly at start or end points) while both are active (i.e., from ti to ti+|Δi| and similarly for j). And each pair is counted only once per person (so each person counts each other person at most once). So essentially we need for each unordered pair (i,j), determine if they meet at some common time and point, and if so, each of i and j increments greeting count.
Constraints n ≤ 1000. O(n^2) pairs is okay. For each pair, we need to check if they meet and greet (i.e., their paths intersect in space-time). Since movement is linear with constant speed 1, we can compute the times of intersection and check if they are within both active intervals. There's also possibility they meet at endpoints or start points.
Simplify: each person's trajectory in space-time: position x(t) = si + di * (t - ti), for t from ti to ti+Li, where Li = |fi - si|, and di = sign(fi - si) (either +1 or -1). After that, undefined.
So for two persons i and j, we need to know if there exists t such that xi(t) = xj(t) and t is within both intervals.
Equation: si + di*(t - ti) = sj + dj*(t - tj). Solve for t:
si - diti + dit = sj - djtj + djt
=> (di - dj) * t = (sj - djtj) - (si - diti)
=> t = [ (sj - djtj) - (si - diti) ] / (di - dj)
But need to handle cases where di = dj, i.e., same direction, then either parallel lines: they move with same speed, so if they start at the same time? Actually if di = dj, then they have same direction. Their position functions are linear with same slope. They will meet if at some t both are at same point. Since slopes equal, the difference xi(t) - xj(t) = (si - diti) - (sj - djtj) + (di-dj)t = constant (since di=dj). So the difference is constant = (si - diti) - (sj - dj*tj). So they are at same point if that constant = 0, i.e., they are on the same line in space-time, i.e., they are exactly coincident for all times when both active. However, because they start and end at different times, they might coincide for overlapping interval. But if the constant is zero, then their trajectories are collinear: they start at different times but same line? Let's examine.
If di = dj, then the position lines are parallel. The condition to have any intersection is that they are on the same line: si - diti = sj - djtj, i.e., they have same "offset". Then for t where both are active, they are at the same position at the same time? Wait: if offsets equal, then for any t, xi(t) = offset + dit. Since both are offset + dit, they are exactly equal for all t. So yes, if both are active, at any common time they are at same position. So they will meet throughout the overlapping interval. However, we need to ensure that they actually coexist at the same time: the intervals of activity overlap. Then they will be at same point for all t in the overlap. And they would greet each other. But note: would they greet each other only once? According to problem, they greet at most once, even if they meet multiple times. However, if they are together for an extended period, they still greet only once. So the pair is counted as greeting. So we need to detect if they have any overlapping active time and also they are on the same line. However, careful: If they are on the same line (offsets equal) but the intervals do not overlap? Then they never exist simultaneously, so they never meet. So need overlapping time.
Alternatively, if they are not on same line, they never meet because they are parallel lines with constant separation.
Now for di != dj (i.e., opposite directions), then we can compute t as above. Then we need to check if t lies within both active intervals (including endpoints). But also we need to consider that they might meet exactly at the moment when one starts or ends? The formula should handle that if t equals ti or ti+Li etc. So we just check inclusive.
But there is a nuance: when di != dj, they might cross exactly at a point that is not reachable because one of them might have already finished? We'll check.
Now complexity O(n^2) with rational arithmetic because t could be fractional? The input times and positions are integers. But movement speed is 1, so positions change linearly. The meeting time t might be integer or half-integer? Since all inputs are integers, and di and dj are ±1, the difference di-dj is either 2, -2, or 0. So t = (something)/(2) or (-2). So t can be integer or half-integer. However, we need to check if t is within intervals which are integer bounds? Actually, Li = |fi - si| is integer, so intervals are [ti, ti+Li] inclusive. These are integer endpoints. But t could be .5. If t is .5 and lies strictly between integer times, we need to consider if they can meet at non-integer times? Yes, because they move continuously. So they can meet at fractional times. So we must consider that. So we need to treat t as rational. But we can avoid floating point by using cross-multiplication: check that the meeting time is within the intervals inclusive. But careful: intervals are continuous, not just integer times. So condition: t >= ti and t <= ti+Li and similarly for j. Since ti, Li are integers, t can be fractional. So we can check using rational comparison: (numerator/denom) >= ti. Multiply both sides by denominator (positive or negative depending). Since denominator di-dj can be ±2, it's nonzero. Better to compute t as rational and compare.
Alternative: Check if two linear functions intersect within intervals by checking sign of positions difference at endpoints. But might be messy.
Simplify: Since n=1000, we can use double with careful epsilon? Risks exist, but given inputs up to 10^6, and t up to maybe 2e6, double precision should be enough to compare exactly for rational numbers of form (integer)/2. Because half-integers are exactly representable in binary? 0.5 is exact, but some numbers like 1/2 = 0.5 exact; 3/2 = 1.5 exact? Actually 1.5 is representable exactly because it's a dyadic rational if denominator is power of 2. 1/2 = 2^{-1} so yes, any integer divided by 2 is exactly representable as binary floating point if the integer is within the mantissa precision? Actually double has 53 bits of mantissa; integers up to 2^53 can be represented exactly. Input values up to 1e6, operations up to maybe 2e6, so numerators up to maybe 4e6? That's far less than 2^53, so integer division by 2 yields exact representation. So double should be safe. But we must be careful with equality comparisons: we can compute t and then check if it is within intervals inclusive with some tolerance. However, we can avoid tolerance by using rational arithmetic with integers: check that (t - ti) >= 0 and (ti+Li - t) >= 0. Represent t = p/q with q=2, so p integer, q=2. Then condition: p/2 >= ti => p >= 2ti. Similarly p/2 <= ti+Li => p <= 2(ti+Li). So we can compute p = numerator when denominator=2. Let's derive.
For di != dj, we have di - dj = ±2. Because if di=1, dj=-1 => di-dj=2; if di=-1, dj=1 => di-dj=-2. So denominator = 2 or -2. We can compute t as:
t = ( (sj - djtj) - (si - diti) ) / (di - dj)
Let A = si - diti, B = sj - djtj. So t = (B - A)/(di - dj). Since di, dj ∈ {1,-1}, di-dj ∈ {2, -2}. So t = (B - A)/d where d = di - dj. Multiply numerator and denominator: if d = 2, t = (B-A)/2; if d = -2, t = (A-B)/2 (since dividing by -2 gives negative). Actually easier: take absolute? Let's compute p as integer such that t = p/2.
We can compute p = (B - A) if di - dj = 2, else p = (A - B) if di - dj = -2. But careful: we want p integer and t = p/2.
Because if di - dj = 2, then t = (B-A)/2. So define p = B - A. Then t = p/2. If di - dj = -2, then t = (B-A)/(-2) = (A-B)/2. So define p = A - B. So p is integer.
Thus we can compute p accordingly. Then we need t ∈ [ti, ti+Li] ∩ [tj, tj+Lj]. That translates to:
p/2 >= ti => p >= 2ti
p/2 <= ti+Li => p <= 2(ti+Li)
Similarly for j: p/2 >= tj => p >= 2*tj (but careful: p is defined based on i and j? Actually p is the numerator for t = p/2. That same t should be used. So we need to use the same p to check j interval. However, note that p computed as B-A or A-B might be positive or negative? But t must be positive obviously. So we check:
if di != dj:
if di == 1 and dj == -1: // di-dj=2
p = (sj - (-1)tj) - (si - 1ti) = (sj + tj) - (si - ti) = (sj + tj) - (si - ti) = (sj - si) + (tj + ti?) Wait, compute:
A = si - di*ti = si - (1)ti = si - ti.
B = sj - djtj = sj - (-1)*tj = sj + tj.
So p = B - A = (sj + tj) - (si - ti) = sj + tj - si + ti = (sj - si) + (ti + tj).
else if di == -1 and dj == 1: // di-dj = -2
// p = A - B
A = si - (-1)ti = si + ti.
B = sj - 1tj = sj - tj.
p = A - B = (si + ti) - (sj - tj) = si + ti - sj + tj = (si - sj) + (ti + tj).
We can unify: p = ( (si - diti) - (sj - djtj) ) * (di - dj)/2? Not needed.
But careful: In the second case, t = p/2, same as before. So we just compute p as integer that gives t = p/2.
Now intervals check:
For person i: active from ti to ti+Li inclusive. So condition: ti <= t <= ti+Li. Since t = p/2, this yields:
2ti <= p <= 2(ti+Li).
Similarly for j:
2tj <= p <= 2(tj+Lj).
But note: p must satisfy both. So we need p to be in the intersection of these two integer interval ranges (since p is integer). However, p could be negative? t must be >=0, but input ti>=1, so it's okay. But anyway check.
But is it always correct that p derived as above is the meeting time's numerator? Let's test: For di=1, dj=-1, we have t = ( (sj+tj) - (si-ti) )/2. That is indeed the meeting time if they cross. But need to also verify that t is within both intervals. However, we must also consider if they meet at the endpoint of one but the other is not exactly at that time? The formula still works, because the linear equations hold at that time. But there's a catch: If they meet exactly when one person starts (t = ti) but at that moment, person i appears at si. At that same time, does person j's position equal si? Check if t = ti solves equation. If so, then p/2 = ti => p = 2*ti. Our inequality includes equality, so fine.
Also, if they meet exactly when one finishes: t = ti+Li, then p = 2*(ti+Li). Good.
Thus for opposite directions, we can compute p and check if it lies within both intervals.
Now for same direction (di = dj). Then we must check if they are on the same line and intervals overlap.
Condition: A = si - diti, B = sj - djtj. Since di = dj, they are on same line if A == B. Then their position functions: xi(t) = A + dit, xj(t) = B + djt = A + di*t, so identical for all t. So they are together whenever both are active and t is in the intersection of their active intervals. So they meet if the intervals overlap in time (including endpoints). So we need to check if the two intervals [ti, ti+Li] and [tj, tj+Lj] intersect (non-empty). Since they are closed intervals, they intersect if max(ti, tj) <= min(ti+Li, tj+Lj). If so, then they meet. But careful: Could they meet at a point where one is at start and the other is at end? Yes, as long as same time. So overlap condition is enough. Also, what about if they are on same line but intervals just touch at a point? That counts as meeting (since at that exact time they are at same point). So if max(ti, tj) == min(ti+Li, tj+Lj) they meet at that instant.
So same direction condition: di == dj and A == B and intervals overlap (i.e., max(ti, tj) <= min(ti+Li, tj+Lj)). Then they greet.
But wait: Could they meet if they are on same line but have opposite direction? Already covered di != dj.
There is also the case when one person's path overlaps in space but they cross at different times? That's already handled by intersection.
But there is nuance: The meeting may occur if they are at same point at same time but one is just starting and the other is ending? That's covered by inclusive intervals.
But we also need to consider if they meet at the start point of one and at the end point of the other? Our general formula for di != dj covers that as well.
Thus algorithm: For each i, count greetings. O(n^2) pairs.
But we must ensure we don't double count greetings for the same pair when both meet multiple times? Our conditions: For opposite directions, there is at most one meeting (since lines intersect at one point). For same direction, if they are on same line, they may meet over an interval. But the pair should be counted only once. So if condition holds, we increment both i and j by 1.
Now complexity: n=1000 => about 500k pairs, each with simple operations, fine.
Edge Cases:
-
When di != dj, but the computed t might be exactly at endpoints, but maybe one person is at that point at that time, but the other person's position at that time might be the same, but we also need to ensure that at that time both are actually on the boulevard? For person i, if t = ti+Li, that's the end time. Person i is at fi at that moment, and then immediately leaves. The problem says: "after a person achieves the destination point fi she moves out of the boulevard and cannot greet anyone else." So at the exact moment of reaching fi, she is still there and can greet. Similarly, at start time ti, she appears at si and can greet. So inclusive.
-
When di != dj, we computed p. But we must also consider that the meeting might happen at a time where both persons are moving towards each other? Yes.
-
One more nuance: When di != dj, we must also verify that the meeting point is actually between si and fi for each person? Because if they cross beyond the segment? Actually the movement is along the line from si to fi. They start at si and travel to fi. The linear function is defined only on the interval t ∈ [ti, ti+Li]. During that interval, x(t) varies from si to fi linearly. So the meeting point xm = xi(t) must lie between si and fi (inclusive). But if t is within the time interval, then automatically x(t) will be between si and fi because it's linear monotonic. But is it guaranteed? For a linear function, if t is between the start and end times, x(t) is between si and fi inclusive (since it's monotonic in time). So no need to separately check x coordinate, as time condition ensures that.
But there is a subtle scenario: Suppose di=1, dj=1 (same direction) but they are not on same line, then they never meet. But could they meet if one overtakes the other? Since same speed, no overtaking; they maintain constant separation. So they only meet if they are exactly coincident.
So algorithm seems straightforward.
But wait: there is possibility of meeting when one person's direction is 0? No, always speed 1, direction ±1, because si != fi.
Now let's test with provided examples.
Example 1:
3
1 1 10
5 8 2
9 9 10
Person1: t1=1, s1=1, f1=10 => direction +1, L1=9, interval [1,10]
Person2: t2=5, s2=8, f2=2 => direction -1, L2=6, interval [5,11]
Person3: t3=9, s3=9, f3=10 => direction +1, L3=1, interval [9,10]
Compute pairs:
1-2: di=+1, dj=-1 => opposite. Compute p? For di=+1,dj=-1: p = (sj + tj) - (si - ti) = (8+5) - (1-1)=13 - 0=13 => t=6.5. Check intervals: Person1: [1,10] => 21=2 <=13<=20 => ok. Person2: [5,11] => 25=10 <=13<=22 => ok. So they meet. So greeting.
1-3: di=+1,dj=+1 => same direction. A1 = s1 - d1t1 = 1 - 11 = 0. A3 = 9 - 1*9 = 0. So A1=A3. Intervals: [1,10] and [9,10] overlap? max(1,9)=9 <= min(10,10)=10 => yes overlap. So they meet. So greeting.
2-3: di=-1,dj=+1 => opposite. For di=-1,dj=+1: p = A - B where A = s2 + t2 = 8+5=13, B = s3 - t3 = 9-9=0 => p = 13-0=13 => t=6.5? Wait compute t = (A - B)/2 =13/2=6.5. But check person2 interval [5,11] => t=6.5 okay. Person3 interval [9,10] => t=6.5 not in [9,10]! Actually 6.5 <9. So they don't meet. But we need to check: p=13, for j: 2tj=18, 2(tj+Lj)=2*10=20. p=13 <18, so fails. So no meeting. So greeting counts: person1 meets 2, person2 meets 1? Actually 2 meets 1 only. Person3 meets 1 only. So counts: 2 1 1. Matches output.
Example2:
3
3 2 4
4 3 4
3 6 4
Person1: t1=3, s1=2, f1=4 => dir +1, L1=2, interval [3,5]
Person2: t2=4, s2=3, f2=4 => dir +1? Actually s2=3,f2=4 => +1, L2=1, interval [4,5]
Person3: t3=3, s3=6, f3=4 => dir -1, L3=2, interval [3,5]
Compute all pairs:
1-2: same direction +1. A1 = s1 - t1 = 2-3 = -1. A2 = 3-4 = -1. So equal. Intervals: [3,5] and [4,5] overlap? max(3,4)=4 <= min(5,5)=5 => yes. So meet.
1-3: opposite? dir1=+1, dir3=-1. Compute p for di=+1,dj=-1: p = (s3 + t3) - (s1 - t1) = (6+3) - (2-3)=9 - (-1)=10 => t=5. Check intervals: Person1: [3,5] => 23=6 <=10<=10 (25=10) => OK (t=5 is end). Person3: [3,5] => 2*3=6 <=10<=10 => OK. So meet at t=5. Note: At t=5, person1 reaches f1=4, person3 reaches f3=4? Person3 starts at 6, moves left 2 units to 4 at t=3+2=5, so both at 4 at t=5. So meet.
2-3: dir2=+1, dir3=-1. Compute p = (s3 + t3) - (s2 - t2) = (6+3) - (3-4)=9 - (-1)=10 => t=5. Check intervals: Person2: [4,5] => 2*4=8 <=10<=10 OK. Person3: [3,5] OK. So meet.
Thus each person meets the other two? Person1 meets 2 and 3 =>2. Person2 meets 1 and 3 =>2. Person3 meets 1 and 2 =>2. Output 2 2 2 matches.
So our logic works.
Now we need to implement. For each i from 0 to n-1, we store ti, si, fi, Li, di. di = sign(fi - si) as int: 1 or -1.
We'll compute greetings array count of size n, initialized 0.
Loop over i, j with j > i. Determine if they meet.
Pseudo:
def meets(i, j):
if di == dj:
// same direction
Ai = si - diti
Aj = sj - djtj
if Ai != Aj: return False
// intervals overlap?
start_i = ti; end_i = ti + Li
start_j = tj; end_j = tj + Lj
if max(start_i, start_j) <= min(end_i, end_j): return True
else: return False
else:
// opposite directions
if di == 1 and dj == -1:
p = (sj + tj) - (si - ti)
else: // di == -1 and dj == 1
p = (si + ti) - (sj - tj)
// Check p within both intervals 2
// But careful: p might not be integer? Actually p is integer because all inputs integers. So check conditions:
if p < 2ti or p > 2*(ti+Li): return False
if p < 2tj or p > 2(tj+Lj): return False
return True
But we must also consider potential overflow? ti, si, fi up to 1e6, so Li up to 1e6. So p can be up to maybe (si+ti) up to 2e6, and differences up to maybe 2e6, so p up to 4e6. 2*(ti+Li) up to 2*2e6=4e6. So within int32 (2e9). So fine.
Now, is there any case where di != dj but p computed leads to t that is within intervals but they don't actually meet because they cross at a point but one person might have already passed that point? Let's examine: For opposite directions, the equation solves for t such that positions are equal. If that t is within both active intervals, then indeed at that time, both are at that point. However, is it possible that the meeting point lies outside the segment that one person covers? But if t is within time interval, then the person's position at that time is somewhere along the line from si to fi. Since the function is monotonic and continuous, the position at t is within [min(si,fi), max(si,fi)]. So yes, it's on the path. So fine.
One nuance: The case where di != dj, but the computed p leads to a t that is integer or half-integer. But we need to be careful about boundaries: Suppose t = ti exactly, but at that moment person i is at si. For that to be meeting, person j must also be at si at same time. That is captured by p = 2ti. Similarly, if t = ti+Li, then p = 2(ti+Li). So our inclusive inequalities are correct.
Now, what about case where both persons have opposite directions but their lines are parallel? No, only same direction leads to parallel.
Now, verify with some edge cases.
Consider two persons moving towards each other but they might meet exactly at the moment one starts and the other ends. E.g., i: ti=0, si=0, fi=10, dir=+1; j: tj=5, sj=10, fj=0, dir=-1. They should meet at t=5 at x=5? Actually compute: i from 0 to 10, j from 5 to 15? Wait j: start at 5 at 10, move left to 0, so at t=5, i is at 5? i at t=5: 0+5=5; j at t=5: just started at 10? Actually j starts at t=5 at 10. So not meeting. To meet exactly at start/end: i: ti=0, si=0, fi=10, dir+1; j: tj=10, sj=10, fj=0, dir-1. At t=10, i reaches 10, j starts at 10. So they meet at t=10 at x=10. Compute p for di=1,dj=-1: p = (sj+tj) - (si - ti) = (10+10) - (0-0)=20. Check intervals: i: [0,10] => 20=0 <=20<=20 ok; j: [10,20]? Actually j: tj=10, Lj=10, so interval [10,20]; 2tj=20 <=20<=40 ok. So p=20 works. So good.
Now consider case where they move in same direction but are on same line but intervals just touch: i: [0,5], j: [5,10]. They meet at t=5? At t=5, i ends at some point, j starts? If they are on same line, then xi(t) = A + dit. If A same and di same, then at t=5, i is at A+di5 and j is at A+di*5 as well, since same function. So they meet. But j starts at t=5 at that point. So they meet at t=5. Our overlap condition: max(0,5)=5 <= min(5,10)=5, so true. So counts.
If intervals don't overlap (max > min), no meet.
Now, is there any scenario where di != dj and they meet but the computed p is not integer? Actually p will always be integer because we derived from integers and di,dj are ±1. So fine.
Thus implementation straightforward.
But wait: There is also scenario where di = dj and Ai = Aj, but intervals overlap, they meet. However, note that they could meet multiple times (over an interval) but we count only once per pair.
Now complexity O(n^2) with n up to 1000 => 1e6 pairs? Actually 1000*999/2 ~ 500k. That's fine.
Now we should output count for each person.
Now let's think about potential pitfalls: We need to be careful about integer overflow? Use 64-bit to be safe (Python handles big ints anyway). But if implementing in C++ use long long.
Now, is there any case where di != dj and the computed p might be negative? That would indicate t negative, but times are positive, so fails interval check anyway.
Now, check if there is any scenario where the pair meets at a point that is not captured by the formula because they meet at the same time but the equation gives t that is valid but maybe they are not both active? Already checked intervals.
Now, what about the case when di != dj but the lines coincide? That would mean they move in opposite directions but same line? Actually if they are on same line but opposite directions, the equation for meeting might have infinite solutions? Let's examine: Suppose they have same offset A? That would require si - diti = sj - djtj. But di != dj. Could that happen? For example: Person i: di=1, si - ti = A. Person j: dj=-1, sj + tj = A. If that holds, then the equation xi(t) = A + t, xj(t) = A - t? Actually: xi(t) = si + (t - ti) = (si - ti) + t = A + t. xj(t) = sj - (t - tj) = (sj + tj) - t = A - t. They are symmetric about A? They would meet when A + t = A - t => t=0. But t=0 may be outside intervals. For t>0, they are apart unless t=0. So they only meet at t=0 (if both are active at t=0). Since ti>=1, they are not active at t=0. So no meeting except maybe if t is negative. So they don't meet. But could they have overlapping intervals with both being on the same line? They would have only one meeting at t=0, but not within active times. So our formula for di != dj yields a unique t = (B - A)/(di - dj). If A == B, then t = 0 (since numerator 0). That t is not within intervals if all ti>=1. So it fails the interval checks. So no issue.
Now, we must also consider potential precision errors when using floating point? But we will avoid floating point and use integer p as described.
Now we need to ensure that for di != dj we compute p correctly for both cases. Derive general formula:
If di == 1 and dj == -1: p = (sj + tj) - (si - ti)
If di == -1 and dj == 1: p = (si + ti) - (sj - tj)
Alternatively, we can unify:
Let A_i = si - diti, A_j = sj - djtj.
If di == dj: check as above.
Else: t = (A_j - A_i)/(di - dj). Since di-dj is ±2, we can compute p = (A_j - A_i) * (di - dj) > 0? Not exactly. Actually compute numerator = (A_j - A_i). Denominator = di - dj = ±2. So t = numerator / (di - dj). We want p = 2*t = 2 * numerator/(di-dj). If di-dj = 2, then p = numerator. If di-dj = -2, then p = - numerator. So p = (A_j - A_i) if di-dj=2, else p = (A_i - A_j) if di-dj=-2. This matches above.
But careful: In the case di = -1, dj = 1, di-dj = -2, so p = (A_i - A_j). Compute A_i = si - (-1)ti = si+ti, A_j = sj - 1tj = sj - tj. So p = (si+ti) - (sj - tj). Good.
Now, is there any chance that p is not integer? All terms integer, so p integer.
Now check intervals: we need p >= 2ti and p <= 2(ti+Li). Similarly for j.
But we also need to consider that t = p/2 is indeed the meeting time. However, we should also ensure that the meeting point is within both paths? Already guaranteed by time inclusion.
Now think about cases where the lines intersect but at a time that is within intervals but one person might be at the start point and the other is at the start point as well but with opposite direction? That's fine.
Now, what about the case where they meet exactly at the same point but one arrives exactly when the other leaves? Already covered.
Now, we should also consider if there is any scenario where two persons meet but our condition for di != dj fails because p is not integer? That can't happen.
Now, complexity is O(n^2). With n=1000, we can do straightforward.
But wait: There might be a scenario where they meet at some time but the meeting point is exactly at some endpoint and the other person also at that endpoint but maybe the other person arrives at that endpoint at a slightly different time? But our formula will capture exactly the time when they are at same point.
Now, consider if the two persons have the same direction and same offset, but intervals overlap partially, then they meet. However, what about meeting at endpoints? That's included as overlap.
Now, what about the case where two persons have the same direction and same offset but one starts exactly when the other ends? Overlap at a point, so meet.
Now, the problem says: "every pair of people greet each other at most once." So in same direction case, even if they are together for a period, they greet only once. So we count only once.
Now, we must be careful that the greetings are counted for each person. So for each pair that meets, we increment both i and j counts.
Now, implementing.
Potential pitfalls: Off-by-one on intervals: The problem says "after a person achieves the destination point fi she moves out of the boulevard and cannot greet anyone else." So at the exact time of arrival, she can greet others who are also there. Similarly, at start time, she appears and can greet. So inclusive of both ends. So our interval includes both ends.
Now check with sample.
Now, one more nuance: The movement is along the boulevard axis. The positions are integer initially but may be fractional at meeting times. That's fine.
Now, I think the solution is correct.
But wait: The problem statement: "If two or more persons meet at the boulevard (they are at the same point at the same time, no matter which directions they are going) they all greet each other. Like in the normal life, every pair of people greet each other at most once." So indeed we count each pair at most once.
Now, confirm that our method correctly handles multiple possible meetings? It counts the pair if any meeting exists. So that's fine.
Now, think about tricky edge cases.
Edge case 1: People with same direction but offset equal, but intervals do not overlap: They never meet.
Edge case 2: People with opposite directions, intervals overlap but the crossing time might be exactly at the boundary of both intervals. That's fine.
Edge case 3: People with opposite directions, but the crossing time might be outside the path of one because the path may not cover that point due to direction? Actually if time is within interval, the position is between start and end inclusive. But is there a scenario where the time is within interval but the position is not actually reachable because the person would have passed that point earlier? No, because with constant speed 1, the position is a linear function that passes through all points between start and end as time progresses. So if time is in interval, the position is between.
But there is a subtle scenario: For a person moving from left to right, the position is increasing. If the meeting time is within the interval, then the position x(t) is between si and fi. So it's on the path. So fine.
Now, what about the case where the meeting occurs at a point that is exactly the start for one and somewhere in the middle for the other? That's fine.
Now, consider that the meeting might occur at exactly the same time when both are at the same point but one is just starting and the other is ending. That's fine.
Now, can there be a scenario where two persons meet at the same point and same time but our formula for opposite directions gives a t that is within intervals but one of the intervals includes the time but the other's interval includes that time? Yes.
Now, one more nuance: When di != dj, we computed p based on the assumption that they are moving towards each other. But what if they are moving away from each other? That is, one moving left, the other moving right, but they start on opposite sides? Actually they might cross if they are moving towards each other, but if they are moving away, they won't meet. But our equation will give some t, which may be within intervals but might correspond to a crossing in the past? Let's test: Suppose i: di=1 (right), start at s=0 at t=0, end at 10. j: dj=1 (right) same direction not opposite. So not considered. For opposite directions, one is left, one is right. If they are moving away from each other, then they start at some positions and go further apart. Could there still be a meeting? Possibly if one passes through the other's starting point after the other has left? But if they are moving opposite directions, they will approach each other only if they are moving towards each other. Let's think: i moving right from left, j moving left from right. They will approach each other if i is to the left of j and both move towards each other. If i is to the right of j, then i moving right and j moving left means they move apart. However, they might still meet if they cross paths? If i is to the right of j, then i moving right goes further right, j moving left goes further left, so they separate. Could they ever be at same point at same time? Possibly if i is to the right and j to the left, but i moving right and j moving left, they are moving away from each other, so they won't meet. But what about if they start at same point? Then directions opposite: at start they meet; but they have same start point? That's possible. If si = sj and they start at times maybe different? Let's check: i starts at ti, j starts at tj. At t = max(ti, tj), could they both be at that point? For them to be at same point at same time with opposite directions, it would require that the meeting point coincides with both positions at that time. For i: xi(t) = si + di*(t-ti). For j: xj(t) = sj + dj*(t-tj). If si = sj = x0, then they meet at t if x0 + di*(t-ti) = x0 + dj*(t-tj) => di*(t-ti) = dj*(t-tj). With di=1, dj=-1, we have (t-ti) = -(t-tj) => 2t = ti+tj => t = (ti+tj)/2. So they meet at that time, provided t >= max(ti, tj)? Actually if ti and tj are different, say ti < tj, then t = (ti+tj)/2. If ti < tj, then t = (ti+tj)/2 < tj? Let's check: ti=0, tj=10 => t=5 < 10, so at t=5, j hasn't started yet (since j starts at 10). So not meeting. So both need t within both intervals. So they might meet if t is within both intervals. That could happen if ti and tj are close. For example, ti=2, tj=4, t=3. i active [2,?], j active [4,?] Actually j starts at 4, so t=3 is before j starts, so not active. So they cannot both be active at t if ti and tj differ? Actually if both have same start point, opposite directions, and if they start at the same time, they meet at start point at that time. If they start at different times, the meeting time will be between them, so only one will be active. So they only meet if they start at same time at same point. That case: ti=tj, si=sj. Then they meet at start time. Our formula for opposite directions: For di=1,dj=-1: p = (sj+tj) - (si - ti). If si=sj and ti=tj, then p = (s+t) - (s - t) = 2t. So p=2t, t = p/2 = t. So t = ti = tj. Check intervals: p >= 2(ti+Li) obviously if Li>=0 (positive). So it qualifies. So they meet. Good.
Now, what about scenario where they are moving away from each other? Let's construct: i: di=1 (right), si=10, fi=20 (rightwards), ti=0. j: dj=-1 (left), sj=5, fj=0 (leftwards), tj=0. i starts at 10, moves right; j starts at 5, moves left. They are moving apart. Equation: di=1, dj=-1. Compute p = (sj+tj) - (si - ti) = (5+0)-(10-0)=5-10=-5 => t=-2.5, not within intervals. So no meet. Good.
Thus our check for p within intervals automatically excludes invalid cases.
So algorithm is correct.
Now we should also consider if there are any corner cases where the computed p yields t within intervals but actually they don't meet because the meeting point is not on the path? But that would imply that the time is within interval but the position is outside [si,fi]? As argued, if t within [ti, ti+Li], then x(t) lies between si and fi because it's linear and monotonic. However, caution: For direction = +1, x(t) = si + (t - ti). Since t >= ti, t-ti >=0, x(t) >= si; and t <= ti+Li => t-ti <= Li => x(t) <= si+Li = fi. So yes. For direction = -1, x(t) = si - (t - ti). t-ti >=0 => x(t) <= si; and t-ti <= Li => x(t) >= si - Li = fi. So x(t) between fi and si inclusive. So always on the segment.
Thus done.
Now, about time bounds: ti up to 1e6, Li up to 1e6, so t up to 2e6. So p up to 4e6. So we can store as int.
Now, about the count: n=1000, O(n^2) fine.
Now, we need to output counts for each person.
Now, we might need to consider that a person can greet multiple people at the same time. That's fine.
Now, let's think about the problem's constraints: n up to 1000. But there is another approach using sorting, but O(n^2) is fine.
Now, implement in Python.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
n = int(input())
t = [0]*n
s = [0]*n
f = [0]*n
d = [0]*n
L = [0]*n
for i in range(n):
ti, si, fi = map(int, input().split())
t[i] = ti
s[i] = si
f[i] = fi
if fi > si:
d[i] = 1
L[i] = fi - si
else:
d[i] = -1
L[i] = si - fi
cnt = [0]*n
for i in range(n):
ti, si, fi, di, Li = t[i], s[i], f[i], d[i], L[i]
Ai = si - di*ti # offset
start_i = ti
end_i = ti + Li
for j in range(i+1, n):
tj, sj, fj, dj, Lj = t[j], s[j], f[j], d[j], L[j]
start_j = tj
end_j = tj + Lj
if di == dj:
Aj = sj - dj*tj
if Ai == Aj:
# same line
if max(start_i, start_j) <= min(end_i, end_j):
cnt[i] += 1
cnt[j] += 1
else:
# opposite directions
if di == 1 and dj == -1:
p = (sj + tj) - (si - ti)
else: # di == -1 and dj == 1
p = (si + ti) - (sj - tj)
# check if p lies in both intervals
if p < 2*ti or p > 2*(ti+Li):
continue
if p < 2*tj or p > 2*(tj+Lj):
continue
cnt[i] += 1
cnt[j] += 1
print(' '.join(map(str, cnt)))
But wait: In the else block, we computed p using formulas derived. But what about the case where di != dj but we used the specific formulas? That covers both cases. But we can also compute generically using Ai, Aj, di, dj:
if di != dj:
# p = 2 * meeting_time
# meeting_time = (Aj - Ai) / (di - dj)
# p = 2 * (Aj - Ai) / (di - dj) = (Aj - Ai) * 2/(di-dj)
# Since di-dj = ±2, p = (Aj - Ai) if di-dj=2 else (Ai - Aj)
if di - dj == 2: # di=1, dj=-1
p = Aj - Ai
else: # di-dj == -2 -> di=-1, dj=1
p = Ai - Aj
But we need to ensure Ai, Aj as defined. That might be simpler.
Compute Ai = si - diti, Aj = sj - djtj.
Then if di - dj == 2: p = Aj - Ai; else: p = Ai - Aj.
Check: For di=1,dj=-1, di-dj=2 => p = Aj - Ai. Aj = sj - (-1)tj = sj + tj, Ai = si - 1ti = si - ti => p = (sj+tj) - (si-ti). Same. For di=-1,dj=1, di-dj = -2 => p = Ai - Aj. Ai = si - (-1)ti = si+ti, Aj = sj - 1tj = sj - tj => p = (si+ti) - (sj-tj). Good.
So we can compute generically.
Now we need to compute Li and Lj as absolute differences.
Also, note: start_i = ti, end_i = ti + Li.
Now, confirm that we didn't miss the case where they meet at a point but one of them is not moving? No, all move.
Now, test with some random cases to verify.
Consider symmetric: Person1: ti=1, si=1, fi=3 (+1). Person2: tj=2, sj=3, fj=1 (-1). They move towards each other. Person1 from 1->3 (duration 2, interval [1,3]). Person2 from 3->1 (duration 2, interval [2,4]). They should meet? Compute meeting time: Eq: x1(t)=1+(t-1)=t; x2(t)=3-(t-2)=5 - t? Wait: For person2: x2(t)= sj + dj*(t-tj)=3 - (t-2)=5 - t. Solve t = 5 - t => 2t=5 => t=2.5. Within intervals: 2.5 ∈ [1,3] and [2,4]. So yes. Compute Ai = 1-11=0; Aj = 3 - (-1)2 = 3+2=5; di-dj = 1-(-1)=2 => p = Aj - Ai = 5 => t=2.5. Check: 2ti=2 <=5<=6 OK; 2tj=4 <=5<=8 OK. So works.
Now consider they meet exactly at the moment when one finishes: Person1: [1,3] at 3 arrives at 3; Person2: [2,4] at 2 starts at 3? Actually Person2 starts at 3 at time 2, so they meet at start/end at time2? That's earlier. To have meeting at Person1's end: Let Person1: ti=1, si=1, fi=3 => arrives at 3 at t=3. Person2: start at 3 at t=3? need to have sj=3, tj=3, fj=0? But if start at 3 at t=3, they meet at t=3. Compute: Person2: tj=3, sj=3, fj=0, direction -1, Lj=3, interval [3,6]. Meeting time t=3. Compute Ai=1-11=0; Aj=3 - (-1)3 = 3+3=6; di-dj=2 => p=6-0=6 => t=3. Check intervals: Person1: 2ti=2 <=6<=6 OK; Person2: 2tj=6 <=6<=12 OK. So works.
Now consider same direction, same offset, intervals just touching: Person1: ti=1, si=1, fi=3 (+1) => Ai=1-11=0, L=2, interval[1,3]. Person2: tj=3, sj=3, fj=5 (+1) => Aj=3-13=0, L=2, interval[3,5]. Overlap at t=3? They meet at x=3 at t=3? Person1 at t=3: at 3, finishes; Person2 at t=3: starts at 3. So meet. Check Ai==Aj and max(1,3)=3 <= min(3,5)=3, so true. Good.
Now same direction, same offset but intervals not overlapping: Person1: [1,2], Person2: [3,4]; Ai=Aj=0. max=3, min=2 => 3 <=2 false, so no meet. Correct.
Now same direction, different offset: they never meet.
Now opposite directions but they might cross exactly at the same time at a point but one of them passes through that point earlier? Already covered.
Now consider the possibility of meeting at multiple points when they go opposite directions? They can meet only once because lines in space-time intersect at most once (unless they are on same line but opposite directions, which would yield infinite solutions only if they are same line and speed? But if same line and opposite directions and same offset, they would be: xi(t)=A+dit, xj(t)=A+djt with di = -dj. Then they are only equal when A+dit = A - dit => 2di*t=0 => t=0. So only one meeting at t=0. So again one.
Thus no risk of double counting.
Now, we should also consider the scenario where they meet at a point that is not on the path of one due to direction? Already argued.
Now