Editorial for Signal Flood
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Approach
The coordinate values are at most , so there are only
unit squares that can contribute to the answer.
Instead of adding to every square inside every rectangle, use a 2D difference
array:
- add
at
- subtract
at
and
- add
at
After processing all rectangles, take 2D prefix sums. The prefix value at cell
is the number of transmitters covering the unit square with lower-left
corner
.
Count how many of those cells have value exactly .
This runs in time.
Solution (Python)
from array import array
import sys
COORD_MAX = 1000
SIZE = COORD_MAX + 1
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
n = next(it)
k = next(it)
diff = [array("i", [0]) * SIZE for _ in range(SIZE)]
for _ in range(n):
x1 = next(it)
y1 = next(it)
x2 = next(it)
y2 = next(it)
diff[x1][y1] += 1
diff[x2][y1] -= 1
diff[x1][y2] -= 1
diff[x2][y2] += 1
answer = 0
for x in range(SIZE):
row = diff[x]
prev_row = diff[x - 1] if x else None
running = 0
for y in range(SIZE):
running += row[y]
covered = running + (prev_row[y] if prev_row is not None else 0)
row[y] = covered
if x < COORD_MAX and y < COORD_MAX and covered == k:
answer += 1
print(answer)
Comments