Skip to content
Snippets Groups Projects
Commit cfde1dab authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Fix seg trees and dsu

parent 466f1242
No related branches found
No related tags found
No related merge requests found
...@@ -8,17 +8,16 @@ ...@@ -8,17 +8,16 @@
struct DisjointSet { struct DisjointSet {
int N; int N;
vector<int> h, par, size; vector<int> rank, par;
Disjoint(int N) : Disjoint(int N) :
N(N), h(N), par(N), size(N) N(N), rank(N), par(N)
{} {}
/// Initializes element x. /// Initializes element x.
void make_set(int x) { void make_set(int x) {
par[x] = x; par[x] = x;
h[x] = 0; rank[x] = 0;
size[x] = 1;
} }
/// Returns set index from element x. /// Returns set index from element x.
...@@ -33,16 +32,13 @@ struct DisjointSet { ...@@ -33,16 +32,13 @@ struct DisjointSet {
x = find_set(x); x = find_set(x);
y = find_set(y); y = find_set(y);
if (x == y) if (x != y) {
return; if (rank[x] > rank[y])
swap(x, y);
if (h[x] > h[y]) par[x] = y;
swap(x, y); if (rank[x] == rank[y])
rank[x]++;
par[x] = y; }
size[y] += size[x];
if (h[x] == h[y])
h[x]++;
} }
}; };
...@@ -6,11 +6,11 @@ ...@@ -6,11 +6,11 @@
/// query_tree -> O(log n) /// query_tree -> O(log n)
/// Complexity (Space): O(n) /// Complexity (Space): O(n)
int N;
struct LazySegmentTree { struct LazySegmentTree {
int N;
vector<int> tree, lazy; vector<int> tree, lazy;
LazySegmentTree(int N, const vector<int> &v) : N(N), tree(N*4), lazy(N*4) { LazySegmentTree(const vector<int> &v) : tree(MAX*4), lazy(MAX*4) {
init(); init();
build(v); build(v);
} }
...@@ -34,9 +34,10 @@ struct LazySegmentTree { ...@@ -34,9 +34,10 @@ struct LazySegmentTree {
return; return;
} }
build(v, left(node), a, (a + b) / 2); int mid = (a + b) / 2;
build(v, right(node), (a + b) / 2 + 1, b); build(v, left(node), a, mid);
tree[node] = tree[node * 2] + tree[node * 2 + 1]; build(v, right(node), mid + 1, b);
tree[node] = tree[left(node)] + tree[right(node)];
} }
/// Propagates value to tree and through lazy tree. /// Propagates value to tree and through lazy tree.
...@@ -70,9 +71,10 @@ struct LazySegmentTree { ...@@ -70,9 +71,10 @@ struct LazySegmentTree {
return; return;
} }
update(i, j, val, left(node), a, (a + b) / 2); int mid = (a + b) / 2;
update(i, j, val, right(node), (a + b) / 2 + 1, b); update(i, j, val, left(node), a, mid);
tree[node] = tree[node * 2] + tree[node * 2 + 1]; update(i, j, val, right(node), mid + 1, b);
tree[node] = tree[left(node)] + tree[right(node)];
} }
/// Returns sum of [i,j]. /// Returns sum of [i,j].
...@@ -87,8 +89,9 @@ struct LazySegmentTree { ...@@ -87,8 +89,9 @@ struct LazySegmentTree {
if (a >= i && b <= j) if (a >= i && b <= j)
return tree[node]; return tree[node];
int q1 = query(i, j, left(node), a, (a + b) / 2); int mid = (a + b) / 2;
int q2 = query(i, j, right(node), (a + b) / 2 + 1, b); int q1 = query(i, j, left(node), a, mid);
int q2 = query(i, j, right(node), mid + 1, b);
return q1 + q2; return q1 + q2;
} }
}; };
...@@ -6,11 +6,11 @@ ...@@ -6,11 +6,11 @@
/// Query -> O(log n) /// Query -> O(log n)
/// Complexity (Space): O(n) /// Complexity (Space): O(n)
int N;
struct SegmentTree { struct SegmentTree {
int N;
vector<int> tree; vector<int> tree;
SegmentTree(int N, const vector<int> &v) : N(N), tree(N*4) { SegmentTree(const vector<int> &v) : tree(MAX*4) {
init(); init();
build(v); build(v);
} }
...@@ -33,9 +33,10 @@ struct SegmentTree { ...@@ -33,9 +33,10 @@ struct SegmentTree {
return; return;
} }
build(v, left(node), a, (a + b) / 2); int mid = (a + b) / 2;
build(v, right(node), 1 + (a + b) / 2, b); build(v, left(node), a, mid);
tree[node] = tree[node * 2] + tree[node * 2 + 1]; build(v, right(node), mid + 1, b);
tree[node] = tree[left(node)] + tree[right(node)];
} }
/// Updates position idx by adding value val. /// Updates position idx by adding value val.
...@@ -50,9 +51,10 @@ struct SegmentTree { ...@@ -50,9 +51,10 @@ struct SegmentTree {
return; return;
} }
update(idx, val, left(node), a, (a + b) / 2); int mid = (a + b) / 2;
update(idx, val, right(node), 1 + (a + b) / 2, b); update(idx, val, left(node), a, mid);
tree[node] = tree[node * 2] + tree[node * 2 + 1]; update(idx, val, right(node), mid + 1, b);
tree[node] = tree[left(node)] + tree[right(node)];
} }
/// Returns sum of [i,j]. /// Returns sum of [i,j].
...@@ -64,8 +66,9 @@ struct SegmentTree { ...@@ -64,8 +66,9 @@ struct SegmentTree {
if (i <= a && b <= j) if (i <= a && b <= j)
return tree[node]; return tree[node];
int q1 = query(i, j, left(node), a, (a + b) / 2); int mid = (a + b) / 2;
int q2 = query(i, j, right(node), 1 + (a + b) / 2, b); int q1 = query(i, j, left(node), a, mid);
int q2 = query(i, j, right(node), mid + 1, b);
return q1 + q2; return q1 + q2;
} }
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment