bzoj4515 [Sdoi2016] Game (tree chain segmentation + Li Chao line segment tree)

We divide the path of x->y into two parts: x->t, t->y to consider (t is the lca of x, y)
to separate functions only related to dis[v]. Then the tree chain segmentation + Li super line segment tree can be used to maintain the minimum value. Because the interval minimum is required, the interval minimum is also maintained.
the complexity O ( n l og3n)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define inf 123456789123456789LL
#define N 100010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,m,h[N],num=0,dep[N],fa[N],dfn[N],dfnum=0,son[N],sz[N];
int pos[N],top[N],tot=0;
ll dis[N];
struct edge{
    int to,next,val;
}data[N<<1];
struct Line{
    ll k,b;
    ll f(ll x){return k*x+b;}
}a[N<<1];
struct node{
    int x;ll mn;
}tr[N<<2];
inline void dfs1(int x){
    sz[x]=1;son[x]=0;
    for(int i=h[x];i;i=data[i].next){
        int y=data[i].to;if(y==fa[x]) continue;
        fa[y]=x;dep[y]=dep[x]+1;dis[y]=dis[x]+data[i].val;
        dfs1(y);sz[x]+=sz[y];if(sz[y]>sz[son[x]]) son[x]=y;
    }
}
inline void dfs2(int x,int tp){
    dfn[x]=++dfnum;top[x]=tp;pos[dfnum]=x;
    if(son[x]) dfs2(son[x],tp);
    for(int i=h[x];i;i=data[i].next){
        int y=data[i].to;if(y==fa[x]||y==son[x]) continue;dfs2(y,y);
    }
}
inline int lca(int x,int y){
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        x=fa[top[x]];
    }return dep[x]<dep[y]?x:y;
}
inline void pushup(int p,int l,int r){
    if(tr[p].x) tr[p].mn=min(a[tr[p].x].f(dis[pos[l]]),a[tr[p].x].f(dis[pos[r]]));
    if(l!=r) tr[p].mn=min(tr[p].mn,min(tr[p<<1].mn,tr[p<<1|1].mn));
}
inline void build(int p,int l,int r){
    tr[p].mn=inf;if(l==r) return;int mid=l+r>>1;
    build(p<<1,l,mid);build(p<<1|1,mid+1,r);
}
inline void ins(int p,int l,int r,int x,int y,int id){
    if(x<=l&&r<=y){
        if(!tr[p].x) tr[p].x=id;
        else{
            bool fl=a[tr[p].x].f(dis[pos[l]])<a[id].f(dis[pos[l]]);
            bool fr=a[tr[p].x].f(dis[pos[r]])<a[id].f(dis[pos[r]]);
            if(fl&&fr) return;
            if(!fl&&!fr) tr[p].x=id;
            else{
                int mid=l+r>>1;
                if(fl){
                    if(a[tr[p].x].f(dis[pos[mid]])<a[id].f(dis[pos[mid]])) ins(p<<1|1,mid+1,r,x,y,id);
                    else ins(p<<1,l,mid,x,y,tr[p].x),tr[p].x=id;
                }else{
                    if(a[tr[p].x].f(dis[pos[mid]])<a[id].f(dis[pos[mid]])) ins(p<<1,l,mid,x,y,id);
                    else ins(p<<1|1,mid+1,r,x,y,tr[p].x),tr[p].x=id;
                }
            }
        }pushup(p,l,r);return;
    }int mid=l+r>>1;
    if(x<=mid) ins(p<<1,l,mid,x,y,id);
    if(y>mid) ins(p<<1|1,mid+1,r,x,y,id);
    pushup(p,l,r);
}
inline void docover(int x,int y,int id){
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        ins(1,1,n,dfn[top[x]],dfn[x],id);x=fa[top[x]];
    }if(dfn[x]>dfn[y]) swap(x,y);
    ins(1,1,n,dfn[x],dfn[y],id);
}
inline ll ask(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y) return tr[p].mn;
    int mid=l+r>>1;ll res=inf;
    if(tr[p].x) res=min(a[tr[p].x].f(dis[pos[x]]),a[tr[p].x].f(dis[pos[y]]));
    if(y<=mid) return min(res,ask(p<<1,l,mid,x,y));
    if(x>mid) return min(res,ask(p<<1|1,mid+1,r,x,y));
    return min(res,min(ask(p<<1,l,mid,x,mid),ask(p<<1|1,mid+1,r,mid+1,y)));
}
inline ll doask(int x,int y){
    ll res=inf;
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        res=min(res,ask(1,1,n,dfn[top[x]],dfn[x]));x=fa[top[x]];
    }if(dfn[x]>dfn[y]) swap(x,y);
    res=min(res,ask(1,1,n,dfn[x],dfn[y]));return res;
}
int main(){
    n=read();m=read();
    for(int i=1;i<n;++i){
        int x=read(),y=read(),val=read();
        data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val;
        data[++num].to=x;data[num].next=h[y];h[y]=num;data[num].val=val;
    }dfs1(1);dfs2(1,1);build(1,1,n);
    while(m--){
        int op=read(),x=read(),y=read();
        if(op==2){printf("%lld\n",doask(x,y));continue;}
        ll aa=read(),b=read();int t=lca(x,y);
        a[++tot].k=-aa;a[tot].b=aa*dis[x]+b;
        docover(x,t,tot);
        a[++tot].k=aa;a[tot].b=(dis[x]-2*dis[t])*aa+b;
        docover(y,t,tot);
    }return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325808853&siteId=291194637