結果

問題 No.2640 traO Stamps
ユーザー 👑 timitimi
提出日時 2024-02-19 22:16:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 763 ms / 2,000 ms
コード長 2,949 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 104,308 KB
最終ジャッジ日時 2024-02-19 22:16:38
合計ジャッジ時間 20,760 ms
ジャッジサーバーID
(参考情報)
judge15 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,460 KB
testcase_01 AC 38 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 35 ms
53,460 KB
testcase_04 AC 34 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 592 ms
95,532 KB
testcase_07 AC 605 ms
95,516 KB
testcase_08 AC 681 ms
100,544 KB
testcase_09 AC 474 ms
91,152 KB
testcase_10 AC 692 ms
100,524 KB
testcase_11 AC 643 ms
93,160 KB
testcase_12 AC 634 ms
103,640 KB
testcase_13 AC 706 ms
93,380 KB
testcase_14 AC 730 ms
100,568 KB
testcase_15 AC 712 ms
93,352 KB
testcase_16 AC 647 ms
100,292 KB
testcase_17 AC 650 ms
100,436 KB
testcase_18 AC 618 ms
93,300 KB
testcase_19 AC 677 ms
103,832 KB
testcase_20 AC 510 ms
100,672 KB
testcase_21 AC 638 ms
93,224 KB
testcase_22 AC 763 ms
103,796 KB
testcase_23 AC 580 ms
91,152 KB
testcase_24 AC 703 ms
91,300 KB
testcase_25 AC 550 ms
103,980 KB
testcase_26 AC 588 ms
104,308 KB
testcase_27 AC 581 ms
104,308 KB
testcase_28 AC 606 ms
104,308 KB
testcase_29 AC 631 ms
104,308 KB
testcase_30 AC 583 ms
103,864 KB
testcase_31 AC 561 ms
103,928 KB
testcase_32 AC 552 ms
98,116 KB
testcase_33 AC 540 ms
100,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M,K=map(int, input().split())
S=list(map(int, input().split()))
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

##############################
n,w =N,M
d = [[float("inf")]*n for i in range(n)] 
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(w):
  x,y,z = map(int,input().split())
  x-=1;y-=1
  d[x][y] = z;d[y][x] = z
for i in range(n):
  d[i][i] = 0 #自身のところに行くコストは0
B=(warshall_floyd(d))

#####segfunc#####
def segfunc(x, y):
    return x+y
#################

#####ide_ele#####
ide_ele = 0
#################

class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(N)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

A=[]
for i in range(K):
  p,q=S[i],S[i+1]
  A.append(B[p-1][q-1])

seg = SegTree(A, segfunc, ide_ele)
Q=int(input())
for i in range(Q):
  t,x,y=map(int, input().split())
  if t==1:
    p,q=x-1,x 
    if x-1>=0:
      l=S[x-1]-1
      d=B[l][y-1]
      seg.update(x-1,d)
    if x+1<=K:
      r=S[x+1]-1
      d=B[r][y-1]
      seg.update(x,d)
    S[x]=y 
  else:
    print(seg.query(x,y))
      
      
      
# print(seg.query(0, 8))
# seg.update(5, 0)
# print(seg.query(0, 8))
0