結果
問題 | No.1705 Mode of long array |
ユーザー |
|
提出日時 | 2023-06-04 19:12:13 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 249 ms / 3,000 ms |
コード長 | 3,162 bytes |
コンパイル時間 | 366 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 94,976 KB |
最終ジャッジ日時 | 2024-12-29 03:42:45 |
合計ジャッジ時間 | 13,444 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 51 |
ソースコード
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 = defaultif func == None:self.func = self.FUNC[mode]else:self.func = funcself.N = Nself.K = (N - 1).bit_length()self.N2 = 1 << self.Kself.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.N2self.dat[i] = ywhile i > 0: # 親の値を変更i >>= 1self.dat[i] = self.func(self.dat[i << 1], self.dat[i << 1 | 1])returndef query(self, L, R): # [L,R)の区間取得L += self.N2R += self.N2vL = self.defaultvR = self.defaultwhile L < R:if L & 1:vL = self.func(vL, self.dat[L])L += 1if R & 1:R -= 1vR = self.func(self.dat[R], vR)L >>= 1R >>= 1return self.func(vL, vR)def find_r(self):"""max,minのインデックスを見つける(右優先)"""m_value = self.dat[1]ind = 1while ind <= (2**(self.K)):ind_l = ind*2ind_r = ind*2 + 1if self.dat[ind_r] == m_value:ind = ind_relse:ind = ind_lreturn ind - 2**(self.K)def find_l(self):"""max,minのインデックスを見つける(左優先)"""m_value = self.dat[1]ind = 1while ind <= (2**(self.K + 1)):ind_l = ind*2ind_r = ind*2 + 1if self.dat[ind_l] == m_value:ind = ind_lelse:ind = ind_rreturn 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] += yelif t == 2:SG[x] -= yelse:ans.append(SG.find_r())print(*ans,sep='\n')