【树链剖分】【边权】

QTREE - Query on a tree

no tags
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3…N-1.

We will ask you to perfrom some instructions of the following form:

CHANGE i ti : change the cost of the i-th edge to ti
or
QUERY a b : ask for the maximum edge cost on the path from node a to node b
Input

The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.

For each test case:

In the first line there is an integer N (N <= 10000),
In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 1000000),
The next lines contain instructions “CHANGE i ti” or “QUERY a b”,
The end of each test case is signified by the string “DONE”.
There is one blank line between successive tests.

Output

For each “QUERY” operation, write one integer representing its result.

Example

Input:
1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Output:
1
3
Submit solution!

这道题只是把点权改成了边权,在做的时候只是维护一下这个点的上一条边和重链顶端的下一条边就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=10100;
int n,point[N],next[N*10],tot=1,siz[N]={0},fa[N][20]={0},son[N]={0};
int deep[N]={0},num=0,pos[N]={0},belong[N]={0},r[N]={0},map[N]={0},tr[N*4];
struct S{
    int st,en,va;
}aa[N*10];
bool use[N];
inline void add(int x,int y,int z)
{
    tot+=1;next[tot]=point[x];point[x]=tot;
    aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;
    tot+=1;next[tot]=point[y];point[y]=tot;
    aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;
}
inline void dfs_1(int x)
{
    int i;
    siz[x]=1;
    use[x]=false;
    for(i=1;i<=14;++i){
        if(deep[x]<(1<<i)) break;
        fa[x][i]=fa[fa[x][i-1]][i-1];
    }
    for(i=point[x];i;i=next[i])
      if(use[aa[i].en]){
        fa[aa[i].en][0]=x;
        deep[aa[i].en]=deep[x]+1;
        dfs_1(aa[i].en);
        siz[x]+=siz[aa[i].en];
      }
}
inline void dfs_2(int x,int y)
{
    int i,k=0;
    num+=1;
    belong[x]=y;
    for(i=point[x];i;i=next[i]){
        if(deep[aa[i].st]>deep[aa[i].en]) map[aa[i].st]=i>>1;
        else map[aa[i].en]=i>>1;
    }
    for(i=point[x];i;i=next[i])
      if(deep[x]<deep[aa[i].en]&&siz[k]<siz[aa[i].en])
        k=aa[i].en,son[x]=i>>1,pos[i>>1]=num;
    if(k==0) {num-=1; return ;}
    dfs_2(k,y);
    for(i=point[x];i;i=next[i])
      if(deep[x]<deep[aa[i].en]&&k!=aa[i].en)
        num+=1,pos[i>>1]=num,dfs_2(aa[i].en,aa[i].en);
}
inline int LCA(int x,int y)
{
    int i;
    if(deep[x]<deep[y])swap(x,y);
    int t=deep[x]-deep[y];
    for(i=0;i<=14;++i)
        if(t&(1<<i))x=fa[x][i];
    for(i=14;i>=0;--i)
        if(fa[x][i]!=fa[y][i]) 
        {x=fa[x][i];y=fa[y][i];}
    if(x==y)return x;
    else return fa[x][0];
}
#define mid (l+r)/2
#define L k<<1,l,mid
#define R k<<1|1,mid+1,r
inline void insert(int k,int l,int r,int x,int y)
{
    if(l==r&&l==x){
        tr[k]=y;
        return ;
    }
    if(x<=mid) insert(L,x,y);
    else insert(R,x,y);
    tr[k]=max(tr[k<<1],tr[k<<1|1]);
}
inline int qurry(int k,int l,int r,int x,int y)
{
    int maxn=-210000000;
    if(x<=l&&y>=r) return tr[k];
    if(x<=mid) maxn=max(maxn,qurry(L,x,y));
    if(y>mid) maxn=max(maxn,qurry(R,x,y));
    return maxn;
}
inline int ask(int x,int y)
{
    int maxn=-210000000,z;
    if(map[x]==0) return maxn;
    if(belong[x]==x&&x!=y){
        maxn=max(maxn,r[map[x]]);
        x=fa[x][0];
    }
    if(map[x]==0) return maxn;
    while(belong[x]!=belong[y]){
        maxn=max(maxn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]]));
        z=fa[belong[x]][0];
        maxn=max(maxn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]));
        x=z;
    }
    if(map[x]==0) return maxn;
    maxn=max(maxn,qurry(1,1,n-1,pos[son[y]],pos[map[x]]));
    return maxn;
}
int main()
{
    int i,j,x,y,z;
    char ch[10];
    memset(use,1,sizeof(use));
    scanf("%d",&n);
    for(i=1;i<n;++i){
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);r[i]=z;
    }
    dfs_1(1);
    dfs_2(1,1);
    for(i=1;i<n;++i) insert(1,1,n-1,pos[i],r[i]);
    while(scanf("%*c%s",&ch)){
        if(ch[0]=='D') break;
        scanf("%d%d",&x,&y);
        if(ch[0]=='C') insert(1,1,n-1,pos[x],y),r[x]=y;
        else{
            int lca=LCA(x,y);
            printf("%d\n",max(ask(x,lca),ask(y,lca)));
        }
    }
}

