This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 要求T具有<运算符 | |
template <typename T> | |
void sort_suffix(const T *s, int n, vector<int>& suf, vector<int>& h) { | |
assert(SZ(suf) >= n && SZ(h) >= n); | |
vector<int> rank(n); | |
iota(all(suf), 0); | |
sort(all(suf), [s](int x, int y) { return s[x] < s[y]; }); | |
rank[suf[0]] = 0; | |
for (int i = 1; i < n; ++i) { | |
rank[suf[i]] = rank[suf[i - 1]] + (s[suf[i - 1]] < s[suf[i]]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef _MSC_VER | |
#include <intrin.h> | |
static inline int __builtin_ctz(uint32_t x) { | |
unsigned long ret; | |
_BitScanForward(&ret, x); | |
return (int)ret; | |
} | |
static inline int __builtin_ctzll(unsigned long long x) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tourist 的逆元模板 | |
template <typename T> | |
T inverse(T a, T m) { | |
T u = 0, v = 1; | |
while (a != 0) { | |
T t = m / a; | |
m -= t * a; swap(a, m); | |
u -= t * v; swap(u, v); | |
} | |
assert(m == 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto solve = [](vi& h, int n) { | |
vi L(n + 1), R(n + 1); | |
// 严格递增的单调栈 | |
vpii inc{{0, 0}}; // pair.first 表示高度,pair.second 表示下标。假设 h[0] = -INF。 | |
assert(SZ(inc) == 1); | |
for (int i = 1; i <= n; i++) { | |
while (inc.back().first >= h[i]) { | |
inc.pop_back(); | |
} | |
L[i] = inc.back().second + 1; //左闭 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MCMF { | |
struct arc { | |
const int to, next, cost; | |
mutable int cap; | |
}; | |
const int n_node; | |
vi head; | |
vector<arc> e; | |
mutable vi d; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T> | |
T inverse(T a, T m) { | |
T u = 0, v = 1; | |
while (a != 0) { | |
T t = m / a; | |
m -= t * a; swap(a, m); | |
u -= t * v; swap(u, v); | |
} | |
assert(m == 1); | |
return u; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* a tutorial on Gauss-Jordan elimination: https://cp-algorithms.com/linear_algebra/linear-system-gauss.html | |
From the article: | |
Strictly speaking, the method described below should be called "Gauss-Jordan", or Gauss-Jordan elimination, | |
because it is a variation of the Gauss method, described by Jordan in 1887. | |
*/ | |
using num = modnum<1000003>; | |
using mat = vv<num>; | |
using vec = vector<num>; | |
// precondition: 系数矩阵不含0,否则需要选主元。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BEGIN 树剖模板 | |
vi g[N]; | |
int fa[N]; | |
int sz[N]; | |
int heavy_son[N]; | |
int dep[N]; | |
void dfs(int u, int f) { | |
fa[u] = f; | |
sz[u] = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int nax = 10005; | |
vector<int> g[nax]; | |
int scc_id[nax]; | |
int dn[nax]; // dfs_no | |
int dfs_clock; | |
int low[nax]; | |
bitset<nax> bs[nax]; | |
vector<bool> in_stack(nax); | |
stack<int> stk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** 朴素AC自动机 ***/ | |
struct node { | |
static const int sigma_size = 2; | |
array<int, sigma_size> pos{}; | |
bool is_accept_state = false; | |
int suffix_pos = 0; | |
}; | |
vector<node> trie; | |
void insert(const int* s, int len) { |
NewerOlder