We need to interpret the problem. There is a storehouse as an n x m matrix. So coordinates: rows from 1 to n (x), columns from 1 to m (y). Each sofa occupies two adjacent cells (by side). They don't overlap. The storehouse may have empty cells.
We are given d sofas. For each sofa i, we have its two cell coordinates (x1, y1) and (x2, y2). They are adjacent (share a side). No two sofas share a cell. So the mapping from cells to sofa index is a partial function.
Now, we need to find the sofa that has exactly cntl sofas to its left, cntr to its right, cntt to its top, cntb to its bottom. The definitions:
- Sofa A is standing to the left of sofa B if there exist two cells a (covered by A) and b (covered by B) such that xa < xb. (Note: they used x as first coordinate? Actually in statement: "xa < xb, a is covered by A and b is covered by B." But they defined matrix n × m. Usually x is row? Let's check examples.
In example 2:
Input:
3
10 10
1 2 1 1
5 5 6 5
6 4 5 4
2 1 2 0
Wait: The last line is 2 1 2 0. That matches cntl=2, cntr=1, cntt=2, cntb=0. They say second sofa matches.
Let's examine sofas:
Sofa 1: (1,2) and (1,1). So both cells in row 1, columns 1 and 2. So horizontally placed? Since (1,2) and (1,1) share side? Yes, adjacent horizontally. So it's in top row.
Sofa 2: (5,5) and (6,5). That is vertical orientation: rows 5 and 6, column 5.
Sofa 3: (6,4) and (5,4). That is also vertical orientation: rows 5 and 6, column 4 (but reversed order).
Now they said for sofa 1: 0 left, 2 right ((1,1) is to the left of both (5,5) and (5,4)), 0 top and 2 bottom. They used (1,1) is to the left of both (5,5) and (5,4). Left condition uses x coordinate? They wrote: "xa < xb". In their example, they say (1,1) is left of (5,5) and (5,4). Here xa=1, xb=5, so indeed 1<5. So left is defined based on x (row) comparison? But then (1,1) has row 1, column 1; (5,5) has row 5, column 5; 1<5, so that qualifies. So left means "has a cell with smaller row index than some cell of the other sofa"? That seems weird because usually left/right would involve column. But they specifically say: "Sofa A is standing to the left of sofa B if there exist two such cells a and b that xa < xb, a is covered by A and b is covered by B." So it's based on x coordinate. And top condition uses ya < yb? Wait: "Sofa A is standing to the top of sofa B if there exist two such cells a and b that ya < yb, a is covered by A and b is covered by B." So top uses y. So coordinates: x is row (vertical axis), y is column (horizontal axis). In typical matrix representation, rows go downwards, so smaller x means higher (top). But left/right uses x? That would be up/down essentially. And top/bottom uses y? That would be left/right. That is swapped: left condition uses x, top uses y. That is confusing.
Let's read original problem statement: Possibly there is a mis-match? Many Codeforces problems define coordinates as (x, y) with x rows, y columns, but left/right is based on column. But here they defined differently. Let's double-check the statement:
"The storehouse is represented as matrix n × m. Every sofa takes two neighbouring by some side cells."
Then: "Sofa A is standing to the left of sofa B if there exist two such cells a and b that xa < xb, a is covered by A and b is covered by B. Sofa A is standing to the top of sofa B if there exist two such cells a and b that ya < yb, a is covered by A and b is covered by B."
So indeed left/right uses x, top/bottom uses y.
Thus: left means smaller x (higher row number? Actually x coordinate: smaller x means more "left"? Maybe they think of x axis horizontal? Actually typical coordinate: (x, y) with x horizontal, y vertical. In matrix terms, we often use rows as first index (vertical) but they may have swapped. Let's see example 2 again: For sofa 1, they said "0 to its left, 2 sofas to its right ((1,1) is to the left of both (5,5) and (5,4))". Since (1,1) has x=1, (5,5) has x=5, so 1<5 means sofa 1 is left of sofa 2? Wait: definition: A is to left of B if there exist cells a in A, b in B with xa < xb. So if we find such cells, then A is left of B. In this case, we have a from sofa1 with x=1, b from sofa2 with x=5 => xa < xb => sofa1 is left of sofa2. So sofa1 left of sofa2. That gives a left relation from sofa1 to sofa2. But we are counting how many sofas are to the left of Grandpa's sofa? Actually conditions: "there are cntl sofas to the left of Grandpa Maks's sofa". That means we consider other sofas B such that B is to the left of Grandpa's sofa? Or Grandpa's sofa is left of them? Let's read: "The note also stated that there are cntl sofas to the left of Grandpa Maks's sofa, cntr — to the right, cntt — to the top and cntb — to the bottom." So for a given candidate sofa S, we need to count the number of other sofas A such that A is to the left of S. But careful: "to the left of S" means those sofas that are situated to the left of S. If we say "sofa A is standing to the left of sofa B", then A is left of B. So "sofas to the left of S" are those A such that A is left of S. That matches: A is left of S if there exist cells a in A, s in S with xa < xs. So we count those A.
Now in example 2, they computed for sofa1: 0 left, 2 right, 0 top, 2 bottom. Let's verify.
For sofa1, cells: (1,1) and (1,2). xs values are both 1. For left count: we need other sofas A such that A is left of sofa1. That is, there exist a in A, s in sofa1 with xa < xs = 1. Since xa are positive integers, xa < 1 is impossible. So 0 left. That matches.
Right count: sofas B such that B is to the right of sofa1? Actually cntr: sofas to the right of sofa1. "to the right" definition: likely symmetric: A is to the right of B if there exist a in A, b in B with xa > xb? But the statement only defines left and top explicitly. It says: "Right and bottom conditions are declared the same way." So right: A is to the right of B if there exist a in A, b in B with xa > xb. Similarly bottom: ya > yb. So for sofa1, to count sofas to the right, we need sofas B such that B is to the right of sofa1, i.e., there exist cells with xb > xs. Since xs=1, any sofa with a cell having x>1 qualifies? But careful: we need B such that B is right of sofa1, i.e., there exist b in B, s in sofa1 with xb > xs. That condition is easy for sofas 2 and 3 (they have cells with x=5,6). So that gives 2 right. That matches cntr=2? They reported 2 sofas to the right, yes.
Top count: cntt sofas to the top of sofa1. Top: A is top of B if ya < yb? Actually top uses y: A is top of B if there exist a in A, b in B with ya < yb. So to count sofas to the top of sofa1, we need A such that A is top of sofa1: ya < y of some cell of sofa1. Sofa1 cells have y=1 and 2. So ya < 1? No. So 0 top. That matches.
Bottom count: sofas to the bottom of sofa1: A is bottom of B if ya > yb? So we need A such that there exists a in A with ya > y of some cell of sofa1. For sofa2 and sofa3, y values are 5 and 4 etc. Both have ya > 2? Actually sofa2: cells (5,5) and (6,5) => y=5. sofa3: (6,4) and (5,4) => y=4. Both have y >=4 > 2, so they are bottom of sofa1. So 2 bottom. That matches.
Thus our interpretation is correct: left/right based on x coordinate; top/bottom based on y coordinate.
Now in example 2, they said second sofa matches cntl=2, cntr=1, cntt=2, cntb=0. Let's verify.
Sofa2: coordinates: (5,5) and (6,5). So x values: 5 and 6; y values: 5 (both). We need to count:
-
Left: sofas A such that A is left of sofa2: there exist a in A, s in sofa2 with xa < xs. xs can be 5 or 6. For any sofa with a cell x<5, they are left. Which sofas? Sofa1 has x=1, so qualifies. Sofa3: cells (6,4) and (5,4) => x=5 and 6. Does sofa3 have any cell with xa < 5? It has x=5 (equal) and 6 (greater). Not less than 5. So only sofa1 qualifies? That would be 1 left, but cntl=2. But they said sofa2 has cntl=2. Let's double-check: Possibly we miscount because relation may be two-way: For sofa2, there could be sofa3 also left? Let's see: need a cell a from sofa3 with xa < xb for some cell b in sofa2. sofa3 has x=5 and 6; sofa2 has x=5 and 6. Pair (xa=5, xb=5): not less. (5,6): 5<6? Yes! xa=5 (from sofa3) < xb=6 (from sofa2). So sofa3 also qualifies because there exists a= (5,4) from sofa3 and b=(6,5) from sofa2? But are those cells allowed? The condition does not require they be distinct? They are distinct sofas, cells can be any. So indeed, we can pick a=(5,4) from sofa3 (x=5) and b=(6,5) from sofa2 (x=6): 5<6, so sofa3 is left of sofa2. So both sofas 1 and 3 are left of sofa2. That yields cntl=2. Good.
-
Right: sofas A such that A is right of sofa2: need xa > xb for some cells. For sofa2, x values: 5,6. Which sofas have a cell >5 or >6? Sofa1 has x=1, no. Sofa3 has x=5,6. For sofa3 to be right of sofa2, need some a from sofa3, b from sofa2 with xa > xb. Check (xa=6 from sofa3, xb=5 from sofa2): 6>5 => yes. So sofa3 is right of sofa2? But then sofa3 is both left and right of sofa2? Possibly, as statement says: "some sofa A can be both to the top of another sofa B and to the bottom of it. The same is for left and right conditions." So it's possible. So sofa3 qualifies as right. That gives one sofa (sofa3) right of sofa2. sofa1 does not. So cntr=1. Matches.
-
Top: sofas A such that A is top of sofa2: need ya < yb for some cells. Sofa2 y=5. Sofa1 y values 1,2. Both <5 => qualifies. Sofa3 y=4. 4<5 => qualifies. So both are top: cntt=2.
-
Bottom: sofas A such that A is bottom of sofa2: need ya > yb. yb=5. Any sofa with ya>5? Sofa1: max y=2, no. Sofa3: y=4, no. So none: cntb=0. Matches.
Thus our interpretation consistent.
Now we need to find the sofa index that matches given counts cntl, cntr, cntt, cntb.
Constraints: d up to 1e5, n,m up to 1e5. So we need O(d log d) or similar. Not too heavy.
We need for each sofa i compute:
- leftCount(i) = number of sofas j (j ≠ i) such that there exists cell of j with x < some cell of i (i.e., min_x of i? Actually condition: exists cell a in j, b in i with xa < xb. That is equivalent to (min x of j) < (max x of i)? Not exactly; because we need existence of a pair: x_j_some < x_i_some. This is equivalent to: min x among cells of j < max x among cells of i. Because if min_x_j < max_x_i, then you can pick a with that min_x_j (or any ≤) and b with max_x_i, then condition holds. Conversely, if there exists some a, b with xa < xb, then min_x_j ≤ xa < xb ≤ max_x_i, so min_x_j < max_x_i. So condition reduces to: min_x_j < max_x_i. Similarly, j is right of i (i.e., j to the right of i) if exists a in j, b in i with xa > xb => min_x_i? Let's derive: j is right of i means exists a∈j, b∈i: xa > xb. Equivalent to: max_x_j > min_x_i? Because if max_x_j > min_x_i, then pick a with max_x_j and b with min_x_i, condition holds. Conversely, if there exists such pair, then max_x_j ≥ xa > xb ≥ min_x_i, so max_x_j > min_x_i. So condition: max_x_j > min_x_i.
But careful: The relation "j is to the left of i" means j left of i => exists a∈j, b∈i: xa < xb. That's min_x_j < max_x_i. And "j is to the right of i" means j right of i => exists a∈j, b∈i: xa > xb => max_x_j > min_x_i.
But note: For a given pair (i,j), j can be both left and right of i if min_x_j < max_x_i AND max_x_j > min_x_i. That's possible if their x ranges overlap and are not identical? Actually if ranges overlap, then both inequalities hold: min_x_j ≤ max_x_j and min_x_i ≤ max_x_i. If they overlap, then min_x_j ≤ max_x_i and max_x_j ≥ min_x_i, so both conditions can be true unless one range is completely to left (max_x_j <= min_x_i) or completely to right (min_x_j >= max_x_i). In case of complete left: max_x_j < min_x_i => then j left of i? Actually if max_x_j < min_x_i, then min_x_j ≤ max_x_j < min_x_i ≤ max_x_i, so min_x_j < max_x_i always holds? Let's test: j range [1,2], i range [3,4]. Then min_x_j=1 < max_x_i=4 => j left of i holds. But j right of i would require max_x_j > min_x_i: max_x_j=2, min_x_i=3 => 2>3 false. So j is left only. If j range [5,6], i range [3,4]: min_x_j=5, max_x_i=4 => min_x_j < max_x_i? 5<4 false, so j is not left; max_x_j=6 > min_x_i=3 true => j is right only. If ranges overlap, say j [2,5], i [3,4]: min_x_j=2 < max_x_i=4 true => left; max_x_j=5 > min_x_i=3 true => right. So both.
Similarly for top/bottom using y: top condition: j top of i if exists a∈j, b∈i: ya < yb => min_y_j < max_y_i. Bottom: j bottom of i if exists a∈j, b∈i: ya > yb => max_y_j > min_y_i.
Thus counts can be computed from min/max x and y of each sofa.
However, there is nuance: The existence condition uses "some cell of A and some cell of B". Since each sofa has exactly two cells, min and max are just the two values (if the two cells are adjacent, they could be equal only if they share the same coordinate? But they are distinct cells, so coordinates differ in one dimension; for a horizontal sofa, x same, y differ; for vertical sofa, y same, x differ. So min_x and max_x may be equal if the sofa is horizontal (both cells same x). In that case, min_x = max_x. Similarly min_y = max_y for vertical sofas. So we can compute these.
Thus for sofa i, define:
- Lx[i] = min x among its two cells.
- Rx[i] = max x among its two cells. (Since only two cells, Rx = max, Lx = min)
- Ly[i] = min y.
- Ry[i] = max y.
Then:
- Count of sofas j (j≠i) to the left of i: # { j ≠ i : Lx[j] < Rx[i] }.
- Count to the right: # { j ≠ i : Rx[j] > Lx[i] }.
- Count to the top: # { j ≠ i : Ly[j] < Ry[i] }.
- Count to the bottom: # { j ≠ i : Ry[j] > Ly[i] }.
But careful: For left count, condition is min_x_j < max_x_i. However, does this exactly match existence? Yes.
But we must check if any j can be counted multiple times? No, it's per sofa.
Now we are given cntl, cntr, cntt, cntb. We need to find i such that:
- leftCount(i) == cntl
- rightCount(i) == cntr
- topCount(i) == cntt
- bottomCount(i) == cntb
It is guaranteed that there is no more than one such sofa. If none, output -1.
Now naive O(d^2) is too slow. We need to compute these counts efficiently.
Observation: The counts for each i can be computed by sorting and using prefix sums, as they are based on comparisons of Lx and Rx, etc. Since d up to 1e5, we can precompute arrays.
Let's denote for each sofa i:
- Lx[i], Rx[i], Ly[i], Ry[i].
We need for each i:
- leftCount(i) = number of j (j≠i) with Lx[j] < Rx[i].
- rightCount(i) = number of j (j≠i) with Rx[j] > Lx[i].
- topCount(i) = number of j (j≠i) with Ly[j] < Ry[i].
- bottomCount(i) = number of j (j≠i) with Ry[j] > Ly[i].
But these counts can be computed by sorting the Lx, Rx, Ly, Ry across all sofas, then using binary search. However, note that for each i, we need to exclude i itself from counts. But since d is large, the effect of excluding self is negligible unless there is possibility of self-comparison? Obviously self shouldn't be counted, but we can compute total count including all sofas and then subtract 1 if i itself would satisfy the condition. However, the condition for self: does Lx[i] < Rx[i]? For any sofa, Lx[i] ≤ Rx[i]; if Lx[i] < Rx[i] (i.e., the sofa is vertical or maybe if cells have different x?), then it would be counted in the total number of sofas with Lx < Rx[i] when including all sofas. Similarly for other counts. So we need to subtract 1 if the sofa i satisfies the condition itself. So we must compute:
leftCount(i) = total number of sofas j such that Lx[j] < Rx[i] minus (1 if Lx[i] < Rx[i] else 0? Actually if Lx[i] < Rx[i] then sofa i satisfies its own condition because Lx[i] < Rx[i] is true. But wait, condition is Lx[j] < Rx[i] for j = i; if we include j=i, does it satisfy? Yes if Lx[i] < Rx[i]. If Lx[i] == Rx[i] (sofa horizontal), then Lx[i] == Rx[i], so Lx[i] < Rx[i] is false. So we subtract 1 if Lx[i] < Rx[i]. But careful: what if the sofa is vertical? Then Lx[i] < Rx[i] (since x coordinates differ by 1). So yes.
Similarly for rightCount: condition Rx[j] > Lx[i]. For j=i: Rx[i] > Lx[i] if they differ (vertical); if horizontal, Rx[i] = Lx[i] so not >. So subtract 1 if Rx[i] > Lx[i] i.e., if Lx[i] < Rx[i] (same condition). So the same: for vertical sofas, both left and right counts need to subtract 1 because i itself would be counted if we include all.
For topCount: condition Ly[j] < Ry[i]. For j=i: Ly[i] < Ry[i] if they differ (horizontal sofa: y coordinates differ; vertical sofa: y same). So if sofa is horizontal, Ly[i] < Ry[i] (since y differ by 1). So subtract 1 if Ly[i] < Ry[i].
For bottomCount: condition Ry[j] > Ly[i]. For j=i: Ry[i] > Ly[i] if they differ; same condition. So subtract 1 if Ly[i] < Ry[i].
Thus, to compute these counts quickly, we can:
- Sort all Lx values into an array, perhaps also record duplicates.
- For each i, compute leftCount(i) = number of Lx values strictly less than Rx[i]. Use binary search (e.g., upper_bound on sorted Lx). Then if Lx[i] < Rx[i], subtract 1.
But wait: the condition is Lx[j] < Rx[i]. That's straightforward. However, we need to be careful: The sofa i's own Lx is included in the sorted array. So when we do binary search on the entire list, we get count including possibly i itself. Then we subtract 1 if Lx[i] < Rx[i]. But is it always correct that i's own Lx is counted exactly when Lx[i] < Rx[i]? Yes because condition for j=i is Lx[i] < Rx[i]; if true, it's counted. So subtract 1 if true. That yields leftCount(i).
Similarly for rightCount: Condition Rx[j] > Lx[i]. So we can sort all Rx values. Count number of Rx > Lx[i] using binary search (e.g., number of Rx values > Lx[i] = total - number of Rx ≤ Lx[i]). Then subtract 1 if Rx[i] > Lx[i] (i.e., if Lx[i] < Rx[i])? Actually condition for j=i: Rx[i] > Lx[i]? If vertical, yes; if horizontal, no. So subtract 1 if true.
Similarly topCount: count of Ly < Ry[i] from sorted Ly; subtract 1 if Ly[i] < Ry[i].
BottomCount: count of Ry > Ly[i] from sorted Ry; subtract 1 if Ry[i] > Ly[i] (which is same as Ly[i] < Ry[i]).
That's O(d log d). Should be fine.
But we need to be careful about the possibility of multiple sofas with same Lx or Rx etc, but binary search handles that.
Now we just compute these counts for each i and compare with given cntl, cntr, cntt, cntb.
However, we must verify that these formulas are exactly what the problem defines. Let's double-check with edge cases.
Consider two sofas that have the same min and max x? For instance, two vertical sofas in same column but different rows that overlap? But they cannot overlap cells. But they could be adjacent? If they are in same column but separated, e.g., sofa A covers (1,1) and (2,1); sofa B covers (3,1) and (4,1). Then Lx(A)=1, Rx(A)=2; Lx(B)=3, Rx(B)=4. For A: leftCount? Lx[j] < Rx[A]=2. B's Lx=3 which is not <2, so leftCount=0 (excluding itself). But is there any j left of A? According to definition: j is left of A if exists cell with x_j < x_A. Since B's smallest x=3, A's largest x=2, we need x_j < x_A. For B, min x=3, A's max x=2, no cell of B has x less than any cell of A? Actually to have B left of A, we need a cell of B with xa < xb for some cell b in A. Since B's smallest x=3 and A's max x=2, 3<2 is false. So indeed B is not left of A. So leftCount 0. Good.
Now suppose two sofas overlap in x range? e.g., A vertical at column y=1: cells (1,1)-(2,1) => Lx=1,Rx=2. B horizontal at row x=2: cells (2,2)-(2,3) => Lx=2,Rx=2 (since x both 2). For A: leftCount: Lx[j] < Rx[A]=2? B's Lx=2, 2<2 false, so leftCount=0. But could B be left of A? Need xa < xb. B's cells: x=2; A's cells: x=1 and 2. Choose b=1 from A? Actually A has x=1 cell. To have B left of A, we need a in B, b in A such that xa < xb. B has x=2; A has x=1 and 2. For xb=1, 2<1 false; for xb=2, 2<2 false. So B is not left. Good.
Now if B's Lx=2, and Rx[A]=2, condition false, matches.
Now consider rightCount for A: count Rx[j] > Lx[A]=1. B's Rx=2 >1, so count 1 (excluding if self). But is B right of A? Need a in B, b in A with xa > xb. B's x=2; A's b could be 1; 2>1 true. So B is right of A. Good.
Now top/bottom similarly.
Thus the formulas hold.
Now we need to compute counts for each i. However, careful: The counts computed this way include all sofas j satisfying the condition regardless of orientation. But the definition uses existence of any cell a in A and b in B such that xa < xb. Since we derived condition min_x_j < max_x_i, this is necessary and sufficient. So correct.
Thus algorithm:
Read d, n, m (though n,m not needed for count? Maybe they are not used directly except to ensure coordinates are within, but we don't need them.)
Read d sofas, each with (x1,y1,x2,y2). For each sofa i:
- Determine Lx = min(x1,x2), Rx = max(x1,x2)
- Ly = min(y1,y2), Ry = max(y1,y2)
Store arrays: Lx[i], Rx[i], Ly[i], Ry[i].
Also store orientation flags: is_vertical = (x1 != x2) i.e., x differ => then Ly == Ry? Actually if vertical, y same, so Ly == Ry. For horizontal, x same, Lx == Rx.
But we only need to know for subtracting self: For left/right subtract if Lx[i] < Rx[i] (i.e., vertical). For top/bottom subtract if Ly[i] < Ry[i] (i.e., horizontal). So we can compute those boolean: subLR = (Lx[i] < Rx[i])? 1:0; subTB = (Ly[i] < Ry[i])? 1:0.
Now compute global sorted arrays:
- sorted_Lx = sort all Lx
- sorted_Rx = sort all Rx
- sorted_Ly = sort all Ly
- sorted_Ry = sort all Ry
We can precompute prefix counts maybe but binary search per i is O(log d) each, total O(d log d). d=1e5, so fine.
For each i:
-
leftCount = count of Lx < Rx[i] in sorted_Lx. Using binary search: index = upper_bound(sorted_Lx, Rx[i] - 1?) Actually we need number of elements strictly less than Rx[i]. So we can use lower_bound to find first element >= Rx[i], then count = position. In C++ we can use lower_bound(Lx.begin(), Lx.end(), Rx[i]) - Lx.begin() ; that gives number of Lx < Rx[i]. (Because lower_bound returns first not less than Rx[i], so all before are < Rx[i]).
-
If subLR[i] is true, then leftCount--.
-
rightCount = count of Rx > Lx[i]. That is total - number of Rx <= Lx[i]. number of Rx <= Lx[i] = upper_bound(sorted_Rx.begin(), sorted_Rx.end(), Lx[i]) - sorted_Rx.begin() . So rightCount = d - count_le. Then if subLR[i] true, rightCount--.
-
topCount = count of Ly < Ry[i]. Similar: lower_bound(sorted_Ly, Ry[i]) - sorted_Ly.begin(). If subTB[i] true, topCount--.
-
bottomCount = count of Ry > Ly[i]. So number of Ry > Ly[i] = d - (upper_bound(sorted_Ry, Ly[i]) - sorted_Ry.begin())? Actually need count of Ry > Ly[i]. Use upper_bound to find first element > Ly[i]? Or count of <= Ly[i] = upper_bound(sorted_Ry, Ly[i]) - begin (since upper_bound gives first > Ly[i], so the index is count of <=). So bottomCount = d - count_le. Then if subTB[i] true, bottomCount--.
Check: For bottom: condition Ry[j] > Ly[i]. So we need count of Ry > Ly[i]. So total d minus count of Ry <= Ly[i]. Count of Ry <= Ly[i] = upper_bound(sorted_Ry, Ly[i]) - begin. Yes.
But careful: upper_bound gives first element > value, so number of elements <= value is that iterator - begin. So that's correct.
Now after computing these, compare with given cntl,cntr,cntt,cntb. If match, output i. Guarantee at most one.
Edge Cases:
- Self subtraction: If the sofa is both vertical and horizontal? Impossible: a sofa covers two adjacent cells; they either share x (horizontal) or y (vertical). So either Lx == Rx (horizontal) or Ly == Ry (vertical). So for a horizontal sofa, subLR false (since Lx == Rx), subTB true (since Ly < Ry). For vertical sofa, subLR true, subTB false. So each sofa has exactly one of these true? Actually check: for a horizontal sofa, Lx = Rx, Ly < Ry. So subLR false, subTB true. For vertical sofa, Lx < Rx, Ly = Ry, so subLR true, subTB false. So exactly one of subLR and subTB is true? Yes because adjacent cells differ in exactly one coordinate. So they are either horizontally adjacent (same x, different y) or vertically adjacent (different x, same y). So each sofa is either horizontal or vertical. So they cannot be both.
But what about diagonal? No, adjacent by side, so only those two.
Thus subtraction for left/right only for vertical sofas; for top/bottom only for horizontal sofas.
Now one more nuance: The condition for left count uses Lx[j] < Rx[i]. But what about j where Lx[j] is exactly equal to Rx[i]? Then not counted. That's correct because need strict <. Similarly right uses >, not >=.
Now, verify with example 1:
Input:
2
3 2
3 1 3 2 (sofa1: row3, columns1&2, so horizontal? Actually x=3,3; y=1,2 => horizontal)
1 2 2 2 (sofa2: (1,2) and (2,2): x differ: vertical? Actually x1=1,y1=2; x2=2,y2=2 => vertical)
1 0 0 1 (cntl=1,cntr=0,cntt=0,cntb=1)
Let's compute manually:
Sofas:
1: Lx=3,Rx=3; Ly=1,Ry=2. subLR false (horizontal), subTB true.
2: Lx=1,Rx=2; Ly=2,Ry=2. subLR true, subTB false.
Compute global arrays:
Lx: [3,1] sorted = [1,3]
Rx: [3,2] sorted = [2,3]
Ly: [1,2] sorted = [1,2]
Ry: [2,2] sorted = [2,2]
Now for sofa1:
leftCount: count Lx < Rx[1]=3 => elements <3 in Lx sorted: [1] => count=1. subLR false => leftCount=1. (matches cntl=1)
rightCount: count Rx > Lx[1]=3? Lx[1]=3. Count Rx >3: sorted_Rx=[2,3]; >3 => none => 0. subLR false => 0. cntr=0 matches.
topCount: count Ly < Ry[1]=2 => Ly<2: [1] => count=1. subTB true => subtract 1 => 0. cntt=0 matches.
bottomCount: count Ry > Ly[1]=1? Ry>1: sorted_Ry=[2,2]; >1 => both? Actually count >1: 2 >1 => two? But need count of Ry > Ly[1]=1. Ry sorted: both are 2, both >1 => count=2. Then subTB true => subtract 1 => 1. But cntb=1 matches? In example, they said cntb=1? The input gives cntb=1? Actually last line "1 0 0 1": cntl=1, cntr=0, cntt=0, cntb=1. Yes cntb=1. So bottomCount=1 matches. But is that correct? Let's manually check: sofa1 bottom? Sofas to bottom of sofa1: need j such that exists cell a in j, b in sofa1 with ya > yb. sofa1's y values: 1 and 2. sofa2's y=2. For bottom: ya > yb. For b=1, ya=2 >1 => qualifies. For b=2, ya=2 >2? false. So sofa2 qualifies. So cntb=1. Good.
Now for sofa2:
leftCount: Lx < Rx[2]=2 => Lx<2: sorted_Lx=[1,3]; <2: [1] => 1. subLR true => subtract 1 => 0. cntl for sofa2? But we are not checking; we are only looking for sofa that matches given conditions (1,0,0,1). Sofa2 would have leftCount=0? Actually we can compute: leftCount(sofa2) = 0 (since only sofa1's Lx=1 <2, but subtract self because subLR true? But need to check if self qualifies: Lx[2]=1 < Rx[2]=2 true, so subtract. So leftCount=0). So sofa2 not match.
Thus algorithm would output 1.
Now example 2 we already verified.
Now example 3:
Input:
2
2 2
2 1 1 1 (sofa1: (2,1) and (1,1) => vertical? x differ: 2 and 1, y both 1 => vertical)
1 2 2 2 (sofa2: (1,2) and (2,2) => vertical? Actually x differ: 1,2; y both 2 => vertical)
1 0 0 0 (cntl=1,cntr=0,cntt=0,cntb=0)
Compute:
Sofa1: Lx=1,Rx=2; Ly=1,Ry=1. subLR true, subTB false.
Sofa2: Lx=1,Rx=2; Ly=2,Ry=2. subLR true, subTB false.
Global:
Lx: [1,1] sorted = [1,1]
Rx: [2,2] sorted = [2,2]
Ly: [1,2] sorted = [1,2]
Ry: [1,2] sorted = [1,2]
Now for sofa1:
leftCount: count Lx < Rx[1]=2 => all Lx <2? Both 1 <2 => count=2. subLR true => subtract 1 => leftCount=1. cntl=1 matches.
rightCount: count Rx > Lx[1]=1 => Rx >1: both 2 >1 => count=2. subLR true => subtract 1 => rightCount=1. But cntr=0, so no match.
topCount: count Ly < Ry[1]=1 => Ly<1: none => 0. subTB false => 0. cntt=0 matches.
bottomCount: count Ry > Ly[1]=1 => Ry>1: Ry values: 1,2 >1? 1>1 false, 2>1 true => count=1. subTB false => 1. cntb=0 not match.
Thus not match.
For sofa2:
leftCount: Lx < Rx[2]=2 => 2 count? Lx all 1<2 => 2. subLR true => 1. cntl=1 okay.
rightCount: Rx > Lx[2]=1 => both 2>1 =>2. subLR true=>1. cntr=0 fails.
So no match -> -1.
Thus algorithm works.
Now we need to implement.
But we must be careful: The condition definitions may have subtlety: "Sofa A is standing to the left of sofa B if there exist two such cells a and b that xa < xb, a is covered by A and b is covered by B." That is exactly our condition. But note that we derived min_x_j < max_x_i for j left of i. However, what if j's min_x is not less than max_x_i but there exists some cell of j with x less than some cell of i? Since min_x_j is the minimum x of j, if there exists any cell a with xa < xb (some b in i), then min_x_j ≤ xa < xb ≤ max_x_i, so min_x_j < max_x_i. So the condition is equivalent. So good.
Now we need to consider if there is any other nuance: The problem statement says "There can be empty cells." That doesn't affect.
Now about handling duplicates: binary searches are fine.
But we need to ensure we treat self subtraction correctly: We subtract 1 only if the sofa itself satisfies the condition based on its own min/max. For leftCount, condition is Lx[i] < Rx[i]. For vertical sofas, true; horizontal false. For rightCount, condition is Rx[i] > Lx[i], same. So we can use flag V (isVertical) to subtract for left and right. Similarly for top/bottom we subtract if horizontal.
But note: what about sofa that is both vertical and horizontal? Not possible.
Thus we can compute.
Now, after we compute counts for each sofa and find matching index, output it or -1.
Complexities: O(d log d). d up to 1e5, okay.
Edge Cases: d=1. Then we have only one sofa. Then cntl, cntr, cntt, cntb must be 0 (since max d-1 = 0). Compute counts: For that sofa, leftCount = number of Lx < Rx[i] among all sofas = count of Lx < Rx[i] (including itself). Then subtract self if condition true. So final leftCount = count_self? For d=1, sorted_Lx has one element, Lx[1] maybe < Rx[1]? If vertical, Lx<Rx, then count Lx < Rx = 1 (since Lx[1] < Rx[1]), subtract 1 => 0. If horizontal, Lx == Rx, then count Lx < Rx = 0, subtract 0 => 0. So leftCount = 0. Similarly others. So if all cnt are 0, output 1 else -1.
Now verify: Is it possible that the sofa's self condition counts if there is duplicate? No duplicate because one sofa.
Now we must be careful: The counts we compute using global sorted arrays include all sofas. But is there any sofa j that may be counted in leftCount(i) even though the condition fails due to j = i? We subtract correctly. However, what about j ≠ i that might be counted but doesn't actually satisfy existence because of some subtlety? Our derivation suggests that Lx[j] < Rx[i] is necessary and sufficient. But let's double-check with a scenario where j's min_x is less than i's max_x, but there is no cell a in j and b in i such that xa < xb? For that to happen, it would require that every cell of j has x >= every cell of i? But if min_x_j < max_x_i, then min_x_j is ≤ some cell of j, and max_x_i is ≥ some cell of i. If min_x_j < max_x_i, then consider a = the cell of j that gives min_x_j (it might be the only cell with that x, but it could be that min_x_j is from one cell; and b = the cell of i that gives max_x_i. Then xa = min_x_j < max_x_i = xb. So condition holds. So indeed existence is guaranteed.
Now what about top? Ly[j] < Ry[i] similar.
Thus correct.
Now we just need to implement.
But wait, there is a nuance: The definition uses "xa < xb". However, what if the two cells are the same? That can't happen because cells are distinct. So okay.
Now algorithm steps:
-
Read d.
-
Read n, m (unused except maybe for bounds? Not needed).
-
For i from 1 to d:
- read x1,y1,x2,y2.
- Lx[i] = min(x1,x2)
- Rx[i] = max(x1,x2)
- Ly[i] = min(y1,y2)
- Ry[i] = max(y1,y2)
- vertical[i] = (x1 != x2) // i.e., Lx < Rx
- horizontal[i] = (y1 != y2) // i.e., Ly < Ry. Actually either vertical or horizontal. We'll just set subLR = vertical[i], subTB = horizontal[i].
But note: For a sofa, exactly one of vertical/horizontal is true. We'll compute both.
-
Read cntl,cntr,cntt,cntb.
-
Create sorted copies:
- Lx_sorted = sorted(Lx)
- Rx_sorted = sorted(Rx)
- Ly_sorted = sorted(Ly)
- Ry_sorted = sorted(Ry)
-
For each i:
- left = lower_bound(Lx_sorted, Rx[i]) - Lx_sorted.begin() (count Lx < Rx[i]).
- if vertical[i] then left -= 1.
- right = d - (upper_bound(Rx_sorted, Lx[i]) - Rx_sorted.begin()) // count Rx > Lx[i]
- if vertical[i] then right -= 1.
- top = lower_bound(Ly_sorted, Ry[i]) - Ly_sorted.begin()
- if horizontal[i] then top -= 1.
- bottom = d - (upper_bound(Ry_sorted, Ly[i]) - Ry_sorted.begin())
- if horizontal[i] then bottom -= 1.
- compare with cntl,cntr,cntt,cntb. If all match, store answer.
-
Output answer if found else -1.
But we must be careful: For right count using upper_bound: we want number of Rx <= Lx[i]. Since Rx can equal Lx[i]; condition is >, so equal does not count. So Rx <= Lx[i] includes equals. So using upper_bound gives first > Lx[i], so number of <= is index of that iterator. So correct.
Similarly bottom count: number of Ry <= Ly[i] = upper_bound(Ry_sorted, Ly[i]) - begin. Then bottom = d - that count.
Now we need to handle potential off-by-one due to 0-indexing. In code, we can just use C++'s lower_bound and upper_bound.
Now complexity O(d log d). Good.
But before finalizing, let's test on some custom cases.
Case: d=2, both vertical overlapping x ranges? But they can't share cells. For example: sofa1: (1,1)-(2,1) vertical; sofa2: (2,2)-(3,2) vertical? They are not overlapping cells, but x ranges: sofa1 Lx=1,Rx=2; sofa2 Lx=2,Rx=3. Compute leftCount for sofa1: Lx < Rx[1]=2 => sofa2 Lx=2 not <2, so leftCount=0? But check definition: is sofa2 left of sofa1? Need xa < xb. sofa2 has min x=2, sofa1 has max x=2. Is there a pair with xa < xb? sofa2 x=2 and sofa1 x=2: 2<2 false; sofa2 x=3 and sofa1 x=1? Wait a in sofa2, b in sofa1: we need xa < xb. The best chance: pick a from sofa2 with smallest x (2) and b from sofa1 with largest x (2): 2<2 false. So sofa2 not left. So leftCount=0 correct. For rightCount: sofa1: Rx > Lx[1]=1 => sofa2 Rx=3 >1, count 1. Check manually: sofa2 right of sofa1? Need xa > xb. Pick a from sofa2 with x=3, b from sofa1 with x=1 => 3>1 true. So rightCount=1. Good.
Now consider both horizontal overlapping y ranges? symmetric.
Now consider mixture.
Seems fine.
Now about input: n,m up to 1e5, but coordinates may be large but fine.
Now about memory: store arrays of size d.
Now we must ensure to use 0-index or 1-index. For output we need sofa number (1-indexed).
Now think about possibility of more than one sofa matching? Guarantee at most one. So we can break after first? But we may want to check all to be sure if there is none.
Now double-check derivation with more complex cases: For a vertical sofa, it has same y, so Ly=Ry. Thus for topCount: condition Ly[j] < Ry[i]? Since Ry[i] = Ly[i] (vertical), so we need Ly[j] < Ly[i]. That's basically compare y positions. For j horizontal, its Ly may be less. For other vertical, they have same Ly=Ry, so they would be considered top only if Ly[j] < Ly[i] (since Ry[i] = Ly[i]). That's consistent.
Now what if a sofa is both left and right? Already considered.
Now we should also consider that the problem's guarantee: "It is guaranteed that there is no more than one sofa of given conditions." Means either 0 or 1. So we don't have to worry about multiple.
Now I think algorithm is correct.
But before finalizing, we must consider if there is any hidden nuance: The definition of "to the left" is based on existence of a pair of cells a in A, b in B with xa < xb. Could there be a scenario where Lx[j] < Rx[i] but no such pair because the cells are not arbitrary pairs? But we already argued that picking the cell achieving min_x_j and the cell achieving max_x_i gives xa < xb. However, is it guaranteed that those specific cells belong to distinct sofas? Yes, j != i, they are distinct sofas. But is it possible that the cell with min_x_j is the same as the cell with max_x_i? No, because they are from different sofas, and sofas do not share cells. So it's fine.
Now is there any subtlety: The existence condition requires "two such cells a and b that xa < xb". That is satisfied by that pair. So it's fine.
Thus algorithm is solid.
Now we can implement.
But wait, in the problem statement, there is a note: "Note that in all conditions A ≠ B. Also some sofa A can be both to the top of another sofa B and to the bottom of it. The same is for left and right conditions." That is consistent.
Now check Example 2 they gave: Actually we computed earlier. Works.
Now ensure we treat input reading properly.
Implementation steps in code:
-
Use vector Lx(d), Rx(d), Ly(d), Ry(d), isVertical(d) or subLR(d), subTB(d).
-
For each i (0-index):
- read x1,y1,x2,y2.
- if x1 == x2: // horizontal
Lx[i]=x1; Rx[i]=x1; // but we can still min/max.
Ly[i]=min(y1,y2); Ry[i]=max(y1,y2);
subLR[i]=false; subTB[i]=true;
else if y1 == y2: // vertical
Lx[i]=min(x1,x2); Rx[i]=max(x1,x2);
Ly[i]=y1; Ry[i]=y1;
subLR[i]=true; subTB[i]=false;
But we can just compute min/max for both coordinates anyway. Then subLR = (Lx[i] < Rx[i]), subTB = (Ly[i] < Ry[i]). Because if horizontal, Lx=Rx, Ly<Ry; if vertical, Lx<Rx, Ly=Ry. So we can compute directly without checking orientation.
-
Then read cntl,cntr,cntt,cntb.
-
Create sorted vectors: sLx = Lx, sRx = Rx, sLy = Ly, sRy = Ry; sort each.
-
Precompute maybe nothing else.
-
Loop i from 0 to d-1:
left = lower_bound(sLx.begin(), sLx.end(), Rx[i]) - sLx.begin(); // counts < Rx[i]
if(subLR[i]) left--;
int leRx = upper_bound(sRx.begin(), sRx.end(), Lx[i]) - sRx.begin(); // count <= Lx[i]
right = d - leRx;
if(subLR[i]) right--;
top = lower_bound(sLy.begin(), sLy.end(), Ry[i]) - sLy.begin();
if(subTB[i]) top--;
int leRy = upper_bound(sRy.begin(), sRy.end(), Ly[i]) - sRy.begin();
bottom = d - leRy;
if(subTB[i]) bottom--;
if(left == cntl && right == cntr && top == cntt && bottom == cntb) {
ans = i+1; break;
}
-
Print ans or -1.
Now, is there any possibility of integer overflow? No.
Now we should test with provided examples.
We'll simulate in mind or code later.
But there might be a nuance: The sorted arrays include all sofas, but for leftCount we used lower_bound(Lx, Rx[i]) which counts all Lx < Rx[i]. However, if there are multiple sofas with Lx exactly equal to Rx[i], they are not counted. Good.
Now for rightCount: we used d - (upper_bound(Rx, Lx[i])). But what about sofas where Rx exactly equals Lx[i]? They are not >, so they shouldn't be counted. The count <= includes those equal, and we subtract from d, giving count of >. Good.
Now for bottom: similar.
Now for top: lower_bound(Ly, Ry[i]) counts Ly < Ry[i] (strict). Good.
Now subtract self only when self qualifies. However, what if self qualifies but the self subtraction may cause negative count? That would only happen if the sofa itself is counted in the total count but we subtract even though it shouldn't be? But self is counted only if condition true. For example, for a vertical sofa, leftCount: total number of Lx < Rx[i] includes itself because Lx[i] < Rx[i] is true. So total count >= 1. Subtract 1 yields non-negative. Similarly, for a horizontal sofa, leftCount total may not include itself (since Lx=Rx, so Lx < Rx false). So subtract 0. So fine.
Now corner case: If d=0? d >= 1 (1 ≤ d).
Now, one more nuance: The problem states: "It is guaranteed that there is no more than one sofa of given conditions." But it does not guarantee that there is at least one. So we output -1 if none.
Now, are there any cases where our computed counts may be different from actual because the existence condition requires "two such cells a and b that xa < xb" but we assume we can always take the min_x of j and max_x of i. However, what if the cell that gives min_x for j is not adjacent? It doesn't matter. So ok.
Now, we should verify that our derived conditions are both necessary and sufficient. Already did.
Thus final solution is straightforward.
Now, might there be an alternative way? No.
Now, check for potential pitfalls: The input may be large; use fast I/O.
Now, test with more random cases to ensure consistency. Let's write a small Python mental test? Not needed.
Now, think about orientation: The coordinates are given as (x1,y1) and (x2,y2). They are adjacent by side. So either x1==x2 or y1==y2. So we can trust that.
Now, we also note that n and m are not needed. However, they might be used to check boundaries but we don't need.
Now, check Example 2's input format: It says:
3
10 10
1 2 1 1
5 5 6 5
6 4 5 4
2 1 2 0
Wait, the last line "2 1 2 0" corresponds to cntl,cntr,cntt,cntb? In the problem statement, they said the last line contains four integers cntl, cntr, cntt, cntb. So in example 2, it's "2 1 2 0". That matches.
But note: In the problem statement text, they wrote: "2 1 2 0" under input? Actually the given input in the problem statement is:
3
10 10
1 2 1 1
5 5 6 5
6 4 5 4
2 1 2 0
So yes.
Now, double-check: sofa 3 coordinates are (6,4) and (5,4). That's fine.
Now, answer is 2.
Now, we should also test a scenario where there are many sofas with same Lx etc.
Now, could there be a sofa where Lx[i] == Rx[i] and Ly[i] == Ry[i]? That would mean both cells same? Not possible because cells are distinct.
Now, we must also consider that for a horizontal sofa, subTB true, but does it affect top and bottom counts? For topCount: condition Ly[j] < Ry[i] includes i itself if Ly[i] < Ry[i] true (horizontal). So subtract 1. For bottomCount: condition Ry[j] > Ly[i] includes i if Ry[i] > Ly[i] true, so subtract 1. So we need to subtract 1 for both top and bottom for horizontal sofas. That matches our plan.
Now, is there any situation where a sofa could be both vertical and horizontal? No.
Now, the code is ready.
But before finalizing, we need to confirm that the derived condition for rightCount: count of Rx > Lx[i] is indeed the number of sofas j that are to the right of i. We used condition: j right of i if max_x_j > min_x_i. Is that correct? Let's prove: j right of i if exists a in j, b in i such that xa > xb. Choose a with max_x_j and b with min_x_i. If max_x_j > min_x_i, then xa = max_x_j > min_x_i = xb. So condition holds. Conversely, if there exists some a,b with xa > xb, then max_x_j ≥ xa > xb ≥ min_x_i, so max_x_j > min_x_i. So equivalent. So count of j such that max_x_j > min_x_i.
Now we need to be careful: For the condition max_x_j > min_x_i, we need strict >. So count of Rx > Lx[i]. Good.
Now, is there any scenario where j has max_x_j > min_x_i but the specific cell achieving max_x_j and the cell achieving min_x_i are from the same sofa and we need a pair from different sofas? They are from different sofas, so fine.
Now, one more nuance: The sofa i might have min_x_i and max_x_i. For j right of i, we require existence of a in j with xa > xb for some b in i. Could it be that j has max_x_j > min_x_i, but the only b in i that is less than that a is not the min_x_i? But since a > min_x_i, we can pick b = any cell of i with x = min_x_i (since min_x_i is the smallest x among i's cells). Then xa > min_x_i. So condition holds. So it's fine.
Now, we must also consider that the definitions are symmetric: left/right based on x, top/bottom based on y. So our approach is correct.
Now, check the sample output: matches.
Now, edge test: d=1, vertical. Lx=1,Rx=2, Ly=1,Ry=1. cntl=0,cntr=0,cntt=0,cntb=0.
Compute: Lx_sorted=[1], Rx_sorted=[2], Ly_sorted=[1], Ry_sorted=[1].
For i=0:
left = lower_bound(Lx, 2) = index of first >=2? Since 1 <2, lower_bound returns end (1), so count =1? Actually lower_bound returns iterator to first element not less than 2. Since 1 <2, the first element is 1? Actually sorted list [1]. lower_bound(2) would return iterator pointing to end() because no element >=2. So count = begin to end = 1? Wait: lower_bound returns end, so distance from begin to end is 1 (since size=1). That means count of elements <2 is 1 (the element 1). Yes. So left=1. Then subLR true -> left=0.
right: leRx = upper_bound(Rx, Lx[i]=1) -> Rx sorted [2]; upper_bound(1) returns iterator to first element >1, which is 2 (position 0? Actually index 0). So leRx = 1? Let's compute: upper_bound returns iterator to first >1, that's the first element (value 2) at index 0? Actually begin+0 is the first element, so the iterator is at position 0? No, the iterator is an address. The distance from begin to that iterator is 0? Wait: In C++, vector v = {2}; upper_bound(v.begin(), v.end(), 1) returns v.begin() because 2 > 1, so it points to the first element. So v.begin() is the start, so the difference (iterator - v.begin()) = 0. That would indicate count of elements <=1 is 0. But we want count of Rx <=1. Since Rx is 2, none. So leRx=0. That's correct. So leRx = 0. Then right = d - leRx = 1 - 0 = 1. Then subtract 1 (subLR) => 0.
top: lower_bound(Ly, Ry[i]=1) -> Ly sorted [1]; lower_bound returns iterator to first >=1, which is the first element (value 1). So count = index 0 => 0 elements <1. So top=0. subTB false => top=0.
bottom: leRy = upper_bound(Ry, Ly[i]=1) -> Ry sorted [1]; upper_bound(1) returns iterator to first >1, which is end(). So leRy = distance(begin, end)=1. So bottom = d - leRy = 1-1=0. subTB false => 0.
All zeros. So matches.
Now d=1, horizontal: Lx=Rx=1, Ly=1,Ry=2. cnt all zero.
Compute:
left = lower_bound(Lx, Rx=1) => Lx sorted [1]; lower_bound(1) returns first >=1 => iterator to first element. Count = distance(begin, it) = 0 (since it points to first element, index 0). So left=0. subLR false -> left=0.
right: leRx = upper_bound(Rx, Lx=1) -> Rx sorted [1]; upper_bound(1) returns end, leRx = 1. right = 1-1=0. subLR false ->0.
top: lower_bound(Ly, Ry=2) -> Ly sorted [1]; lower_bound(2) returns end, count = 1. top=1. subTB true -> top=0.
bottom: leRy = upper_bound(Ry, Ly=1) -> Ry sorted [2]; upper_bound(1) returns iterator to first >1, that's the first element, index 0? Actually Ry [2]; upper_bound(1) returns iterator to first >1, which is the element 2 at position 0, so leRy = 0? Wait: We need to be careful: Ry sorted = [2]; upper_bound(1) -> since 2 > 1, it returns iterator to element 2, which is at index 0. So leRy = 0 (because distance from begin to that iterator = 0). That means count of Ry <= 1 is 0. So bottom = d - 0 = 1. subTB true -> bottom=0. So all zero. Good.
Now check correctness of top: lower_bound(Ly, Ry=2) gave end, distance = 1 => count of Ly < 2 is 1 (since Ly=1). So top=1 before subtraction. Since horizontal, subtract 1 => 0. Good.
Now bottom: upper_bound(Ry, Ly=1) gave iterator to 2 (index 0) because 2 > 1, so count of <=1 is 0. Then bottom = 1-0=1. Subtract 1 => 0. Good.
Thus works.
Now, let's think about the note: "It is guaranteed that there is no more than one sofa of given conditions." This means that if we compute counts using our method, we should get at most one match. So we can break early.
Now, one potential pitfall: The condition for left and right may double count the same sofa if it satisfies both? But we are counting per sofa, not pairs. That's fine.
Now, we must also ensure that our counting method respects the "A ≠ B" condition. By subtracting self when it would be counted, we ensure that.
Now, what about the possibility that j's condition might be satisfied by multiple pairs, but still count as one. That's fine.
Now, we can implement.
But wait: There is one more subtlety: The condition "xa < xb" uses coordinates of cells. However, a sofa occupies two cells. Could it be that Lx[j] < Rx[i] but the cell with Lx[j] is not the one we can use because maybe the cell with Rx[i] is the other cell of the same sofa? But we already argued we can pair them. However, is there any restriction that a and b must be from different sofas? Yes, A ≠ B. So that's fine.
Thus final answer.
Now, could there be a scenario where for leftCount, we have Lx[j] < Rx[i], but the cell with Lx[j] might be the same as the cell with Rx[i]? Not possible because different sofas cannot share cells.
Thus done.
Now, maybe there is a more efficient way but we don't need.
Now, let's implement in C++.
Implementation details:
- Use vectors.
- Use sort from algorithm.
- Use lower_bound and upper_bound.
- Use 0-based indexing.
Now, sample test:
Example 1:
d=2
Lx=[3,1] => sLx=[1,3]
Rx=[3,2] => sRx=[2,3]
Ly=[1,2] => sLy=[1,2]
Ry=[2,2] => sRy=[2,2]
Now for i=0 (sofa1): Rx[0]=3, left = lower_bound(sLx,3) => index? sLx [1,3]; lower_bound(3) points to element 3 at index 1, count=1. subLR false (since Lx=Rx) => left=1. right: leRx = upper_bound(sRx, Lx[0]=3) => sRx [2,3]; upper_bound(3) returns end, leRx=2, right = 2-2=0. top: lower_bound(sLy, Ry[0]=2) => sLy [1,2]; lower_bound(2) returns iterator to element 2 index1, count=1. subTB true => top=0. bottom: leRy = upper_bound(sRy, Ly[0]=1) => sRy [2,2]; upper_bound(1) returns iterator to first element? Since 2>1, returns iterator to element 2 at index0, leRy=0, bottom = 2-0=2, subTB true => bottom=1. So matches (1,0,0,1).
For i=1: Rx[1]=2, left = lower_bound(sLx,2) => sLx [1,3]; lower_bound(2) returns iterator to 3? Actually first >=2 is 3 at index1, count=1. subLR true => left=0. right: leRx = upper_bound(sRx, Lx[1]=1) => sRx [2,3]; upper_bound(1) returns iterator to 2 at index0, leRx=0, right = 2-0=2, subLR true => 1. top: lower_bound(sLy, Ry[1]=2) => count of Ly<2: sLy [1,2]; lower_bound(2) => index1 count=1, subTB false => top=1. bottom: leRy = upper_bound(sRy, Ly[1]=2) => sRy [2,2]; upper_bound(2) returns end, leRy=2, bottom = 2-2=0, subTB false => 0. So (0,1,1,0). Not match.
Output 1.
Works.
Now Example 2: Already tested mentally, should work.
Now Example 3: Works.
Thus solution ready.
Now we should also test with a scenario where multiple sofas satisfy, but guarantee says at most one, so fine.
Now we may also consider whether the algorithm can be optimized by not doing four binary searches per sofa? But it's fine.
Now, there might be an alternative using prefix sums on coordinate ranges, but n,m up to 1e5 and d up to 1e5, binary search is fine.
Now, think about large d, like 1e5, sorting O(d log d) fine, four sorts maybe but still fine.
Now, memory: storing four vectors of size 1e5 each, fine.
Now, final answer: implement as described.
But wait, we need to read input. The sofas lines contain four integers x1,y1,x2,y2. However, note that the order of cells is arbitrary. So we must compute min and max accordingly.
Now, there is a nuance: Some sofas might be given with x1,y1 and x2,y2 such that they are not sorted. So using min/max is fine.
Now, what about orientation detection? Not needed but for subtraction we can just compute boolean vertical = (x1 != x2) or equivalently Lx < Rx.
Now, let's write code.
Potential pitfalls: Using lower_bound on vector requires sorted order. We'll sort each.
Now, one more nuance: The sorted vectors are of size d. For binary search, we need to ensure we are using the correct comparator. In C++, lower_bound returns first element >= value. So for counting < value, we use lower_bound. Good.
Now, for right count: we need count of Rx > Lx[i]. Using upper_bound to get number of Rx <= Lx[i]. Then subtract from d.
But note: upper_bound returns first > value. So count of <= value = upper_bound - begin. Yes.
Now, for bottom: similar.
Now, ensure we use long long? Not needed because counts are up to d-1 <= 1e5, fits in int.
Now, code.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int d;
cin >> d;
int n, m;
cin >> n >> m;
vector<int> Lx(d), Rx(d), Ly(d), Ry(d);
vector<bool> subLR(d, false), subTB(d, false);
for (int i = 0; i < d; ++i) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Lx[i] = min(x1, x2);
Rx[i] = max(x1, x2);
Ly[i] = min(y1, y2);
Ry[i] = max(y1, y2);
if (Lx[i] < Rx[i]) subLR[i] = true; // vertical
if (Ly[i] < Ry[i]) subTB[i] = true; // horizontal
}
int cntl, cntr, cntt, cntb;
cin >> cntl >> cntr >> cntt >> cntb;
vector<int> sLx = Lx, sRx = Rx, sLy = Ly, sRy = Ry;
sort(sLx.begin(), sLx.end());
sort(sRx.begin(), sRx.end());
sort(sLy.begin(), sLy.end());
sort(sRy.begin(), sRy.end());
int ans = -1;
for (int i = 0; i < d; ++i) {
int left = lower_bound(sLx.begin(), sLx.end(), Rx[i]) - sLx.begin();
if (subLR[i]) left--;
int right = d - (upper_bound(sRx.begin(), sRx.end(), Lx[i]) - sRx.begin());
if (subLR[i]) right--;
int top = lower_bound(sLy.begin(), sLy.end(), Ry[i]) - sLy.begin();
if (subTB[i]) top--;
int bottom = d - (upper_bound(sRy.begin(), sRy.end(), Ly[i]) - sRy.begin());
if (subTB[i]) bottom--;
if (left == cntl && right == cntr && top == cntt && bottom == cntb) {
ans = i + 1;
break;
}
}
cout << ans << endl;
return 0;
}
Now, test with sample inputs.
Now, possible off-by-one errors: Let's test with example 2 manually using code reasoning.
d=3
Sofas:
0: (1,2)-(1,1) -> Lx=1,Rx=1, Ly=1,Ry=2, subLR=false, subTB=true.
1: (5,5)-(6,5) -> Lx=5,Rx=6, Ly=5,Ry=5, subLR=true, subTB=false.
2: (6,4)-(5,4) -> Lx=5,Rx=6, Ly=4,Ry=4, subLR=true, subTB=false.
sLx = [1,5,5] sorted -> [1,5,5]
sRx = [1,6,6] sorted -> [1,6,6]
sLy = [1,5,4] sorted -> [1,4,5]
sRy = [2,5,4] sorted -> [2,4,5]
Now for sofa 1 (index 1) we expect counts (2,1,2,0). Compute:
Rx[1] = 6; left = lower_bound(sLx,6) -> sLx [1,5,5]; lower_bound(6) returns end, index = 3, so left=3. subLR true => left=2. OK.
Lx[1] = 5; right = d - upper_bound(sRx,5) count. upper_bound(sRx,5) -> sRx [1,6,6]; first >5 is 6 at index 1? Actually sorted: [1,6,6]. upper_bound(5) returns iterator to first element >5, which is 6 at position 1 (0-based index 1). So le = 1. right = 3 - 1 = 2. subLR true => right=1. OK.
Ry[1] = 5; top = lower_bound(sLy,5) -> sLy [1,4,5]; lower_bound(5) returns iterator to element 5 at index 2, count=2. subTB false => top=2. OK.
Ly[1] = 5; bottom = d - upper_bound(sRy,5) -> sRy [2,4,5]; upper_bound(5) returns iterator to first >5, which is end, index=3. So le=3, bottom = 3-3=0. subTB false => 0. OK.
Matches. Good.
Now for sofa 0:
Rx[0]=1; left = lower_bound(sLx,1) -> sLx [1,5,5]; lower_bound(1) returns iterator to first 1 at index0, count=0. subLR false => left=0.
Lx[0]=1; right = d - upper_bound(sRx,1) -> sRx [1,6,6]; upper_bound(1) returns iterator to first >1, which is 6 at index1, le=1, right = 3-1=2. subLR false =>2.
Ry[0]=2; top = lower_bound(sLy,2) -> sLy [1,4,5]; lower_bound(2) returns iterator to first >=2, which is 4 at index1? Actually 1<2, 4>=2, so index1, count=1. subTB true => top=0.
Ly[0]=1; bottom = d - upper_bound(sRy,1) -> sRy [2,4,5]; upper_bound(1) returns iterator to first >1, which is 2 at index0, le=0, bottom = 3-0=3. subTB true => bottom=2? Wait subTB true for sofa0 (horizontal) so subtract 1 => bottom=2. But expected cntb for sofa0 in note? They said first sofa has 2 to bottom. That matches. So bottom=2. But earlier we computed bottomCount for sofa1 (vertical) correctly. Good.
Thus all correct.
Now, check for sofa2 (index 2):
Rx[2]=6; left = lower_bound(sLx,6)=3, subLR true =>2.
Lx[2]=5; right = d - upper_bound(sRx,5)=3 - (upper_bound -> index of first >5: [1,6,6] -> index1) =>2, subLR true =>1.
Ry[2]=4; top = lower_bound(sLy,4) -> sLy [1,4,5]; lower_bound(4) returns iterator to element 4 at index1, count=1. subTB false =>1.
Ly[2]=4; bottom = d - upper_bound(sRy,4) -> sRy [2,4,5]; upper_bound(4) returns first >4 which is 5 at index2? Actually sorted [2,4,5]; upper_bound(4) returns iterator to 5 at index2, so le=2, bottom = 3-2=1, subTB false =>1. So counts (2,1,1,1) as mentioned. Good.
Thus algorithm works.
Now, final check: Could there be a scenario where the sofa's own min_x or max_x is such that Lx[i] < Rx[i] but the self count might be counted more than once? For leftCount we only subtract 1, which is correct because only one instance of the sofa in the Lx list. For rightCount, similarly subtract 1. However, what if the sofa's Lx equals Rx? Then it doesn't get counted in left or right total, so no subtraction needed.
Thus fine.
Now, think about potential integer overflow or negative counts? Should not happen.
Now, think about alternative: maybe we need to consider that the condition for left is "A is standing to the left of B if there exist two such cells a and b that xa < xb". This is for a specific ordered pair (A,B). So for counting sofas to the left of S, we count B such that B is left of S? Actually "cntl sofas to the left" means sofas that are to the left of Grandpa's sofa. That is, we count A such that A is left of S. So we used condition Lx[A] < Rx[S]. That matches: A left of S if exists a in A, s in S with xa < xs. So condition we used is Lx[A] < Rx[S]. But we computed leftCount = number of sofas j with Lx[j] < Rx[i]. That's counting j left of i. So correct.
Now, confirm right: cntr sofas to the right. That means sofas that are to the right of S. So j is right of i if exists a in j, b in i with xa > xb. So condition Rx[j] > Lx[i]. So we used that.
Top: cntt sofas to the top. j top of i if ya < yb => Ly[j] < Ry[i].
Bottom: cntb sofas to the bottom. j bottom of i if ya > yb => Ry[j] > Ly[i].
All consistent.
Now, final note: The input sizes: d up to 1e5, but n and m up to 1e5, but we never use them except maybe to validate? But not needed.
Now, we should consider if there is any scenario where the sofa is placed such that its two cells are not in sorted order and our min/max works.
Now, I think this solution is correct.
But let's double-check the problem statement: "Sofa A is standing to the left of sofa B if there exist two such cells a and b that xa < xb, a is covered by A and b is covered by B." This definition is for A left of B. So for "cntl sofas to the left of Grandpa Maks's sofa", we need sofas A such that A is left of Grandpa's sofa. So yes.
Now, note: They also say "Sofa A is standing to the top of sofa B if there exist two such cells a and b that ya < yb". So A top of B. So for cntt, we need sofas that are top of Grandpa's sofa. So condition Ly[A] < Ry[Grandpa]. So good.
Now, confirm that we didn't reverse top/bottom: They might have defined top using y, but sometimes matrix rows are x (vertical) and columns are y (horizontal). So top means smaller y? That is typical: smaller y coordinate is "higher" on screen if y increases downward. So they said ya < yb means A top of B, so smaller y is top. So we used that: Ly (min y) < Ry (max y) of the other? Actually for A top of B, we need ya < yb for some cells. So if A's min y < B's max y, then there exists such pair. So yes.
Now, for bottom: A bottom of B if ya > yb. So condition max y of A > min y of B. So we used Ry[A] > Ly[B]. So for counting sofas bottom of Grandpa, we need j such that Ry[j] > Ly[i]. So yes.
Now, all consistent.
Now, we should also consider whether we need to use 64-bit integers? d up to 1e5, counts up to d-1, fits in int (32-bit). So fine.
Now, final code.
But I want to consider if there is any chance that the problem expects that we also consider the fact that the sofa occupies two cells, and the definitions might be based on the cells themselves and not just min/max? But we already reasoned.
Now, test a more complicated scenario to ensure no oversight.
Suppose we have three sofas:
A: vertical at (1,1)-(2,1) => Lx=1,Rx=2, Ly=1,Ry=1.
B: horizontal at (2,2)-(2,3) => Lx=2,Rx=2, Ly=2,Ry=3.
C: vertical at (3,3)-(4,3) => Lx=3,Rx=4, Ly=3,Ry=3.
Now compute leftCount for B:
Lx < Rx[B]=2 => Lx values: A Lx=1 <2, B Lx=2 not <2, C Lx=3 not <2 => count=1. Subtract self? B is horizontal, subLR false. So leftCount=1. Check manually: sofas to left of B? A left of B? Need xa < xb for some cells. A has x=1,2; B has x=2 only. Can we find a in A, b in B with xa < xb? xa could be 1, xb=2 => 1<2 true => A left of B. C? xa=3,4; xb=2; can't have xa<2. So only A. So leftCount=1. Good.
RightCount for B: Rx > Lx[B]=2 => Rx values: A Rx=2 not >2, B Rx=2 not >2, C Rx=4 >2 => count=1. Subtract self? false =>1. Manual: sofas right of B? C right of B? need xa > xb: C has x=3,4; B has x=2; 3>2 true => C right. So rightCount=1.
TopCount for B: Ly < Ry[B]=3 => Ly values: A Ly=1 <3, B Ly=2 <3, C Ly=3 not <3 => count=2. subtract self (subTB true for B) =>1. Manual: sofas top of B? A top of B? Need ya < yb: A y=1; B y=2 or3; 1<2 true => A top. B itself? Not counted. C? y=3; need ya<yb: ya=3 < yb? yb could be 2? Actually B's y values 2 and3. For C to be top, need a from C with ya < b from B's y. C y=3; B y=2? 3<2 false; B y=3? 3<3 false. So C not top. So topCount=1. Good.
BottomCount for B: Ry > Ly[B]=2 => Ry values: A Ry=1 not >2, B Ry=3 >2, C Ry=3 >2 => count=2. subtract self =>1. Manual: sofas bottom of B? C bottom of B? need ya > yb: C y=3; B y=2 => 3>2 true => C bottom. A? ya=1 > yb? yb could be 3? 1>3 false; yb=2 false. So A not bottom. So bottomCount=1. Good.
Thus works.
Now, another scenario: Two sofas with overlapping x and y ranges: maybe they can be both left and right? Already covered.
Now, potential off-by-one in subtraction: For a vertical sofa, we subtract 1 for leftCount and rightCount. But is it possible that the sofa's own Lx < Rx, but the sofa itself might not be counted in leftCount because Lx[i] < Rx[i] is true, so it is counted in the total Lx < Rx[i] (since Lx[i] is included in the Lx list). So subtract 1. For a horizontal sofa, Lx[i] == Rx[i], so Lx[i] < Rx[i] is false, so it's not counted, so no subtraction. Good.
Similarly for rightCount: For vertical sofa, Rx[i] > Lx[i] true, so it's counted in the total Rx > Lx[i], subtract 1. For horizontal, false, no subtraction.
Now, for topCount: For horizontal sofa, Ly[i] < Ry[i] true, so it's counted in total Ly < Ry[i], subtract 1. For vertical, false.
For bottomCount: For horizontal, Ry[i] > Ly[i] true, so counted, subtract 1. For vertical, false.
Thus consistent.
Now, there is no scenario where a sofa could be counted twice for self in the same count? No.
Now, final answer.
But we must ensure to read input correctly: The last line contains four integers. In the examples, the numbers are separated by spaces. So cin works.
Now, the code seems fine.
Now, let's test with a random small case by brute force to validate. We can mentally test a few.
Consider d=2, both horizontal at different rows? Actually horizontal means same x, different y. So two horizontal sofas at different x rows: Sofa1 at row1, columns 1-2; Sofa2 at row2, columns 1-2. So Lx1=1,Rx1=1, Ly1=1,Ry1=2. Lx2=2,Rx2=2, Ly2=1,Ry2=2.
Now compute counts:
sLx = [1,2] sRx=[1,2] sLy=[1,1] sRy=[2,2]
Sofa1:
left: lower_bound(Lx,Rx1=1) -> count of Lx <1 =0. subLR false =>0.
right: d - upper_bound(Rx, Lx1=1) -> upper_bound(1) on [1,2] returns iterator to 2 at index1, le=1, right =2-1=1. subLR false =>1. (Sofa2 right of Sofa1? Need xa > xb: Sofa2 x=2 > Sofa1 x=1 => true, so rightCount=1)
top: lower_bound(Ly, Ry1=2) -> sLy [1,1]; lower_bound(2) returns end, count=2. subTB true =>1. Actually count of Ly<2 is 2 (both sofas have Ly=1). Then subtract 1 => top=1. Check manually: Sofa2 top of Sofa1? top: ya < yb. Sofa2 y values are 1 and 2; Sofa1 y values 1 and 2. Need some a from Sofa2, b from Sofa1 with ya < yb. Since both have same y values, is there a pair? a=1, b=2 gives 1<2 true, so Sofa2 top of Sofa1? But top means A top of B: need A's y < B's y. Here A=Sofa2, B=Sofa1 => ya=1, yb=2 => true. So Sofa2 is top of Sofa1. Also Sofa1 top of itself? Not counted. So topCount=1. Good.
bottom: d - upper_bound(Ry, Ly1=1) -> sRy [2,2]; upper_bound(1) returns iterator to first >1, which is 2 at index0, le=0, bottom=2-0=2. subTB true =>1. Check: sofas bottom of Sofa1? Sofa2 bottom? need ya > yb. Sofa2 y=2 > Sofa1 y=1? true (a=2, b=1). So bottomCount=1. Good.
Thus counts (0,1,1,1). Sofa2 would be symmetric? Probably (1,0,1,1) etc.
Now, this matches our expectation.
Now, test with both vertical at same y but different x? Already similar.
Now, everything seems correct.
Thus we can implement.
But I recall a known Codeforces problem: "1019A? Actually maybe 1029C? Actually problem "DecoForces" is fictional; the real problem is CF Round? Actually I think this is problem "Grandfather Maks and his sofa" or something. But I'm not 100% sure if there are any hidden traps. Let's search memory: There is a problem "Sofas" maybe? I recall a problem from Codeforces: "Sereja and D" no. I think there is a problem called "The Sofa" or something, but I'm not sure.
Given the straightforward solution, it's likely correct.
Now, one more nuance: The note says: "The note also stated that there are cntl sofas to the left of Grandpa Maks's sofa, cntr — to the right, cntt — to the top and cntb — to the bottom." It doesn't specify whether these counts include sofas that are both left and right? But counts are just numbers; each sofa either is left or not left according to definition. But a sofa could be both left and right? That would cause it to be counted in both left and right if the definitions allow? But the counts are separate: cntl counts those that are left; cntr counts those that are right. The same sofa can be both left and right, so it would be included in both counts. That's fine; our counts reflect that because for a sofa j that is both left and right of i, it satisfies Lx[j] < Rx[i] and Rx[j] > Lx[i]. So it will be counted in leftCount and rightCount. So counts may sum to more than d-1. That's allowed.
Our formulas produce that. Good.
Now, verify with a scenario where two sofas overlapping in both dimensions: For example, sofa A: vertical at x=1,2 y=5. B: horizontal at x=2 y=4,5? But they can't share cells, but could be placed such that A occupies (1,5),(2,5); B occupies (2,4),(2,5)? They would share (2,5) - not allowed. So can't. So overlapping ranges can exist but no cell overlap. So it's fine.
Now, confirm that we didn't need to consider the fact that each sofa takes two cells and the existence condition might be satisfied only if the cells are not the same? Already considered.
Now, think about potential time: 1e5 sofas, sorting four vectors O(4 * 1e5 log 1e5) ~ 41e517 = 6.8e6, fine. Then for each sofa, four binary searches O(41e5log 1e5) similar, total ~ 1.3e7 operations, fine.
Now, one more test: d=100000, all sofas vertical at same column but different rows with non-overlapping rows. Then Lx[i] = some increasing, Rx[i] = Lx[i]+1. Sorted Lx will be increasing. Binary searches will be log d. Works.
Now, final code.
But I'd like to consider whether there is any possible misinterpretation: Could "left" be defined based on the positions of the sofas as a whole, rather than existence of any two cells? For example, maybe they consider the "sofa's position" as its bounding box? But the definition they gave is explicit: existence of two cells a and b with xa < xb. So our interpretation matches.
Now, could the conditions be reversed? For example, they might have defined left using y, but they explicitly said x. So okay.
Now, think about sample 2 again: They said for first sofa: "0 to its left, 2 sofas to its right ((1, 1) is to the left of both (5, 5) and (5, 4))". Wait, they said "(1,1) is to the left of both (5,5) and (5,4)". That indicates that sofa1's cell (1,1) is left of sofa2 and sofa3. That matches: (1,1) has x=1, (5,5) x=5, 1<5. So left uses x. Good.
Now, the note: They mention "The note also stated that there are cntl sofas to the left of Grandpa Maks's sofa, cntr — to the right, cntt — to the top and cntb — to the bottom." So for candidate sofa, we need to find counts exactly.
Now, one more nuance: The input last line contains cntl, cntr, cntt, cntb (0 ≤ each ≤ d-1). So maybe d-1 could be up to 99999, still fits.
Now, the answer is the sofa number (1-indexed). Print -1 if none.
Now, the problem statement: "If there is no such sofa that all the conditions are met for it then output -1." So we output -1 if none.
Now, all done.
But wait: The problem might have a twist: The sofa occupies two cells that are adjacent. But in counting left/right, it might be that the existence condition requires that the two cells a and b are such that they are not from the same sofa obviously, but also maybe they must be "neighbouring"? No, the definition doesn't say that. So it's fine.
Now, maybe the problem originally from Codeforces had a more complex condition: Some people might have misinterpreted left/right as based on columns, but here it's based on rows. But we have confirmed via sample.
Now, finalize solution.
Now, we should think about performance: Could we avoid sorting four times? We could but not necessary.
Now, potential pitfalls: Using vector for subLR/subTB may be slower, but it's fine. Or we can use vector with 0/1.
Now, for correctness, ensure that we used upper_bound for right and bottom correctly: The condition is >, so we want count of elements > value. Using d - (upper_bound for <=). Upper_bound returns first >, so number of elements <= value is that index. So count > = d - that. Good.
Now, confirm with an example: sRx = [1,2,3], Lx[i]=2. Then upper_bound(2) returns iterator to first >2, which is 3 at index2? Actually array: index0=1,1=2,2=3. Upper_bound(2) returns iterator to element 3 at index2. So count <=2 = index of that iterator? That is 2 (since begin+2). Because elements at index0,1 are <=2 (1 and 2). So count <=2 =2. Then count >2 = 3-2=1. That's correct: only 3 >2.
Now, if Lx[i]=2 and sRx contains multiple 2s, say [2,2,3]. Upper_bound(2) returns iterator to first >2, which is 3 at index2? Actually sorted: [2,2,3]; upper_bound(2) returns index2 (value 3). So count <=2 =2, count >2 =1. Good.
Now, if Lx[i]=4, upper_bound(4) returns end, count <=4 = size =3, count >4 =0.
Now, bottom similarly.
Now, for left and top, we use lower_bound for <. With sLx = [1,2,2,3], Rx[i]=2. lower_bound(2) returns iterator to first element >=2, index1 (first 2). Count of <2 = index1 =1 (only 1). That's correct: only 1 <2. If Rx[i]=2 and there are many 1's, fine.
Now, if Rx[i] = 1, lower_bound(1) returns iterator to first >=1, index0, count <1 =0.
Now, all good.
Now, final answer.
But before final, consider the possibility that there is exactly one sofa that matches and the counts are such that self-subtraction leads to negative? Not possible.
Now, we should also check for the scenario where a sofa is counted in leftCount but not in rightCount, but self subtraction might cause leftCount to become negative if leftCount total was 0 and we subtract 1. Could leftCount total be 0 but self qualifies? If self qualifies, then leftCount total must be at least 1 because it includes itself. Because Lx[i] < Rx[i] true, then in sorted Lx list, there is at least that element which is less than Rx[i]? Actually we need to check: leftCount total = number of Lx values (including i) that are < Rx[i]. Since Lx[i] < Rx[i], that count is at least 1. So after subtraction we get >=0. Similarly for other counts. So safe.
Now, everything.
Now, I'll finalize.
But let me recall that in some problems, there is a trick: The sofa may be placed such that one cell has smaller x and the other larger, but the existence condition might be satisfied by the cell with larger x from j and smaller x from i? But we already handled.
Now, one more nuance: The note says: "Sofa A is standing to the left of sofa B if there exist two such cells a and b that xa < xb, a is covered by A and b is covered by B." It doesn't require that the cells are distinct from each other? They are obviously distinct because they belong to different sofas, but could they be the same cell? Not possible because no cell is covered by more than one sofa.
Thus fine.
Now, code done.
But wait, the problem may be tricky: The definition of left and right might be swapped: Usually left/right refer to y coordinates, but here they use x. However, the sample confirms that.
Now, we should also consider whether the orientation of the sofa (horizontal/vertical) could affect the counting due to the fact that a sofa occupies two cells, and the existence condition might require that the two cells a and b are not from the same sofa, which we already accounted. So good.
Now, final code.
Potential optimization: Instead of four binary searches per sofa, we could precompute prefix counts for Lx, Rx etc, but not needed.
Now, but there is a catch: The subtraction of self should only be done if the sofa itself satisfies the condition. But our condition for self is Lx[i] < Rx[i] for left/right. For a vertical sofa, that's true. However, is it always true that if Lx[i] < Rx[i] then the sofa i itself qualifies as left of itself? Wait, the definition requires A ≠ B. So self is not considered. So we should not count self. So we subtract if self would be counted in the total. So our subtraction is correct. But is it possible that self is counted even if Lx[i] < Rx[i] is true? Yes, because the total count of Lx < Rx[i] includes i's own Lx, as long as Lx[i] < Rx[i]. So subtract. Good.
Now, what about a scenario where Lx[i] < Rx[i] but Lx[i] is not < Rx[i] because we compare with its own Rx? Actually Lx[i] < Rx[i] is true, so it satisfies the condition for left of itself? But condition requires A ≠ B, so we must exclude. So we subtract.
Now, there is no scenario where self could be counted in leftCount without Lx[i] < Rx[i]? No, because condition for self would require Lx[i] < Rx[i] exactly.
Thus fine.
Now, we should also check that for rightCount, self qualifies if Rx[i] > Lx[i] (same condition). So subtract accordingly.
Now, for top/bottom, self qualifies if Ly[i] < Ry[i] (horizontal). So subtract accordingly.
Thus all good.
Now, final answer.
Now, I think we should provide the solution.
But there might be another approach: using order statistics trees or something, but not needed.
Now, I'll craft final explanation.
Now, let's produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int d;
cin >> d;
int n, m; // not used
cin >> n >> m;
vector<int> Lx(d), Rx(d), Ly(d), Ry(d);
vector<bool> subLR(d, false), subTB(d, false); // subtract self for left/right and top/bottom
for (int i = 0; i < d; ++i) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Lx[i] = min(x1, x2);
Rx[i] = max(x1, x2);
Ly[i] = min(y1, y2);
Ry[i] = max(y1, y2);
if (Lx[i] < Rx[i]) subLR[i] = true; // vertical – self qualifies for left/right
if (Ly[i] < Ry[i]) subTB[i] = true; // horizontal – self qualifies for top/bottom
}
int cntl, cntr, cntt, cntb;
cin >> cntl >> cntr >> cntt >> cntb;
vector<int> sLx = Lx, sRx = Rx, sLy = Ly, sRy = Ry;
sort(sLx.begin(), sLx.end());
sort(sRx.begin(), sRx.end());
sort(sLy.begin(), sLy.end());
sort(sRy.begin(), sRy.end());
int ans = -1;
for (int i = 0; i < d; ++i) {
// left: count of sofas with Lx < Rx[i]
int left = lower_bound(sLx.begin(), sLx.end(), Rx[i]) - sLx.begin();
if (subLR[i]) left--;
// right: count with Rx > Lx[i]
int right = d - (upper_bound(sRx.begin(), sRx.end(), Lx[i]) - sRx.begin());
if (subLR[i]) right--;
// top: count with Ly < Ry[i]
int top = lower_bound(sLy.begin(), sLy.end(), Ry[i]) - sLy.begin();
if (subTB[i]) top--;
// bottom: count with Ry > Ly[i]
int bottom = d - (upper_bound(sRy.begin(), sRy.end(), Ly[i]) - sRy.begin());
if (subTB[i]) bottom--;
if (left == cntl && right == cntr && top == cntt && bottom == cntb) {
ans = i + 1; // sofas are numbered from 1
break;
}
}
cout << ans << '\n';
return 0;
}