結果
問題 | No.876 Range Compress Query |
ユーザー | neterukun |
提出日時 | 2020-09-27 23:44:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 258 ms / 2,000 ms |
コード長 | 2,509 bytes |
コンパイル時間 | 185 ms |
コンパイル使用メモリ | 82,020 KB |
実行使用メモリ | 92,844 KB |
最終ジャッジ日時 | 2024-06-30 15:24:31 |
合計ジャッジ時間 | 4,554 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,064 KB |
testcase_01 | AC | 61 ms
68,132 KB |
testcase_02 | AC | 40 ms
54,056 KB |
testcase_03 | AC | 57 ms
67,088 KB |
testcase_04 | AC | 46 ms
60,160 KB |
testcase_05 | AC | 41 ms
57,236 KB |
testcase_06 | AC | 55 ms
67,724 KB |
testcase_07 | AC | 53 ms
66,664 KB |
testcase_08 | AC | 50 ms
68,000 KB |
testcase_09 | AC | 51 ms
66,156 KB |
testcase_10 | AC | 50 ms
65,596 KB |
testcase_11 | AC | 241 ms
92,844 KB |
testcase_12 | AC | 214 ms
90,892 KB |
testcase_13 | AC | 222 ms
90,372 KB |
testcase_14 | AC | 241 ms
92,484 KB |
testcase_15 | AC | 202 ms
89,000 KB |
testcase_16 | AC | 243 ms
92,060 KB |
testcase_17 | AC | 247 ms
91,928 KB |
testcase_18 | AC | 258 ms
92,824 KB |
ソースコード
import sys input = sys.stdin.buffer.readline class SegmentTree: def __init__(self, n, op, e): self.n = n self.op = op self.e = e self.size = 2 ** ((n - 1).bit_length()) self.node = [self.e] * (2 * self.size) def __getitem__(self, i): return self.node[i + self.size] def __setitem__(self, i, val): i += self.size self.node[i] = val while i > 1: i >>= 1 self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1]) def build(self, array): for i, val in enumerate(array, self.size): self.node[i] = val for i in range(self.size - 1, 0, -1): self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1]) def all_fold(self): return self.node[1] def fold(self, l, r): l, r = l + self.size, r + self.size vl, vr = self.e, self.e while l < r: if l & 1: vl = self.op(vl, self.node[l]) l += 1 if r & 1: r -= 1 vr = self.op(self.node[r], vr) l, r = l >> 1, r >> 1 return self.op(vl, vr) def max_right(self, l, f): if l == self.n: return self.n l += self.size v = self.e init = True while init or (l & -l) != l: init = False while l % 2 == 0: l >>= 1 if not f(self.op(v, self.node[l])): while l < self.size: l <<= 1 if f(self.op(v, self.node[l])): v = self.op(v, self.node[l]) l += 1 return l - self.size v = self.op(v, self.node[l]) l += 1 return self.n n, q = map(int, input().split()) a = list(map(int, input().split())) queries = [list(map(int, input().split())) for i in range(q)] diff = [a[i + 1] - a[i] for i in range(n - 1)] diff_flag = [1 if d != 0 else 0 for d in diff] op = lambda x1, x2: x1 + x2 e = 0 st = SegmentTree(n - 1, op, e) st.build(diff_flag) for flag, *query in queries: if flag == 1: l, r, x = query l -= 1 if l - 1 >= 0: diff[l - 1] += x st[l - 1] = int(diff[l - 1] != 0) r -= 1 if r < n - 1: diff[r] -= x st[r] = int(diff[r] != 0) else: l, r = query l -= 1 r -= 1 print(1 + st.fold(l, r))