We need to find maximum number of overlapping axis-aligned rectangles (seals). Overlap region can be any region (not necessarily integer points). The maximum overlapping count is the maximum depth of rectangles covering any point.
We are given N up to 100,000, coordinates integer in [0,1000]. That's small coordinate range. However rectangles are axis-aligned, coordinates given as (x1, y1) top-left and (x2, y2) bottom-right? Actually top-left and bottom-right coordinates. Since y1 < y2? It says top-left (x1,y1) and bottom-right (x2,y2). Usually top-left has smaller y? Actually in typical computer graphics coordinate with y increasing downward, top-left has lower y, bottom-right has higher y. But they say top-left and bottom-right, and y1<y2, so consistent with that orientation. However for overlapping count, we just care about the rectangle area.
We can solve using sweep line and segment tree because coordinates are discrete? Since coordinates up to 1000, we could even use a 2D difference array of size 1001x1001, but careful: rectangles are defined on continuous region. Overlap count at any point is maximum when we consider points on the plane. Since rectangles are axis-aligned and boundaries are integer, the overlap count is constant over open regions between integer lines. The maximum can be found by considering the grid of half-integer? Actually since rectangles include all points with x in [x1, x2) maybe? Or inclusive? Typically, rectangle defined by two corners: includes points with x between x1 and x2, y between y1 and y2. But boundaries can be considered inclusive? Overlap count on boundaries might be ambiguous but maximum over interior points will be same as considering integer grid cells? Actually we need maximum number of rectangles covering any point. Since boundaries are lines, the count changes only at x = x1 or x2, and y = y1 or y2. So the maximum can be found by examining all regions defined by the unique x and y coordinates. Because coordinates are integer and up to 1000, we have at most 2000 distinct x coordinates and 2000 distinct y coordinates. So we can compress coordinates and use a 2D difference array (implicitly using sweep line). However N=100k, but coordinate range small (0..1000). So we could simply create an array of size 1001x1001 and use difference technique: for each rectangle, we add +1 to diff[x1][y1]? Actually careful: If we treat grid cells as unit squares [i,i+1) x [j,j+1) for integer i,j from 0 to 999 (since x2 <=1000, y2<=1000). The overlap count on each cell will be constant because rectangles cover whole cells if they cover that continuous region. We can compute the count for each cell by 2D prefix sum after difference updates. Since coordinate range is 0..1000, we have 1000 possible intervals? Actually number of cells = (max_x) * (max_y) but x2 up to 1000, so possible x indices from 0 to 999. So 1000 x 1000 = 1e6 cells, which is manageable. We can compute difference array of size 1002 x 1002 for safety.
But careful: rectangles are defined with integer coordinates; do they include the right and bottom edges? Typically if we have top-left (x1,y1) and bottom-right (x2,y2), the rectangle includes points where x1 <= x <= x2 and y1 <= y <= y2? But then if two rectangles touch at edges, does that count as overlap? The problem says "overlapped seals". Overlap typically requires interior intersection, not just boundary. But in example 2: rectangles: (0,0)-(2,2) and (2,0)-(4,2). They share a vertical line at x=2. Output is 1, meaning they don't count as overlapping (maximum overlap is 1). So boundaries do not count as overlapping; they must have positive area overlap. So for any point strictly inside both rectangles, they would overlap? But if they only share a line, there is no interior point. So we need to consider open rectangles? Typically in these problems, to avoid boundary issues, treat rectangles as covering [x1, x2) x [y1, y2) (or (x1,x2) x (y1,y2) with appropriate endpoints). Since coordinates are integers and we want to count overlapping area, using half-open intervals [x1, x2) x [y1, y2) will treat the right and bottom boundaries as not overlapping with adjacent rectangles that start exactly at x2. That matches example 2: first rectangle x in [0,2), second x in [2,4). They don't overlap because the first is exclusive at x=2 and second inclusive at x=2? If we use [x1, x2) for both, first: [0,2), second: [2,4). They are disjoint. So output max overlap 1.
Thus we can treat each rectangle as covering integer grid cells that are fully inside: cell [i,i+1) x [j,j+1) is covered if x1 <= i < x2 and y1 <= j < y2 (assuming y increases downward? But we need consistent orientation: y1 < y2, rectangle covers y from y1 to y2. If we treat y axis downward, then top-left y1 is smaller, bottom-right y2 is larger, so rectangle covers y in [y1, y2). So same half-open approach.
Thus we can compute a 2D difference array: For each rectangle, we add +1 to diff[x1][y1], subtract at diff[x2][y1], diff[x1][y2], and add at diff[x2][y2] (since it's 2D prefix sum). But careful: In typical 2D difference for covering cells, if we have grid cells indexed by integer coordinates from 0 to max, we define an array arr sized (maxX+1) x (maxY+1) where we increment at (x1, y1) +1, (x2, y2) +1, (x1, y2) -1, (x2, y1) -1. Then after 2D prefix sum, arr[x][y] becomes the number of rectangles covering cell (x,y) (if we consider cell as [x, x+1) x [y, y+1) )? Let's verify:
Suppose we have a 2D difference array D of size (W+1) x (H+1). For each rectangle covering x in [x1, x2) and y in [y1, y2), we do D[x1][y1] += 1, D[x2][y1] -= 1, D[x1][y2] -= 1, D[x2][y2] += 1. Then compute prefix sum S[x][y] = sum over i<=x, j<=y D[i][j]. Then S[x][y] gives the number of rectangles covering the point (x, y)? Actually typical use: The value at a cell (i,j) after prefix sum is the number of rectangles covering that cell if we treat cell as covering area [i, i+1) x [j, j+1). However the prefix sum S[x][y] after processing includes contributions from updates at indices <= x and <= y. At a coordinate (x,y) that is integer, the value S[x][y] should represent the number of rectangles covering the top-left corner of cell (x,y)? I need to be precise.
Standard technique: Let’s have a grid of cells indexed by (i,j) for i=0..W-1, j=0..H-1 representing unit squares [i,i+1) x [j,j+1). We want to compute for each cell the number of rectangles covering it. We can use a difference array of size (W+1)x(H+1). For each rectangle covering x in [x1, x2) and y in [y1, y2), we increment D[x1][y1] += 1, D[x2][y1] -= 1, D[x1][y2] -= 1, D[x2][y2] += 1. Then compute the prefix sum over i from 0..W, j from 0..H, and the value at (i,j) after prefix sum (where i < W, j < H) will be the number of rectangles covering cell (i,j). But note that the computed prefix sum at index (i,j) (where i and j are integer) is sum over all updates with x <= i, y <= j. For a given cell (i,j), consider any rectangle with x1 <= i < x2? Actually if we have rectangle covering [x1, x2) x [y1, y2). It will contribute +1 to D[x1][y1] and -1 to D[x2][y1] etc. For a given (i,j), when we sum D up to i and j, we include the +1 if x1 <= i and y1 <= j. But we also include -1 from D[x2][y1] if x2 <= i and y1 <= j. So net contribution from that rectangle is 1 if x1 <= i < x2 and y1 <= j < y2? Let's check:
- +1 condition: x1 <= i and y1 <= j.
- -1 from D[x2][y1]: if x2 <= i and y1 <= j.
- -1 from D[x1][y2]: if x1 <= i and y2 <= j.
- +1 from D[x2][y2]: if x2 <= i and y2 <= j.
So the total contribution is:
indicator(x1 <= i) * indicator(y1 <= j) - indicator(x2 <= i) * indicator(y1 <= j) - indicator(x1 <= i) * indicator(y2 <= j) + indicator(x2 <= i) * indicator(y2 <= j)
= indicator(x1 <= i) * [indicator(y1 <= j) - indicator(y2 <= j)] - indicator(x2 <= i) * [indicator(y1 <= j) - indicator(y2 <= j)]
= [indicator(x1 <= i) - indicator(x2 <= i)] * [indicator(y1 <= j) - indicator(y2 <= j)]
Now indicator(x1 <= i) - indicator(x2 <= i) is 1 if x1 <= i < x2, else 0? Because if i < x1, both 0 => 0. If x1 <= i < x2, then first indicator 1, second 0 => 1. If i >= x2, both 1 => 0. Similarly for y. So product is 1 exactly when x1 <= i < x2 and y1 <= j < y2. So yes, after prefix sum, S[i][j] gives the count for cell (i,j) (with i from 0 to maxX-1, j from 0 to maxY-1). But note that S is defined for indices up to maxX, maxY. But for cells with i = maxX (i.e., W) we don't care. So we can allocate array size (MAX+1) x (MAX+1) where MAX=1000 (since x2 <=1000, so x indices up to 999 for cells). The difference array D would be of size (1001+1?) Actually to handle x2 up to 1000 inclusive, we need indices up to 1000 for difference. If we use coordinates as integer from 0 to 1000, we have width 1000 cells (0..999). The rectangle covering [x1, x2) affects cells with x from x1 to x2-1. So our D array should have size (1001) maybe: indices from 0 to 1000 inclusive because we need to access D[x2][...] where x2 can be 1000. So array size (1001) x (1001). Then after computing prefix sum, we consider i from 0 to 999, j from 0 to 999.
Thus we can process each rectangle in O(1) updates, then do 2D prefix sum over 1001x1001 ~ 1,002,001 cells = about 1e6, which is fine. Complexity O(N + 1001^2) ~ 1e5 + 1e6 = 1.1e6, very fast.
But careful: The coordinate range is 0..1000 inclusive, but rectangle boundaries are integers. However, note that the rectangle is defined with top-left (x1,y1) and bottom-right (x2,y2). Typically, if top-left is (x1,y1) and bottom-right (x2,y2), the rectangle covers x from x1 to x2 inclusive? Actually in many geometry problems, the rectangle includes both boundaries. But example shows that touching at edges does not count as overlap. So we need to treat boundaries as exclusive for overlapping region. The half-open interpretation [x1, x2) x [y1, y2) works. But also note orientation: Top-left has smaller y, bottom-right larger y, so y1 < y2. So [y1, y2) works. So the grid cell method works.
But is there any possibility that the maximum overlap occurs at a point that is not inside any unit cell? Since the count is constant over each cell, maximum over cells equals maximum over any point? However consider overlapping at a point where boundaries meet: but those points are not interior to any rectangle? Actually if many rectangles share a common point that lies on boundaries, the count on that point might be higher than any interior cell? But since boundaries are lines, and we consider open interior (overlap means area?), the problem likely wants the maximum number of rectangles covering any point (including boundaries?). The example suggests boundaries are not considered overlapping. If we had three rectangles: one covering [0,2]x[0,2], another covering [2,4]x[0,2], another covering [0,2]x[2,4] (if they included boundaries), then at point (2,2) they all meet but interior not common. So max overlap would be 2? Actually if rectangles include boundaries, point (2,2) would be in all three? But if they include boundaries, first covers x up to 2, y up to 2; second covers x from 2 to 4, y 0 to 2; third covers x 0 to 2, y 2 to 4. So (2,2) is in all three if closed intervals. However example 2 shows touching rectangles do not count. So boundaries are not considered overlapping. So we need open intervals. The half-open interpretation gives correct result.
But consider a point exactly at (x1, y1) for some rectangle. That point is on the top-left corner. According to half-open, the rectangle covers points with x >= x1 and x < x2, y >= y1 and y < y2. So the corner (x1,y1) is included? Actually if we have x >= x1 and x < x2, then x=x1 satisfies? Yes, because x1 >= x1 and x1 < x2 (since x1<x2). Similarly y1 satisfies. So the top-left corner is included. But the bottom-right corner (x2,y2) is not included because x must be < x2. So the rectangle includes its top and left edges but excludes bottom and right edges? That's a common half-open convention. However, this could cause overlapping at a point that is the top-left corner of one rectangle and bottom-right corner of another? Let's test: rectangle A: [0,2)x[0,2). rectangle B: [2,4)x[2,4)? They don't share any points because B's x starts at 2, but A's x < 2, so point (2,2) is not in A because x<2, and not in B because x=2>=2? Actually B includes x>=2 and x<4, so x=2 is included. y=2 is included? B's y starts at 2, so y=2 is included. So (2,2) is in B. But not in A. So no overlap. If rectangle C: [0,2)x[2,4) (i.e., left bottom?), it includes y >=2 and y<4, so (2,2) is included? y=2 is included, x=2? x must be <2, so x=2 is not included. So not in C. So (2,2) only belongs to B. So no overlap. So boundaries that are included (top/left) may cause overlap at points that are shared by multiple rectangles if they both include that point. For example, if we have rectangle D: [0,2)x[0,2) (includes top-left corner) and rectangle E: [0,2)x[0,2) (same rectangle), they obviously overlap fully. But consider rectangle F: [2,4)x[0,2) and rectangle G: [0,2)x[0,2)? Their boundaries: F includes point (2,0)? Actually F includes x=2, y in [0,2). G includes x<2, y in [0,2). So they don't share any point with x=2? Because G excludes x=2, F includes x=2, but any point with x=2 and y in (0,2) is in F but not in G. So no overlap. The vertical line at x=2 is not overlapping. However if both rectangles include the left/top boundaries, then a point that is exactly at the top-left corner of both might be included if both have that point. But if two rectangles are adjacent without overlapping interior, they won't share any point because for them to share a point, that point must satisfy both intervals. For rectangle A: [0,2)x[0,2) and rectangle B: [2,4)x[0,2), they could share points with x=2? But A requires x<2, B requires x>=2. No common x. So they share no points. Good.
Thus using half-open intervals is consistent with counting overlaps only when interiors (or at least some area) overlap. The maximum overlap will be achieved in some open region, not on boundaries, because if there is a region of positive area with a certain count, boundaries might have lower count due to some rectangles ending or starting. However there could be a case where maximum occurs only at a point where many rectangles meet at a common point that is included by all? Could that happen without any area overlap? For example, many rectangles that all share a single common point but no area? That would require all rectangles to have that point on their boundaries. But since each rectangle includes its top and left edges, if many rectangles have that point as a top-left corner or as a point on the left or top edge, they all include it. Could that produce a count higher than any interior region? Consider two rectangles that intersect only along a line; the interior of the line has zero area, but points on the line are included by both. However do we consider that as overlapped seals? The problem likely intends overlapping area, i.e., the region where they all cover some area. But typical definition: "overlapped seals" meaning the region where multiple seals overlap. Usually overlapping means the intersection has non-zero area. But they ask "find the number of overlapped seals on the region which has the maximum number of overlapped seals." That is ambiguous. In many problems, "maximum overlapping rectangles" means the maximum depth of coverage at any point, including points on boundaries. But then example 2 would yield 2 if they counted boundaries, because at points with x=2 and y between 0 and 2, rectangle1? Actually rectangle1: (0,0)-(2,2) includes its right edge? If rectangle is defined as closed interval [0,2]x[0,2], then points with x=2 and y in (0,2) are in rectangle1. Rectangle2: (2,0)-(4,2) includes its left edge? If closed, includes x=2. So points with x=2 and y in (0,2) are in both, so overlap count 2. But they output 1. So they don't count the boundaries as overlapping. So they consider overlap only when interior? Actually the common points on the line have zero area but still are points. So they likely require that the region has positive area, or they treat rectangles as half-open. So we must use half-open intervals. And then maximum will be found in some cell interior. So the grid cell method is valid.
But is it possible that the maximum depth occurs in a region that is not aligned with integer grid cells? Since all rectangle boundaries are integer, the depth is constant within each unit cell (the product of open intervals between integer lines). Indeed, the plane is partitioned by vertical lines at integer x and horizontal lines at integer y. Within each cell (x between integers, y between integers), the inclusion in each rectangle is constant because rectangle boundaries are integer. So the maximum depth will be the maximum over these cells. So our method works.
Thus straightforward: initialize diff array of size (1002,1002) maybe just 1001+1 to avoid off-by-one? Actually we need indices up to 1000 inclusive because x2 can be 1000. So array dimension: (MAX_COORD+1) where MAX_COORD = 1000. So diff[1001][1001] (0-indexed: 0..1000 inclusive). Then for each rectangle, do diff[x1][y1] += 1; diff[x2][y1] -= 1; diff[x1][y2] -= 1; diff[x2][y2] += 1. Then compute prefix sum: for i from 0 to MAX_COORD-1? Actually after prefix sum, the value at (i,j) where i<MAX_COORD and j<MAX_COORD will represent cell count. But to compute the prefix sum, we need to propagate across the whole diff array up to MAX_COORD. Typically we compute cumulative sums: for i from 0 to MAX_COORD: for j from 0 to MAX_COORD: if i>0 add from above; if j>0 add from left; then if i>0 and j>0 subtract diagonal? Actually easiest: first compute 2D prefix sum: for i in 0..MAX_COORD: for j in 0..MAX_COORD: if i>0: diff[i][j] += diff[i-1][j]; if j>0: diff[i][j] += diff[i][j-1]; if i>0 and j>0: diff[i][j] -= diff[i-1][j-1]; Then after that, diff[i][j] for i<MAX_COORD and j<MAX_COORD (i up to 999, j up to 999) is the count for cell [i,i+1)x[j,j+1). But careful: after the addition, the array becomes the prefix sum. We need to keep original diff? We can reuse same array.
But we need to ensure we don't exceed bounds. We'll allocate 1002x1002 to be safe.
But is it correct that x and y coordinates are up to 1000 inclusive? Yes. So we set maxCoord = 1000. diff size: (maxCoord+1) x (maxCoord+1) = 1001x1001.
But wait: coordinates y: 0 <= y1 < y2 <= 1000. So y2 could be 1000. Then our cell indices for y go from 0 to 999. So we compute prefix sums up to i=1000? Actually after prefix sum, the values at i=1000 or j=1000 correspond to sum covering up to that coordinate, but they are not needed. But they might be used for propagation. In the algorithm, we need to compute prefix sums for i from 0 to maxCoord, j from 0 to maxCoord. So include i=1000, j=1000. But we only query for i<maxCoord and j<maxCoord. That's fine.
Now test with examples.
Example1:
2
0 0 3 2
2 1 4 3
maxCoord? x up to 4, y up to 3. But our array is up to 1000, so fine.
Rectangle1: x1=0,y1=0,x2=3,y2=2. So add diff[0][0]+=1; diff[3][0]-=1; diff[0][2]-=1; diff[3][2]+=1.
Rectangle2: x1=2,y1=1,x2=4,y2=3. Add diff[2][1]+=1; diff[4][1]-=1; diff[2][3]-=1; diff[4][3]+=1.
Now compute prefix sums.
We can test: cells: x 0..? Actually cells x from 0 to 3? Since max x2=4, so cells x 0,1,2,3. y cells: 0,1,2.
Which cells have overlap? The overlapping region should be rectangle from x=2..3? Actually rectangle1 covers x 0,1,2 (since x2=3, so x<3 => up to 2), y 0,1 (since y2=2, y<2 => y 0,1). Rectangle2 covers x 2,3 (since x2=4 => x 2,3), y 1,2 (since y2=3 => y 1,2). Overlap region: x=2, y=1 (common cell). So max overlap 2. Should be found.
Example2:
2
0 0 2 2
2 0 4 2
Rectangle1: [0,2)x[0,2). diff: +1 at (0,0); -1 at (2,0); -1 at (0,2); +1 at (2,2)
Rectangle2: [2,4)x[0,2). diff: +1 at (2,0); -1 at (4,0); -1 at (2,2); +1 at (4,2)
Now compute: For cell x=1,y=1: contributions? Should be only from rect1 =>1. For cell x=2? Actually cell x=2 not exist because x<2? cell x indices: 0,1 (since x2=2 => max x=1). Actually x cells: 0,1 for rect1; rect2 cells: x=2,3 (since x2=4 => x<4 => x=2,3). So no cell with x=2? But cell x=2 exists because x range is 0..3? Wait x2 up to 4, so cells are x=0,1,2,3. But rect1 does not cover x=2 because it's [0,2) => x<2, so x=2 not included. rect2 covers x=2,3. So there is a cell x=2,y=0 and y=1. Those cells are only covered by rect2. So max=1. So correct.
Example3:
3 rectangles all same [0,2)x[0,2). So cell (0,0) and (0,1),(1,0),(1,1) should have count 3.
Thus method works.
But note: N up to 100k, coordinate range small. We need to be careful with memory: 1001x1001 array of integers. Each integer may need to hold up to N (100,000). So use int (4 bytes) -> 1e6 * 4 = 4 MB, fine.
Implementation steps:
-
Read N.
-
Initialize diff array of size (MAX+1) x (MAX+1) all zeros. MAX=1000.
-
For each rectangle:
- read x1,y1,x2,y2.
- diff[x1][y1] += 1
- diff[x2][y1] -= 1
- diff[x1][y2] -= 1
- diff[x2][y2] += 1
-
After processing all rectangles, compute 2D prefix sum:
for i from 0 to MAX:
for j from 0 to MAX:
if i>0: diff[i][j] += diff[i-1][j]
if j>0: diff[i][j] += diff[i][j-1]
if i>0 and j>0: diff[i][j] -= diff[i-1][j-1]
But careful: We are using diff as both difference and prefix. We'll need to keep a temporary? Actually we can compute in-place, but we must ensure we use updated values correctly. Standard approach: Compute cumulative sums row-wise then column-wise, but since we are iterating in order, the formula above works if we add contributions from already updated cells (left, above, diagonal). However if we do in-place as described, it works because when we reach (i,j), diff[i-1][j] and diff[i][j-1] have already been updated to prefix sums, and diff[i-1][j-1] is also prefix. The addition then yields the prefix sum at (i,j). However note that the original diff values at (i,j) are the difference updates. So that algorithm correctly computes the prefix sum in place. It's standard.
Alternative: Do separate loops: first for i from 0..MAX: for j from 1..MAX: diff[i][j] += diff[i][j-1] (row prefix). Then for j from 0..MAX: for i from 1..MAX: diff[i][j] += diff[i-1][j] (col prefix). But careful: after row prefix, diff[i][j] becomes sum over row updates, then after column prefix, becomes 2D prefix. However if we do row then column, we need to ensure we treat correctly. Actually simpler: use double loop with the formula above.
However note: After this, diff[i][j] for i<MAX and j<MAX gives the count for cell (i,j). But we also have diff[MAX][] and diff[][MAX] which we ignore.
-
After computing prefix sums, iterate i from 0 to MAX-1, j from 0 to MAX-1, keep track of max value.
-
Print max.
Edge cases: N=0? But N>=1. Coordinates could be 0. So fine.
Potential pitfalls: The given coordinates: (x1,y1) top-left, (x2,y2) bottom-right. But note: if we treat y increasing downward, the rectangle covers y from y1 (top) to y2 (bottom). Since y1<y2, [y1,y2) works. So same as x.
But what if the rectangle is given with top-left having larger y? Usually in math coordinate systems, y increases upward, so top-left would have larger y. But they specify top-left and bottom-right, and y1 < y2. So consistent with downward y. So our interpretation is fine.
Now about reading: The input format shows each line: x1 y1 x2 y2. So we read accordingly.
Now we must check constraints: x2 <= 1000, y2 <=1000. So our MAX=1000 works.
But note: x1, y1 could be 0, and x2 could be 1000, etc.
Now complexity: O(N + MAX^2) ~ 1e5 + 1e6 = 1.1e6. Memory: 1001*1001 ints ~ 4MB.
One more check: Is it possible that maximum overlap occurs at a region not aligned with integer cells due to boundaries? Actually if we treat half-open intervals, the partition lines are at integers. But the rectangle boundaries are integer. Within each unit square (open square between integer lines), inclusion is constant. However there could be lines (vertical/horizontal) and points at integer coordinates where inclusion may differ. But those are measure-zero and may have different counts. But since those points are not interior to cells, they might have higher count? For example, suppose we have three rectangles: R1: [0,1) x [0,1); R2: [0,1) x [0,1); R3: [0,1) x [0,1). All same. Then cell (0,0) has count 3. Points at boundaries also have count 3 because they are included? Actually if rectangles include top and left boundaries, then points like (0,0) (top-left corner) are included in all three. So point (0,0) also has count 3. So max still 3. However there could be scenario where maximum count occurs only at a point but not in any cell. Example: consider two rectangles: A: [0,1) x [0,1) ; B: [1,2) x [0,1). They don't overlap in area, but do they share any point? The point (1,0) is in A? A includes x <1, so x=1 not included. B includes x>=1, includes x=1. So no common point. So no overlap.
But could we have three rectangles that all include a common point on a vertical line but none share area? For them all to include a point, that point must satisfy each rectangle's intervals. If rectangles are thin (zero width) we can't. Actually, if we have rectangles with zero width? Not allowed because x1 < x2, so width >0. So each rectangle covers a continuous interval of x. For a point to be common to many rectangles, the x-coordinate of that point must lie in each interval. Since intervals are continuous, there will be an open interval around that point where all intervals include it? Actually if a point x0 lies in the interior of each interval (not at endpoints), then there exists epsilon >0 such that (x0 - epsilon, x0 + epsilon) is within all intervals (since each interval is open at right? Actually intervals are half-open [x1,x2). If x0 is strictly greater than x1 and strictly less than x2 (since x2 is exclusive), then there is some neighborhood around x0 that stays within (x1,x2) as long as we stay away from endpoints. So the set of x where all intervals contain x0 is open, thus there will be a small area around the point where rectangles overlap. So maximum will be achieved on an open region, not just a point. If x0 equals x1 for some rectangle, that rectangle includes x0 because it includes its left endpoint. But if x0 equals x1 for some rectangle, then there might be no neighborhood to the left because left of x0 is less than x1 and not included. However to the right, it's included. So at a point on left boundary, the rectangle still covers an area to the right, so if all rectangles include that point and also extend to the right, there will be a small area to the right where they all overlap. So the maximum count would also be achieved in an area. For instance, if all rectangles have x1=0 and x2=1, then point (0,0.5) is interior? Actually (0,0.5) x=0 is left boundary. But there exists points with x>0 and small that are also in all rectangles. So the count is constant in the open region (0,1) x ... So again area. The only potential issue is if all rectangles only meet at a point because they are arranged such that the intersection of their interiors is empty, but the intersection of their closures is a point. That would require each rectangle to have the point as a corner, and they extend outward in different quadrants. For example, rectangle A: [0,1]x[0,1] (if closed), B: [0,1]x[-1,0] (if closed) would intersect at point (0,0) only. But our half-open representation: A: [0,1)x[0,1) includes point (0,0). B: [0,1)x[-1,0)?? But y1=-1, y2=0, includes top-left? Wait orientation: top-left has smaller y? If we have B: top-left (0,-1) bottom-right (1,0), that would cover y from -1 to 0. Then half-open: [0,1)x[-1,0). That includes points with y>= -1 and y<0. So (0,0) has y=0, which is not <0, so not included. So not overlapped. So not possible to have point-only intersection with half-open intervals that are all aligned? Could we have rectangles that all share a single point that is included by all? Example: rectangle R1: [0,1) x [0,1); R2: [0,1) x [0,1)? They overlap area. For a point-only intersection, you need rectangles that only touch at a corner. With half-open, if two rectangles share a corner: one includes its top-left corner, the other includes its bottom-right? But let's try: R1: [0,1)x[0,1); R2: [1,2)x[1,2). The point (1,1) is top-left of R2? R2 includes x>=1 and x<2, y>=1 and y<2, so includes (1,1). R1 includes x<1, y<1, so does not include (1,1). So no. If R2: [1,2)x[0,1), then (1,0) is included by R2? y=0 included if y>=0? Yes, includes (1,0). R1 includes (1,0)? x=1 not included, so no. So no common point. What about rectangles that all have the same corner (0,0) included and extend outward? That would be area overlapping.
Thus max will be achieved on cells.
So the grid method is valid.
But there is one nuance: The rectangle coordinates are given as integers, but does the rectangle include the boundaries? We concluded no, based on example 2. However, sometimes problems define rectangles by two corners and consider them inclusive of boundaries, but then treat overlapping as interior overlapping? But they gave example 2 which would yield 2 if boundaries inclusive, so they clearly treat boundaries as not overlapping. So half-open is safe.
But what about the case where x1 = x2? Not allowed because x1 < x2. So width>0.
Alright.
Now we should implement.
But careful: The constraints say 0 <= x1_i < x2_i <= 1000, same for y. So our MAX=1000.
Implementation in C++:
#include
#include
using namespace std;
const int MAX = 1000;
int diff[MAX+2][MAX+2]; // maybe +2 for safety
int main() {
int N;
cin >> N;
for (int i=0; i<N; ++i) {
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
diff[x1][y1]++;
diff[x2][y1]--;
diff[x1][y2]--;
diff[x2][y2]++;
}
int ans = 0;
// Compute prefix sums and track max for i<MAX, j<MAX
for (int i=0; i<=MAX; ++i) {
for (int j=0; j<=MAX; ++j) {
if (i>0) diff[i][j] += diff[i-1][j];
if (j>0) diff[i][j] += diff[i][j-1];
if (i>0 && j>0) diff[i][j] -= diff[i-1][j-1];
if (i<MAX && j<MAX) ans = max(ans, diff[i][j]);
}
}
cout << ans << endl;
return 0;
}
But wait: In the loops we update diff and then check for i<MAX && j<MAX to update ans. However note that after prefix sum, diff[i][j] for i and j up to MAX might be large due to accumulated contributions beyond the grid, but we only care about cells. This should work.
But is there any off-by-one? Let's test with small example manually using code simulation.
Set MAX=4? Actually maximum coordinate 4? But we use MAX=1000 constant. For test, we can set MAX=4 for coordinate range up to 4. But our diff array indexes up to 4? Actually if MAX=4, then x can be 0..4. cells are 0..3. So we should set MAX=1000 to include 1000 as index.
Testing example 1: N=2, rectangles as above.
We'll run mental simulation with code. It should produce 2.
But we must ensure that the prefix sum loops include i=0..MAX, j=0..MAX. For i=0, j=0: diff[0][0] initially 1 (from rect1) + maybe others? Actually rect1 gave +1 at (0,0). rect2 gave none at (0,0). So diff[0][0]=1. then ans? i<MAX? MAX=1000, i=0<1000 true, j=0<1000 true, ans=1. Next j=1,... etc. After processing, diff[2][1] should become 2? Let's compute systematically:
We'll do on small scale with MAX=4 (since coordinates up to 4). But careful: The code sets MAX constant to 1000, but we can mentally set to 4.
But the algorithm: After all diff updates, we compute prefix sum.
Let's compute manually for example1 with coordinates 0..4.
Initialize diff 5x5 zero. Updates:
Rect1: (0,0)+=1; (3,0)-=1; (0,2)-=1; (3,2)+=1.
Rect2: (2,1)+=1; (4,1)-=1; (2,3)-=1; (4,3)+=1.
So diff:
(0,0): +1
(3,0): -1
(0,2): -1
(3,2): +1
(2,1): +1
(4,1): -1
(2,3): -1
(4,3): +1
All other zeros.
Now compute prefix sums iterating i=0..4, j=0..4.
We'll do row by row.
i=0:
j=0: diff[0][0]=1; if i>0? no; if j>0? no; ans if i<4 and j<4 -> yes, ans=1.
j=1: diff[0][1] originally 0; i>0? no; j>0: add diff[0][0] (which is updated to 1) -> diff[0][1]=1; ans? i=0<4, j=1<4, ans= max(1,1)=1.
j=2: diff[0][2] originally -1; add diff[0][1] (1) -> becomes 0; ans=0? Actually ans remains 1.
j=3: diff[0][3] originally 0; add diff[0][2] (0) -> 0; ans=1.
j=4: diff[0][4] originally 0; add diff[0][3] (0) -> 0; but i<4? i=0<4 but j=4 is not <4? j<4 false, so no ans update.
i=1:
j=0: diff[1][0] originally 0; i>0: add diff[0][0] (1) -> 1; ans if i<4 (1<4) and j<4 (0<4) -> ans= max(1,1)=1.
j=1: diff[1][1] originally 0; i>0 add diff[0][1] (1) -> 1; j>0 add diff[1][0] (1) ->2; i>0 && j>0 subtract diff[0][0] (1) ->1; ans i=1<4,j=1<4 -> ans = max(1,1)=1 (still 1).
j=2: diff[1][2] originally 0; i>0 add diff[0][2] (0) ->0; j>0 add diff[1][1] (1) ->1; subtract diff[0][1] (1) ->0; ans=1.
j=3: diff[1][3] originally 0; i>0 add diff[0][3] (0); j>0 add diff[1][2] (0); subtract diff[0][2] (0) ->0; ans=1.
j=4: similar, ans not updated.
i=2:
j=0: diff[2][0] originally 0; i>0 add diff[1][0] (1) ->1; ans if i<4 j<4 -> ans=1.
j=1: diff[2][1] originally +1 (from rect2). But note we have already added contributions from previous updates? We'll compute: before adding contributions, diff[2][1] = 1 (from rect2). Then i>0 add diff[1][1] (1) -> becomes 2; j>0 add diff[2][0] (1) -> becomes 3; subtract diff[1][0] (1) -> becomes 2. So diff[2][1] = 2. Now i=2<4, j=1<4 -> ans = max(1,2)=2.
j=2: diff[2][2] originally 0; i>0 add diff[1][2] (0) ->0; j>0 add diff[2][1] (2) ->2; subtract diff[1][1] (1) ->1; ans=2.
j=3: diff[2][3] originally -1; i>0 add diff[1][3] (0) -> -1; j>0 add diff[2][2] (1) ->0; subtract diff[1][2] (0) ->0; ans=2.
j=4: diff[2][4] originally 0; etc.
i=3:
j=0: diff[3][0] originally -1; i>0 add diff[2][0] (1) ->0; ans? i=3<4 j=0<4 -> ans stays 2.
j=1: diff[3][1] originally 0; i>0 add diff[2][1] (2) ->2; j>0 add diff[3][0] (0) ->2; subtract diff[2][0] (1) ->1; ans=2.
j=2: diff[3][2] originally +1; i>0 add diff[2][2] (1) ->2; j>0 add diff[3][1] (1) ->3; subtract diff[2][1] (2) ->1; ans=2.
j=3: diff[3][3] originally 0; i>0 add diff[2][3] (0) ->0; j>0 add diff[3][2] (1) ->1; subtract diff[2][2] (1) ->0; ans=2.
j=4: diff[3][4] originally 0; etc.
i=4:
j=0: diff[4][0] originally 0; i>0 add diff[3][0] (0) ->0; ans? i=4 is not <4 (since MAX=4, cells only up to 3), so no update. But we still compute.
... final ans=2.
So matches.
Thus implementation correct.
But there is a catch: The coordinates are given as (x1, y1) top-left and (x2, y2) bottom-right. In our half-open interpretation, we used [x1, x2) x [y1, y2). However, what if the rectangle includes bottom and right edges but not top and left? That would be opposite. But example 2 suggests that the overlapping is not counted when rectangles touch at x=2. In our half-open, they do not overlap because one excludes x=2 and the other includes it. But if the rectangles were defined as inclusive of both ends, then they would overlap at the line. So indeed boundaries not overlapping.
But what if we used [x1, x2) x (y1, y2] (i.e., include bottom but not top)? That could also cause different results. But typical approach in computational geometry for rectangle overlap counting with integer coordinates is to treat them as covering intervals [x1, x2) and [y1, y2) after mapping coordinates to integers. The example strongly suggests that.
Thus solution is fine.
However, note that the coordinates are up to 1000, but N up to 100k. So we might have many rectangles, but the diff array size is small, so it's fine.
But wait: The problem might have intended a more general sweep-line solution because coordinate range is small, but our approach works.
Now confirm: Input format: first line N, then N lines each with four integers. So reading as above.
Edge case: Could the maximum overlap be 0? N>=1, so at least one rectangle, so max>=1.
Alright.
Now let's consider if there is any trick: Coordinates are integer and small, but rectangle corners are given as top-left and bottom-right. The plane orientation: Usually y increases upward, but they say top-left and bottom-right implying y decreases downward? However regardless, the rectangle region is defined. Since we treat the rectangle as covering all points between the corners, the actual ordering of y (whether y1y2) matters. But constraints state y1 < y2, so consistent with the typical computer graphics coordinate system where y increases downward. So our half-open intervals [x1, x2) x [y1, y2) are correct.
Now, think about possibility of negative coordinates? Constraints say 0 <= coordinates, so fine.
Thus answer.
But wait: The constraints say 0 <= x1_i < x2_i <= 1000, 0 <= y1_i < y2_i <= 1000. So coordinates are in range [0,1000]. That's quite small. This problem is essentially a 2D range add, point query max. But with small grid, difference array suffices.
Alternative approaches: Sweep line with segment tree. But not needed.
Thus final solution is straightforward.
Now, we need to output the maximum number of overlapped seals.
Implementation details in C++:
We'll code:
#include <bits/stdc++.h>
using namespace std;
const int MAXC = 1000;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<vector> diff(MAXC+2, vector(MAXC+2, 0));
for (int i=0; i<N; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
diff[x1][y1]++;
diff[x2][y1]--;
diff[x1][y2]--;
diff[x2][y2]++;
}
int ans = 0;
for (int i=0; i<=MAXC; i++) {
for (int j=0; j<=MAXC; j++) {
if (i>0) diff[i][j] += diff[i-1][j];
if (j>0) diff[i][j] += diff[i][j-1];
if (i>0 && j>0) diff[i][j] -= diff[i-1][j-1];
if (i < MAXC && j < MAXC) {
if (diff[i][j] > ans) ans = diff[i][j];
}
}
}
cout << ans << '\n';
return 0;
}
We can also use fixed-size array:
int diff[MAXC+2][MAXC+2] = {0};
But careful: MAXC+2=1002, but we only need up to 1000, but 1002 is fine.
Now confirm memory: 1002*1002 ~ 1e6 ints, about 4 MB.
Now, is there any chance that the maximum overlap is not captured in the cells? For example, consider a rectangle with x1=0, x2=1, y1=0, y2=1. That covers cell (0,0). Overlap count at point (0.5,0.5) is same as cell. Good.
But what about rectangles that are very thin? They have width at least 1 because x2 > x1 and both integers. So there is at least one unit of width? Actually if x1=0, x2=1, the rectangle covers x in [0,1). That includes all x from 0 to less than 1, which is width 1 in continuous sense. But the cell (0,0) is fully covered. So fine.
But what about x1=0, x2=0.5? Not possible because coordinates are integers.
Thus method robust.
But one more nuance: The input specifies "axis-aligned rectangular seals". The region which has the maximum number of overlapped seals. Overlap region: Could be a region of any shape. But as argued, maximum depth occurs in some cell.
Thus answer.
Now, we also need to consider the possibility of coordinates up to 1000 inclusive, but rectangle may have x2=1000. Then the cells go from 0 to 999. However, what about the region near x=999.5? It's in cell 999. Good. But what about x=999.9? Also cell 999. So fine.
Now, we must ensure that we correctly treat y coordinates similarly.
Now, we should also consider if there is any off-by-one due to top-left vs bottom-right orientation: In typical computer graphics, (x1,y1) is top-left, (x2,y2) bottom-right, so the rectangle covers x from x1 to x2-1? Actually if pixels, inclusive of both boundaries? But here it's continuous, not pixel. We already used half-open. But confirm with sample 2: output 1. Good.
Now, we can also test another sample: Suppose 2 rectangles: (0,0)-(1,1) and (0,0)-(1,1). Overlap count 2. Our method: diff updates then prefix sum: cell (0,0) will be 2. Good.
Suppose 2 rectangles: (0,0)-(1,2) and (0,1)-(1,3). They overlap in cell (0,1) only. (0,0)-(1,2): covers y=0,1 (since y2=2 => y<2). (0,1)-(1,3): covers y=1,2. So overlap at y=1. So max=2. Good.
Now, suppose rectangles share only a vertical line: (0,0)-(1,2) and (1,0)-(2,2). In our method: first covers x=0, y=0,1; second covers x=1, y=0,1. No common cell (x=1 not in first, x=0 not in second). So max=1. Good.
Thus implementation works.
Now, the problem may be from some contest where coordinate range is larger (like 10^9). But here constraints are small. So solution easy.
But wait: The constraints say 0 ≤ x1_i < x2_i ≤ 1000, 0 ≤ y1_i < y2_i ≤ 1000. So indeed small.
Thus answer.
Now, maybe the problem expects a sweep line approach but we are using difference array, which is simpler.
We'll produce final answer.
But before finalizing, double-check memory and potential overflow: diff values can go up to N=100000, which fits in int (2^31-1). So int fine.
Now, we can further optimize by not storing full 2D array but using 1D sweep line? But not needed.
Now, code.
But consider that we have to treat y coordinates as we treat x. In our algorithm, we used diff[x][y] with same orientation. But what if the rectangles are defined with y1 > y2? Not the case.
All good.
Now, finalize.
But there is one more nuance: The rectangle's definition: "top-left and the bottom-right corner" might imply that the rectangle includes its boundaries. But then overlapping at boundaries could be considered. However example 2 shows output 1, meaning they don't consider boundaries overlapping. So it's consistent with half-open. However if they considered boundaries inclusive, then would the answer be 2? Let's check: If inclusive, both rectangles cover x=2? Actually rectangle1: x from 0 to 2 inclusive; rectangle2: x from 2 to 4 inclusive. So they overlap at x=2 line. So any point with x=2, y in [0,2] would be in both, so max overlap 2. But they output 1. So boundaries are not overlapping. So half-open.
Thus final answer.
Now produce solution.
But wait: Could the problem's intended solution be a sweep line with coordinate compression? Because coordinates are only up to 1000, maybe they expect simple 2D array.
Given the constraints, it's fine.
Now, some might think about using bitsets, but not needed.
Now, I'll write the solution in the answer.
Additionally, note: The problem statement says "axis-aligned rectangular seals". Might be from JOI? No matter.
Now, implement.
But before output, we should consider that the maximum number of overlapped seals might be in a region that is not a cell? Actually we already argued it's constant per cell.
But we can also think about using 2D prefix sum technique for continuous values: For each rectangle, we add 1 to area. The 2D difference method gives the count for each unit cell after integration. So fine.
Now, time complexity: O(1000^2 + N) = ~1e6, fine.
Now, answer.
Will we encounter any issue with reading using scanf? Use cin with sync.
Now, write final solution.
But maybe we can further reduce memory by using array of short? Not needed.
Now, final code in C++.
Wait: I recall that some online judges might have input with extra spaces, but fine.
Now, produce final answer. ```cpp
#include
#include
#include
using namespace std;
const int MAXC = 1000; // coordinates are in [0, 1000]
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
// difference array, size (MAXC+1) x (MAXC+1) – indices 0..MAXC
vector<vector<int>> diff(MAXC + 2, vector<int>(MAXC + 2, 0));
for (int i = 0; i < N; ++i) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
// treat rectangle as [x1, x2) × [y1, y2)
diff[x1][y1] += 1;
diff[x2][y1] -= 1;
diff[x1][y2] -= 1;
diff[x2][y2] += 1;
}
int ans = 0;
// 2D prefix sum in-place, also track max for cells (i,j) with i<MAXC, j<MAXC
for (int i = 0; i <= MAXC; ++i) {
for (int j = 0; j <= MAXC; ++j) {
if (i > 0) diff[i][j] += diff[i-1][j];
if (j > 0) diff[i][j] += diff[i][j-1];
if (i > 0 && j > 0) diff[i][j] -= diff[i-1][j-1];
if (i < MAXC && j < MAXC) {
ans = max(ans, diff[i][j]);
}
}
}
cout << ans << '\n';
return 0;
}