This file contains hidden or 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
| ll bin_exp(ll base, ll exp){ | |
| if (exp==0) return 1; | |
| return bin_exp(base*base%MOD, exp/2)*(exp%2==1 ? base : 1)%MOD; | |
| } | |
| ll mod_inv(ll a){ | |
| return bin_exp(a, MOD-2); | |
| } |
This file contains hidden or 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
| vector<int> ans; | |
| for (auto x : A) { | |
| auto it = lower_bound(ans.begin(), ans.end(), x); | |
| if (it == ans.end()) | |
| ans.push_back(x); | |
| else | |
| *it = x; | |
| } |
This file contains hidden or 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
| struct MinCostFlow { | |
| const static int MAXV = MAXN; | |
| const int INF = 1e9 + 5; | |
| const int s = MAXV - 2, t = MAXV - 1; | |
| struct edge { | |
| int from, to, cap, flow, cost; | |
| }; | |
| int cost[MAXV][MAXV]; | |
| vector<edge> edges; |
This file contains hidden or 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
| double simpson(function<double(double)> f, double a, double b) { | |
| double c = (a + b) / 2; | |
| return (b - a) / 6 * (f(a) + 4 * f(c) + f(b)); | |
| } | |
| double rec(function<double(double)> f, double a, double b, double eps, double S, bool rel = true) { | |
| double c = (a + b) / 2; | |
| double S1 = simpson(f, a, c), S2 = simpson(f, c, b), T = S1 + S2; | |
| if (abs(T - S) <= 15 * eps || b - a < 1e-10 || (rel && abs((T - S) / S) <= 15 * eps)) | |
| return T + (T - S) / 15; | |
| return rec(f, a, c, eps / 2, S1, rel) + rec(f, c, b, eps / 2, S2, rel); |
This file contains hidden or 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
| int sz[MAXN]; | |
| int cpar[MAXN]; | |
| bool dead[MAXN]; | |
| int sizedfs(int cur, int p) { | |
| sz[cur] = 1; | |
| for (auto i : adj[cur]) { | |
| if (i != p && !dead[i]) | |
| sz[cur] += sizedfs(i, cur); | |
| } | |
| return sz[cur]; |
This file contains hidden or 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
| struct Line { | |
| mutable ll m, b, p; | |
| bool operator<(const Line &o) const { return m < o.m; } | |
| bool operator<(const ll &x) const { return p < x; } | |
| }; | |
| struct LineContainer : multiset<Line, less<>> { // upper convex hull. | |
| // (for doubles, use inf = 1/.0, div(a,b) = a/b) | |
| const ll inf = LLONG_MAX; | |
| ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } |
This file contains hidden or 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
| static char buf[450 << 20]; | |
| void *operator new(size_t s) { | |
| static size_t i = sizeof buf; | |
| assert(s < i); | |
| return (void *)&buf[i -= s]; | |
| } | |
| void operator delete(void *) {} |
This file contains hidden or 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
| pair<bool, array<int, 2>> canMerge(array<int, 4> pts) { | |
| auto cmp = [&](int a, int b) { return make_pair(lca.depth[a], a) > make_pair(lca.depth[b], b); }; | |
| sort(pts.begin(), pts.end(), cmp); | |
| do { | |
| if (lca.dist(pts[0], pts[1]) + lca.dist(pts[1], pts[2]) + lca.dist(pts[2], pts[3]) == lca.dist(pts[0], pts[3])) | |
| return {true, {pts[0], pts[3]}}; | |
| } while (next_permutation(pts.begin() + 1, pts.end(), cmp)); | |
| return {false, {0, 0}}; | |
| } |
This file contains hidden or 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 <int MAXN> struct SCC { | |
| int num[MAXN], low[MAXN]; | |
| bool curProcess[MAXN]; | |
| stack<int> curVis; | |
| int dfsCnt = 0; | |
| SCC() { fill(begin(num), end(num), -1), fill(begin(low), end(low), -1); } | |
| void dfs(int cur) { | |
| num[cur] = low[cur] = dfsCnt++; | |
| curProcess[cur] = true; | |
| curVis.push(cur); |
This file contains hidden or 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
| int fac; | |
| template <class It> int perm_to_int(It begin, It end) { | |
| int x = 0, n = 0; | |
| for (It i = begin; i != end; ++i, ++n) | |
| if (*i < *begin) | |
| ++x; | |
| int val = 0; | |
| if (n > 2) | |
| val = perm_to_int(++begin, end); | |
| else |
OlderNewer