Tree

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 5672 Accepted: 1560
Description

You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N − 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:

CHANGE i v Change the weight of the ith edge to v
NEGATE a b Negate the weight of every edge on the path from a to b
QUERY a b Find the maximum weight of edges on the path from a to b
Input

The input contains multiple test cases. The first line of input contains an integer t (t ≤ 20), the number of test cases. Then follow the test cases.

Each test case is preceded by an empty line. The first nonempty line of its contains N (N ≤ 10,000). The next N − 1 lines each contains three integers a, b and c, describing an edge connecting nodes a and b with weight c. The edges are numbered in the order they appear in the input. Below them are the instructions, each sticking to the specification above. A lines with the word “DONE” ends the test case.

Output

For each “QUERY” instruction, output the result on a separate line.

Sample Input

1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE
Sample Output

1
3

思路:这个题只是加个标记就好了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10100;
int n,point[N],next[N*10],tot=1,siz[N]={0},fa[N][20]={0},son[N]={0};
int deep[N]={0},num=0,pos[N]={0},belong[N]={0},r[N]={0},map[N]={0},de[N*4]={0};
struct S{
    int st,en,va;
}aa[N*10];
struct C{
    int maxn,minn,lazy;
}tr[N*4];
bool use[N];
inline void add(int x,int y,int z)
{
    tot+=1;next[tot]=point[x];point[x]=tot;
    aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;
    tot+=1;next[tot]=point[y];point[y]=tot;
    aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;
}
inline void dfs_1(int x)
{
    int i;
    siz[x]=1;
    use[x]=false;
    for(i=1;i<=14;++i){
        if(deep[x]<(1<<i)) break;
        fa[x][i]=fa[fa[x][i-1]][i-1];
    }
    for(i=point[x];i;i=next[i])
      if(use[aa[i].en]){
        fa[aa[i].en][0]=x;
        deep[aa[i].en]=deep[x]+1;
        dfs_1(aa[i].en);
        siz[x]+=siz[aa[i].en];
      }
}
inline void dfs_2(int x,int y)
{
    int i,k=0;
    num+=1;
    belong[x]=y;
    for(i=point[x];i;i=next[i]){
        if(deep[aa[i].st]>deep[aa[i].en]) map[aa[i].st]=i>>1;
        else map[aa[i].en]=i>>1;
    }
    for(i=point[x];i;i=next[i])
      if(deep[x]<deep[aa[i].en]&&siz[k]<siz[aa[i].en])
        k=aa[i].en,son[x]=i>>1,pos[i>>1]=num;
    if(k==0) {num-=1; return ;}
    dfs_2(k,y);
    for(i=point[x];i;i=next[i])
      if(deep[x]<deep[aa[i].en]&&k!=aa[i].en)
        num+=1,pos[i>>1]=num,dfs_2(aa[i].en,aa[i].en);
}
inline int LCA(int x,int y)
{
    int i;
    if(deep[x]<deep[y])swap(x,y);
    int t=deep[x]-deep[y];
    for(i=0;i<=14;++i)
        if(t&(1<<i))x=fa[x][i];
    for(i=14;i>=0;--i)
        if(fa[x][i]!=fa[y][i]) 
        {x=fa[x][i];y=fa[y][i];}
    if(x==y)return x;
    else return fa[x][0];
}
#define mid (l+r)/2
#define L k<<1,l,mid
#define R k<<1|1,mid+1,r
inline void solve(int k)
{
    swap(tr[k].maxn,tr[k].minn);
    tr[k].maxn=-tr[k].maxn;
    tr[k].minn=-tr[k].minn;
}
inline void pushdown(int k)
{
    if(de[k]!=0){
        if((k<<1)<N*4){
            solve(k<<1); de[k<<1]=!de[k<<1];
            solve(k<<1|1); de[k<<1|1]=!de[k<<1|1];
        }
        de[k]=0;
    }
}
inline void insert(int k,int l,int r,int x,int y)
{   
    pushdown(k);
    if(l==r&&l==x){
        tr[k].maxn=tr[k].minn=y;
        return ;
    }
    if(x<=mid) insert(L,x,y);
    else insert(R,x,y);
    tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
    tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
}
inline int qurry(int k,int l,int r,int x,int y)
{
    int maxn=-210000000;
    pushdown(k);
    if(x<=l&&y>=r) return tr[k].maxn;
    if(x<=mid) maxn=max(maxn,qurry(L,x,y));
    if(y>mid) maxn=max(maxn,qurry(R,x,y));
    return maxn;
}
inline int ask(int x,int y)
{
    int maxn=-210000000,z;
    if(map[x]==0) return maxn;
    if(belong[x]==x&&x!=y){
        maxn=max(maxn,qurry(1,1,n-1,pos[map[x]],pos[map[x]]));
        x=fa[x][0];
    }
    if(map[x]==0) return maxn;
    while(belong[x]!=belong[y]){
        maxn=max(maxn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]]));
        z=fa[belong[x]][0];
        maxn=max(maxn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]));
        x=z;
    }
    if(map[x]==0) return maxn;
    maxn=max(maxn,qurry(1,1,n-1,pos[son[y]],pos[map[x]]));
    return maxn;
}
inline void change(int k,int l,int r,int x,int y)
{
    pushdown(k);
    if(x<=l&&y>=r){
        solve(k); de[k]=1;
        return ;
    }
    if(x<=mid) change(L,x,y);
    if(y>mid) change(R,x,y);
    tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
    tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
}
inline void negate_(int x,int y)
{
    int z;
    if(map[x]==0) return ;
    if(belong[x]==x&&x!=y){
        change(1,1,n-1,pos[map[x]],pos[map[x]]);
        x=fa[x][0];
    }
    if(map[x]==0) return ;
    while(belong[x]!=belong[y]){
        change(1,1,n-1,pos[son[belong[x]]],pos[map[x]]);
        z=fa[belong[x]][0];
        change(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]);
        x=z;
    }
    if(map[x]==0) return ;
    change(1,1,n-1,pos[son[y]],pos[map[x]]);
    return ;
}
int main()
{
    int i,j,x,y,z;
    char ch[10];
    memset(use,1,sizeof(use));
    scanf("%d",&n);
    for(i=1;i<n;++i){
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);r[i]=z;
    }
    dfs_1(1);
    dfs_2(1,1);
    for(i=1;i<n;++i) insert(1,1,n-1,pos[i],r[i]);
    while(scanf("%*c%s",&ch)){
        if(ch[0]=='D') break;
        scanf("%d%d",&x,&y);
        if(ch[0]=='C') insert(1,1,n-1,pos[x],y);
        if(ch[0]=='Q'){
            int lca=LCA(x,y);
            printf("%d\n",max(ask(x,lca),ask(y,lca)));
        }
        if(ch[0]=='N'){
            int lca=LCA(x,y);
            negate_(x,lca);negate_(y,lca);
        }
    }
}

