版本比较
Kruskal 重构树
正文
增加 212 行删除 0 行
658 行未改动全部展开659660661662663664665666667668226 行未改动全部展开895896897898899900901902903904189 行未改动全部展开1094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813096 行未改动658 行未改动全部展开:::
::::
---
### P4768 [NOI2018] 归程
简化题意,给出一个 $n$ 个结点 $m$ 条边的无向连通图,每条边有两个边权 $l_i,h_i$。每次询问给出一个二元组 $(s,d)$,边权 $h_i\le d$ 的边我们称为“不好的边”。
226 行未改动全部展开}```::::
---
### P9638 「yyOI R1」youyou 的军训
一些 tips 放在了这里 <https://www.luogu.com.cn/article/0as8n2fv>。
189 行未改动全部展开}```::::
---
### P3684 [CERC2016] 机棚障碍 Hangar Hurdles
题意就是给一个 01 矩阵,可以从每个点展开一个长度为奇数的正方形。要求询问两个点之间,可以通过的最大正方形。
我们给每一个点预处理赋予权值 $v_{x,y}$,代表位于 $(x,y)$ 最大可展开的正方形的长度。
发现时间复杂度最多是平方带一个 $\log$ 的,不妨思考枚举每个坐标然后二分一下。
此时二分的检验应该是 $O(1)$ 的。
预处理是好文明。我们再 $O(n^2)$ 预处理出 $d_{x,y}$ 表示从 $(x,y)$ 往下最长空白长度。再 $O(n^2)$ 预处理出 $f_{x,y}$ 表示以 $(x,y)$ 为左上角向右下可以延伸的最大正方形。
第一个显然是好做的。第二个显然也是好做的。
我们不妨模拟一下,推导 $(x,y)$ 时,我们往后寻找到 $(x+k,y)$ 时,需要满足 $\min\limits_{i\in[x,x+k]} d_{i,y}\ge k+1$。一个显然的是,$f_{x+1,y}\ge f_{x,y}-1$,不妨继承了之后再从已经有的右边界向右延伸。
对于求解 $\min$ 的问题,不妨用单调队列维护。
做完后我们就可以 $O(n^2\log n)$ 求 $v_{x,y}$ 了,然后再 $O(n^2)$ 建一下图,相邻点的边权为权值更小值。
然后跑个重构树就做完了。
为了辨别并查集数组把 $f$ 用 $g$ 代替。
::::info[$\texttt{code}$]```cpp#include <bits/stdc++.h>// #define int long long#define lint long long#define endl '\n'using namespace std;const int N = 1e3+5;
struct node{ int x,y,z;
node(int a = 0,int b = 0,int c = 0): x(a),y(b),z(c) {}
inline bool operator <(const node& other) const { return this->z < other.z; } inline bool operator >(const node& other) const { return this->z > other.z; }}arr[(N*N)<<1];
int n,m,cntNode;bool a[N][N];int f[N*N*2],v[N][N];int d[N][N],g[N][N];deque<int > que;vector<int> e[N*N*2];int val[N*N*2];
int fa[N*N*2];int dep[N*N*2];int lb[N*N*2];
inline int getId(int x,int y){ return n*(x-1) + y;}
inline bool check(int x,int y,int k){ int lx = x-k+1,ly = y-k+1,rx = x+k-1,ry = y+k-1; if(!(1 <= lx && lx <= n && 1 <= ly && ly <= n)) return false; if(!(1 <= rx && rx <= n && 1 <= ry && ry <= n)) return false; return g[lx][ly] >= k*2-1;}
inline int find(int x){ if(f[x] == x) return x; return f[x] = find(f[x]);}
inline void dfs(int x,int father){ fa[x] = father; dep[x] = dep[father] + 1;
int p = lb[father],q = lb[p]; lb[x] = (dep[father]-dep[p] == dep[p]-dep[q] ? q : father);
for(int y : e[x]){ if(y != father) dfs(y,x); }}
inline int lca(int x,int y){ if(dep[x] < dep[y]) swap(x,y); while(dep[x] > dep[y]){ if(dep[lb[x]] >= dep[y]) x = lb[x]; else x = fa[x]; } while(x != y){ if(lb[x] != lb[y]) x = lb[x],y = lb[y]; else x = fa[x],y = fa[y]; } return x;}
signed main(){ ios::sync_with_stdio(0); cin.tie(0); // freopen("app.in","r",stdin); // freopen("app.out","w",stdout); cin >> n;
char character; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ cin >> character; a[i][j] = (character == '.'); } }
for(int i = n; i >= 1; i--){ for(int j = 1; j <= n; j++){ if(!a[i][j]) continue; d[i][j] = d[i+1][j] + 1; } }
for(int i = 1; i <= n; i++){ int end = 1;
que.clear(); for(int j = 1; j <= n; j++){ if(!a[i][j]) continue; end = max(end,j+1); g[i][j] = max(1,g[i][j-1]-1);
while(!que.empty() && que.front() < j) que.pop_front(); if(que.empty()) que.push_back(j);
while(a[i][end]){ while(!que.empty() && d[i][que.back()] >= d[i][end]) que.pop_back(); que.push_back(end); if(g[i][j]+1 > d[i][que.front()]) break; else g[i][j]++;
end++; } } }
for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ int l = 1,r = n; while(l <= r){ int mid = l + r >> 1; if(check(i,j,mid)){ v[i][j] = (mid<<1) - 1; l = mid + 1; }else r = mid - 1; } } }
for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(i > 1) arr[++m] = node(getId(i-1,j),getId(i,j),min(v[i-1][j],v[i][j])); if(j > 1) arr[++m] = node(getId(i,j-1),getId(i,j),min(v[i][j-1],v[i][j])); } }
for(int i = 1; i <= n*n*2; i++) f[i] = i;
sort(arr+1,arr+1+m,greater<node>());
cntNode = n*n; for(int i = 1; i <= m; i++){ int x = arr[i].x,y = arr[i].y,z = arr[i].z; int fx = find(x),fy = find(y); if(fx != fy){ val[++cntNode] = z,f[fx] = f[fy] = cntNode;; e[fx].push_back(cntNode),e[cntNode].push_back(fx); e[fy].push_back(cntNode),e[cntNode].push_back(fy); } }
dfs(cntNode,0);
int q; cin >> q; int Ax,Ay,Bx,By; while(q--){ cin >> Ax >> Ay >> Bx >> By; cout << val[lca(getId(Ax,Ay),getId(Bx,By))] << endl; } return 0;}```::::
---
### P4197 [ONTAK2010] Peaks
很典的两个 trick 结合。
6 行未改动