結果
| 問題 |
No.1000 Point Add and Array Add
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 20:46:24 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 391 ms / 2,000 ms |
| コード長 | 2,403 bytes |
| コンパイル時間 | 190 ms |
| コンパイル使用メモリ | 82,320 KB |
| 実行使用メモリ | 191,196 KB |
| 最終ジャッジ日時 | 2025-03-20 20:46:31 |
| 合計ジャッジ時間 | 5,743 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
class FenwickTree:
def __init__(self, size):
self.n = size
self.tree = [0] * (self.n + 2) # 1-based indexing up to size + 1
def add(self, idx, val):
while idx <= self.n:
self.tree[idx] += val
idx += idx & -idx
def query(self, idx):
res = 0
while idx > 0:
res += self.tree[idx]
idx -= idx & -idx
return res
def main():
import sys
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
Q = int(input[ptr])
ptr += 1
a = list(map(int, input[ptr:ptr + N]))
ptr += N
a = [0] + a # Convert to 1-based index
queries = []
diff = [0] * (N + 2) # For prefix sum to calculate cnt_b
for time in range(1, Q + 1):
c = input[ptr]
ptr += 1
x = int(input[ptr])
ptr += 1
y = int(input[ptr])
ptr += 1
if c == 'B':
queries.append((time, 'B', x, y))
# Update the difference array for cnt_b
diff[x] += 1
if y + 1 <= N:
diff[y + 1] -= 1
else:
queries.append((time, 'A', x, y))
# Compute cnt_b using the difference array
cnt_b = [0] * (N + 1)
current = 0
for i in range(1, N + 1):
current += diff[i]
cnt_b[i] = current
# Initialize B array with a[i] * cnt_b[i]
B = [0] * (N + 1)
for i in range(1, N + 1):
B[i] = a[i] * cnt_b[i]
# Prepare events for processing in reverse order
events = []
for q in queries:
if q[1] == 'B':
t, typ, x, y = q
events.append((t, 'B', x, y))
else:
t, typ, x, y = q
events.append((t, 'A', x, y))
# Sort events by descending time; B comes before A at the same time
events.sort(key=lambda e: (-e[0], 0 if e[1] == 'B' else 1))
# Process events using Fenwick Tree
ft = FenwickTree(N + 1) # Size is N+1 to handle up to N+1
for event in events:
if event[1] == 'B':
x = event[2]
y = event[3]
ft.add(x, 1)
ft.add(y + 1, -1)
else:
x = event[2]
val_y = event[3]
count = ft.query(x)
B[x] += val_y * count
# Output the result for B[1..N]
print(' '.join(map(str, B[1:N+1])))
if __name__ == "__main__":
main()
lam6er