結果

問題 No.1705 Mode of long array
ユーザー kohei2019kohei2019
提出日時 2023-06-04 19:12:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 264 ms / 3,000 ms
コード長 3,162 bytes
コンパイル時間 565 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 95,896 KB
最終ジャッジ日時 2023-08-28 08:21:31
合計ジャッジ時間 14,411 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,064 KB
testcase_01 AC 74 ms
71,400 KB
testcase_02 AC 74 ms
71,272 KB
testcase_03 AC 122 ms
78,480 KB
testcase_04 AC 119 ms
78,300 KB
testcase_05 AC 130 ms
78,236 KB
testcase_06 AC 167 ms
79,144 KB
testcase_07 AC 156 ms
78,632 KB
testcase_08 AC 154 ms
78,616 KB
testcase_09 AC 151 ms
78,844 KB
testcase_10 AC 152 ms
78,932 KB
testcase_11 AC 154 ms
78,732 KB
testcase_12 AC 142 ms
79,212 KB
testcase_13 AC 242 ms
91,656 KB
testcase_14 AC 213 ms
84,348 KB
testcase_15 AC 213 ms
79,628 KB
testcase_16 AC 227 ms
80,368 KB
testcase_17 AC 208 ms
79,308 KB
testcase_18 AC 194 ms
79,448 KB
testcase_19 AC 235 ms
81,764 KB
testcase_20 AC 204 ms
80,496 KB
testcase_21 AC 217 ms
89,764 KB
testcase_22 AC 255 ms
94,804 KB
testcase_23 AC 185 ms
78,084 KB
testcase_24 AC 181 ms
77,708 KB
testcase_25 AC 188 ms
77,768 KB
testcase_26 AC 191 ms
77,736 KB
testcase_27 AC 181 ms
77,804 KB
testcase_28 AC 181 ms
77,708 KB
testcase_29 AC 188 ms
77,772 KB
testcase_30 AC 193 ms
77,856 KB
testcase_31 AC 182 ms
77,820 KB
testcase_32 AC 184 ms
77,848 KB
testcase_33 AC 229 ms
95,624 KB
testcase_34 AC 230 ms
95,512 KB
testcase_35 AC 229 ms
95,740 KB
testcase_36 AC 228 ms
95,852 KB
testcase_37 AC 227 ms
95,764 KB
testcase_38 AC 224 ms
95,896 KB
testcase_39 AC 225 ms
95,668 KB
testcase_40 AC 225 ms
95,744 KB
testcase_41 AC 226 ms
95,644 KB
testcase_42 AC 225 ms
95,672 KB
testcase_43 AC 232 ms
95,720 KB
testcase_44 AC 239 ms
95,560 KB
testcase_45 AC 242 ms
95,764 KB
testcase_46 AC 248 ms
95,728 KB
testcase_47 AC 263 ms
95,680 KB
testcase_48 AC 264 ms
95,708 KB
testcase_49 AC 242 ms
95,596 KB
testcase_50 AC 246 ms
95,756 KB
testcase_51 AC 246 ms
95,688 KB
testcase_52 AC 247 ms
95,896 KB
testcase_53 AC 250 ms
95,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree:
    DEFAULT = {
        'min': 1 << 60,
        'max': -(1 << 60),
    }

    FUNC = {
        'min': min,
        'max': max,
    }

    def __init__(self, ls, mode='min', func=None, default=None):
        """
        要素ls, 関数mode (min,max,sum,prd(product),gcd,lmc,^,&,|)
        func,defaultを指定すれば任意の関数、単位元での計算が可能
        """
        N = len(ls)
        if default == None:
            self.default = self.DEFAULT[mode]
        else:
            self.default = default
        if func == None:
            self.func = self.FUNC[mode]
        else:
            self.func = func
        self.N = N
        self.K = (N - 1).bit_length()
        self.N2 = 1 << self.K
        self.dat = [self.default] * (2**(self.K + 1))
        for i in range(self.N):  # 葉の構築
            self.dat[self.N2 + i] = ls[i]
        self.build()

    def build(self):
        for j in range(self.N2 - 1, -1, -1):
            self.dat[j] = self.func(self.dat[j << 1], self.dat[j << 1 | 1])  # 親が持つ条件

    def leafvalue(self, x):  # リストのx番目の値
        return self.dat[x + self.N2]

    def update(self, x, y):  # index(x)をyに変更
        i = x + self.N2
        self.dat[i] = y
        while i > 0:  # 親の値を変更
            i >>= 1
            self.dat[i] = self.func(self.dat[i << 1], self.dat[i << 1 | 1])
        return

    def query(self, L, R):  # [L,R)の区間取得
        L += self.N2
        R += self.N2
        vL = self.default
        vR = self.default
        while L < R:
            if L & 1:
                vL = self.func(vL, self.dat[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.dat[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)
    
    def find_r(self):
        """
        max,minのインデックスを見つける(右優先)
        """
        m_value = self.dat[1]
        ind = 1
        while ind <= (2**(self.K)):
            ind_l = ind*2
            ind_r = ind*2 + 1
            if self.dat[ind_r] == m_value:
                ind = ind_r
            else:
                ind = ind_l
        return ind - 2**(self.K)

    def find_l(self):
        """
        max,minのインデックスを見つける(左優先)
        """
        m_value = self.dat[1]
        ind = 1
        while ind <= (2**(self.K + 1)):
            ind_l = ind*2
            ind_r = ind*2 + 1
            if self.dat[ind_l] == m_value:
                ind = ind_l
            else:
                ind = ind_r
        return ind - 2**(self.K)

    def __iter__(self):
        for i in range(self.N):
            yield self[i]

    def __getitem__(self, x): return self.leafvalue(x)
    def __setitem__(self, x, val): return self.update(x, val)

N,M = map(int,input().split())
lsA = [0]+list(map(int,input().split()))
Q = int(input())
SG = SegTree(lsA,mode='max')
ans = []
for i in range(Q):
    t,x,y = map(int,input().split())
    if t == 1:
        SG[x] += y
    elif t == 2:
        SG[x] -= y
    else:
        ans.append(SG.find_r())
print(*ans,sep='\n')
0