def euler_tour_technique(tree, root, values):
values_in_dfs_order = []
subtree_ranges = [[None, None] for _ in tree]
for node in tgraph.dfs(tree, root, yields_on_leave=True):
if subtree_ranges[node][0] is None:
subtree_ranges[node][0] = len(values_in_dfs_order)
values_in_dfs_order.append(values[node])
else:
subtree_ranges[node][1] = len(values_in_dfs_order)
return values_in_dfs_order, subtree_ranges
def euler_tour_technique(tree, root):
subtree_ranges = [[None, None] for _ in tree]
order = 0
for node in tgraph.dfs(tree, root, yields_on_leave=True):
if subtree_ranges[node][0] is None:
subtree_ranges[node][0] = order
order += 1
else:
subtree_ranges[node][1] = order
return subtree_ranges
values_in_dfs_order = [None] * N
for node_index, (dfs_order, _) in enumerate(subtree_ranges):
values_in_dfs_order[dfs_order] = values[node_index]
[to be filled..]
def merge(l, r):
l_lmax, l_rmax, l_max, l_all = l
r_lmax, r_rmax, r_max, r_all = r
return (max(l_lmax, l_all + r_lmax), max(r_rmax, l_rmax + r_all),
max(l_max, r_max, l_rmax + r_lmax), l_all + r_all)
segtree = SegmentTree(((x,) * 4 for x in arr), merge)
answer = segtree.query(beg, end)[2]
[To be Filled]
prefix_sum = list(itertools.accumulate(a, initial=0))
이것 대신에v = 0
prefix_sum = [v] + [v := v + x for x in a]
이것을 쓰자int query(int l, int r) { // sum on interval [l, r)
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) res += t[l++];
if (r&1) res += t[--r];
}
return res;
}
int query(int l, int r) { // sum on interval [l, r)
int res = t[l + n];
for (l += (n + 1), r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) res += t[l++];
if (r&1) res += t[--r];
}
return res;
}
번호 | 제목 | n | q | 내 기록 (teflib) | 1등 기록 |
---|---|---|---|---|---|
2042 | 구간 합 구하기 | 1,000,000 | 20,000 | 1000ms | 760ms |
16978 | 수열과 쿼리 22 | 100,000 | 100,000 | 820ms | 820ms |
18436 | 수열과 쿼리 38 | 100,000 | 100,000 | 692ms | 664ms |
2268 | 수들의 합 | 1,000,000 | 1,000,000 | 5964ms | 5300ms |
def query(self, beg: int, end: int) -> float:
res = 0
l, r = beg - 1, end - 1
while l != r:
if l < r:
res += self._tree[r]
r = (r & (r + 1)) - 1
else:
res -= self._tree[l]
l = (l & (l + 1)) - 1
return res
def query(self, beg: int, end: int) -> float:
res = 0
l, r = beg - 1, end - 1
c = (beg ^ end).bit_length()
t = (beg >> c) << c
while r >= t:
res += self._tree[r]
r = (r & (r + 1)) - 1
while l >= t:
res -= self._tree[l]
l = (l & (l + 1)) - 1
return res