結果
問題 |
No.2230 Good Omen of White Lotus
|
ユーザー |
|
提出日時 | 2025-06-15 23:48:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 557 ms / 2,000 ms |
コード長 | 2,251 bytes |
コンパイル時間 | 417 ms |
コンパイル使用メモリ | 82,380 KB |
実行使用メモリ | 115,764 KB |
最終ジャッジ日時 | 2025-06-15 23:48:34 |
合計ジャッジ時間 | 14,604 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 44 |
ソースコード
## https://yukicoder.me/problems/no/2435 MOD = 998244353 class SegmentTree: """ 非再帰版セグメント木。 更新は「加法」、取得は「最大値」のもの限定。 """ def __init__(self, init_array): n = 1 while n < len(init_array): n *= 2 self.size = n self.array = [0] * (2 * self.size) for i, a in enumerate(init_array): self.array[self.size + i] = a end_index = self.size start_index = end_index // 2 while start_index >= 1: for i in range(start_index, end_index): self.array[i] = max(self.array[2 * i], self.array[2 * i + 1]) end_index = start_index start_index = end_index // 2 def set(self, x, a): index = self.size + x self.array[index] = a while index > 1: index //= 2 self.array[index] = max(self.array[2 * index], self.array[2 * index + 1]) def get_max(self, l, r): L = self.size + l; R = self.size + r # 2. 区間[l, r)の最大値を求める s = 0 while L < R: if R & 1: R -= 1 s = max(s, self.array[R]) if L & 1: s = max(s, self.array[L]) L += 1 L >>= 1; R >>= 1 return s def main(): H, W, N, P = map(int, input().split()) xy = [] for _ in range(N): x, y = map(int, input().split()) xy.append((x - 1, y - 1)) h_array = [[] for _ in range(H)] for x, y in xy: h_array[x].append(y) for h in range(H): h_array[h].sort() seg_tree = SegmentTree([0] * W) for h in range(H): for w in h_array[h]: x = seg_tree.get_max(0, w + 1) y = x + 1 seg_tree.set(w, y) x = seg_tree.get_max(0, W) inv_p = pow(P, MOD - 2, MOD) inv_px = pow(inv_p, H + W - 3, MOD) p1 = pow(P - 1, H + W - 3 - x, MOD) p2 = pow(P - 2, x, MOD) answer = (p1 * p2) % MOD answer *= inv_px answer %= MOD answer = 1 - answer answer %= MOD print(answer) if __name__ == '__main__': main()