Query on a tree 树链剖分 [SPOJ-QTREE]
题目:https://www.spoj.com/problems/QTREE/en/
树链剖分模板题
注意一点,我们用dfs序对节点进行编号,其实也对应线段树中该节点与其父节点连边的编号
因此统计到最后时(两节点在同一条重链上)不能用在上面的节点的的编号,而是其重儿子的编号
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 2e5;
struct edge {
int u,v,cost;
};
int fa[N],lv[N],siz[N],son[N],top[N],dfn[N],cnt=0;
vector<pii> G[N];
int n,seg[N<<2];
edge e[N];
int query(int s,int t,int l,int r,int p) {
if(s<=l&&r<=t) return seg[p];
int m=(l+r)/2;
if(t<=m) return query(s,t,l,m,p<<1);
if(m+1<=s) return query(s,t,m+1,r,p<<1|1);
return max(query(s,t,l,m,p<<1),query(s,t,m+1,r,p<<1|1));
}
void update(int pos,int val,int l,int r,int p){
if(l==r) {
seg[p]=val;
return;
}
int m=(l+r)/2;
if(pos<=m) update(pos,val,l,m,p<<1);
else update(pos,val,m+1,r,p<<1|1);
seg[p]=max(seg[p<<1],seg[p<<1|1]);
}
int dfs1(int x,int fx) {
son[x]=-1;
siz[x]=1;
fa[x]=fx;
lv[x]=lv[fx]+1;
for(auto p:G[x]) {
int to = p.first;
if(to==fx) continue;
siz[x] += dfs1(to,x);
if(son[x] == -1 || siz[to] > siz[son[x]]) son[x] = to;
}
return siz[x];
}
void dfs2(int x,int root) {
top[x]=root;
dfn[x]=++cnt;
if(son[x]==-1) return;
dfs2(son[x],root);
for(auto p:G[x]) {
int to = p.first;
if(to == son[x] || to == fa[x]) continue;
dfs2(to,to);
}
}
int ask(int x,int y) {
int ans = 0;
while(top[x] != top[y]) {
if(lv[top[x]]<lv[top[y]]) swap(x,y);
ans=max(ans,query(dfn[top[x]],dfn[x],1,n,1));
x=fa[top[x]];
}
if(dfn[x] != dfn[y]) {
if(dfn[x]>dfn[y]) swap(x,y);
ans=max(ans,query(dfn[son[x]],dfn[y],1,n,1));
}
return ans;
}
int a,b,c;
string o;
void solve() {
memset(seg,0,sizeof(seg));
cnt = 0;
// cin>>n;
scanf("%d",&n);
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<n;i++){
scanf("%d %d %d",&a,&b,&c);
G[a].push_back({b,c});
G[b].push_back({a,c});
e[i]={a,b,c};
}
dfs1(1,-1);
dfs2(1,1);
for(int i=1;i<n;i++){
if(lv[e[i].u] > lv[e[i].v]) swap(e[i].u, e[i].v);
update(dfn[e[i].v], e[i].cost, 1,n,1);
}
char str[10];
while(true) {
scanf(" %s",str);
if(str[0] == 'Q') {
// cin>>a>>b;
scanf("%d %d",&a,&b);
// cout<<ask(a,b)<<'\n';
printf("%d\n",ask(a,b));
} else if(str[0] == 'C') {
// cin>>a>>b;
scanf("%d %d",&a,&b);
update(dfn[e[a].v], b, 1,n,1);
} else break;
}
}
int main(){
ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
while(t--) solve();
return 0;
}