結果

問題 No.2065 Sum of Min
ユーザー ygd.ygd.
提出日時 2022-09-06 22:58:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 674 ms / 2,000 ms
コード長 2,788 bytes
コンパイル時間 462 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 109,636 KB
最終ジャッジ日時 2024-05-01 17:34:57
合計ジャッジ時間 14,600 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,736 KB
testcase_01 AC 39 ms
53,248 KB
testcase_02 AC 39 ms
52,992 KB
testcase_03 AC 39 ms
52,608 KB
testcase_04 AC 442 ms
105,836 KB
testcase_05 AC 414 ms
106,588 KB
testcase_06 AC 506 ms
107,488 KB
testcase_07 AC 329 ms
104,356 KB
testcase_08 AC 500 ms
107,996 KB
testcase_09 AC 590 ms
107,092 KB
testcase_10 AC 589 ms
109,284 KB
testcase_11 AC 588 ms
108,680 KB
testcase_12 AC 664 ms
107,192 KB
testcase_13 AC 657 ms
108,996 KB
testcase_14 AC 674 ms
108,588 KB
testcase_15 AC 637 ms
107,236 KB
testcase_16 AC 633 ms
108,836 KB
testcase_17 AC 644 ms
108,588 KB
testcase_18 AC 632 ms
109,392 KB
testcase_19 AC 660 ms
109,636 KB
testcase_20 AC 636 ms
108,456 KB
testcase_21 AC 655 ms
109,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy #DeepCopy: hoge = [_[:] for _ in hogehoge]
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]
#dx8 = [1,1,0,-1,-1,-1,0,1]
#dy8 = [0,1,1,1,0,-1,-1,-1]

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

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):
        self.update(i, 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)


def main():
    N,Q = map(int,input().split()); INF = pow(10,18)
    A = list(map(int,input().split()))
    PQ = []
    for i,a in enumerate(A):
        heappush(PQ,(-a,i))
    queries = []
    for i in range(Q):
        l,r,x = map(int,input().split())
        l -= 1; r -= 1
        queries.append((l,r,x,i))
    
    queries.sort(key = lambda x: x[2], reverse=True)
    ans = [-1]*Q

    originalTree = SegmentTree(A, segfunc, 0)
    changedTree = SegmentTree([0]*N, segfunc, 0) #変更されたものを1とする。

    for l,r,x,i in queries:
        # print("X",x)
        # print("pre",PQ)
        while PQ and -PQ[0][0] >= x:
            v,idx = heappop(PQ)
            v *= -1
            # print("V",v,"X",x)
            originalTree.update(idx,0)
            changedTree.update(idx,1)
        # print(PQ)
        # print(originalTree.sum(l,r+1),changedTree.sum(l,r+1))
        temp = originalTree.sum(l,r+1) + changedTree.sum(l,r+1) * x
        ans[i] = temp
    
    print(*ans,sep="\n")




if __name__ == '__main__':
    main()
0