Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../common/common.hpp"

namespace refcode {
// 1. Fenwick Tree
struct Fenwick { // 0-indexed
int flag, cnt; // array size
Expand All @@ -23,7 +23,7 @@ struct Fenwick { // 0-indexed
}
void modify(int p, ll value) { // set value at position p
add(p, value - arr[p]);
};
}
ll query(int x) {
ll ret = 0;
while (x >= 0) ret += t[x], x = (x & (x + 1)) - 1;
Expand All @@ -43,10 +43,9 @@ struct Fenwick { // 0-indexed
}
return l;
}
} fw;

};
// 2. Fenwick Tree Range Update Point Query
struct Fenwick { // 1-indexed
struct FenwickRUPQ { // 1-indexed
int flag;
vector<ll> t;
void build(int N) {
Expand All @@ -62,8 +61,7 @@ struct Fenwick { // 1-indexed
for (; x; x ^= x & -x) ret += t[x];
return ret;
}
} fw;

};
// 3. 2D Fenwick Tree
// INPUT: Given an 2D array of integers of size N * M.
// Can modify the value of the (x, y)th element.
Expand Down Expand Up @@ -130,4 +128,5 @@ struct Fenwick2D { // 0-indexed
if (sx && sy) ret += query(sx - 1, sy - 1);
return ret;
}
} fw2d;
};
} // namespace refcode
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "../common/common.hpp"

namespace refcode {
// INPUT: Initially, a 2d plane in which no linear function exists is given.
// Two types of queries are given.
// 1 a b : The linear function f(x) = ax + b is added.
// 2 x : Find the max(f(x)) among the linear functions given so far.
// OUTPUT: For each query 2 x, output the max(f(x)) among the linear functions given so far.
// TIME COMPLEXITY: O(qlogq)
#define Line pair<ll, ll>

const Line e = {0, -1e18};
using Line = pair<ll, ll>;
constexpr Line e = {0, -1e18};
struct LiChaoTree {
ll f(Line l, ll x) { return l.first * x + l.second; }
struct Node {
Expand Down Expand Up @@ -56,4 +55,5 @@ struct LiChaoTree {
else ret = max(ret, query(x, t[n].r));
return ret;
}
} lct;
};
} // namespace refcode
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "../common/common.hpp"

namespace refcode {
constexpr int MAX_MST = 1 << 17;
// 1. Merge Sort Tree
// INPUT: Given a sequence A_1, A_2, ..., A_n of length n. Given a query (i, j, k).
// OUTPUT: For each query (i, j, k), output the number of elements greater than k among elements A_i, A_{i+1}, ..., A_j.
// TIME COMPLEXITY: O(nlogn) for initialize Merge Sort Tree, O(log^2n) for each query.
const int MAX_MST = 1 << 17;
struct MergeSortTree {
vector<int> t[MAX_MST << 1];
void build(const vector<int> &arr) {
Expand All @@ -22,14 +22,12 @@ struct MergeSortTree {
int mid = (nl + nr) >> 1;
return query(l, r, k, n << 1, nl, mid) + query(l, r, k, n << 1 | 1, mid + 1, nr);
}
} mstree;

};
// 2. Iterative Merge Sort Tree
// INPUT: Given a sequence A_1, A_2, ..., A_n of length n. Given a query (i, j, k).
// OUTPUT: For each query (i, j, k), output the number of elements greater than k among elements A_i, A_{i+1}, ..., A_j.
// TIME COMPLEXITY: O(nlogn) for initialize Merge Sort Tree, O(log^2n) for each query.
const int MAX_MST = 1 << 17;
struct MergeSortTree {
struct MergeSortTreeIter {
vector<int> t[MAX_MST << 1];
void build(const vector<int> &arr) {
for (int i = 0; i < sz(arr); i++)
Expand All @@ -49,4 +47,5 @@ struct MergeSortTree {
}
return ret;
}
} mstree;
};
} // namespace refcode
41 changes: 16 additions & 25 deletions 1-data-structure/segment_tree.cpp → refcode/1-ds/segment_tree.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../common/common.hpp"

namespace refcode {
int flag; // array size
// 1. Segment Tree
int flag; // array size
struct Seg { // 1-indexed
vector<ll> t;
void build(int n) {
Expand All @@ -19,12 +19,11 @@ struct Seg { // 1-indexed
int mid = (nl + nr) / 2;
return query(l, r, n << 1, nl, mid) + query(l, r, n << 1 | 1, mid + 1, nr);
}
} seg;

};
// 2. Iterative Segment Tree
const int MAXN = 1010101; // limit for array size
struct Seg { // 0-indexed
int n; // array size
constexpr int MAXN = 1010101; // limit for array size
struct SegIter { // 0-indexed
int n; // array size
ll t[2 * MAXN];
void build(int N) {
n = N;
Expand All @@ -42,11 +41,9 @@ struct Seg { // 0-indexed
}
return ret;
}
} seg;

};
// 3. k-th Segment Tree
int flag; // array size
struct Seg {
struct SegKth {
vector<ll> t;
void build(int n) {
for (flag = 1; flag < n; flag <<= 1);
Expand All @@ -61,11 +58,9 @@ struct Seg {
if (k <= t[n << 1]) return kth(k, n << 1);
else return kth(k - t[n << 1], n << 1 | 1);
}
} seg;

};
// 4. Segment Tree with Lazy Propagation
int flag; // array size
struct Seg { // 1-indexed
struct SegLazy { // 1-indexed
vector<ll> t, lazy;
void build(int n) {
for (flag = 1; flag < n; flag <<= 1);
Expand Down Expand Up @@ -105,8 +100,7 @@ struct Seg { // 1-indexed
lazy[n] = 0;
}
}
} seg;

};
// 5. Persistent Segment Tree
// TIME COMPLEXITY: O(n) for initialize PST, O(logn) for each query.
// SPACE COMPLEXITY: O(nlogm).
Expand Down Expand Up @@ -153,7 +147,6 @@ struct PST { // 1-indexed
t[n2].val = val;
return;
}

int mid = (l + r) >> 1;
if (p <= mid) {
t[n2].r = t[n1].r;
Expand Down Expand Up @@ -184,17 +177,14 @@ struct PST { // 1-indexed
assert(n < sz(root));
return query(l, r, root[n], 1, flag);
}
} pst;

};
// 6. Dynamic Segment Tree
const int MAXL = 1, MAXR = 1000000;

constexpr int MAXL = 1, MAXR = 1000000;
struct Node {
ll x;
int l, r;
};

struct Dyseg {
struct DySeg {
vector<Node> t = {{0, -1, -1}, {0, -1, -1}};
void modify(int p, ll x, int n = 1, int nl = MAXL, int nr = MAXR) {
if (p < nl || nr < p) return;
Expand Down Expand Up @@ -237,4 +227,5 @@ struct Dyseg {
}
return ret;
}
} dyseg;
};
} // namespace refcode
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "../common/common.hpp"

namespace refcode {
struct UF {
vector<int> uf;
void build(int N) {
void build(int n) {
uf.clear();
uf.resize(N + 1, -1);
uf.resize(n + 1, -1);
}
int find(int v) {
if (uf[v] < 0) return v;
Expand All @@ -16,4 +16,5 @@ struct UF {
uf[U] += uf[V];
uf[V] = U;
}
} uf;
};
} // namespace refcode
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions common/common.hpp → refcode/common/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define fr first
#define sc second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
Empty file added test/.gitkeep
Empty file.