結果
| 問題 |
No.1000 Point Add and Array Add
|
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2020-02-28 22:13:40 |
| 言語 | Ruby (3.4.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 987 bytes |
| コンパイル時間 | 424 ms |
| コンパイル使用メモリ | 7,424 KB |
| 実行使用メモリ | 34,688 KB |
| 最終ジャッジ日時 | 2024-10-13 17:49:49 |
| 合計ジャッジ時間 | 11,782 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 9 WA * 13 |
コンパイルメッセージ
Syntax OK
ソースコード
class BinaryIndexTree
attr_reader :bit, :size
def initialize(size, init: 0)
@bit = Array.new(size + 1, init)
@size = size
end
def sum(i)
s = 0
while i > 0
s += bit[i]
i -= i & -i
end
s
end
def add(i, x)
while i <= size
bit[i] += x
i += i & -i
end
end
end
N, Q = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
B = Array.new(N + 1, 0)
C = Array.new(N + 2, 0)
bit = BinaryIndexTree.new(N + 2)
Q.times do
c, x, y = gets.chomp.split
x = x.to_i
y = y.to_i
if c == 'A'
cnt = bit.sum(x)
# p [x, cnt]
if cnt > 0
# p 'middle update'
B[x] += cnt * A[x - 1]
# p B
bit.add(x, -1)
bit.add(x + 1, 1)
C[x] -= cnt
C[x + 1] += cnt
end
A[x - 1] += y
else
bit.add(x, 1)
bit.add(y + 1, -1)
C[x] += 1
C[y + 1] -= 1
end
# p [:C, C]
end
c = 0
1.upto(N) do |i|
c += C[i]
B[i] += A[i - 1] * c
end
# p A
puts B[1..-1].join(' ')
siman