Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5ea4b9

Browse filesBrowse files
committed
Migrate code to this repo
1 parent 48802f3 commit f5ea4b9
Copy full SHA for f5ea4b9

File tree

4 files changed

+158
-0
lines changed
Filter options

4 files changed

+158
-0
lines changed

‎src/data_structures/fenwick_tree.cc

Copy file name to clipboard
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Fenwick Tree
3+
*/
4+
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
class FenwickTree {
10+
private:
11+
vector<int> tree;
12+
public:
13+
FenwickTree(int n) : tree(n + 1) {}
14+
15+
int range_sum(int pos) {
16+
++pos;
17+
int ret = 0;
18+
while (pos > 0) {
19+
ret += tree[pos];
20+
pos &= (pos - 1);
21+
}
22+
return ret;
23+
}
24+
25+
void update(int pos, int val) {
26+
++pos;
27+
while (pos < tree.size()) {
28+
tree[pos] += val;
29+
pos += (pos & -pos);
30+
}
31+
}
32+
};
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* "Rankless" Union-find disjoint set
3+
*/
4+
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
class RanklessUnionFind {
10+
private:
11+
vector<int> p, rank, setSize;
12+
int numSets;
13+
public:
14+
RanklessUnionFind(int N) {
15+
setSize.assign(N, 1);
16+
numSets = N;
17+
rank.assign(N, 0);
18+
p.assign(N, 0);
19+
for (int i = 0; i < N; i++) p[i] = i;
20+
}
21+
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
22+
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
23+
void unionSet(int i, int j) {
24+
if (!isSameSet(i, j)) {
25+
numSets--;
26+
int x = findSet(i), y = findSet(j);
27+
p[y] = x;
28+
setSize[x] += setSize[y];
29+
}
30+
}
31+
int numDisjointSets() { return numSets; }
32+
int sizeOfSet(int i) { return setSize[findSet(i)]; }
33+
};

‎src/data_structures/rmq.cc

Copy file name to clipboard
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Range Minimum Query
3+
*/
4+
5+
#include <algorithm>
6+
#include <numeric>
7+
#include <vector>
8+
9+
using namespace std;
10+
11+
class RMQ {
12+
private:
13+
vector<long long> tree;
14+
int len;
15+
16+
long long init_tree(vector<long long>& arr, int lo, int hi, int node) {
17+
if (lo == hi) return tree[node] = arr[lo];
18+
int mid = (lo + hi) / 2;
19+
long long left = init_tree(arr, lo, mid, node * 2);
20+
long long right = init_tree(arr, mid + 1, hi, node * 2 + 1);
21+
return tree[node] = min(left, right);
22+
}
23+
24+
long long query(int lo, int hi, int node, int node_lo, int node_hi) {
25+
if (hi < node_lo || node_hi < lo)
26+
return numeric_limits<long long>::max();
27+
if (lo <= node_lo && node_hi <= hi) return tree[node];
28+
int mid = (node_lo + node_hi) / 2;
29+
return min(query(lo, hi, node * 2, node_lo, mid),
30+
query(lo, hi, node * 2 + 1, mid + 1, node_hi));
31+
}
32+
33+
long long update(int idx, int newval, int node, int node_lo, int node_hi) {
34+
if (idx < node_lo || node_hi < idx) return tree[node];
35+
if (node_lo == node_hi) return tree[node] = newval;
36+
int mid = (node_lo + node_hi) / 2;
37+
return tree[node] = min(
38+
update(idx, newval, node * 2, node_lo, mid),
39+
update(idx, newval, node * 2 + 1, mid + 1, node_hi)
40+
);
41+
}
42+
public:
43+
long long query(int lo, int hi) {
44+
return query(lo, hi, 1, 0, len - 1);
45+
}
46+
47+
long long update(int idx, int newval) {
48+
return update(idx, newval, 1, 0, len - 1);
49+
}
50+
51+
RMQ(vector<long long>& arr) {
52+
len = arr.size();
53+
tree.resize(len * 4);
54+
init_tree(arr, 0, len - 1, 1);
55+
}
56+
};

‎src/data_structures/union_find.cc

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Union-find disjoint set
3+
*/
4+
5+
typedef vector<int> vi;
6+
7+
class UnionFind {
8+
private:
9+
vi p, rank, setSize;
10+
int numSets;
11+
public:
12+
UnionFind(int N) {
13+
setSize.assign(N, 1);
14+
numSets = N;
15+
rank.assign(N, 0);
16+
p.assign(N, 0);
17+
for (int i = 0; i < N; i++) p[i] = i;
18+
}
19+
int findSet(int i) { return (p[i] == i) ? i : (p[i] = findSet(p[i])); }
20+
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
21+
void unionSet(int i, int j) {
22+
if (!isSameSet(i, j)) {
23+
numSets--;
24+
int x = findSet(i), y = findSet(j);
25+
if (rank[x] > rank[y]) {
26+
p[y] = x;
27+
setSize[x] += setSize[y];
28+
} else {
29+
p[x] = y;
30+
setSize[y] += setSize[x];
31+
if (rank[x] == rank[y]) rank[y]++;
32+
}
33+
}
34+
}
35+
int numDisjointSets() { return numSets; }
36+
int sizeOfSet(int i) { return setSize[findSet(i)]; }
37+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.