题意不在描述.
首先我们注意到改变每个点的值时,只会变的更小,每次要除的数为1e18 , 由于log2(1e18) <= 60 .那么最多只需要除以至多60次大于等于2的数变回变成0,所以我们
只以任意一点为根建立有根树,并且在每次遍历一条链时进行压缩,当该路经值为1,应该被压缩,每次走到多余六十个点直接结束。
#include <bits/stdc++.h> #define fst first #define snd second #define ALL(a) a.begin(), a.end() #define clr(a, x) memset(a, x, sizeof a) #define rep(i,x) for(int i=0;i<(int)x;i++) #define rep1(i,x,y) for(int i=x;i<=(int)y;i++) #define LOGN 22 typedef long long ll; using namespace std; const int N = 2e5 + 10000; vector<int> tree[N]; int fa[N][LOGN],pa[N] , realpa[N]; int depth[N]; ll val[N*2] , ided[N]; void dfs(int u, int p, int d) { depth[u] = d; pa[u] = p; realpa[u] = p; fa[u][0] = p; for (int i = 0; i < tree[u].size(); ++i) { if (tree[u][i] != p) dfs(tree[u][i], u, d + 1); } } int LCA(int u, int v) { if (depth[u] > depth[v]) swap(u, v); for (int i = 0; i < LOGN; ++i) { if (((depth[v] - depth[u]) >> i) & 1) v = fa[v][i]; } if (u == v) return u; for (int i = LOGN - 1; i >= 0; --i) { if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } } return fa[u][0]; } void predo(int n) { int root = 1; pa[root] = -1; dfs(root, -1, 0); depth[0] = -1; for (int j = 0; j + 1 < LOGN; ++j) { for (int i = 1; i <= n; ++i) { if (fa[i][j] < 0) fa[i][j + 1] = -1; else fa[i][j + 1] = fa[fa[i][j]][j]; } } } typedef pair<int,int> pii; map<pii,int> M; ll get_c(int u){ return val[ided[u]]; } int n,m; int find(int u){ if(realpa[pa[u]] == -1 || get_c(pa[u]) > 1) return pa[u]; else return pa[u] = find(pa[u]); } ll cal1(int a,int b,ll c){ int lca = LCA(a,b); int u = a , v = b; ll now = c; vector<ll> lt,rt; while(u != v){ if(depth[u] >= depth[v]){ int fau = find(u); if(depth[fau] < depth[lca]) fau = lca; if(get_c(u) > 1) lt.push_back(get_c(u)); u = fau; } else { int fav = find(v); if(depth[fav] < depth[lca]) fav = lca; if(get_c(v) > 1) rt.push_back(get_c(v)); v = fav; } if(lt.size() + rt.size() > 60) return 0; } if(rt.size()) reverse(ALL(rt)); rep(i,rt.size()) lt.push_back(rt[i]); for(int i=0;i<lt.size();i++){ now/=lt[i]; } return now; } void read(){ scanf("%d %d",&n,&m); rep(i,n-1){ int u,v; ll w; scanf("%d %d %I64d",&u,&v,&w); M[pii(u,v)] = M[pii(v,u)] = 1 + i; val[1+i] = w; tree[u].push_back(v); tree[v].push_back(u); } predo(n); rep1(i,2,n) ided[i] = M[pii(i,realpa[i])] ; rep1(i,1,m){ ll cmd , x ,y , z; scanf("%I64d %I64d %I64d",&cmd,&x,&y); if(cmd == 1) { scanf("%I64d",&z); printf("%I64d\n",cal1(x,y,z)); } else { val[x] = y; } } } int main() { read(); return 0; }