結果

問題 No.2065 Sum of Min
ユーザー lilictakalilictaka
提出日時 2022-09-03 13:26:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,031 ms / 2,000 ms
コード長 2,534 bytes
コンパイル時間 236 ms
コンパイル使用メモリ 82,492 KB
実行使用メモリ 136,396 KB
最終ジャッジ日時 2024-04-28 11:56:40
合計ジャッジ時間 18,869 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,272 KB
testcase_01 AC 39 ms
54,272 KB
testcase_02 AC 39 ms
54,144 KB
testcase_03 AC 43 ms
54,912 KB
testcase_04 AC 694 ms
129,072 KB
testcase_05 AC 632 ms
129,024 KB
testcase_06 AC 522 ms
112,932 KB
testcase_07 AC 293 ms
105,888 KB
testcase_08 AC 506 ms
112,868 KB
testcase_09 AC 920 ms
135,492 KB
testcase_10 AC 778 ms
136,396 KB
testcase_11 AC 826 ms
136,364 KB
testcase_12 AC 1,031 ms
135,224 KB
testcase_13 AC 958 ms
135,260 KB
testcase_14 AC 958 ms
135,348 KB
testcase_15 AC 1,027 ms
135,136 KB
testcase_16 AC 970 ms
135,476 KB
testcase_17 AC 1,005 ms
135,472 KB
testcase_18 AC 962 ms
135,600 KB
testcase_19 AC 991 ms
135,640 KB
testcase_20 AC 982 ms
135,344 KB
testcase_21 AC 1,004 ms
135,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree(object):
    def __init__(self, A, dot, unit):
        n = 1 << (len(A) - 1).bit_length()
        tree = [unit] * (2 * n)
        for i, v in enumerate(A):
            tree[i + n] = v
        for i in range(n - 1, 0, -1):
            tree[i] = dot(tree[i << 1], tree[i << 1 | 1])
        self._n = n
        self._tree = tree
        self._dot = dot
        self._unit = unit

    def __getitem__(self, i):
        return self._tree[i + self._n]

    def update(self, i, v):
        i += self._n
        self._tree[i] = v
        while i != 1:
            i >>= 1
            self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1])

    def add(self, i, v,ope):
        self.update(i, ope(self[i],v))

    def sum(self, l, r): #これで[l,r)から取り出す。
        l += self._n
        r += self._n
        l_val = r_val = self._unit
        while l < r:
            if l & 1:
                l_val = self._dot(l_val, self._tree[l])
                l += 1
            if r & 1:
                r -= 1
                r_val = self._dot(self._tree[r], r_val)
            l >>= 1
            r >>= 1
        return self._dot(l_val, r_val)
from collections import defaultdict
from bisect import bisect_left
def compress(A):
    tmp = defaultdict(int)
    B = list(set(A))
    B.sort()
    zipped = [] #i番目のAの要素は何番目に大きいか
    unzipped = [] # i番目に大きいAの要素は何か
    for index,b in enumerate(B):
        tmp[b] = index
        unzipped.append(b)
    for a in A:
        zipped.append(tmp[a])
    return zipped,unzipped
N,Q = map(int,input().split())
A = [0] + list(map(int,input().split()))
zipped,unzipped = compress(A)
sA = sorted(list(set(A)))
n = len(sA)
seg = SegmentTree([0] * n,lambda x,y:x+y,0)
cnt = SegmentTree([0] * n,lambda x,y:x+y,0)
query = [0] * Q
board = [[] for _ in range(N+1)]
for i in range(Q):
    l,r,x = map(int,input().split())
    l-=1
    board[l].append((-1,i,x))
    board[r].append((1,i,x))
for i in range(N+1):
    seg.add(zipped[i],A[i],lambda x,y:x+y)
    cnt.add(zipped[i],1,lambda x,y:x+y)
    for flag,index,x in board[i]:
        if flag == -1:
            l = bisect_left(sA,x+1)
            query[index] -= seg.sum(0,n)
            query[index] += seg.sum(l,n)
            query[index] -= cnt.sum(l,n) * x
        elif flag == 1:
            l = bisect_left(sA,x+1)
            query[index] += seg.sum(0,n)
            query[index] -= seg.sum(l,n)
            query[index] += cnt.sum(l,n) * x
print(*query,sep='\n')
0