結果
問題 | No.833 かっこいい電車 |
ユーザー |
![]() |
提出日時 | 2020-03-13 21:36:58 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 632 ms / 2,000 ms |
コード長 | 2,101 bytes |
コンパイル時間 | 119 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 30,436 KB |
最終ジャッジ日時 | 2024-11-22 19:50:36 |
合計ジャッジ時間 | 11,208 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
#!/usr/bin/env python3.8# %%import sysread = sys.stdin.buffer.readreadline = sys.stdin.buffer.readlinereadlines = sys.stdin.buffer.readlines# %%N, Q = map(int, readline().split())A = [0] + list(map(int, readline().split()))m = map(int, read().split())QX = tuple(zip(m, m))# %%class BinaryIndexedTree():def __init__(self, seq):self.size = len(seq)self.depth = self.size.bit_length()self.build(seq)def build(self, seq):data = seqsize = self.sizefor i, x in enumerate(data):j = i + (i & (-i))if j < size:data[j] += data[i]self.data = datadef __repr__(self):return self.data.__repr__()def get_sum(self, i):data = self.datas = 0while i:s += data[i]i -= i & -ireturn sdef add(self, i, x):data = self.datasize = self.sizewhile i < size:data[i] += xi += i & -idef find_kth_element(self, k):data = self.data; size = self.sizex, sx = 0, 0dx = 1 << (self.depth)for i in range(self.depth - 1, -1, -1):dx = (1 << i)if x + dx >= size:continuey = x + dxsy = sx + data[y]if sy < k:x, sx = y, syreturn x + 1# %%value = BinaryIndexedTree(A)connection = [0] * (N + 1)right_end_count = BinaryIndexedTree([0] + [1] * N)for q, x in QX:if q == 1:if not connection[x]:connection[x] = 1right_end_count.add(x, -1)elif q == 2:if connection[x]:connection[x] -= 1right_end_count.add(x, 1)elif q == 3:value.add(x, 1)elif q == 4:k = right_end_count.get_sum(x - 1)if k == 0:L = 1else:L = right_end_count.find_kth_element(k) + 1R = right_end_count.find_kth_element(k + 1)S = value.get_sum(R) - value.get_sum(L - 1)print(S)