結果

問題 No.2065 Sum of Min
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-09-02 22:48:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 732 ms / 2,000 ms
コード長 1,642 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 106,920 KB
最終ジャッジ日時 2024-04-27 22:26:55
合計ジャッジ時間 14,294 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,152 KB
testcase_01 AC 39 ms
53,648 KB
testcase_02 AC 40 ms
54,156 KB
testcase_03 AC 40 ms
53,236 KB
testcase_04 AC 573 ms
106,908 KB
testcase_05 AC 574 ms
106,676 KB
testcase_06 AC 449 ms
106,280 KB
testcase_07 AC 307 ms
106,892 KB
testcase_08 AC 433 ms
104,596 KB
testcase_09 AC 728 ms
104,904 KB
testcase_10 AC 717 ms
106,072 KB
testcase_11 AC 718 ms
106,920 KB
testcase_12 AC 726 ms
104,368 KB
testcase_13 AC 727 ms
104,652 KB
testcase_14 AC 714 ms
104,400 KB
testcase_15 AC 728 ms
104,268 KB
testcase_16 AC 720 ms
104,740 KB
testcase_17 AC 709 ms
104,776 KB
testcase_18 AC 729 ms
104,276 KB
testcase_19 AC 716 ms
105,040 KB
testcase_20 AC 732 ms
104,920 KB
testcase_21 AC 714 ms
105,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def build(self, list):
        self.tree[1:] = list.copy()
        for i in range(self.size+1):
            j = i + (i & (-i))
            if j < self.size+1:
                self.tree[j] += self.tree[i]

    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    # 総和がx以上になる位置のindex をbinary search
    def bsearch(self,x):
        le = 0
        ri = 1<<(self.size.bit_length()-1)
        while ri > 0:
            if le+ri <= self.size and self.tree[le+ri]<x:
                x -= self.tree[le+ri]
                le += ri
            ri >>= 1
        return le+1


n,q = map(int,input().split())
A = list(map(int,input().split()))
Q = []
for i in range(q):
    l,r,x = map(int,input().split())
    Q.append([i,l,r,x])

ans = [0]*q

bit_sum = BIT(n+5)
bit_num = BIT(n+5)

SA = [[a,i+1] for i,a in enumerate(A)]
SA.sort()
Q.sort(key=lambda x: x[3])

now = 0

for ind,l,r,x in Q:

    while now < n and SA[now][0] <= x:
        a,i = SA[now]
        bit_sum.add(i,a)
        bit_num.add(i,1)
        now += 1

    num = bit_num.sum(r+1)-bit_num.sum(l)
    count = bit_sum.sum(r+1)-bit_sum.sum(l) + (r-l+1-num)*x 
    # print(l,r,x,num,count,now)
    ans[ind] = count

for i in ans:
    print(i)   
0