http://codeforces.com/problemset/problem/593/D
题意
一个含有n个点的树,边上都有一个正整数权值,进行两种操作:
1、1 u v w ,用w依次除以u到v简单路径上所有边的权值,输出结果
2、2 p c,将第p条边的权值变为c,边权值只会变小,不会变大
思路
注意到一个不大于1e18的数进行除运算,最多会除以O(log)次大于1的数。随意指定一个根建树,每次的询问利用lca找到对应的简单路径,用并查集做路径压缩,当一条边的权值变为1之后,就没有访问的必要了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define INF 1e9
#define SZ(c) int(c.size())
#define ALL(c) c.begin(), c.end()
#define SET(c, v) memset(c, v, sizeof (c))
const int N = 2e5 + 9;
const int M = 1e9 + 7;
ll w[N];
vector <pii> g[N];
int p[N], in[N], out[N], idx[N], id;
void dfs(int u) {
in[u] = ++id;
for (auto v : g[u]) {
if (v.first == p[u]) continue;
p[v.first] = u;
idx[v.first] = v.second;
dfs(v.first);
}
out[u] = id;
}
int next(int u) {
return w[idx[u]] == 1 ? p[u] = next(p[u]) : u;
}
void toLCA(int u, int v, ll &c) {
if (!u || !c) return;
if (in[u] <= in[v] && out[u] >= out[v]) return;
//cout<<"in"<<u<<' '<<in[u]<<' '<<"out"<<u<<' '<<out[u]<<' '<<"in"<<v<<' '<<in[v]<<' '<<"out"<<v<<' '<<out[v]<<endl;
c /= w[idx[u]];
toLCA(p[u] = next(p[u]), v, c);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
ll c;
int n, m, u, v;
scanf("%d %d", &n, &m);
for (int i = 1; i < n; i++) {
scanf("%d %d %I64d", &u, &v, &w[i]);
g[u].push_back({v, i});
g[v].push_back({u, i});
}
dfs(1);
while (m--) {
scanf("%d", &u);
if (u == 1) {
scanf("%d %d %lld", &u, &v, &c);
toLCA(u, v, c);
toLCA(v, u, c);
printf("%lld\n", c);
} else {
scanf("%d %lld", &u, &c);
w[u] = c;
}
}
return 0;
}