Speed

Portal: Speed

Subject to the effect

N points to an unrooted trees, each tree edge i and ri represents the speed of li is given in [li, ri] to pass inside this edge.
Now there are m inquiry, each challenge gives a speed x, x, seek speed (can not be changed) can be adopted in the tree path of maximum length (start and end arbitrarily).
n, m <= 70000; 1 <= li, ri <= n;

Source & write because

This problem is dalao in their last exam training outside into the T3, I heard no one was AC, really hard, I asked myself also will write n ^ 2 violence.
Positive Solutions for full use of the property line of the tree, the tree line the other day the new school was built to optimize figure also echoes the feeling realized that some of the essence of the tree line, so to write a solution to a problem.

answer

n m with the same level, that is to give an answer when the determined speed n 1 ~, then O (1) to answer questions.
For a speed x, if we only left side contains a tree speed range of x, deleting the other side, will be a forest, the answer is one of the longest diameter. So we certainly have to consider the dynamic maintenance of a forest longest diameter, that is to say, the maintenance of which the longest chain of each communication block, bordered support operations. Disjoint-set plus tree diameter nature 1 can be easily achieved.
The question is what the intervals containing x it?
Setting n = 8, x = 3
and found to contain x is the section containing [3] or [3,4] or [1,4] of all sections.
This inspired us to do with the tree line.
A first node for each node of the tree line, able to save all of the node represented by section comprising edges (direct memory as a linked list).
Then traverse the tree line again when seeking answers, each went to a node node, to exert influence side node of the list in memory, and each node leave a node, on the revocation of the side effects linked list node stored. When this way when traversing to a leaf node of the tree line, set l = r = x, we just applied the effect of x contains all sides, the answer there.
Note that we need to undo influence,Persistable disjoint-setIn fact store operations with a stack of information, how to change the Retrospective on how to change back like, you can enter the number of stack elements are found in nlogn. But to do so it must not be disjoint-set path compression, so complexity becomes very metaphysical, but found even in the case of chain also run fly ==
natural language is too weak, or look at the code.

#include<bits/stdc++.h>
using namespace std;
const int N=70005,M=N*20;
inline int read(){
    int x=0,w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return w?-x:x; 
}
vector<int> to[N];
namespace sp{
    int dep[N],fa[N],sz[N],son[N],top[N];
    void dfs1(int x,int pre){
        dep[x]=dep[pre]+1;fa[x]=pre;sz[x]=1;
        for(int i=0;i<to[x].size();++i){
            int &y=to[x][i];
            if(y==pre) continue;
            dfs1(y,x);
            sz[x]+=sz[y];
            if(sz[y]>sz[son[x]]) son[x]=y;
        } 
    }   
    void dfs2(int x,int pre){
        top[x]=pre;
        if(son[x]) dfs2(son[x],pre);
        for(int i=0;i<to[x].size();++i){
            int &y=to[x][i];
            if(y==son[x]||y==fa[x]) continue;
            dfs2(y,y);
        }   
    }
    int lca(int x,int y){
        while(top[x]^top[y]){
            if(dep[top[x]]<dep[top[y]]) x^=y^=x^=y;
            x=fa[top[x]];
        }
        return dep[x]<dep[y]?x:y;
    } 
    int dist(int x,int y){return dep[x]+dep[y]-(dep[lca(x,y)]<<1);}
}
int n,m,cur=0;
namespace st{
    int fa[N],l[N],r[N],f[N];
    void init(){
        for(int i=1;i<=n;++i) fa[i]=l[i]=r[i]=i,f[i]=0;
    }
    inline int find(int x){return x==fa[x]?x:find(fa[x]);}
    struct opt{
        int id,x,val;
    }q[N<<2];
    using sp::dist;
    void merge(int x,int y,int &ans){
        x=find(x),y=find(y);
        if(x==y) return ;
        int res=0,nl,nr,t=-1;
        if(f[x]>res) nl=l[x],nr=r[x],res=f[x];
        if(f[y]>res) nl=l[y],nr=r[y],res=f[y];
        t=dist(l[x],l[y]);
        if(t>res) nl=l[x],nr=l[y],res=t;
        t=dist(r[x],r[y]);
        if(t>res) nl=r[x],nr=r[y],res=t;
        t=dist(l[x],r[y]);
        if(t>res) nl=l[x],nr=r[y],res=t;
        t=dist(r[x],l[y]);
        if(t>res) nl=r[x],nr=l[y],res=t;
        q[++cur]=(opt){0,y,0};
        q[++cur]=(opt){1,x,l[x]};
        q[++cur]=(opt){2,x,r[x]};
        q[++cur]=(opt){3,x,f[x]};
        fa[y]=x,l[x]=nl,r[x]=nr,f[x]=res;
        ans=max(ans,res); 
    }
    void retrace(int tim){
        while(cur>tim){
            switch(q[cur].id){
                case 0:fa[q[cur].x]=q[cur].x;break;
                case 1:l[q[cur].x]=q[cur].val;break;
                case 2:r[q[cur].x]=q[cur].val;break;
                case 3:f[q[cur].x]=q[cur].val;break;
            } 
            --cur;
        }
    }
}
namespace sg{
    int tot,tail[N<<2];
    struct edge{
        int u,v,last;
    }e[M];
    #define tl id<<1
    #define tr id<<1|1
    #define mid (l+r>>1)
    #define lson tl,l,mid
    #define rson tr,mid+1,r 
    void update(int id,int l,int r,int ll,int rr,int u,int v){
        if(ll<=l&&r<=rr){
            e[++tot]=(edge){u,v,tail[id]};
            tail[id]=tot;
            return ;
        }
        if(rr<=mid) update(lson,ll,rr,u,v);
        else if(ll>mid) update(rson,ll,rr,u,v);
        else update(lson,ll,rr,u,v),update(rson,ll,rr,u,v);
    }
    int ans[N];
    void solve(int id,int l,int r,int res){
        int tim=cur;
        for(int p=tail[id];p;p=e[p].last) st::merge(e[p].u,e[p].v,res);
        if(l==r){
            ans[l]=res;
            st::retrace(tim);
            return ;
        }
        solve(lson,res),solve(rson,res);
        st::retrace(tim);
    }
}
int main(){
    n=read(),m=read();
    for(int i=1,u,v,l,r;i<n;++i){
        u=read(),v=read(),l=read(),r=read();
        to[u].push_back(v),to[v].push_back(u);
        sg::update(1,1,n,l,r,u,v);
    }
    sp::dfs1(1,0);sp::dfs2(1,1);    
    st::init();
    sg::solve(1,1,n,0);
    while(m--) printf("%d\n",sg::ans[read()]);
    return 0;
}

  1. A tree and a tree set B, endpoint A tree diameter, la and ra, end lb of tree diameter B and rb. A and B are connected between a rear edge, the diameter of the end points obtained new tree must be La, two ra, lb, rb of.

Guess you like

Origin www.cnblogs.com/gosick/p/11517140.html