P4319 解题报告

· · 题解

前言

LCT 题单里的,刚好我的总结文章里把这题当做了练习题,就写篇题解吧。

题意分析

题意:

询问在第 i 天的可用边的 MST 的边权和。

首先看到 MST,以及边会变为可用/不可用状态。

优先考虑下动态 MST。

动态 MST 就不讲了,详情可以见我总结文章中的『6.1.1.』。

然后因为一条边是在一个连续的时间范围中可用/不可用,所以我们可以考虑使用线段树分治弄出来时间范围。

每走到一个节点,就把上面标记的边都加到 LCT 上,注意回溯的时候也要把这些边断掉。

走到叶子节点的时候就是时间为一个点的时候,记录下答案。

然后用 LCT 动态维护 MST 即可。

代码

#include <bits/stdc++.h>
#define ls son[x][0]
#define rs son[x][1]
#define tls (x<<1)
#define trs (x<<1|1)
#define mid ((l+r)>>1)
#define int long long
using namespace std;
const int N=50010,mod=1e9+7,INF=0x3f3f3f3f3f3f3f3f,M=200010,S=N+M,lim=32766;
struct edge{int u,v,w;}e[M];
int n,m,ans=1,tot,tp;
int f[S],s[S],son[S][2],st[S],w[S],r[S],mp[S],s1[S],s2[S];
vector<int>g[S<<2];
static char buf[1000000],*paa=buf,*pd=buf;
#define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
inline int read(void){
    int u(0),t(1);char fc(getchar());
    while(!isdigit(fc)){if(fc=='-') t=-1;fc=getchar();}
    while(isdigit(fc)) u=(u<<1)+(u<<3)+(fc^48),fc=getchar();
    return u*t;
}
inline void print(int u)
{
    if(u<0) putchar('-'),u=-u;
    if(u>9) print(u/10);
    putchar(u%10+'0');
}
inline int ksm(int a,int b,int mod)
{
    int u=a%mod,res=1;
    while(b)
    {
        if(b&1) res=res*u%mod;
        b>>=1;u=u*u%mod;
    }
    return res;
}
bool cmp(edge x,edge y){return x.w<y.w;}
bool ntrt(int x){return son[f[x]][1]==x||son[f[x]][0]==x;}
void pushup(int x)
{
    s[x]=x;
    if(w[s[ls]]>w[s[x]]) s[x]=s[ls];
    if(w[s[rs]]>w[s[x]]) s[x]=s[rs];
}
void pushson(int x){swap(ls,rs),r[x]^=1;}
void pushdown(int x)
{
    if(r[x])
    {
        if(ls) pushson(ls);
        if(rs) pushson(rs);
        r[x]=0;
    }
}
void rotate(int x)
{
    int y=f[x],z=f[y],k=(son[y][1]==x),w=son[x][!k];
    if(ntrt(y)) son[z][son[z][1]==y]=x;son[x][!k]=y;son[y][k]=w;
    if(w) f[w]=y;f[y]=x;f[x]=z;
    pushup(y);
}
void Splay(int x)
{
    int y=x,tot=0;st[++tot]=y;
    while(ntrt(y)) st[++tot]=y=f[y];
    while(tot) pushdown(st[tot--]);
    while(ntrt(x))
    {
        y=f[x];int z=f[y];
        if(ntrt(y)) rotate((son[y][0]==x)^(son[z][0]==y)?x:y);
        rotate(x);
    }
    pushup(x);
}
void access(int x){for(int y=0;x;x=f[y=x]) Splay(x),rs=y,pushup(x);}
void makert(int x){access(x);Splay(x);pushson(x);}
int findrt(int x)
{
    access(x);Splay(x);
    while(ls) pushdown(x),x=ls;
    Splay(x);return x;
}
int split(int x,int y){makert(x);access(y);Splay(y);return s[y];}
void link(int x,int y){makert(x);if(findrt(y)!=x)f[x]=y;}
void cut(int x,int y)
{
    makert(x);
    if(findrt(y)==x&&f[y]==x&&!son[y][0]) f[y]=son[x][1]=0,pushup(x);
}
bool check(int x, int y){makert(x);return findrt(y)==x;}
void modify(int u,int l,int r,int nl,int nr,int k)
{
    if(nl<=l&&r<=nr){g[u].push_back(k);return;}
    if(nl<=mid) modify(u<<1,l,mid,nl,nr,k);
    if(nr>mid) modify(u<<1|1,mid+1,r,nl,nr,k);
}
void solve(int l,int r,int x)
{
    int u,v,w,d,lst=tp;
    for(auto j:g[x])
    {
        u=e[j].u,v=e[j].v,w=e[j].w;
        if(check(u,v))
        {
            d=split(u,v)-n;if(e[d].w<=w) continue;
            ans-=e[d].w;cut(e[d].u,d+n),cut(e[d].v,d+n); 
            s1[++tp]=d,s2[tp]=-1;
        } 
        link(u,n+j),link(n+j,v);
        s1[++tp]=j,s2[tp]=1,ans+=w;
    }
    if(l==r) print(ans),puts("");
    else solve(l,mid,x<<1),solve(mid+1,r,x<<1|1);
    while(tp>lst)
    {
        d=s1[tp],u=e[d].u,v=e[d].v,w=e[d].w;
        if(s2[tp]==-1) link(u,d+n),link(v,d+n),ans+=w;
        else cut(u,d+n),cut(v,d+n),ans-=w;tp--;
    }
}
signed main()
{
    n=read();
    for(int i=1,u,v,ww;i<n;i++) u=read(),v=read(),ww=read(),e[++tot]=(edge){u,v,ww},w[n+tot]=ww,modify(1,1,lim,1,lim,tot); 
    m=read();for(int i=1,u,v,ww,l,r;i<=m;i++)
    {
        u=read(),v=read(),ww=read(),l=read(),r=read();
        e[++tot]=(edge){u,v,ww},w[n+tot]=ww,modify(1,1,lim,l,r,tot);
    }solve(1,lim,1);
    return 0;
}