版本比较
题解:P6666 [清华集训 2016] 数据交互
正文
增加 151 行删除 1 行
12334567180 行未改动全部展开188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341# 震撼首发
无卡常拿到**目前**最有解,且总时间**稳定**比第二名快 1s 多。无卡常拿到**目前**最优解,且总时间**稳定**比第二名快 1s 多。
目前在网上还没有这种优化方式(我自己没有找到),但其实非常简单,题解目前仅此一篇,快来学吧。(本人认为讲解的很详尽,请认真看到最后,最后还有对时间复杂度的解析)
$\textcolor{blue}{有参考其他题解的思路}$。180 行未改动全部展开
不过可惜还是不能在时间复杂度上更近一步,还是有点遗憾的吧,所以我也希望更多 oier 可以让思路不局限在题解中,哪怕是这种偏冷门的算法,也能在优化中找寻乐趣,祝大家做题愉快。
点个赞呗!
```cpp#include <bits/stdc++.h>using namespace std;const int N=100010;vector<int> ed[N];int n,m,siz[N],son[N],lc[N],rc[N],fa[N],fat[18][N],pre[N],b[N],Q,RT,dep[N];long long tag[N];struct Heap{ priority_queue<long long> q1,q2; inline void insert(long long x){q1.push(x);} inline void erase(long long x){q2.push(x);} inline void pop(){while(q2.size()&&q1.top()==q2.top()) q1.pop(),q2.pop();} inline long long fir(){ pop(); return q1.top(); } inline long long sec(){ pop(); long long x=q1.top();q1.pop(); pop(); long long ret=q1.size()?q1.top():0; q1.push(x); return ret; } }s[N],g;struct Node{ int x,y,w;}ad[N];struct mat{ long long a,b,c,d; mat(){a=b=c=d=0;} mat(long long A,long long B,long long C,long long D){a=A,b=B,c=C,d=D;} inline mat operator * (const mat &B)const{ mat res; res.a=a+B.a; res.b=max(a+B.b,b); res.c=max(c+B.a,B.c); res.d=max({c+B.b,B.d,d}); return res; } inline mat operator + (const long long &A)const{ mat res={a,b,c+A,d+A}; return res; }}val[N],tr[N];void dfs(int x,int fa){ siz[x]=1;fat[0][x]=fa;dep[x]=dep[fa]+1; for(int i=1;i<=16;i++) fat[i][x]=fat[i-1][fat[i-1][x]]; for(int v:ed[x]){ if(v==fa) continue; dfs(v,x); siz[x]+=siz[v]; if(siz[v]>siz[son[x]]) son[x]=v; }}int cbuild(int cl,int cr){ int l=cl,r=cr; while(l+1!=r){ int mid=(l+r)>>1; if(((pre[mid]-pre[cl])<<1)<=pre[cr]-pre[cl]) l=mid; else r=mid; } int rt=b[l]; if(l>cl) lc[rt]=cbuild(cl,l),fa[lc[rt]]=rt; if(l+1<cr) rc[rt]=cbuild(l+1,cr),fa[rc[rt]]=rt; return rt;}int build(int x){ int y=x; do{ for(int v:ed[y]){ if(v==son[y]||v==fat[0][y]) continue; s[y].insert(0); fa[build(v)]=y; } y=son[y]; }while(y); do{ b[y++]=x;pre[y]=pre[y-1]+siz[x]-siz[son[x]]; x=son[x]; }while(x); g.insert(0); return cbuild(0,y);}inline int LCA(int a,int b){ if(dep[a]>dep[b]) swap(a,b); int c=dep[b]-dep[a]; for(int i=16;i>=0;i--) if((1<<i)<=c) c-=(1<<i),b=fat[i][b]; if(a==b) return a; for(int i=16;i>=0;i--) if(fat[i][a]!=fat[i][b]) a=fat[i][a],b=fat[i][b]; return fat[0][a];}inline void solve(int id,int x,int w){ if(!x) return; bool ok=1; if(id==2) val[x].a+=w,val[x].b+=w; while(x){ if(id==1&&ok){ val[x]=val[x]+w; if(lc[x])val[lc[x]]=val[lc[x]]+w,tr[lc[x]]=tr[lc[x]]+w,tag[lc[x]]+=w; ok=0; } mat pre=tr[x]; tr[x]=val[x]; if(lc[x]) tr[x]=(tr[lc[x]]+tag[x])*val[x]; if(rc[x]) tr[x]=tr[x]*(tr[rc[x]]+tag[x]); mat now=tr[x]; if((lc[fa[x]]!=x&&rc[fa[x]]!=x)||!fa[x]) g.insert(now.d),g.erase(pre.d); if(!fa[x]) break; if(lc[fa[x]]!=x&&rc[fa[x]]!=x){ long long pmx=s[fa[x]].fir(),pcmx=s[fa[x]].sec(); s[fa[x]].erase(pre.b); s[fa[x]].insert(now.b); long long mx=s[fa[x]].fir(),cmx=s[fa[x]].sec(); val[fa[x]].b+=mx-pmx;val[fa[x]].c+=mx-pmx; val[fa[x]].d+=1ll*mx+cmx-pmx-pcmx; } if(lc[fa[x]]!=x) ok=1; x=fa[x]; }}inline void change(int id){ int x=ad[id].x,y=ad[id].y,w=ad[id].w; int lca=LCA(x,y); solve(2,lca,w); if(w>=0) solve(1,x,w),solve(1,y,w),solve(1,lca,-w),solve(1,fat[0][lca],-w); else solve(1,lca,-w),solve(1,fat[0][lca],-w),solve(1,x,w),solve(1,y,w);}int main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n>>m; for(int i=1;i<n;i++){ int x,y;cin>>x>>y; ed[x].emplace_back(y); ed[y].emplace_back(x); } dfs(1,0); RT=build(1); for(int i=1;i<=m;i++){ char s[3];cin>>s; if(s[0]=='+') cin>>ad[i].x>>ad[i].y>>ad[i].w,change(i); else cin>>ad[i].x,ad[ad[i].x].w*=-1,change(ad[i].x); cout<<g.fir()<<'\n'; } return 0;}```