0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ABC442を解いた【セグ木】

Posted at

筆者はレート800前後の茶~緑コーダ

ABC442のD問題を解いていく

実装コード

区間和とピンポイントの値更新ってことでセグ木を頑張って使う

セグ木の実装は以下を使用する

main.py
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, Counter, deque
from functools import reduce, lru_cache
from itertools import product, accumulate, groupby, combinations
import sys
import os
def rI(): return int(sys.stdin.readline().rstrip())
def rLI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def rI1(): return (int(sys.stdin.readline().rstrip())-1)
def rLI1(): return list(map(lambda a:int(a)-1,sys.stdin.readline().rstrip().split()))
def rS(): return sys.stdin.readline().rstrip()
def rLS(): return list(sys.stdin.readline().rstrip().split())
IS_LOCAL = int(os.getenv("ATCODER", "0"))==0
err = (lambda *args, **kwargs: print(*args, **kwargs, file=sys.stderr)) if IS_LOCAL else (lambda *args, **kwargs: None)

class segtree():
    #省略

def main():
    N, Q = rLI()
    A = rLI()
    
    G=segtree(A,(lambda x,y:x+y),0)
    
    for _ in range(Q):
        n, *q = rLI()
        if n == 1:
            x = q[0]-1
            a0 = G.get(x)
            a1 = G.get(x+1)
            G.set(x,a1)
            G.set(x+1,a0)
        if n == 2:
            l,r=q
            l-=1
            print(G.prod(l,r))
    
    
if __name__ == '__main__':
    main()

感想

パット見でD問題を解けてワーイとか思ってたけど
diffが灰色の問題だしなんなら
公式解説によると累積和の更新だけでOKでよかった話だしで卒倒

まだまだ精進が足りませんなー

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?