結果
| 問題 |
No.655 E869120 and Good Triangles
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 20:55:12 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,806 bytes |
| コンパイル時間 | 327 ms |
| コンパイル使用メモリ | 82,820 KB |
| 実行使用メモリ | 280,016 KB |
| 最終ジャッジ日時 | 2025-03-20 20:55:55 |
| 合計ジャッジ時間 | 5,000 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | AC * 10 TLE * 1 -- * 19 |
ソースコード
import sys
from collections import deque
def main():
N, K, P = map(int, sys.stdin.readline().split())
blacks = [tuple(map(int, sys.stdin.readline().split())) for _ in range(K)]
# Initialize distance matrix with infinity
INF = float('inf')
dist = [ [INF] * (x + 1) for x in range(N + 1) ]
q = deque()
for x, y in blacks:
dist[x][y] = 0
q.append((x, y))
# Multi-source BFS
while q:
x, y = q.popleft()
# Directions: up-left, up-right, left, right, down-left, down-right
directions = [ (-1, -1), (-1, 0), (0, -1), (0, 1), (1, 0), (1, 1) ]
for dx, dy in directions:
nx = x + dx
ny = y + dy
if 1 <= nx <= N and 1 <= ny <= nx:
if dist[nx][ny] > dist[x][y] + 1:
dist[nx][ny] = dist[x][y] + 1
q.append((nx, ny))
# Compute row_prefix
row_prefix = [ [0] * (N + 2) for _ in range(N + 2) ]
for x in range(1, N + 1):
for y in range(1, x + 1):
row_prefix[x][y] = row_prefix[x][y - 1] + dist[x][y]
# Fill rest with row_prefix[x][x]
for y in range(x + 1, N + 2):
row_prefix[x][y] = row_prefix[x][x]
# Compute column_prefix for columns 0 to N (max y is N since row N has y up to N)
max_col = N
column_prefix = [ [0] * (N + 2) for _ in range(max_col + 2) ]
for y in range(0, max_col + 1):
current_sum = 0
column_prefix[y][0] = 0
for x in range(1, N + 1):
if y == 0:
val = 0
else:
if y > x:
val = 0
else:
val = row_prefix[x][y]
current_sum += val
column_prefix[y][x] = current_sum
# Compute diagonal_prefix for c from 0 to -N+1
diagonal_prefix = {}
start_x_for_c = {}
max_c = 0
min_c = - (N - 1)
for c in range(min_c, 1):
start_x = max(1, 1 - c)
arr = [0] * (N + 2)
sum_c = 0
current_sum = 0
prev = 0
for x in range(1, N + 1):
y = x + c
if x < start_x or y < 1 or y > x:
arr[x] = current_sum
prev = current_sum
continue
current_sum += row_prefix[x][y]
arr[x] = current_sum
prev = current_sum
diagonal_prefix[c] = arr
start_x_for_c[c] = start_x
total = 0
for i in range(1, N + 1):
max_s = N - i + 1
if max_s < 1:
continue
for j in range(1, i + 1):
c = j - i
if c < min_c:
continue
# Binary search for minimal s where sum >= P
low = 1
high = max_s
s0 = high + 1
while low <= high:
mid = (low + high) // 2
s = mid
start_x = start_x_for_c.get(c, 1)
valid_low = max(i, start_x)
valid_high = min(i + s - 1, N)
sum_d = 0
if valid_low <= valid_high:
sum_d = diagonal_prefix[c][valid_high] - (diagonal_prefix[c][valid_low - 1] if valid_low > 1 else 0)
# Compute sum_col
if j == 1:
sum_col = 0
else:
sum_col = column_prefix[j - 1][i + s - 1] - column_prefix[j - 1][i - 1]
total_sum = sum_d - sum_col
if total_sum >= P:
s0 = mid
high = mid - 1
else:
low = mid + 1
if s0 <= max_s:
total += max_s - s0 + 1
print(total)
if __name__ == "__main__":
main()
lam6er