結果
問題 | No.1000 Point Add and Array Add |
ユーザー | convexineq |
提出日時 | 2020-02-28 23:13:32 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 417 ms / 2,000 ms |
コード長 | 3,013 bytes |
コンパイル時間 | 310 ms |
コンパイル使用メモリ | 82,100 KB |
実行使用メモリ | 135,048 KB |
最終ジャッジ日時 | 2024-04-21 20:33:16 |
合計ジャッジ時間 | 6,371 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,864 KB |
testcase_01 | AC | 41 ms
53,120 KB |
testcase_02 | AC | 41 ms
53,120 KB |
testcase_03 | AC | 41 ms
52,864 KB |
testcase_04 | AC | 41 ms
52,992 KB |
testcase_05 | AC | 40 ms
52,824 KB |
testcase_06 | AC | 41 ms
53,248 KB |
testcase_07 | AC | 41 ms
52,864 KB |
testcase_08 | AC | 40 ms
52,864 KB |
testcase_09 | AC | 41 ms
52,608 KB |
testcase_10 | AC | 42 ms
53,120 KB |
testcase_11 | AC | 41 ms
52,864 KB |
testcase_12 | AC | 77 ms
73,840 KB |
testcase_13 | AC | 70 ms
71,680 KB |
testcase_14 | AC | 97 ms
77,396 KB |
testcase_15 | AC | 89 ms
76,800 KB |
testcase_16 | AC | 321 ms
118,528 KB |
testcase_17 | AC | 298 ms
115,708 KB |
testcase_18 | AC | 411 ms
134,780 KB |
testcase_19 | AC | 417 ms
135,048 KB |
testcase_20 | AC | 322 ms
133,128 KB |
testcase_21 | AC | 388 ms
134,920 KB |
testcase_22 | AC | 381 ms
134,956 KB |
testcase_23 | AC | 406 ms
134,712 KB |
ソースコード
class segment_tree_dual: def __init__(self, N, compose, funcval, ID_M=None): self.compose = compose self.ID_M = ID_M self.funcval = funcval self.height = (N-1).bit_length() #木の段数 self.N0 = 1<<self.height #木の横幅 >= N self.laz = [self.ID_M]*(2*self.N0) #作用素の木 self.val = None #値の配列 #初期値の配列を作る def build(self,initial): self.val = initial[:] #laz[k] を子に伝える、k が一番下の場合は laz[k] を val に反映する def propagate(self,k): if self.laz[k] == self.ID_M: return; if self.N0 <= k: self.val[k-self.N0] = self.funcval(self.val[k-self.N0], self.laz[k]) self.laz[k] = self.ID_M else: self.laz[(k<<1) ] = self.compose(self.laz[(k<<1) ],self.laz[k]); self.laz[(k<<1)+1] = self.compose(self.laz[(k<<1)+1],self.laz[k]); self.laz[k] = self.ID_M; # 遅延をすべて解消する def propagate_all(self): upto = self.N0 + len(self.val) for i in range(1,upto): self.propagate(i) # laz[k]およびその上に位置する作用素をすべて伝播 def thrust(self,k): for i in range(self.height,-1,-1): self.propagate(k>>i) # 区間[l,r]に関数 f を作用 def update(self, L,R,f): L += self.N0; R += self.N0+1 """まず伝播させる(オペレータが可換なら必要ない)""" #self.thrust(L) #self.thrust(R-1) #登りながら関数 f を合成 while L < R: if R & 1: R -= 1 self.laz[R] = self.compose(self.laz[R],f) if L & 1: self.laz[L] = self.compose(self.laz[L],f) L += 1 L >>= 1; R >>= 1 # values[k] を取得。 def point_get(self, k): res = self.val[k] k += self.N0 while k: if self.laz[k] != self.ID_M: res = self.funcval(res, self.laz[k]) k //= 2 return res # values[k] = x 代入する def point_set(self, k): self.thrust(k+self.N0) self.val[k] = x # coding: utf-8 # Your code here! import sys readline = sys.stdin.readline read = sys.stdin.read #n,*a = [int(i) for i in read().split()] #a = [int(i) for i in readline().split()] #n = int(input()) from itertools import accumulate n,q = [int(i) for i in readline().split()] a = [int(i) for i in readline().split()] cxy = [readline().split() for _ in range(q)] from operator import add seg = segment_tree_dual(n, add,add, ID_M=0) seg.build([0]*n) ans = [0]*n for c,x,y in cxy[::-1]: x = int(x)-1 y = int(y) if c == "A": ans[x] += y*seg.point_get(x) else: seg.update(x,y-1,1) #print(*ans) seg.propagate_all() for x,ax in enumerate(a): ans[x] += ax*seg.val[x] print(*ans)