Skip to content
Snippets Groups Projects
Commit 26fb6f5e authored by Fernando K's avatar Fernando K
Browse files

Reorganiza caderno

parent 8e1787ea
No related branches found
No related tags found
No related merge requests found
Showing
with 297 additions and 331 deletions
No preview for this file type
This diff is collapsed.
......@@ -8,8 +8,7 @@ struct node {
vector<node> aca (1);
void ins(string ne, int ix) {
int u = 0;
for (int i = 0; i < ne.size(); i++) {
int u = 0; for (int i = 0; i < ne.size(); i++) {
int ch = to_i(ne[i]);
if (aca[u].nxt[ch] == -1) {
aca[u].nxt[ch] = aca.size();
......@@ -18,8 +17,7 @@ void ins(string ne, int ix) {
}
u = aca[u].nxt[ch];
}
aca[u].leaf = 1;
aca[u].ix.push_back(ix);
aca[u].leaf = 1; aca[u].ix.push_back(ix);
}
int go(int u, int c);
......
......@@ -7,14 +7,11 @@ int get_points_inside(int i, double r, int n) {
ang.push_back(dbi(a - b, j));
ang.push_back(dbi(a + b, j));
}
sort(begin(ang), end(ang));
sort(all(ang));
int count = 1, res = 1;
for (auto angle : ang) {
count += angle.second ? 1 : -1;
res = max(res, count);
}
return res;
}
......@@ -8,7 +8,8 @@ int calc_size(int u, int p) {
}
int find_centroid(int u, int p, int n) {
for (int v : g[u]) if (v != p && !vis[v] && sz[v] > n/2)
for (int v : g[u])
if (v != p && !vis[v] && sz[v] > n/2)
return find_centroid(v, u, n);
return u;
}
......
void compress(vector<int>& v, vector<int>& cv) {
set<int> vs (begin(v), end(v));
set<int> vs (all(v));
map<int, int> to, fm;
int ix = 0;
for (int i : vs) { to[i] = ix; fm[ix] = i; ix++; }
......
......@@ -9,7 +9,8 @@ struct custom_hash {
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
chrono::steady_clock::now()
.time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
......@@ -13,6 +13,6 @@ ll digit_dp(int i, int l, bool lo, bool nz) {
}
ll solve(ll b) {
num = to_string(b); reverse(begin(num), end(num));
num = to_string(b); reverse(all(num));
return digit_dp(num.size()-1, 10, 0, 0);
}
......@@ -2,8 +2,7 @@ void dijkstra(int src, int dest, int n) {
vector<ll> d (n, oo); vector<bool> vis (n);
vector<vector<int>> dag (n);
priority_queue<ii, vector<ii>, greater<ii>> q;
d[src] = 0;
q.push({0, src});
d[src] = 0; q.push({0, src});
while (!q.empty()) {
auto [c, u] = q.top(); q.pop();
if (vis[u]) { continue; }
......
struct edge { int u, v; ll cap, flow; };
vector<edge> edges;
const ll oo = 1e18;
vector<vector<int>> res (N);
vector<int> level (N), ptr (N);
queue<int> q;
void link(int u, int v, ll cap) {
res[u].push_back(edges.size());
edges.push_back({ u, v, cap, 0 });
res[v].push_back(edges.size());
edges.push_back({ v, u, 0, 0 });
}
struct edge { int u, v; ll cap, flow = 0; };
vector<edge> edges; queue<int> q;
bool minimum_path(int s, int t) {
while (!q.empty()) {
......@@ -29,15 +20,13 @@ ll dfs(int u, int t, ll pushed) {
if (pushed == 0) { return 0; }
if (u == t) { return pushed; }
for (int& cid = ptr[u]; cid < res[u].size(); cid++) {
int i = res[u][cid];
int v = edges[i].v;
int i = res[u][cid], v = edges[i].v;
if (level[u] + 1 != level[v]) { continue; }
if (edges[i].cap - edges[i].flow < 1) { continue; }
ll tr = dfs(v, t,
min(pushed, edges[i].cap - edges[i].flow));
if (tr == 0) { continue; }
edges[i].flow += tr;
edges[i^1].flow -= tr;
edges[i].flow += tr; edges[i^1].flow -= tr;
return tr;
}
return 0;
......@@ -46,11 +35,11 @@ ll dfs(int u, int t, ll pushed) {
ll dinic /* O(EV^2) */(int s, int t) {
ll max_flow = 0;
while (1) {
fill(begin(level), end(level), -1);
fill(all(level), -1);
level[s] = 0; q.push(s);
if (!minimum_path(s, t)) { break; }
fill(begin(ptr), end(ptr), 0);
while (ll pushed = dfs(s, t, oo))
fill(all(ptr), 0);
while (ll pushed = dfs(s, t, 1e18))
max_flow += pushed;
}
return max_flow;
......
......@@ -15,7 +15,8 @@ vector<int> eulerian_path(int src, int n) {
st.push(v);
}
}
for (int u = 0; u < n; u++) if (g[u].size()) { return {}; }
reverse(begin(res), end(res));
for (int u = 0; u < n; u++) if (g[u].size())
return {};
reverse(all(res));
return res;
}
......@@ -11,7 +11,7 @@ void link(int u, int v, int cap, int cost) {
}
bool minimum_path(int s, int t) {
fill(begin(dist), end(dist), oo);
fill(all(dist), oo);
queue<int> q; q.push(s); dist[s] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
......
......@@ -9,7 +9,7 @@ void link(int u, int v, int c) {
}
bool minimum_path(int s, int t) {
fill(begin(dist), end(dist), oo);
fill(all(dist), oo);
queue<int> q; q.push(s); dist[s] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
......
vector<pt> convex_hull(vector<pt>& ps, bool col = 0) {
pt p0 = *min_element(begin(ps), end(ps), [](pt a, pt b) {
pt p0 = *min_element(all(ps), [](pt a, pt b) {
return make_pair(a.py, a.px) < make_pair(b.py, b.px);
});
sort(begin(ps), end(ps), [&p0](pt a, pt b) {
sort(all(ps), [&p0](pt a, pt b) {
int o = seg_ornt(p0, a, b);
return o < 0 || (o == 0 && norm(p0-a) < norm(p0-b));
});
......
......@@ -11,7 +11,8 @@ int hld_len(int u, int v) {
int hld_kth(int u, int v, int k) {
bool sw = 0; int l = 0, r = hld_len(u, v)-1;
for (; hds[u] != hds[v]; v = par[hds[v]]) {
if (dep[hds[u]] > dep[hds[v]]) { swap(u, v); sw ^= 1; }
if (dep[hds[u]] > dep[hds[v]])
{ swap(u, v); sw ^= 1; }
int sz = ixs[v]-ixs[hds[v]]+1;
int i = sw ? k-l : r-k;
if (0 <= i && i < sz) { return rixs[ixs[v]-i]; }
......@@ -26,7 +27,8 @@ vector<int> hld_path(int u, int v, int k) {
int len = hld_len(u, v); vector<int> path (len);
bool sw = 0; int l = 0, r = len-1;
for (; hds[u] != hds[v]; v = par[hds[v]]) {
if (dep[hds[u]] > dep[hds[v]]) { swap(u, v); sw ^= 1; }
if (dep[hds[u]] > dep[hds[v]])
{ swap(u, v); sw ^= 1; }
for (int i = ixs[v]; i >= ixs[hds[v]]; i--) {
path[sw ? l++ : r--] = rixs[i];
}
......
ll inclusion_exclusion(int n, int m, ll a, ll d) {
vector<ll> exc { a, a+d, a+2*d, a+3*d, a+4*d };
ll total = 0;
for (int b = 1; b < (1<<5); b++) {
ll x = 1;
for (int i = 0; i < 5; i++) if (b & (1<<i)) {
if (x > ceil_div(m, exc[i]) * gcd(x, exc[i])) {
x = b+1; break;
}
x = b+1; break; }
x = lcm(x, exc[i]);
}
ll s = (__builtin_popcount(b) % 2) ? +1 : -1;
total += s * multiples_inclusive(n, m, x);
}
cout << (m-n+1) - total << "\n";
}
......@@ -5,20 +5,18 @@ int get_ii(pt pair, int ix) {
struct kdtree {
struct node {
node(pt& p) : p(p), left(nullptr), right(nullptr) {}
node(pt& p) : p(p), left(0), right(0) {}
double dist_sq(const pt& o) { return norm(o - p); }
pt p; node* left, *right;
};
node* root = 0;
vector<double> best;
vector<node> t;
vector<double> best; vector<node> t;
struct cmp {
cmp(size_t _index) : ix(_index) {}
size_t ix; cmp(size_t _index) : ix(_index) {}
bool operator()(const node& n1, const node& n2) {
return get_ii(n1.p, ix) < get_ii(n2.p, ix);
}
size_t ix;
};
node* make_tree(size_t begin, size_t end, size_t ix) {
......@@ -32,12 +30,10 @@ struct kdtree {
}
void nearest_k(node* r, const pt& p, size_t ix, int k) {
if (r == nullptr)
return;
if (r == nullptr) { return; }
double d = r->dist_sq(p);
if (best.size() < k || d < best.back()) {
best.push_back(d);
sort(begin(best), end(best));
best.push_back(d); sort(all(best));
if (best.size() > k)
best.erase(begin(best)+k, end(best));
}
......@@ -56,12 +52,10 @@ struct kdtree {
double nearest_k(const pt& p, int k) {
if (root == 0) { throw logic_error("empty tree"); }
best.clear();
nearest_k(root, p, 0, k);
best.clear(); nearest_k(root, p, 0, k);
double acc = 0;
for (int i = 0; i < best.size(); i++) {
for (int i = 0; i < best.size(); i++)
acc += sqrt(best[i]);
}
return acc;
}
};
void compute_automaton(string s, vector<vector<int>>& aut) {
s += '#';
int n = s.size();
vector<vector<int>> kmp_automaton(string s) {
s += '#'; int n = s.size();
vector<int> pi = pre(s);
aut.assign(n, vector<int>(26));
for (int i = 0; i < n; i++) {
vector<vector<int>> aut (n, vector<int>(26));
for (int i = 0; i < n; i++)
for (int c = 0; c < 26; c++) {
int j = i;
while (j > 0 && 'a' + c != s[j])
j = pi[j-1];
if ('a' + c == s[j])
j++;
if ('a' + c == s[j]) { j++; }
aut[i][c] = j;
}
}
return aut;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment