版本比较
无题
正文
增加 110 行删除 382 行
4 行未改动567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939491011122 行未改动151617181051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292301920212220 行未改动434445462594748495051525354555610 行未改动67686970279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345717273746 行未改动818283843603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604618586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951964 行未改动### [$\text{\color{yellow}P14361}$](https://www.luogu.com.cn/problem/P14361)
[前情提要。](https://www.luogu.com.cn/article/ko8k545y)
```cpp#include<bits/stdc++.h>//if atcoder//#pragma GCC optimize(3)#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define int long long#define I using#define AK namespace#define CSPS2025 stdI AK CSPS2025;const int maxn=1e6+10,maxm=1e3+10,mod=998244353;int t,n,m,x,y,u,v,w,tot,ans,res[maxn];struct node{ int x,y,z;}arr[maxn];bool cmpx(node x,node y){ return (x.x-max(x.y,x.z))>(y.x-max(y.y,y.z));}signed main(){ cin>>t; while(t--) { vector<int>cnt; u=0,v=0,w=0,tot=0,ans=0; cin>>n; for(int i=1;i<=n;i++) { res[i]=0; } for(int i=1;i<=n;i++) { cin>>arr[i].x>>arr[i].y>>arr[i].z; ans+=max({arr[i].x,arr[i].y,arr[i].z}); if(arr[i].x>=max(arr[i].y,arr[i].z)) { res[i]=1; u++; } else if(arr[i].y>=max(arr[i].x,arr[i].z)) { res[i]=2; v++; } else if(arr[i].z>=max(arr[i].x,arr[i].y)) { res[i]=3; w++; } } for(int i=1;i<=n;i++) { if(u>(n>>1) and res[i]==1) { cnt.push_back(arr[i].x-max(arr[i].y,arr[i].z)); } if(v>(n>>1) and res[i]==2) { cnt.push_back(arr[i].y-max(arr[i].x,arr[i].z)); } if(w>(n>>1) and res[i]==3) { cnt.push_back(arr[i].z-max(arr[i].x,arr[i].y)); } } stable_sort(cnt.begin(),cnt.end()); while((u--)>(n>>1)) { ans-=cnt[tot++]; } while((v--)>(n>>1)) { ans-=cnt[tot++]; } while((w--)>(n>>1)) { ans-=cnt[tot++]; } cout<<ans<<"\n"; } return 0;}```
## 11.14
### [$\text{\color{cyan}P14362}$](https://www.luogu.com.cn/problem/P14362)
2 行未改动观察复杂度 $\Theta(2^k (kn+m)\log(kn+m))$ 的瓶颈,$2^k$ 显然无法优化,发现 $m \leq 10^6$ 是很大的,于是可以从这个角度入手。先跑一遍原图 MST ,将不属于原图 MST 的边删掉,这样做是干什么呢?其实后续村庄的操作就是在原图上加边,既然原图这些边都用不到,之后当然也用不到了。这样 $\Theta(2^k kn\log kn)$ 就得到了 $80pts$。
继续观察复杂度,如果把这个 $\log$ 去掉就可以过了,于是我们可以在一开始将预处理出的 $n-1$ 条边和后面总共要加的 $kn$ 条边放一起排序,每一次遍历的时候就不用去排序了,记录此时选的村庄,遇到属于不选择改造的,边不管它就行了,时间复杂度 $\Theta(2^k kn)$,可以通过。
```cpp#include<bits/stdc++.h>//#pragma GCC optimize(3)#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)#define int long long#define I using#define AK namespace#define this_contest stdI AK this_contest;const int maxn=6e6+10,maxm=1e3+10,mod=998244353;int t,n,m,k,x,y,u,v,w,q,tot,cnt,cur,ans,answer,arr[maxn],c[maxn],fa[maxn],vis[maxn];pair<int,pair<int,int> >e[maxn],g[maxn];int getfa(int x){ if(fa[x]==x) { return x; } return fa[x]=getfa(fa[x]);}void merge(int x,int y){ if(x==y) { return; } fa[getfa(x)]=getfa(y);}int kruskal(){ int res=0; sort(e+1,e+m+1); for(int i=1;i<=m;i++) { if(getfa((e[i].second).first)!=getfa((e[i].second).second)) { res+=e[i].first; merge((e[i].second).first,(e[i].second).second); g[++cnt]=e[i]; } if(cnt>=n-1) { return res; } }// cerr<<res<<" deni g "; return -1;}signed main(){ FastIO; cin>>n>>m>>k; for(int i=1;i<=n;i++) { fa[i]=i; } for(int i=1;i<=m;i++) { cin>>u>>v>>w; e[i]={w,{u,v}}; } if(!k) { cout<<kruskal(); return 0; } answer=kruskal(); for(int i=1;i<=k;i++) { cin>>c[i]; for(int j=1;j<=n;j++) { cin>>u; g[++cnt]={u,{j,n+i}}; } } stable_sort(g+1,g+cnt+1); for(int s=1;s<=(1<<k);s++) { cur=0,ans=0,tot=0; for(int i=1;i<=k;i++) { if((s>>(i-1))&1) { vis[i]=1; tot++; ans+=c[i]; } else { vis[i]=0; } } for(int i=1;i<=n+k+1;i++) { fa[i]=i; } q=n+tot-1; for(int i=1;i<=cnt;i++) { u=(g[i].second).first,v=(g[i].second).second; if(v>n) { if(!vis[v-n]) { continue; } } if(getfa(u)!=getfa(v)) { ans+=g[i].first; merge((g[i].second).first,(g[i].second).second); cur++; if(cur>=q) { break; } } } answer=min(ans,answer); } cout<<answer; return 0;}```
## 11.15
早上打了一堆板子。已严肃学习 ST 表。已严肃学习 LCA。
20 行未改动## 11.21
无法在 2h 内 AK 入门赛/fn
咕咕咕。### [$\text{\color{cyan}P14139}$](https://www.luogu.com.cn/problem/P14139)
会了没写。
后续:11.26
## 11.22
### [$\text{\color{orange}P3370}$](https://www.luogu.com.cn/problem/P3370)
10 行未改动这个限制已经很开放了,我们可以直接尝试去贪心。对于一个节点 $u$ 的子树,他们一定都能达到这个节点,直接 dfs 统计,如果节点数超过 $B$ 个就分,并将 $u$ 作为省会,这个东西可以用一个栈来维护。处理完所有子树后,将 $u$ 也入栈,此时栈中的节点将被返回到上一层,经过这种操作后,栈的大小最大为 $B−1$ 个子树节点加 $u$ 本身,即栈中结点不超过 $B$ 个,所以一个子树会对集合最多增加 $B-1$ 个节点,那么每个省的大小最大为 $2B-1$,满足 $\leq 3B$。
在 dfs 结束后,栈中可能还会有点,大小同理,也 $\leq B-1$,把他并入根节点所在的省,那么此时根节点这个块的大小最大可能为 $3B−1$,方法成立。
```cpp#include<bits/stdc++.h>#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)#define int long long#define I using#define AK namespace#define CSPS2026 stdI AK CSPS2026;const int maxn=1e6+10,maxm=1e3+10,mod=998244353;int t,n,m,x,y,z,u,v,w,tot,arr[maxn],pro[maxn],ans[maxn];vector<int>e[maxn];stack<int>s;void dfs(int u,int fa){ int sz=(int)(s.size()); for(auto v:e[u]) { if(v==fa) { continue; } dfs(v,u); if((int)(s.size())-sz>=m) { ans[++tot]=u; while((int)(s.size())>sz) { pro[s.top()]=tot; s.pop(); } } } s.push(u);}signed main(){ cin>>n>>m; for(int i=1;i<n;i++) { cin>>u>>v; e[u].push_back(v); e[v].push_back(u); } dfs(1,-1); if(!tot) { ans[++tot]=1; } while(!s.empty()) { pro[s.top()]=tot; s.pop(); } cout<<tot<<"\n"; for(int i=1;i<=n;i++) { cout<<pro[i]<<" "; } cout<<"\n"; for(int i=1;i<=tot;i++) { cout<<ans[i]<<" "; } return 0;}```
### [$\text{\color{cyan}ABC383E}$](https://www.luogu.com.cn/problem/AT_abc383_e)
作为重构树大蛇我必须要感谢这道让我一眼秒掉的题!!!
6 行未改动这个时候我们发现重构树上的边点 $1w,2w,5w$ 由深到浅依次增大,进一步推广至所有的边点都是如此,这个不难证明。
那么思路就出来了,尽量把 LCA 往下放,每次加边时将两子树内的点分别连接即可。
```cpp#include<bits/stdc++.h>//if atcoder//#pragma GCC optimize(3)#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)#define int long long#define I using#define AK namespace#define CSPS2026 stdI AK CSPS2026;const int maxn=1e6+10,maxm=1e3+10,mod=998244353;int n,m,q,x,y,k,tot,answer,arr[maxn],u[maxn],v[maxn],head[maxn],fa[maxn],cntx[maxn],cnty[maxn],ans[maxn];struct node{ int to,nxt,val;}g[maxn];struct edge{ int to,nxt;}e[maxn];void add(int u,int v){ e[++tot].to=v; e[tot].nxt=head[u]; head[u]=tot; }int cmp(node x,node y){ return x.val<y.val;}int getfa(int x){ if(fa[x]==x) { return x; } return fa[x]=getfa(fa[x]);}void merge(int x,int y){ if(x==y) { return; } fa[getfa(x)]=getfa(y);}void rebuild(){ int cnt=n; sort(g+1,g+m+1,cmp); for(int i=1;i<=m;i++) { int U=getfa(g[i].to),V=getfa(g[i].nxt); if(U!=V) { fa[U]=fa[V]=++cnt; add(cnt,U),add(cnt,V); ans[cnt]=g[i].val; int cur=min(cntx[U],cnty[V]); answer+=cur*g[i].val; cntx[U]-=cur,cnty[V]-=cur; cur=min(cnty[U],cntx[V]); answer+=cur*g[i].val; cnty[U]-=cur,cntx[V]-=cur; cntx[cnt]=cntx[U]+cntx[V],cnty[cnt]=cnty[U]+cnty[V]; } } return; }signed main(){ FastIO; cin>>n>>m>>k; for(int i=1;i<=n*2;i++) { fa[i]=i; } for(int i=1;i<=m;i++) { cin>>g[i].to>>g[i].nxt>>g[i].val; } for(int i=1;i<=k;i++) { cin>>u[i]; cntx[u[i]]++; } for(int i=1;i<=k;i++) { cin>>v[i]; cnty[v[i]]++; } rebuild(); cout<<answer; return 0;}/*在 kruskal 重构树上搞这个东西,显然选lca更深的更优重构树从叶子到根节点点权依次增大每次加边时将两子树内的点分别连接即可*/```
### [$\text{\color{cyan}CF938D}$](https://www.luogu.com.cn/problem/CF938D)
也是一眼秒了。
先把这个柿子简化了,$\min_{j=1}^{n}\left(d(i,j)+a_j+d(j,i)\right)=\min_{j=1}^{n}\left(2d(i,j)+a_j\right)$。建图的时候边权 $\times 2$。
全源最短路显然是不可取的,从数据范围看,只支持一个单源,理所当然地想到建一个超级源点,与每个点连一条边权为该点点权的边,跑 dijkstra 即可。
咕咕咕。
## 11.26
### [$\text{\color{cyan}P14139}$](https://www.luogu.com.cn/problem/P14139)
我们把这两个很丑的东西画出来,发现他们是反函数,然后就做完了。
原式可化为 $n^2+\sum^{n}_{x=1}[n \mid x^2]$。Pollard-Rho 艹过去了。
### [$\text{\color{cyan}CF1860E}$](https://www.luogu.com.cn/problem/CF1860E)
[挺好的一道题。](https://www.luogu.com.cn/article/ccy2hp61)
## 12.05
### [$\text{\color{yellow}P14635}$](https://www.luogu.com.cn/problem/P14635)
显然只对一种糖 $x$ 选两次及以上,我们枚举有多少个物品只选了一次,计算剩下的代价能选多少次 $x$ 即可。时间复杂度 $\Theta(n\log n)$。
### [$\text{\color{cyan}P10935}$](https://www.luogu.com.cn/problem/P10935)
双倍经验。
## 12.06
上午打了一些板子。
### [$\text{\color{green}P3396}$](https://www.luogu.com.cn/problem/P3396)
根号分治,越暴力的东西越好用。记 $ans_{i,j}$ 为对 $i$ 取模为 $j$ 的答案,$\sqrt{n}$ 以下这么做,$\sqrt{n}$ 以上直接暴力。
### [$\text{\color{cyan}P2801}$](https://www.luogu.com.cn/problem/P2801)
分块板子,虽说我暴力直接艹过去了。
### $\text{\color{red}ABC435ABCD}$
water
### $\text{\color{green}ABC435E}$
区间推平直接上珂朵莉树,然后赛时 set 神秘调用原因调半小时,所幸最后十分钟把 DE 切掉了。
## 12.12
水了一些【数列分块入门】。
### [$\text{\color{cyan}P1903}$](https://www.luogu.com.cn/problem/P1903)
自学了一下带修莫队,就是普通莫队加一维,在同一块内的询问按照时间从小到大完成。
## 12.13
### [$\text{\color{green}P3386/2071/1640/1894/2319}$](https://www.luogu.com.cn/problem/P3386)
二分图最大匹配板子。
### [$\text{\color{cyan}P10937}$](https://www.luogu.com.cn/problem/P10937)
把 $n$ 行看为左部点,把 $m$ 列作为右部点,转化为二分图最大匹配。
### [$\text{\color{cyan}P10939/3355/5030}$](https://www.luogu.com.cn/problem/P10939)
寻找一个染色方法,发现黑白交叉染色后一个位置所能跳到的另一个位置颜色一定不相同,建图判一下奇偶求二分图最大独立集就做完了。P5030 也是差不多的道理。
### [$\text{\color{black}P9139}$](https://www.luogu.com.cn/problem/P9139)
想当初学 2-SAT 的时候找到了这题,发现爆搜能过然后打了一遍死掉了就没管,蝴蝶发了一个帖子让我重新注意到了这题,然后就写过了。
## 12.31
### $\text{\color{red}CF2178A}\text{\color{orange}B}$
简单题。
### $\text{\color{orange}CF2178C}$
只能做 $n-1$ 次操作就很显然了,枚举那个剩下的,这个位置前除了第一位是可以随意选择加减的,这个位置之后就只能全减了,这个不难证明,前缀和随便做一下就完了。
### $\text{\color{green}CF2178D}$
赛时读错题了/fn,没看到 $h_i=a_i$ 然后就罚坐了 30 min。
### $\text{\color{cyan}CF2178E}$
很好玩的交互啊。
注意到一开始的拼接可以看作用一个更大的 $2^k$ 扁平得到,于是我们直接舍弃掉拼接操作。
对于扁平我们一定能找到一个位置使得左右两边和相同。由于扁平操作是对于**当前最大的元素 $x$** 做的,所以做更少的操作最大值一定更大,长度一定更少。然后直接二分搜这个位置就完了,时间复杂度 $\Theta(\log^2 n)$。
中间数据删除了
05.29
ABC003
推式子,类似于:
$$ \frac{1}{2}(\frac{1}{2}(c+r_1)+(c+r_2)...)=\frac{1}{4}(c+r_1)+\frac{1}{2}(c+r_2)$$
大的尽可能往后放。