結果

問題 No.877 Range ReLU Query
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-06-25 15:51:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 918 ms / 2,000 ms
コード長 1,782 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 82,500 KB
実行使用メモリ 114,588 KB
最終ジャッジ日時 2024-04-25 22:37:10
合計ジャッジ時間 10,425 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,812 KB
testcase_01 AC 73 ms
74,288 KB
testcase_02 AC 66 ms
72,652 KB
testcase_03 AC 78 ms
76,756 KB
testcase_04 AC 45 ms
61,312 KB
testcase_05 AC 55 ms
67,864 KB
testcase_06 AC 58 ms
70,180 KB
testcase_07 AC 55 ms
67,380 KB
testcase_08 AC 80 ms
76,476 KB
testcase_09 AC 41 ms
61,584 KB
testcase_10 AC 65 ms
73,280 KB
testcase_11 AC 837 ms
111,844 KB
testcase_12 AC 754 ms
108,264 KB
testcase_13 AC 647 ms
101,932 KB
testcase_14 AC 689 ms
103,024 KB
testcase_15 AC 913 ms
113,776 KB
testcase_16 AC 897 ms
112,152 KB
testcase_17 AC 918 ms
113,480 KB
testcase_18 AC 867 ms
111,648 KB
testcase_19 AC 848 ms
113,940 KB
testcase_20 AC 900 ms
114,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree:
    """ define what you want to do with 0 index, ex) size = tree_size, func = min or max, sta = default_value """
    
    def __init__(self,size,func,sta):
        self.n = size
        self.size = 1 << size.bit_length()
        self.func = func
        self.sta = sta
        self.tree = [sta]*(2*self.size)

    def build(self, list):
        """ set list and update tree"""
        for i,x in enumerate(list,self.size):
            self.tree[i] = x

        for i in range(self.size-1,0,-1):
            self.tree[i] = self.func(self.tree[i<<1],self.tree[i<<1 | 1])

    def set(self,i,x):
        i += self.size
        self.tree[i] = x
        while i > 1:
            i >>= 1
            self.tree[i] = self.func(self.tree[i<<1],self.tree[i<<1 | 1])

    
    def get(self,l,r):
        """ take the value of [l r) with func (min or max)"""
        l += self.size
        r += self.size
        res = self.sta

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


n,q = map(int,input().split())
A = list(map(int,input().split()))
Q = [list(map(int,input().split())) for i in range(q)]
sA = [[a,i] for i,a in enumerate(A)]
sQ = [[Q[i][3],i] for i in range(q)]
sA.sort(reverse=True)
sQ.sort(reverse=True)
def func(x,y):
    return x+y
seg1 = SegTree(n,func,0)
seg2 = SegTree(n,func,0)
now = 0
ans = [0]*q
for x,ind in sQ:
    while now < n and sA[now][0] >= x:
        a,i = sA[now]
        seg1.set(i,a)
        seg2.set(i,1)
        now += 1
    _,l,r,_ = Q[ind]

    count = seg1.get(l-1,r)-x*seg2.get(l-1,r)
    ans[ind] = count
    
for i in ans:
    print(i)
0