結果
問題 | No.1290 Addition and Subtraction Operation |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:53:33 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,546 bytes |
コンパイル時間 | 242 ms |
コンパイル使用メモリ | 82,236 KB |
実行使用メモリ | 127,108 KB |
最終ジャッジ日時 | 2025-03-26 15:54:32 |
合計ジャッジ時間 | 18,703 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 70 WA * 15 |
ソースコード
import sysclass FenwickTree:def __init__(self, size):self.n = sizeself.tree = [0] * (self.n + 2) # 1-based indexingdef add(self, idx, delta):while idx <= self.n:self.tree[idx] += deltaidx += idx & -idxdef query(self, idx):res = 0while idx > 0:res += self.tree[idx]idx -= idx & -idxreturn resdef main():input = sys.stdin.read().split()ptr = 0N = int(input[ptr])ptr += 1M = int(input[ptr])ptr += 1B = list(map(int, input[ptr:ptr+N]))ptr += Noperations = []for _ in range(M):L = int(input[ptr])ptr += 1R = int(input[ptr])ptr += 1operations.append((L, R))# Sort operations in decreasing order of R, then decreasing Loperations.sort(key=lambda x: (-x[1], -x[0]))max_size = N + 2even_tree = FenwickTree(max_size)odd_tree = FenwickTree(max_size)for (L, R) in operations:# Compute K_icurrent_R = R# Query the current contributionif current_R % 2 == 0:contrib = even_tree.query(current_R)else:contrib = odd_tree.query(current_R)original_B = B[current_R - 1]current_B_R = original_B - contrib# Compute exponent R - Lexponent = current_R - Lsign = 1 if (exponent % 2 == 0) else -1K_i = current_B_R * sign# Compute C_i = K_i * (-1)^LL_parity = L % 2c_sign = -1 if L_parity else 1C_i = K_i * c_sign# Apply range update to even and odd trees# Even positions in [L, R]start_even = L if (L % 2 == 0) else L + 1end_even = R if (R % 2 == 0) else R - 1if start_even <= end_even:even_tree.add(start_even, C_i)even_tree.add(end_even + 1, -C_i)# Odd positions in [L, R]start_odd = L if (L % 2 == 1) else L + 1end_odd = R if (R % 2 == 1) else R - 1if start_odd <= end_odd:odd_tree.add(start_odd, -C_i)odd_tree.add(end_odd + 1, C_i)# Check all positionsall_zero = Truefor j in range(1, N + 1):if j % 2 == 0:contrib = even_tree.query(j)else:contrib = odd_tree.query(j)original = B[j - 1]if original - contrib != 0:all_zero = Falsebreakprint("YES" if all_zero else "NO")if __name__ == "__main__":main()