正文
增加 91 行删除 622 行
4 行未改动5678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697982 行未改动10110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323444 行未改动2792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523536 行未改动3603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485492 行未改动5525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267274 行未改动### [$\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。44 行未改动这个限制已经很开放了,我们可以直接尝试去贪心。对于一个节点 $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 即可。```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,inf=1e18;int t,n,m,s,x,y,u,v,w,arr[maxn],dis[maxn];vector<pair<int,int> >e[maxn];struct node{int id,dis;bool operator<(const node &x)const{return dis>x.dis;}};priority_queue<node>q;void dijkstra(int u){for(int i=1;i<=n;i++){dis[i]=inf;}dis[u]=0;q.push({u,0});while(!q.empty()){auto cur=q.top();q.pop();int x=cur.id;if(cur.dis>dis[x]){continue;}for(auto v:e[x]){if(dis[v.first]>dis[x]+v.second){dis[v.first]=dis[x]+v.second;q.push({v.first,dis[v.first]});}}}return;}signed main(){FastIO;cin>>n>>m;for(int i=1;i<=m;i++){cin>>u>>v>>w;e[u].push_back({v,w*2});e[v].push_back({u,w*2});}for(int i=1;i<=n;i++){cin>>w;e[s].push_back({i,w});}dijkstra(s);for(int i=1;i<=n;i++){cout<<dis[i]<<" ";}return 0;}```咕咕咕。## 11.262 行未改动我们把这两个很丑的东西画出来,发现他们是反函数,然后就做完了。原式可化为 $n^2+\sum^{n}_{x=1}[n \mid x^2]$。Pollard-Rho 艹过去了。```cpp#include<bits/stdc++.h>#define FastIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)#define int __int128_t#define I using#define AK namespace#define CSPS2026 std#define gcd __gcdI AK CSPS2026;const int maxn=1e6+10,maxm=1e3+10,mod=1e9+7;const __int128 mul=1;const int prime[]={0,2,3,5,7,11,13,17,19,23,29,31,37};int t,n,m,x,y,z,u,v,w,p,cur,sum,ans,arr[maxn];mt19937 rnd;__int128 read(){__int128 a=0,b=1;char ch=getchar();while(ch<'0' or ch>'9'){if(ch=='-'){b=-1;}ch=getchar();}while(ch>='0' and ch<='9'){a=a*10+ch-'0';ch=getchar();}return a*b;}void write(__int128 c){if(c<0){putchar('-');c=-c;}if(c>9){write(c/10);}putchar(c%10+'0');}int calc(int n,int m,int mod){return (mul*n*n%(mod)+m)%mod;}int kasumi(int x,int y,int p){int res=x,ans=1;while(y){if(y&1){ans*=res;ans%=p;}res*=res;res%=p;y>>=1;}return ans;}bool miller_rabin(int n){if(n==1){return false;}for(int i=1;i<=12;i++){if(n==prime[i]){return true;}if(n<prime[i]){return false;}int m=n-1,power=kasumi(prime[i],m,n);while(power==1 and !(m&1)){m>>=1,power=kasumi(prime[i],m,n);}if(power!=1 and power!=n-1){return false;}}return true;}int pollard_rho(int n){if(n==4){return 2;}uniform_int_distribution<long long>randnum(3,n-1);int x=randnum(rnd),y=x,z=randnum(rnd);x=calc(x,z,n),y=calc(calc(y,z,n),z,n);for(int i=1;x!=y;i=min((int)(i<<1),(int)(128))){int cnt=1;for(int j=1;j<=i;j++){if(x>=y){cnt=mul*cnt*(x-y)%n;}else{cnt=mul*cnt*(y-x)%n;}if(!cnt){break;}x=calc(x,z,n),y=calc(calc(y,z,n),z,n);}int r=gcd(cnt,n);if(r!=1){return r;}}return n;}int solve(int n){if(miller_rabin(n)){return n;}int res=n;while(res==n){res=pollard_rho(n);}return max(solve(n/res),solve(res));}signed main(){t=read();while(t--){n=read();p=sum=0,cur=1,ans=n*n%mod;while(n!=1){p=solve(n);sum=0;while(!(n%p)){n/=p;sum++;}sum>>=1;cur=cur*kasumi(p,sum,mod)%mod;}write((ans+cur)%mod);cout<<"\n";}return 0;}```### [$\text{\color{cyan}CF1860E}$](https://www.luogu.com.cn/problem/CF1860E)[挺好的一道题。](https://www.luogu.com.cn/article/ccy2hp61)4 行未改动567891011122 行未改动151617181920212244 行未改动67686970717273746 行未改动818283848586878889909192939495962 行未改动991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951964 行未改动### [$\text{\color{yellow}P14361}$](https://www.luogu.com.cn/problem/P14361)[前情提要。](https://www.luogu.com.cn/article/ko8k545y)## 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)$,可以通过。## 11.15早上打了一堆板子。已严肃学习 ST 表。已严肃学习 LCA。44 行未改动这个限制已经很开放了,我们可以直接尝试去贪心。对于一个节点 $u$ 的子树,他们一定都能达到这个节点,直接 dfs 统计,如果节点数超过 $B$ 个就分,并将 $u$ 作为省会,这个东西可以用一个栈来维护。处理完所有子树后,将 $u$ 也入栈,此时栈中的节点将被返回到上一层,经过这种操作后,栈的大小最大为 $B−1$ 个子树节点加 $u$ 本身,即栈中结点不超过 $B$ 个,所以一个子树会对集合最多增加 $B-1$ 个节点,那么每个省的大小最大为 $2B-1$,满足 $\leq 3B$。在 dfs 结束后,栈中可能还会有点,大小同理,也 $\leq B-1$,把他并入根节点所在的省,那么此时根节点这个块的大小最大可能为 $3B−1$,方法成立。### [$\text{\color{cyan}ABC383E}$](https://www.luogu.com.cn/problem/AT_abc383_e)作为重构树大蛇我必须要感谢这道让我一眼秒掉的题!!!6 行未改动这个时候我们发现重构树上的边点 $1w,2w,5w$ 由深到浅依次增大,进一步推广至所有的边点都是如此,这个不难证明。那么思路就出来了,尽量把 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.262 行未改动我们把这两个很丑的东西画出来,发现他们是反函数,然后就做完了。原式可化为 $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.29ABC003推式子,类似于:$$ \frac{1}{2}(\frac{1}{2}(c+r_1)+(c+r_2)...)=\frac{1}{4}(c+r_1)+\frac{1}{2}(c+r_2)$$大的尽可能往后放。