Still ablaze
常熟太大被【数据删除】卡常了喵,实现千万要精细喵。
这题的思路是简单的(至少我的写法是这样)。
考虑每一个点的贡献,显然 \(u \to v\) 的距离是肯定有的,考虑加点的影响。显然的,如果那个点是路径上的点的子树内的点,贡献就是两倍到链上的距离,否则肯定在根节点到 \(lca(u,v)\) 的路径上,贡献那就是两倍 \(dep_{lca(u,v)} - dep_{fa} + dep_u\)。
肯定是考虑树剖维护这个东西啊!具体的,直接就是大力轻重链剖分,对于 \(lca\) 是要用到第三小值的,直接维护用。然后就可以了。
时间复杂度 \(O(n \log n + q \log n)\)。
Code
有什么话跟我的 \(7.8k\) 代码说去吧。这份代码卡常不完全,但是已经可以通过了。
什么,你说有更好的实现?听不懂,和我的大分代码说去吧。
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0),cout.tie(0)
#define File(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#define LL long long
#define fi first
#define se second
#define pii pair<LL,int>
const int N = 5e5 + 10;
const LL INF = 1e15;
int n,Q;
vector< pair<int,LL> > G[N];
LL dis[N];
int fa[N],top[N],dfn[N],idfn[N],son[N],tot,siz[N],dep[N];
inline LL max(const LL &x,const LL &y){
if(x < y) return y;
return x;
}
inline LL getmax(const LL &a,const LL &b){
if(a > b) return a;
return b;
}
struct node{
pii mx,cmx;
node(LL val = -INF,int id = -1){
mx = {val,id};
cmx = {-INF,-1};
}
}ans[N],ans2[N],tmpans[N],tmpans2[N],mxans[N],mxans2[N];
LL ans3[N];
node merge(const node &u,const node &v){
node res = u;
if(res.mx.fi < v.mx.fi){
res.cmx = res.mx;
res.mx = v.mx;
}
else if(res.mx.fi == v.mx.fi)
res.cmx = v.mx;
else if(res.cmx.fi < v.mx.fi)
res.cmx = v.mx;
if(res.cmx.fi < v.cmx.fi)
res.cmx = v.cmx;
return res;
}
node del(const node &u,int id){
node res = u;
if(res.mx.se != id && res.cmx.se != id) return u;
if(res.mx.se == id)
res.mx = res.cmx;
res.cmx = {-INF,-1};
return res;
}
void dfs(int u,int rt){
fa[u] = rt;
siz[u] = 1;
dep[u] = dep[rt] + 1;
for(int i=0;i<G[u].size();i++){
int v = G[u][i].fi;
int w = G[u][i].se;
if(v == rt) continue;
if(v > n) exit(1);
dis[v] = dis[u] + w;
dfs(v,u);
if(siz[v] > siz[son[u]]) son[u] = v;
siz[u] += siz[v];
}
for(int i=0;i<G[u].size();i++){
int v = G[u][i].fi;
int w = G[u][i].se;
if(v == rt) continue;
if(v != son[u])
ans[u] = merge(ans[u],node(mxans[v].mx.fi + w,v));
mxans[u] = merge(mxans[u],node(mxans[v].mx.fi + w,v));
}
for(int i=0;i<G[u].size();i++){
int v = G[u][i].fi;
int w = G[u][i].se;
if(v == rt) continue;
if(v == mxans[u].mx.se || v == mxans[u].cmx.se) continue;
ans3[u] = max(ans3[u],mxans[v].mx.fi + w);
}
ans[u] = merge(ans[u],node(0,u));
mxans[u] = merge(mxans[u],node(0,u));
ans2[u] = ans[u];
mxans2[u] = mxans[u];
if(ans2[u].mx.se != -1)
ans2[u].mx.fi -= dis[u];
if(ans2[u].cmx.se != -1)
ans2[u].cmx.fi -= dis[u];
if(mxans2[u].mx.se != -1)
mxans2[u].mx.fi -= dis[u];
if(mxans2[u].cmx.se != -1)
mxans2[u].cmx.fi -= dis[u];
return ;
}
void dfs2(int u,int rt,int tp){
dfn[u] = ++tot;
idfn[tot] = u;
top[u] = tp;
if(son[u])
dfs2(son[u],u,tp);
for(int i=0;i<G[u].size();i++){
int v = G[u][i].fi;
int w = G[u][i].se;
if(v == rt || v == son[u]) continue;
dfs2(v,u,v);
}
return ;
}
int LCA(int x,int y){
while(top[x] != top[y]){
if(dep[top[x]] > dep[top[y]]) swap(x,y);
y = top[y];
y = fa[y];
}
return ((dep[x] < dep[y]) ? x : y);
}
struct segment_tree{
LL tre[N << 2];
int flag;
void pushup(int p){
tre[p] = max(tre[p*2],tre[p*2+1]);
}
void build(int pl,int pr,int p){
if(pl == pr){
if(flag == 0) tre[p] = ans[idfn[pl]].mx.fi;
else tre[p] = ans2[idfn[pl]].mx.fi;
return ;
}
int mid = pl + pr >> 1;
build(pl,mid,p*2);
build(mid+1,pr,p*2+1);
pushup(p);
}
void update(int x,int pl,int pr,int p,LL val){
if(x < 1) return ;
if(pl == pr){
tre[p] = val;
return ;
}
int mid = pl + pr >> 1;
if(x <= mid) update(x,pl,mid,p*2,val);
else update(x,mid+1,pr,p*2+1,val);
pushup(p);
return ;
}
LL query(int L,int R,int pl,int pr,int p){
if(L > R || pl > pr || pl > R || pr < L) return -INF;
if(L <= pl && pr <= R)
return tre[p];
int mid = pl + pr >> 1;
return max(query(L,R,pl,mid,p*2),query(L,R,mid+1,pr,p*2+1));
}
}sgt1,sgt2;
int change1(int x,int lca){
int nw = x,prenw = x;
int tp = top[x];
// if(son[x]){
// ans[x] = merge(ans[x],mxans[x]);
// sgt1.update(dfn[x],1,n,1,ans[x].mx.fi);
// }
while(dep[tp] > dep[lca]){
prenw = nw;
// ans[fa[tp]] = del(mxans[fa[tp]],tp);
// cout << fa[tp] << "\n";
// cout << mxans[fa[tp]].cmx.fi << " " << mxans[fa[tp]].cmx.se << "\n";
// sgt1.update(dfn[fa[tp]],1,n,1,ans[fa[tp]].mx.fi);
nw = tp;
nw = fa[nw];
tp = top[nw];
}
if(nw == lca) return top[prenw];
return son[lca];
}
void rec1(int x,int lca){
return ;
int nw = x;
int tp = top[x];
while(dep[tp] > dep[lca]){
// ans[fa[tp]] = tmpans[fa[tp]];
// sgt1.update(dfn[fa[tp]],1,n,1,ans[fa[tp]].mx.fi);
nw = tp;
nw = fa[nw];
tp = top[nw];
}
return ;
}
LL query1(int x,int lca){
int nw = x;
int tp = top[x];
LL res = 0;
int prenw = nw;
while(dep[tp] > dep[lca]){
// cout << nw << " " << res << "\n";
res = max(res,sgt1.query(dfn[tp],dfn[fa[nw]],1,n,1));
if(nw == x && son[x])
res = max(res,mxans[x].mx.fi);
else if(nw != x)
res = max(res,del(mxans[nw],prenw).mx.fi);
nw = tp;
prenw = nw;
nw = fa[nw];
tp = top[nw];
}
if(nw == x && son[x] && nw != lca)
res = max(res,mxans[x].mx.fi);
else if(nw != x && nw != lca){
res = max(res,del(mxans[nw],prenw).mx.fi);
}
if(dep[nw] >= dep[lca]){
res = max(res,sgt1.query(dfn[lca],dfn[fa[nw]],1,n,1));
}
return res;
}
void change2(int x,int lca){
int nw = x;
int tp = top[x];
while(dep[tp] > dep[lca]){
ans2[fa[tp]] = del(mxans2[fa[tp]],tp);
sgt2.update(dfn[fa[tp]],1,n,1,ans2[fa[tp]].mx.fi);
nw = tp;
nw = fa[nw];
tp = top[nw];
}
}
void rec2(int x,int lca){
int nw = x;
int tp = top[x];
if(son[x]){
ans2[x] = tmpans2[x];
sgt2.update(dfn[x],1,n,1,ans2[x].mx.fi);
}
while(dep[tp] > dep[lca]){
ans2[fa[tp]] = tmpans2[fa[tp]];
sgt2.update(dfn[fa[tp]],1,n,1,ans2[fa[tp]].mx.fi);
nw = tp;
nw = fa[nw];
tp = top[nw];
}
return ;
}
LL query2(int x,int lca){
// cout << x << " " << lca << "\n";
int nw = x;
int tp = top[x];
LL res = 0;
while(dep[tp] >= dep[lca]){
// cout << dfn[tp] << " " << dfn[nw] << " " << idfn[dfn[tp]] << " " << idfn[dfn[nw]] << "\n";
res = max(res,sgt2.query(dfn[tp],dfn[nw],1,n,1));
nw = tp;
nw = fa[nw];
tp = top[nw];
}
if(dep[nw] >= dep[lca])
res = max(res,sgt2.query(dfn[lca],dfn[nw],1,n,1));
return res;
}
LL query(int x,int y){
// x -> lca && y -> lca
int lca = LCA(x,y);
if(dep[x] < dep[y]) swap(x,y);
LL mx = 0;
if(lca == y){
int tp1;
tp1 = change1(x,lca);
// cout << tp1 << "\n";
sgt1.update(dfn[lca],1,n,1,-INF);
mx = max(mx,query1(x,lca));
mx = max(mx,del(mxans[lca],tp1).mx.fi);
rec1(x,lca);
sgt1.update(dfn[lca],1,n,1,tmpans[lca].mx.fi);
}
else{
int tp1,tp2;
tp1 = change1(x,lca);
tp2 = change1(y,lca);
sgt1.update(dfn[lca],1,n,1,-INF);
// cout << mx << "\n";
mx = max(mx,query1(x,lca));
// cout << mx << "\n";
mx = max(mx,query1(y,lca));
// cout << mx << " " << tp1 << " " << tp2 <<" " << "\n";
if(mxans[lca].mx.se != -1){
if(top[mxans[lca].mx.se] != top[tp1] && top[mxans[lca].mx.se] != top[tp2])
mx = max(mx,mxans[lca].mx.fi);
}
if(mxans[lca].cmx.se != -1){
if(top[mxans[lca].cmx.se] != top[tp1] && top[mxans[lca].cmx.se] != top[tp2])
mx = max(mx,mxans[lca].cmx.fi);
}
mx = max(mx,ans3[lca]);
sgt1.update(dfn[lca],1,n,1,tmpans[lca].mx.fi);
rec1(x,lca);
rec1(y,lca);
}
// cout << x << " " << y << " " << lca << " " << mx << "\n";
if(fa[lca]){
if(son[fa[lca]] != lca){
ans2[fa[lca]] = del(mxans2[fa[lca]],lca);
sgt2.update(dfn[fa[lca]],1,n,1,ans2[fa[lca]].mx.fi);
}
change2(fa[lca],1);
mx = max(mx,dis[lca] + query2(fa[lca],1));
// cout << dis[lca] << "\n";
rec2(fa[lca],1);
}
// cout << mx << " " << dis[x] + dis[y] - 2 * dis[lca] << "\n";
return mx * 2 + dis[x] + dis[y] - 2 * dis[lca];
}
int main(){
IOS;
cin >> n >> Q;
for(int i=1;i<n;i++){
int u,v,w;
cin >> u >> v >> w;
G[u].push_back({v,w});
G[v].push_back({u,w});
}
dfs(1,0);
dfs2(1,0,1);
sgt1.flag = 0;
sgt2.flag = 1;
sgt1.build(1,n,1);sgt2.build(1,n,1);
for(int i=1;i<=n;i++)
tmpans[i] = ans[i],tmpans2[i] = ans2[i];
while(Q -- ){
int x,y;
cin >> x >> y;
LL res = query(x,y);
cout << res << "\n";
}
return 0;
}

浙公网安备 33010602011771号