結果

問題 No.789 範囲の合計
ユーザー convexineqconvexineq
提出日時 2021-03-19 03:06:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,672 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 50,224 KB
最終ジャッジ日時 2024-04-28 16:47:00
合計ジャッジ時間 12,844 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,752 KB
testcase_01 AC 32 ms
10,752 KB
testcase_02 TLE -
testcase_03 AC 638 ms
25,600 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 628 ms
25,856 KB
testcase_08 TLE -
testcase_09 AC 947 ms
39,356 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 27 ms
10,880 KB
testcase_14 AC 28 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class segment_tree:
    __slots__ = ["op_M", "e_M","N","N0","dat"]
    def __init__(self, N, operator_M, e_M):
        self.op_M = operator_M
        self.e_M = e_M
        self.N = N
        self.N0 = 1<<(N-1).bit_length()
        self.dat = [self.e_M]*(2*self.N0)
    
    # 長さNの配列 initial で初期化
    def build(self, initial):
        assert self.N == len(initial)
        self.dat[self.N0:self.N0+len(initial)] = initial[:]
        for k in range(self.N0-1,0,-1):
            self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])

    # a_k の値を x に更新
    def update(self,k,x):
        k += self.N0
        self.dat[k] = x
        k >>= 1
        while k:
            self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])
            k >>= 1

    # 区間[L,R]をopでまとめる
    def query(self,L,R):
        L += self.N0; R += self.N0 + 1 
        sl = sr = self.e_M
        while L < R:
            if R & 1:
                R -= 1
                sr = self.op_M(self.dat[R],sr)
            if L & 1:
                sl = self.op_M(sl,self.dat[L])
                L += 1
            L >>= 1; R >>= 1
        return self.op_M(sl,sr)

    def get(self, k): #k番目の値を取得。query[k,k]と同じ
        return self.dat[k+self.N0]

n = int(input())
query = [list(map(int,input().split())) for _ in range(n)]
s = set()
for i,a,b in query:
    s.add(a)
    s.add(b)
s = sorted(s)
za = {x:i for i,x in enumerate(s)}
from operator import add
ans = 0
seg = segment_tree(len(za), add, 0)
for i,a,b in query:
    if i==0:
        j = za[a]
        seg.update(j,seg.get(j)+b)
    else:
        ans += seg.query(za[a],za[b])
print(ans)
0