[bzoj]2157: 旅游

Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 420 Solved: 248
[Submit][Status][Discuss]
Description

Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。

Input

输入的第一行包含一个整数N,表示T 城中的景点个数。景点编号为 0…N − 1。接下来N − 1 行,每行三个整数u、v 和w,表示有一条u 到v,使 Ray 愉悦度增加w 的桥。桥的编号为1…N − 1。|w| <= 1000。输入的第N + 1 行包含一个整数M,表示Ray 的操作数目。接下来有M 行,每行描述了一个操作,操作有如下五种形式: C i w,表示Ray 对于经过第i 座桥的愉悦度变成了w。 N u v,表示Ray 对于经过景点u 到v 的路径上的每一座桥的愉悦度都变成原来的相反数。 SUM u v,表示询问从景点u 到v 所获得的总愉悦度。 MAX u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最大愉悦度。 MIN u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最小愉悦度。测试数据保证,任意时刻,Ray 对于经过每一座桥的愉悦度的绝对值小于等于1000。

Output

对于每一个询问(操作S、MAX 和MIN),输出答案。

思路:跟上面的题一样。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100100;
int n,m,point[N],next[N*10],tot=1,siz[N]={0},fa[N][20]={0},son[N]={0};
int deep[N]={0},num=0,pos[N]={0},belong[N]={0},r[N]={0},map[N]={0},de[N*4]={0};
struct S{
    int st,en,va;
}aa[N*10];
struct C{
    int maxn,minn,sum;
}tr[N*4];
bool use[N];
inline void add(int x,int y,int z)
{
    tot+=1;next[tot]=point[x];point[x]=tot;
    aa[tot].st=x;aa[tot].en=y;aa[tot].va=z;
    tot+=1;next[tot]=point[y];point[y]=tot;
    aa[tot].st=y;aa[tot].en=x;aa[tot].va=z;
}
inline void dfs_1(int x)
{
    int i;
    siz[x]=1;
    use[x]=false;
    for(i=1;i<=14;++i){
        if(deep[x]<(1<<i)) break;
        fa[x][i]=fa[fa[x][i-1]][i-1];
    }
    for(i=point[x];i;i=next[i])
      if(use[aa[i].en]){
        fa[aa[i].en][0]=x;
        deep[aa[i].en]=deep[x]+1;
        dfs_1(aa[i].en);
        siz[x]+=siz[aa[i].en];
      }
}
inline void dfs_2(int x,int y)
{
    int i,k=0;
    num+=1;
    belong[x]=y;
    for(i=point[x];i;i=next[i]){
        if(deep[aa[i].st]>deep[aa[i].en]) map[aa[i].st]=i>>1;
        else map[aa[i].en]=i>>1;
    }
    for(i=point[x];i;i=next[i])
      if(deep[x]<deep[aa[i].en]&&siz[k]<siz[aa[i].en])
        k=aa[i].en,son[x]=i>>1,pos[i>>1]=num;
    if(k==0) {num-=1; return ;}
    dfs_2(k,y);
    for(i=point[x];i;i=next[i])
      if(deep[x]<deep[aa[i].en]&&k!=aa[i].en)
        num+=1,pos[i>>1]=num,dfs_2(aa[i].en,aa[i].en);
}
inline int LCA(int x,int y)
{
    int i;
    if(deep[x]<deep[y])swap(x,y);
    int t=deep[x]-deep[y];
    for(i=0;i<=14;++i)
        if(t&(1<<i))x=fa[x][i];
    for(i=14;i>=0;--i)
        if(fa[x][i]!=fa[y][i]) 
        {x=fa[x][i];y=fa[y][i];}
    if(x==y)return x;
    else return fa[x][0];
}
#define mid (l+r)/2
#define L k<<1,l,mid
#define R k<<1|1,mid+1,r
inline void solve(int k)
{
    swap(tr[k].maxn,tr[k].minn);
    tr[k].maxn=-tr[k].maxn;
    tr[k].minn=-tr[k].minn;
    tr[k].sum=-tr[k].sum;
}
inline void pushdown(int k)
{
    if(de[k]!=0){
        if((k<<1)<N*4){
            solve(k<<1); de[k<<1]=!de[k<<1];
            solve(k<<1|1); de[k<<1|1]=!de[k<<1|1];
        }
        de[k]=0;
    }
}
inline void insert(int k,int l,int r,int x,int y)
{   
    pushdown(k);
    if(l==r&&l==x){
        tr[k].maxn=tr[k].minn=tr[k].sum=y;
        return ;
    }
    if(x<=mid) insert(L,x,y);
    else insert(R,x,y);
    tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
    tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
    tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}
