[CF593D]Happy Tree Party

题目

传送门

题解

思维好题.

必须明白,一个数 \(x(x\le 10^{18})\) 在反复执行除以某个数下取整,即反复 \(x=\lfloor \frac{x}{t} \rfloor(t\ge 2)\) 超过 \(60\) 次之后必定为 \(0\),因为 \(\log _210^{18}<60\),所以,如果去除那些边权为 \(1\) 的边,我们最多使用暴力爬山法 \(60\) 次,这个 \(x\) 就会变成 \(0\),而题目中又有一个边权值变化是具有特殊性的,即变化范围在 \([1,x_{p_i}]\),说明一条边只会往小变,并且在边权为 \(1\) 之后不会再变回来,那么我们可以用类似并查集的数据结构,将那些边权为 \(1\) 的边跳过不算,这样就可以保证我们暴力爬山只会执行最多 \(60\) 次,那么时间复杂度为 \(\mathcal O(60m+\alpha m)\),并查集的反阿克曼不能忘记。

代码

#include<cstdio>
#include<algorithm>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x<y?y:x;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int maxn=200000;
const int logmaxn=17;

int fa[maxn+5];LL val[maxn+5];
inline void build(){rep(i,1,maxn)fa[i]=i;}
int root(const int u){return fa[u]==u?u:fa[u]=root(fa[u]);}

struct edge{int to,nxt;}e[maxn*2+5];
int tail[maxn+5],ecnt=1;//从 1 开始, 这样 index>>1 即为编号
inline void add_edge(const int u,const int v){
    e[++ecnt]=edge{v,tail[u]};tail[u]=ecnt;
    e[++ecnt]=edge{u,tail[v]};tail[v]=ecnt;
}
int down[maxn+5];
LL w[maxn+5];

int n,m;

inline void Init(){
    n=read(1),m=read(1);
    int u,v;
    rep(i,1,n-1){
        u=read(1),v=read(1),w[i]=read(1ll);
        add_edge(u,v);
    }
}

int f[maxn+5][logmaxn+5];
int d[maxn+5];

void Dfs(const int u,const int pre){
    d[u]=d[pre]+1,f[u][0]=pre;
    rep(j,1,logmaxn)f[u][j]=f[f[u][j-1]][j-1];
    erep(i,u)if(v^pre){
        if(w[i>>1]==1)fa[root(v)]=root(u);
        down[i>>1]=v,val[v]=w[i>>1];
        Dfs(v,u);
    }
}

inline int getlca(int u,int v){
    if(d[u]<d[v])swap(u,v);
    fep(j,logmaxn,0)if(d[f[u][j]]>=d[v])u=f[u][j];
    if(u==v)return u;
    fep(j,logmaxn,0)if(f[u][j]^f[v][j])
        u=f[u][j],v=f[v][j];
    return f[u][0];
}

inline void Get_query(){
    int opt,lca;LL x,y,z;
    while(m--){
        opt=read(1),x=read(1ll),y=read(1ll);
        if(opt==1){
            z=read(1ll);
            lca=getlca(x,y);
            // printf("node %lld and %lld , lca == %d\n",x,y,lca);
            while(d[x]>d[lca] && z){
                if(val[x])z/=val[x];
                x=root(f[x][0]);
            }
            while(d[y]>d[lca] && z){
                if(val[y])z/=val[y];
                y=root(f[y][0]);
            }
            writc(z,'\n');
        }else{
            int node=down[x];
            val[node]=y;
            if(val[node]==1)fa[node]=root(f[node][0]);
        }
    }
}

signed main(){
    Init();
    build();
    Dfs(1,0);
    Get_query();
    return 0;
}
posted @ 2020-08-24 14:45  Arextre  阅读(158)  评论(0编辑  收藏  举报