Skip to content

Instantly share code, notes, and snippets.

@NamPE286
NamPE286 / BCC.cpp
Created December 6, 2023 16:52
Block cut tree (Biconnected component)
// https://cses.fi/problemset/result/7855807/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class BinaryLifting {
vector<vector<ll>> graph;
vector<vector<ll>> parent;
@NamPE286
NamPE286 / mergeSortTreeImmutable.cpp
Created December 5, 2023 07:44
Merge Sort Tree Immutable
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class MergeSortTreeImmutable {
struct Node {
vector<ll> v;
pair<ll, ll> range;
Node* left = nullptr;
@NamPE286
NamPE286 / dsuRollBack.cpp
Last active December 8, 2023 03:27
DSU with rollback
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class DSU {
vector<ll> p; // parent if positive, size if negative, parent of root is the size of component
stack<pair<ll, ll>> log; // keep track of p[i] value history, first is index, second is value
stack<ll> size;
@NamPE286
NamPE286 / scc.cpp
Created November 1, 2023 03:02
Find strongly connected component (SCC) with dfs tree
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class DFSTree {
vector<ll> nums, low;
vector<bool> deleted;
stack<ll> st;
vector<ll> scc;
@NamPE286
NamPE286 / hld.cpp
Last active December 4, 2023 07:31
Heavy Light Decomposition
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class SegmentTree {
private:
struct Node {
ll value;
pair<ll, ll> range;
@NamPE286
NamPE286 / sparseTable.cpp
Last active October 26, 2023 09:14
Sparse table
// https://cses.fi/problemset/task/1647
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class SparseTable {
private:
vector<vector<ll>> ST;
@NamPE286
NamPE286 / monotoneChain.cpp
Created September 26, 2023 01:33
Convex Hull (monotone chain algorithm)
// https://cses.fi/problemset/task/2195/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll crossProd(pair<ll, ll> a, pair<ll, ll> b, pair<ll, ll> c) {
return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first);
}
@NamPE286
NamPE286 / binaryLifing.cpp
Last active September 16, 2023 04:34
Binary lifting template
// https://cses.fi/problemset/task/1688/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
class BinaryLifting {
ll MAXLOG;
vector<vector<ll>> parent;
@NamPE286
NamPE286 / LCABinaryJumping.cpp
Created September 15, 2023 19:15
Lowest common ancestor with binary jumping (binary lifting)
// https://cses.fi/problemset/task/1688/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXLOG = 18;
void preCalc(vector<vector<ll>>& parent) {
@NamPE286
NamPE286 / binaryJumping.cpp
Last active September 15, 2023 18:57
Binary Jumping (binary lifting)
// https://cses.fi/problemset/task/1687/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll jump(vector<vector<ll>>& parent, ll x, ll k) {
for(ll i = 0; i <= 18; i++) {
if(k & (1 << i)) {