学习树链剖分的第一题,第二个dfs忘记递归了(太蠢),re了两发,改过来以后就1A了。
学习树链剖分可以参考这篇博客:http://blog.sina.com.cn/s/blog_7a1746820100wp67.html 个人感觉写得非常好!!!
树链剖分听起来很吊,其实就是把数边扔到线段树上,然后搞出一点事情来。主要就是一个重儿子,重链,从而使复杂度降到了(logn)。这个非常关键。关于初始化建树:先剖分,求出size,son,w,top,fa,dep数组,就能把树链扔到线段树树上实现了,这个过程也就是两个dfs实现。然后就是简单的区间修改的问题了。(再次感觉那篇博客写得太好了)
树的边权的问题可以转化为树链剖分的问题!!!
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <cmath> #include <map> #define LL long long #define FOR(i,x,y) for(int i = x;i < y;i ++) #define IFOR(i,x,y) for(int i = x;i > y;i --) #define lrt rt<<1 #define rrt rt<<1|1 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define MAXN 11000 using namespace std; int n,val[MAXN]; int head[MAXN<<1],edge_cnt; map <int,int> mat; struct Edge{ int u,v,idd,value; int nt; }edge[MAXN<<1]; void add_edge(int u,int v,int idd){ edge[edge_cnt].u = u; edge[edge_cnt].v = v; edge[edge_cnt].idd = idd; edge[edge_cnt].nt = head[u]; head[u] = edge_cnt++; } int sz[MAXN],son[MAXN],fa[MAXN],dep[MAXN],top[MAXN],id[MAXN],w[MAXN],val0[MAXN],totw; void dfs1(int u,int father,int depth){ sz[u] = 1; dep[u] = depth; fa[u] = father; int idd = -1,maxx = -1; for(int i = head[u];i != -1;i = edge[i].nt){ int v = edge[i].v; if(v == father) continue; id[v] = edge[i].idd; dfs1(v,u,depth+1); sz[u] += sz[v]; if(sz[v] > maxx) {idd = v;maxx = sz[v];} } son[u] = idd; } void dfs2(int u,int father){ if(son[u] == -1) return; top[son[u]] = top[u]; w[son[u]] = ++totw; val[totw] = val0[id[son[u]]]; mat[id[son[u]]] = totw; dfs2(son[u],u); for(int i = head[u];i != -1;i = edge[i].nt){ int v = edge[i].v; if(v == father) continue; if(v == son[u]) continue; top[v] = v; w[v] = ++totw; val[totw] = val0[id[v]]; mat[id[v]] = totw; dfs2(v,u); } } struct Tree{ int l,r; int maxx; }tree[MAXN<<2]; void PushUp(int rt){ tree[rt].maxx = max(tree[lrt].maxx,tree[rrt].maxx); } void Build(int rt,int l,int r){ tree[rt].l = l; tree[rt].r = r; if(l == r){ tree[rt].maxx = val[l]; return; } int mid = (l+r)>>1; Build(lson); Build(rson); PushUp(rt); } void Modify(int rt,int x,int value){ if(tree[rt].l == tree[rt].r){ tree[rt].maxx = value; return; } int mid = (tree[rt].l + tree[rt].r)>>1; if(x <= mid) Modify(lrt,x,value); else Modify(rrt,x,value); PushUp(rt); } int Query(int rt,int l,int r){ if(tree[rt].l == l && tree[rt].r == r){ return tree[rt].maxx; } int mid = (tree[rt].l + tree[rt].r)>>1; if(r <= mid) return Query(lrt,l,r); else if(l > mid) return Query(rrt,l,r); else return max(Query(lson),Query(rson)); } int solve(int l,int r){ int ans = -1; while(top[l] != top[r]){ if(dep[top[l]] >= dep[top[r]]){ ans = max(ans,Query(1,w[top[l]],w[l])); l = fa[top[l]]; } else{ ans = max(ans,Query(1,w[top[r]],w[r])); r = fa[top[r]]; } } if(l == r) return ans; if(dep[l] >= dep[r]){return max(ans,Query(1,w[r]+1,w[l]));} else return max(ans,Query(1,w[l]+1,w[r])); } int main(){ //freopen("test.in","r",stdin); int T; scanf("%d",&T); while(T--){ scanf("%d",&n); int u,v,value; memset(head,-1,sizeof(head)); edge_cnt = 0; FOR(i,1,n){ scanf("%d%d%d",&u,&v,&value); add_edge(u,v,i); add_edge(v,u,i); val0[i] = value; } dfs1(1,-1,1); totw = 0; top[1] = 1; dfs2(1,-1); Build(1,1,totw); char op[10]; while(~scanf("%s",op) && strcmp(op,"DONE")){ if(strcmp(op,"QUERY") == 0){ int l,r; scanf("%d%d",&l,&r); printf("%d ",solve(l,r)); } else{ int x,value; scanf("%d%d",&x,&value); Modify(1,mat[x],value); } } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。