diff options
author | Ta180m | 2020-04-27 12:40:29 -0500 |
---|---|---|
committer | Ta180m | 2020-04-27 12:40:29 -0500 |
commit | 573198e9da591d810fa9019e4d73866dea8aaf86 (patch) | |
tree | 8becb2547fe8d2836b547f8f352212e71240b2ef /Data Structures | |
parent | beacea7a524b34b9c5afe3b66bf4a1bf60fed147 (diff) |
Create segment_tree_v3.cpp
Diffstat (limited to 'Data Structures')
-rw-r--r-- | Data Structures/segment_tree_v3.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Data Structures/segment_tree_v3.cpp b/Data Structures/segment_tree_v3.cpp new file mode 100644 index 0000000..38d833e --- /dev/null +++ b/Data Structures/segment_tree_v3.cpp @@ -0,0 +1,29 @@ +int seg[400004], tmp[400004]; +inline void pull(int n) { seg[n] = seg[n << 1] + seg[n << 1 | 1]; } +inline void push(int l, int r, int n) { + seg[n] += (r - l + 1) * tmp[n]; + if (l != r) tmp[n << 1] += tmp[n], tmp[n << 1 | 1] += tmp[n]; + tmp[n] = 0; +} +void update(int a, int b, int v, int l = 0, int r = -1, int n = 1) { + if (r == -1) r = N - 1; + if (tmp[n]) push(l, r, n); + if (l > b || r < a) return; + if (l >= a && r <= b) { + tmp[n] += v; + push(l, r, n); + } + else { + int m = (l + r) >> 1; + update(a, b, v, l, m, n << 1), update(a, b, v, m + 1, r, n << 1 | 1); + pull(n); + } +} +int query(int a, int b, int l = 0, int r = -1, int n = 1) { + if (r == -1) r = N - 1; + if (a > b || l > b || r < a) return 0; + if (tmp[n]) push(l, r, n); + if (l >= a && r <= b) return seg[n]; + int m = (l + r) >> 1; + return query(a, b, l, m, n << 1) + query(a, b, m + 1, r, n << 1 | 1); +}
\ No newline at end of file |