aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Wang2020-08-23 09:41:20 -0500
committerAnthony Wang2020-08-23 09:41:20 -0500
commit4477e06f4e411efc82f95c6b87372072c41a9925 (patch)
treef06cfba07ea18389ccb084aad5cead79f7eda50e
parent8ee9dec7237079dd70bb59198bad7b83a14adf00 (diff)
Convert to template
-rw-r--r--Data Structures/segment_tree.cpp77
1 files changed, 44 insertions, 33 deletions
diff --git a/Data Structures/segment_tree.cpp b/Data Structures/segment_tree.cpp
index a2095ce..dd92117 100644
--- a/Data Structures/segment_tree.cpp
+++ b/Data Structures/segment_tree.cpp
@@ -1,38 +1,49 @@
-int seg[4*MN], tmp[4*MN];
-inline int pull(const int & a, const int & b) { return a + b; }
-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 x, int v, int l = 0, int r = -1, int n = 1) {
- if (r == -1) r = N - 1;
- if (l == r) seg[n] += v;
- else {
- int m = (l + r) >> 1;
- x <= m ? update(x, v, l, m, n << 1) : update(x, v, m + 1, r, n << 1 | 1);
- seg[n] = pull(seg[n << 1], seg[n << 1 | 1]);
+template<typename T> struct segtree {
+ T seg[4*MN], tmp[4*MN];
+ inline T pull(const T & a, const T & b) { return a+b; }
+ 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;
- push(l, r, n);
- if (l > r || l > b || r < a) return;
- if (l >= a && r <= b) {
- tmp[n] += v;
+ void build(T v, int l = 0, int r = -1, int n = 1) {
+ if (r == -1) r = N-1;
+ if (l == r) seg[n] = v;
+ else {
+ int m = (l+r)>>1;
+ build(l, m, n<<1), build(m+1, r, n<<1|1);
+ seg[n] = pull(seg[n<<1], seg[n<<1|1]);
+ }
+ }
+ void update(int x, T v, int l = 0, int r = -1, int n = 1) {
+ if (r == -1) r = N-1;
+ if (l == r) seg[n] += v;
+ else {
+ int m = (l+r)>>1;
+ x <= m ? update(x, v, l, m, n<<1) : update(x, v, m+1, r, n<<1|1);
+ seg[n] = pull(seg[n<<1], seg[n<<1|1]);
+ }
+ }
+ void update(int a, int b, T v, int l = 0, int r = -1, int n = 1) {
+ if (r == -1) r = N-1;
push(l, r, n);
+ if (l > r || 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);
+ seg[n] = pull(seg[n<<1], seg[n<<1|1]);
+ }
}
- else {
- int m = (l + r) >> 1;
- update(a, b, v, l, m, n << 1), update(a, b, v, m + 1, r, n << 1 | 1);
- seg[n] = pull(seg[n << 1], seg[n << 1 | 1]);
+ T 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;
+ push(l, r, n);
+ if (l >= a && r <= b) return seg[n];
+ int m = (l+r)>>1;
+ return pull(query(a, b, l, m, n<<1), query(a, b, m+1, r, n<<1|1));
}
}
-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;
- push(l, r, n);
- if (l >= a && r <= b) return seg[n];
- int m = (l + r) >> 1;
- return pull(query(a, b, l, m, n << 1), query(a, b, m + 1, r, n << 1 | 1));
-}