Skip to content

Commit b0be61d

Browse files
chore: apply macros and aliases (#45)
1 parent 4c3a5cd commit b0be61d

104 files changed

Lines changed: 696 additions & 788 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
My implementations of various data structures and algorithms for competitive programming.
33

44
## Contents (GitHub links, `main`)
5+
- [0-common](https://github.com/manoflearning/cp-reference-codes/tree/main/src/0-common)
6+
- [`common.hpp`](https://github.com/manoflearning/cp-reference-codes/blob/main/src/0-common/common.hpp)
57
- [1-ds](https://github.com/manoflearning/cp-reference-codes/tree/main/src/1-ds)
68
- [`erasable_pq.cpp`](https://github.com/manoflearning/cp-reference-codes/blob/main/src/1-ds/erasable_pq.cpp)
79
- [`fenwick_tree.cpp`](https://github.com/manoflearning/cp-reference-codes/blob/main/src/1-ds/fenwick_tree.cpp)
@@ -69,5 +71,3 @@ My implementations of various data structures and algorithms for competitive pro
6971
- [`stress_test.py`](https://github.com/manoflearning/cp-reference-codes/blob/main/src/8-misc/stress_test.py)
7072
- [`system_of_difference_constraints.cpp`](https://github.com/manoflearning/cp-reference-codes/blob/main/src/8-misc/system_of_difference_constraints.cpp)
7173
- [`ternary_search.cpp`](https://github.com/manoflearning/cp-reference-codes/blob/main/src/8-misc/ternary_search.cpp)
72-
- [common](https://github.com/manoflearning/cp-reference-codes/tree/main/src/common)
73-
- [`common.hpp`](https://github.com/manoflearning/cp-reference-codes/blob/main/src/common/common.hpp)
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
#include <bits/stdc++.h>
44
using namespace std;
5-
65
using ll = long long;
76
using ld = long double;
8-
using ull = unsigned long long;
97
using pii = pair<int, int>;
108
using pll = pair<ll, ll>;
11-
9+
using vi = vector<int>;
10+
using vl = vector<ll>;
11+
using vvi = vector<vector<int>>;
12+
using vvl = vector<vector<ll>>;
1213
#define fr first
1314
#define sc second
15+
#define pb push_back
1416
#define sz(x) (int)(x).size()
1517
#define all(x) (x).begin(), (x).end()

src/1-ds/erasable_pq.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../common/common.hpp"
1+
#include "../0-common/common.hpp"
22

33
// what: priority queue that supports deleting arbitrary values via lazy deletion.
44
// time: push/pop/erase O(log n); memory: O(n)
@@ -7,7 +7,7 @@
77
template <class T, class cmp = less<T>>
88
struct erase_pq {
99
priority_queue<T, vector<T>, cmp> q, del;
10-
int size() { return (int)q.size() - (int)del.size(); }
10+
int size() { return sz(q) - sz(del); }
1111
bool empty() { return size() == 0; }
1212
const T &top() { return (fix(), q.top()); } // result: current extreme element.
1313
void push(const T &x) { q.push(x); }

src/1-ds/fenwick_tree.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
#include "../common/common.hpp"
1+
#include "../0-common/common.hpp"
22

33
// what: maintain prefix sums with point updates and range sum queries.
44
// time: build O(n), update/query O(log n); memory: O(n)
55
// constraint: 0-indexed; kth needs all values >= 0.
66
// usage: fenwick fw; fw.build(a); fw.add(p, x); fw.sum(l, r); fw.kth(k);
77
struct fenwick {
88
int n;
9-
vector<ll> a, t;
9+
vl a, t;
1010
void init(int n_) {
1111
// goal: allocate arrays for size n.
1212
n = n_;
1313
a.assign(n, 0);
1414
t.assign(n, 0);
1515
}
16-
void build(const vector<ll> &v) {
16+
void build(const vl &v) {
1717
// goal: build fenwick in O(n) from initial array.
1818
n = sz(v);
1919
a = v;
@@ -60,7 +60,7 @@ struct fenwick {
6060
// usage: fenw_range fw; fw.init(n); fw.add(l, r, x); ll v = fw.get(p);
6161
struct fenw_range { // 1-indexed
6262
int n;
63-
vector<ll> t;
63+
vl t;
6464
void init(int n_) {
6565
// goal: allocate internal tree (1-indexed).
6666
n = n_;
@@ -85,19 +85,19 @@ struct fenw_range { // 1-indexed
8585
// usage: fenw_2d fw; fw.build(a); fw.add(x, y, v); fw.sum(x1, y1, x2, y2);
8686
struct fenw_2d { // 0-indexed
8787
int n, m;
88-
vector<vector<ll>> a, t;
88+
vvl a, t;
8989
void init(int n_, int m_) {
9090
// goal: allocate arrays for n x m.
9191
n = n_, m = m_;
92-
a.assign(n, vector<ll>(m, 0));
93-
t.assign(n, vector<ll>(m, 0));
92+
a.assign(n, vl(m, 0));
93+
t.assign(n, vl(m, 0));
9494
}
95-
void build(const vector<vector<ll>> &v) {
95+
void build(const vvl &v) {
9696
// goal: build 2D fenwick in O(n*m).
9797
n = sz(v);
9898
m = n ? sz(v[0]) : 0;
9999
a = v;
100-
t.assign(n, vector<ll>(m, 0));
100+
t.assign(n, vl(m, 0));
101101
for (int i = 0; i < n; i++) {
102102
for (int j = 0; j < m; j++) {
103103
t[i][j] += a[i][j];

src/1-ds/li_chao_tree.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../common/common.hpp"
1+
#include "../0-common/common.hpp"
22

33
// what: maintain max of lines on a fixed x-range with online add + point query.
44
// time: add/query O(log X); memory: O(n)
@@ -22,7 +22,7 @@ struct li_chao {
2222
void init(ll xl, ll xr) {
2323
// goal: set x-range and clear tree.
2424
t.clear();
25-
t.push_back({xl, xr, -1, -1, LINE_E});
25+
t.pb({xl, xr, -1, -1, LINE_E});
2626
}
2727
void add(lc_line nw, int v = 0) {
2828
// goal: insert a new line into the segment.
@@ -40,14 +40,14 @@ struct li_chao {
4040
t[v].ln = hi;
4141
if (t[v].r == -1) {
4242
t[v].r = sz(t);
43-
t.push_back({mid + 1, xr, -1, -1, LINE_E});
43+
t.pb({mid + 1, xr, -1, -1, LINE_E});
4444
}
4545
add(lo, t[v].r);
4646
} else {
4747
t[v].ln = lo;
4848
if (t[v].l == -1) {
4949
t[v].l = sz(t);
50-
t.push_back({xl, mid, -1, -1, LINE_E});
50+
t.pb({xl, mid, -1, -1, LINE_E});
5151
}
5252
add(hi, t[v].l);
5353
}

src/1-ds/merge_sort_tree.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
#include "../common/common.hpp"
1+
#include "../0-common/common.hpp"
22
constexpr int MAX_MST = 1 << 17;
33

44
// what: static range count queries by storing sorted lists on a segment tree.
55
// time: build O(n log n), query O(log^2 n); memory: O(n log n)
66
// constraint: MAX_MST >= n; values fit in int; 0-indexed [l, r]; build once.
77
// usage: merge_seg st; st.build(a); st.query(l, r, k);
88
struct merge_seg {
9-
vector<int> t[MAX_MST << 1];
10-
void build(const vector<int> &a) {
9+
vi t[MAX_MST << 1];
10+
void build(const vi &a) {
1111
// goal: build sorted lists for each node.
1212
for (int i = 0; i < sz(a); i++)
13-
t[i + MAX_MST].push_back(a[i]);
13+
t[i + MAX_MST].pb(a[i]);
1414
for (int i = MAX_MST - 1; i >= 1; i--) {
1515
t[i].resize(sz(t[i << 1]) + sz(t[i << 1 | 1]));
1616
merge(all(t[i << 1]), all(t[i << 1 | 1]), t[i].begin());
@@ -31,11 +31,11 @@ struct merge_seg {
3131
// constraint: MAX_MST >= n; values fit in int; 0-indexed [l, r]; build once.
3232
// usage: merge_seg_it st; st.build(a); st.query(l, r, k);
3333
struct merge_seg_it {
34-
vector<int> t[MAX_MST << 1];
35-
void build(const vector<int> &a) {
34+
vi t[MAX_MST << 1];
35+
void build(const vi &a) {
3636
// goal: build sorted lists for each node.
3737
for (int i = 0; i < sz(a); i++)
38-
t[i + MAX_MST].push_back(a[i]);
38+
t[i + MAX_MST].pb(a[i]);
3939
for (int i = MAX_MST - 1; i >= 1; i--) {
4040
t[i].resize(sz(t[i << 1]) + sz(t[i << 1 | 1]));
4141
merge(all(t[i << 1]), all(t[i << 1 | 1]), t[i].begin());

src/1-ds/pbds.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../common/common.hpp"
1+
#include "../0-common/common.hpp"
22
#include <ext/pb_ds/assoc_container.hpp>
33
#include <ext/pb_ds/tree_policy.hpp>
44
using namespace __gnu_pbds;
@@ -20,18 +20,15 @@ void m_insert(omset &os, ll val) {
2020
// goal: insert one occurrence of val.
2121
os.insert({val, om_uid++});
2222
}
23-
2423
void m_erase(omset &os, ll val) {
2524
// goal: erase one occurrence of val.
2625
auto it = os.lower_bound({val, LLONG_MIN});
2726
os.erase(it);
2827
}
29-
3028
int m_order(const omset &os, ll val) {
3129
// result: number of elements strictly less than val.
3230
return os.order_of_key({val, LLONG_MIN});
3331
}
34-
3532
ll m_kth(const omset &os, int k) {
3633
// result: k-th value (0-indexed) by order.
3734
return os.find_by_order(k)->fr;

src/1-ds/segment_tree.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#include "../common/common.hpp"
1+
#include "../0-common/common.hpp"
22

33
// what: point update + range sum on a fixed-size array using a tree.
44
// time: build O(n), update/query O(log n); memory: O(n)
55
// constraint: 1-indexed [1, n]; a[0] unused.
66
// usage: seg_tree st; st.build(a); st.set(p, v); st.query(l, r);
77
struct seg_tree {
88
int flag;
9-
vector<ll> t;
10-
void build(const vector<ll> &a) {
9+
vl t;
10+
void build(const vl &a) {
1111
// goal: build tree from 1-indexed array.
1212
int n = sz(a) - 1;
1313
flag = 1;
@@ -36,8 +36,8 @@ struct seg_tree {
3636
// usage: seg_tree_it st; st.build(a); st.set(p, v); st.query(l, r);
3737
struct seg_tree_it { // 0-indexed
3838
int n;
39-
vector<ll> t;
40-
void build(const vector<ll> &a) {
39+
vl t;
40+
void build(const vl &a) {
4141
// goal: build tree from 0-indexed array.
4242
n = sz(a);
4343
t.assign(2 * n, 0);
@@ -65,7 +65,7 @@ struct seg_tree_it { // 0-indexed
6565
// usage: seg_tree_kth st; st.init(n); st.add(p, v); st.kth(k);
6666
struct seg_tree_kth {
6767
int flag;
68-
vector<ll> t;
68+
vl t;
6969
void init(int n) {
7070
// goal: allocate tree for size n.
7171
flag = 1;
@@ -91,8 +91,8 @@ struct seg_tree_kth {
9191
// usage: seg_tree_lz st; st.build(a); st.add(l, r, v); st.query(l, r);
9292
struct seg_tree_lz {
9393
int flag;
94-
vector<ll> t, lz;
95-
void build(const vector<ll> &a) {
94+
vl t, lz;
95+
void build(const vl &a) {
9696
// goal: build tree and clear lazy tags.
9797
int n = sz(a) - 1;
9898
flag = 1;
@@ -149,19 +149,19 @@ struct seg_pst {
149149
};
150150
int n;
151151
vector<node> t;
152-
vector<int> root;
152+
vi root;
153153

154-
void newnd() { t.push_back({-1, -1, 0}); }
155-
void build(int n_, const vector<ll> &a) {
154+
void newnd() { t.pb({-1, -1, 0}); }
155+
void build(int n_, const vl &a) {
156156
// goal: build initial version.
157157
n = n_;
158158
t.clear();
159159
root.clear();
160160
newnd();
161-
root.push_back(0);
161+
root.pb(0);
162162
build(1, n, root[0], a);
163163
}
164-
void build(int l, int r, int v, const vector<ll> &a) {
164+
void build(int l, int r, int v, const vl &a) {
165165
// goal: build node v for range [l, r].
166166
if (l == r) {
167167
t[v].val = a[l];
@@ -179,7 +179,7 @@ struct seg_pst {
179179
void set(int p, ll val) {
180180
// goal: create new version with a[p] = val.
181181
newnd();
182-
root.push_back(sz(t) - 1);
182+
root.pb(sz(t) - 1);
183183
set(p, val, 1, n, root[sz(root) - 2], root.back());
184184
}
185185
void set(int p, ll val, int l, int r, int v1, int v2) {
@@ -246,13 +246,13 @@ struct seg_sparse {
246246
if (p <= mid) {
247247
if (t[v].l == -1) {
248248
t[v].l = sz(t);
249-
t.push_back({0, -1, -1});
249+
t.pb({0, -1, -1});
250250
}
251251
add(p, x, t[v].l, nl, mid);
252252
} else {
253253
if (t[v].r == -1) {
254254
t[v].r = sz(t);
255-
t.push_back({0, -1, -1});
255+
t.pb({0, -1, -1});
256256
}
257257
add(p, x, t[v].r, mid + 1, nr);
258258
}
@@ -275,11 +275,11 @@ struct seg_sparse {
275275
// usage: seg_2d st; st.build(a); st.set(x, y, v); st.query(x1, x2, y1, y2);
276276
struct seg_2d { // 0-indexed
277277
int n;
278-
vector<vector<ll>> t;
279-
void build(const vector<vector<ll>> &a) {
278+
vvl t;
279+
void build(const vvl &a) {
280280
// goal: build 2D tree from initial grid.
281281
n = sz(a);
282-
t.assign(2 * n, vector<ll>(2 * n, 0));
282+
t.assign(2 * n, vl(2 * n, 0));
283283
for (int i = 0; i < n; i++)
284284
for (int j = 0; j < n; j++)
285285
t[i + n][j + n] = a[i][j];
@@ -325,24 +325,24 @@ struct seg_2d { // 0-indexed
325325
// usage: seg2d_comp st(n); st.mark_set(x, y); st.mark_qry(x1, x2, y1, y2); st.prep(); st.set(x, y, v); st.query(x1, x2, y1, y2);
326326
struct seg2d_comp { // 0-indexed
327327
int n;
328-
vector<vector<ll>> a;
329-
vector<vector<int>> used;
328+
vvl a;
329+
vvi used;
330330
unordered_map<ll, ll> mp;
331331
seg2d_comp(int n) : n(n), a(2 * n), used(2 * n) {}
332332
void mark_set(int x, int y) {
333333
// goal: record y-coordinates that will be updated.
334-
for (x += n; x >= 1; x >>= 1) used[x].push_back(y);
334+
for (x += n; x >= 1; x >>= 1) used[x].pb(y);
335335
}
336336
void mark_qry(int x1, int x2, int y1, int y2) {
337337
// goal: record y-coordinates needed for queries.
338338
for (x1 += n, x2 += n + 1; x1 < x2; x1 >>= 1, x2 >>= 1) {
339339
if (x1 & 1) {
340-
used[x1].push_back(y1);
341-
used[x1++].push_back(y2);
340+
used[x1].pb(y1);
341+
used[x1++].pb(y2);
342342
}
343343
if (x2 & 1) {
344-
used[--x2].push_back(y1);
345-
used[x2].push_back(y2);
344+
used[--x2].pb(y1);
345+
used[x2].pb(y2);
346346
}
347347
}
348348
}

src/1-ds/union_find.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "../common/common.hpp"
1+
#include "../0-common/common.hpp"
22

33
// what: maintain dynamic connectivity with union-find and size queries.
44
// time: init O(n), join/find amortized a(n); memory: O(n)
55
// constraint: 1-indexed [1, n].
66
// usage: dsu d; d.init(n); d.join(a, b); int r = d.find(x); int s = d.size(x);
77
struct dsu {
8-
vector<int> p;
8+
vi p;
99
void init(int n) { p.assign(n + 1, -1); } // goal: reset to n singletons.
1010
int find(int x) { return p[x] < 0 ? x : p[x] = find(p[x]); } // result: root of x.
1111
int size(int x) { return -p[find(x)]; }

0 commit comments

Comments
 (0)