inline int qurry(int k,int l,int r,int x,int y,int kind)
{
    int maxn=-210000000,minn=210000000,sum=0;
    pushdown(k);
    if(x<=l&&y>=r){
        if(kind==0) return tr[k].maxn;
        if(kind==1) return tr[k].minn;
        if(kind==2) return tr[k].sum;
    }
    if(x<=mid){
        if(kind==0) maxn=max(maxn,qurry(L,x,y,kind));
        if(kind==1) minn=min(minn,qurry(L,x,y,kind));
        if(kind==2) sum+=qurry(L,x,y,kind);
    }
    if(y>mid){
        if(kind==0) maxn=max(maxn,qurry(R,x,y,kind));
        if(kind==1) minn=min(minn,qurry(R,x,y,kind));
        if(kind==2) sum+=qurry(R,x,y,kind);
    }
    if(kind==0) return maxn;
    if(kind==1) return minn;
    if(kind==2) return sum;
}
inline int work(int x,int y,int kind)
{
    int maxn=-210000000,z,minn=210000000,sum=0;
    if(map[x]==0){if(kind==0) return maxn;if(kind==1) return minn;if(kind==2) return sum;}
    if(belong[x]==x&&x!=y){
        if(kind==0) maxn=max(maxn,qurry(1,1,n-1,pos[map[x]],pos[map[x]],kind));
        if(kind==1) minn=min(minn,qurry(1,1,n-1,pos[map[x]],pos[map[x]],kind));
        if(kind==2) sum+=qurry(1,1,n-1,pos[map[x]],pos[map[x]],kind);
        x=fa[x][0];
    }
    if(map[x]==0){if(kind==0) return maxn;if(kind==1) return minn;if(kind==2) return sum;}
    while(belong[x]!=belong[y]){
        if(kind==0) maxn=max(maxn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]],kind));
        if(kind==1) minn=min(minn,qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]],kind));
        if(kind==2) sum+=qurry(1,1,n-1,pos[son[belong[x]]],pos[map[x]],kind);
        z=fa[belong[x]][0];
        if(kind==0) maxn=max(maxn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]],kind));
        if(kind==1) minn=min(minn,qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]],kind));
        if(kind==2) sum+=qurry(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]],kind);
        x=z;
    }
    if(map[x]==0){if(kind==0) return maxn;if(kind==1) return minn;if(kind==2) return sum;}
    if(kind==0) maxn=max(maxn,qurry(1,1,n-1,pos[son[y]],pos[map[x]],kind));
    if(kind==1) minn=min(minn,qurry(1,1,n-1,pos[son[y]],pos[map[x]],kind));
    if(kind==2) sum+=qurry(1,1,n-1,pos[son[y]],pos[map[x]],kind);
    if(kind==0) return maxn;if(kind==1) return minn;if(kind==2) return sum;
}
inline void change(int k,int l,int r,int x,int y)
{
    pushdown(k);
    if(x<=l&&y>=r){
        solve(k); de[k]=1;
        return ;
    }
    if(x<=mid) change(L,x,y);
    if(y>mid) change(R,x,y);
    tr[k].maxn=max(tr[k<<1].maxn,tr[k<<1|1].maxn);
    tr[k].minn=min(tr[k<<1].minn,tr[k<<1|1].minn);
    tr[k].sum=tr[k<<1].sum+tr[k<<1|1].sum;
}
inline void negate_(int x,int y)
{
    int z;
    if(map[x]==0) return ;
    if(belong[x]==x&&x!=y){
        change(1,1,n-1,pos[map[x]],pos[map[x]]);
        x=fa[x][0];
    }
    if(map[x]==0) return ;
    while(belong[x]!=belong[y]){
        change(1,1,n-1,pos[son[belong[x]]],pos[map[x]]);
        z=fa[belong[x]][0];
        change(1,1,n-1,pos[map[belong[x]]],pos[map[belong[x]]]);
        x=z;
    }
    if(map[x]==0) return ;
    change(1,1,n-1,pos[son[y]],pos[map[x]]);
    return ;
}
int main()
{
    int i,j,x,y,z;
    char ch[10];
    memset(use,1,sizeof(use));
    scanf("%d",&n);
    for(i=1;i<n;++i){
        scanf("%d%d%d",&x,&y,&z);
        x+=1;y+=1;
        add(x,y,z);r[i]=z;
    }
    dfs_1(1);
    dfs_2(1,1);
    for(i=1;i<n;++i) insert(1,1,n-1,pos[i],r[i]);
    scanf("%d",&m);
    while(m--){
        scanf("%*c%s",&ch);
        scanf("%d%d",&x,&y);
        if(ch[0]=='C') insert(1,1,n-1,pos[x],y);
        else{
            x+=1;y+=1;
            int lca=LCA(x,y);
            if(ch[0]=='N') negate_(x,lca),negate_(y,lca);
            if(ch[0]=='S') printf("%d\n",work(x,lca,2)+work(y,lca,2));
            if(ch[0]=='M'){
                if(ch[1]=='A') printf("%d\n",max(work(x,lca,0),work(y,lca,0)));
                if(ch[1]=='I') printf("%d\n",min(work(x,lca,1),work(y,lca,1)));
            }
        }
    }
}
### 树链剖分概念及其实现 树链剖分是一种用于高效处理树上路径查询和修改操作的技术。它通过对树进行解,使得任意两点之间的路径可以被划为不超过 \(O(\log n)\) 条连续的重链[^1]。 #### 原理概述 树链剖分的核心思想是将一棵树按照某种方式划成若干条“链”,并通过这些链来加速对树的操作。具体来说,树链剖分会定义一条从根节点到叶子节点的主要路径(称为重儿子),并将其余子树视为轻儿子。这种划能够保证任何两个节点间的路径都可以表示为少量的链连接而成。 #### 实现步骤 以下是树链剖分的一般实现流程: 1. **预处理阶段** 需要先完成 DFS 序列化以及计算每个节点的深度、父节点、重儿子等信息。 2. **建立重链** 使用递归来遍历整棵树,在每一步中优先访问当前节点的重儿子,并记录下属于同一条重链的所有节点。 3. **映射关系维护** 将原树上的节点与其对应的重链编号关联起来,便于后续快速定位。 下面给出一段基于 C++ 的简单实现代码示例: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 7; vector<int> adj[MAXN]; int sz[MAXN], heavyChild[MAXN], parentOfHeavyChain[MAXN], chainHead[MAXN], posInBaseArray[MAXN], currentPos, depthOfNode[MAXN]; void dfsSize(int u, int p){ sz[u]=1; heavyChild[u]=-1; for(auto &v : adj[u]){ if(v !=p ){ depthOfNode[v] =depthOfNode[u]+1 ; parentOfHeavyChain[v ]=u ; dfsSize( v , u ); sz[ u ]+=sz[ v ]; if (heavyChild[u]==-1 || sz[v]>sz[heavyChild[u]]) { heavyChild[u]=v; } } } } void decomposeTreeIntoChains(int u,int h){ chainHead[currentPos]=h; posInBaseArray[u]=currentPos++; if(heavyChild[u]!=-1){ decomposeTreeIntoChains(heavyChild[u],h); for(auto &v:adj[u]){ if(v!=parentOfHeavyChain[u] && v!=heavyChild[u]){ decomposeTreeIntoChains(v,v); } } } } ``` 此代码片段展示了如何利用两次DFS别求解大小数组 `sz[]` 和配重链头指针的过程。 #### 应用场景 树链剖分广泛应用于各种涉及复杂树结构的问题解决当中,尤其适合以下几种情况: - 路径上的加减乘除运算; - 子树范围内的统计汇总; - LCA最近公共祖先查找等问题; 例如,在动态规划或者图论题目里经常遇到需要频繁询问某棵子树内部的信息总和或是两节点之间边权最大最小值等情况时,采用树链剖分会显著提升效率。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值