본문 바로가기

라이브러리12

Segment tree struct Segt { using T = int; static constexpr T def = 1= 1; ) { seg[i] = segfun(seg[i 2021. 8. 12.
BIT struct Bit { ll cap; vector arr, tree; Bit(int size) { cap = size + 1; arr.resize(cap, 0); tree.resize(cap, 0); } void update(int i, ll x) { ll diff = x; arr[i] += x; while (i < cap) { tree[i] += diff; i += i & -i; } } ll query(int l, int r) const { return pquery(r) - pquery(l - 1); } ll pquery(int i) const { ll ret = 0; while (i > 0) { ret += tree[i]; i -= i & -i; } return ret; } }; 2021. 3. 22.
DSU int dsfind(int tar) { if (tar == root[tar]) return tar; return root[tar] = dsfind(root[tar]); } void dsmerge(int a, int b) { a = dsfind(a), b = dsfind(b); if (a != b) root[a] = b; } 2021. 3. 22.
Rabin karp #include #define sad std::cout.flush(), system("pause") using namespace std; using ll = long long; using pii = pair; using pll = pair; namespace rbk { const ll x = 131, md1 = 1e9+7, md2 = 1e9+9; int plen; pll d; void init(int n) { plen = n; d = {1, 1}; for (int i = 0; i < n; ++i) { d.first = (d.first * x) % md1, d.second = (d.second * x) % md2; } } pll hash(string &s, int len = 0, int b = 0) { p.. 2021. 2. 10.
BFS #include using namespace std; typedef long long ll; typedef pair pii; int R, C; int board[100][100]; int dir8[2][8]{ {0, -1, 0, 1, -1, -1, 1, 1}, {1, 0, -1, 0, 1, -1, -1, 1} }; bool dup[100][100]; void bfs(pii init) { int cr, cc, nr, nc; queue que; dup[init.first][init.second] = true; for (que.push(init); !que.empty(); que.pop()) { tie(cr, cc) = que.front(); for (int i = 0; i < 4; ++i) { nr = cr.. 2020. 12. 13.
Suffix Array와 LCP Array #include using namespace std; typedef long long ll; typedef pair pii; vector suffixArray, LCPArray; void getLCP(vector& sa, vector& lcpa, string& s) { int i, j, k, l = 0, m = 26, sLen = s.length(); sa.resize(sLen, 0); lcpa.resize(sLen, 0); // cnt: radix cnt | x: rank vector cnt(max(sLen, m), 0), x(sLen, 0), y(sLen, 0); for (i = 0; i < sLen; ++i) cnt[x[i] = s[i] - 'a']++; for (i = 0; i < m; ++i) .. 2020. 9. 23.