Skip to content

Commit

Permalink
Minify some files
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeletWithoutEgg committed Oct 1, 2023
1 parent cee9d75 commit ccf0932
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
40 changes: 15 additions & 25 deletions codes/Geometry/KDTree.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
struct KDTree {
struct Node {
int x, y, x1, y1, x2, y2, id, f;
Node *L, *R;
int x, y, x1, y1, x2, y2, id, f; Node *L, *R;
} tree[maxn], *root;
lld dis2(int x1, int y1, int x2, int y2) {
lld dx = x1 - x2, dy = y1 - y2;
Expand All @@ -16,21 +15,17 @@ struct KDTree {
tree[i].x = ip[i].first;
tree[i].y = ip[i].second;
}
root = build_tree(0, n-1, 0);
root = build(0, n-1, 0);
}
Node* build_tree(int L, int R, int d) {
if (L>R) return nullptr;
int M = (L+R)/2; tree[M].f = d%2;
Node* build(int L, int R, int d) {
if (L>R) return nullptr; int M = (L+R)/2;
nth_element(tree+L,tree+M,tree+R+1,d%2?cmpy:cmpx);
tree[M].x1 = tree[M].x2 = tree[M].x;
tree[M].y1 = tree[M].y2 = tree[M].y;
tree[M].L = build_tree(L, M-1, d+1);
tree[M].R = build_tree(M+1, R, d+1);
for (Node *s: {tree[M].L, tree[M].R}) if (s) {
tree[M].x1 = min(tree[M].x1, s->x1);
tree[M].x2 = max(tree[M].x2, s->x2);
tree[M].y1 = min(tree[M].y1, s->y1);
tree[M].y2 = max(tree[M].y2, s->y2);
Node &o = tree[M]; o.f = d % 2;
o.x1 = o.x2 = o.x; o.y1 = o.y1 = o.y;
o.L = build(L, M-1, d+1); o.R = build(M+1, R, d+1);
for (Node *s: {o.L, o.R}) if (s) {
o.x1 = min(o.x1, s->x1); o.x2 = max(o.x2, s->x2);
o.y1 = min(o.y1, s->y1); o.y2 = max(o.y2, s->y2);
}
return tree+M;
}
Expand All @@ -43,18 +38,13 @@ struct KDTree {
void dfs(int x, int y, P &mn, Node *r) {
if (!r || !touch(x, y, mn.first, r)) return;
mn = min(mn, P(dis2(r->x, r->y, x, y), r->id));
// search order depends on split dim
if (r->f == 1 ? y < r->y : x < r->x) {
dfs(x, y, mn, r->L);
dfs(x, y, mn, r->R);
} else {
dfs(x, y, mn, r->R);
dfs(x, y, mn, r->L);
}
if (r->f == 1 ? y < r->y : x < r->x)
dfs(x, y, mn, r->L), dfs(x, y, mn, r->R);
else
dfs(x, y, mn, r->R), dfs(x, y, mn, r->L);
}
int query(int x, int y) {
P mn(INF, -1);
dfs(x, y, mn, root);
P mn(INF, -1); dfs(x, y, mn, root);
return mn.second;
}
} tree;
34 changes: 13 additions & 21 deletions codes/Matching_and_Flow/Dinic.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
template <typename Cap = int64_t>
class Dinic{
template <typename Cap = int64_t> class Dinic {
private:
struct E{
int to, rev;
Cap cap;
};
int n, st, ed;
vector<vector<E>> G;
vector<int> lv, idx;
bool BFS(){
struct E { int to, rev; Cap cap; }; int n, st, ed;
vector<vector<E>> G; vector<int> lv, idx;
bool BFS() {
lv.assign(n, -1);
queue<int> bfs;
bfs.push(st); lv[st] = 0;
while (not bfs.empty()){
queue<int> bfs; bfs.push(st); lv[st] = 0;
while (not bfs.empty()) {
int u = bfs.front(); bfs.pop();
for (auto e: G[u]) {
if (e.cap <= 0 or lv[e.to]!=-1) continue;
if (e.cap <= 0 or lv[e.to] != -1) continue;
bfs.push(e.to); lv[e.to] = lv[u] + 1;
}
}
return lv[ed] != -1;
}
Cap DFS(int u, Cap f){
Cap DFS(int u, Cap f) {
if (u == ed) return f;
Cap ret = 0;
for(int &i = idx[u]; i < int(G[u].size()); ++i) {
auto &e = G[u][i];
if (e.cap <= 0 or lv[e.to]!=lv[u]+1) continue;
if (e.cap <= 0 or lv[e.to] != lv[u]+1) continue;
Cap nf = DFS(e.to, min(f, e.cap));
ret += nf; e.cap -= nf; f -= nf;
G[e.to][e.rev].cap += nf;
Expand All @@ -37,16 +30,15 @@ class Dinic{
}
public:
void init(int n_) { G.assign(n = n_, vector<E>()); }
void add_edge(int u, int v, Cap c){
void add_edge(int u, int v, Cap c) {
G[u].push_back({v, int(G[v].size()), c});
G[v].push_back({u, int(G[u].size())-1, 0});
}
Cap max_flow(int st_, int ed_){
st = st_, ed = ed_; Cap ret = 0;
Cap max_flow(int st_, int ed_) {
st = st_, ed = ed_; Cap ret = 0, f;
while (BFS()) {
idx.assign(n, 0);
Cap f = DFS(st, numeric_limits<Cap>::max());
ret += f;
f = DFS(st, numeric_limits<Cap>::max()); ret += f;
if (f == 0) break;
}
return ret;
Expand Down

0 comments on commit ccf0932

Please sign in to comment.