CodeForces 593D Happy Tree Party

Description

Bogdan has a birthday today and mom gave him a tree consisting of n vertecies. For every edge of the tree i, some number xi was written on it. In case you forget, a tree is a connected non-directed graph without cycles. After the present was granted, m guests consecutively come to Bogdan's party. When the i-th guest comes, he performs exactly one of the two possible operations:

  1. Chooses some number yi, and two vertecies ai and bi. After that, he moves along the edges of the tree from vertex ai to vertex biusing the shortest path (of course, such a path is unique in the tree). Every time he moves along some edge j, he replaces his current number yi by , that is, by the result of integer division yi div xj.
  2. Chooses some edge pi and replaces the value written in it xpi by some positive integer ci < xpi.

As Bogdan cares about his guests, he decided to ease the process. Write a program that performs all the operations requested by guests and outputs the resulting value yi for each i of the first type.

Input

The first line of the input contains integers, n and m (2 ≤ n ≤ 200 0001 ≤ m ≤ 200 000) — the number of vertecies in the tree granted to Bogdan by his mom and the number of guests that came to the party respectively.

Next n - 1 lines contain the description of the edges. The i-th of these lines contains three integers uivi and xi (1 ≤ ui, vi ≤ nui ≠ vi1 ≤ xi ≤ 1018), denoting an edge that connects vertecies ui and vi, with the number xi initially written on it.

The following m lines describe operations, requested by Bogdan's guests. Each description contains three or four integers and has one of the two possible forms:

  • 1 ai bi yi corresponds to a guest, who chooses the operation of the first type.
  • 2 pi ci corresponds to a guests, who chooses the operation of the second type.
It is guaranteed that all the queries are correct, namely  1 ≤ ai, bi ≤ n1 ≤ pi ≤ n - 11 ≤ yi ≤ 1018 and  1 ≤ ci < xpi, where  xpirepresents a number written on edge  pi at this particular moment of time that is not necessarily equal to the initial value  xpi, as some decreases may have already been applied to it. The edges are numbered from  1 to  n - 1 in the order they appear in the input.

Output

For each guest who chooses the operation of the first type, print the result of processing the value yi through the path from ai to bi.

Sample Input

Input
6 6
1 2 1
1 3 7
1 4 4
2 5 5
2 6 2
1 4 6 17
2 3 2
1 4 6 17
1 5 5 20
2 4 1
1 5 1 3
Output
2
4
20
3
Input
5 4
1 2 7
1 3 3
3 4 2
3 5 5
1 4 2 100
1 5 4 1
2 2 2
1 1 3 4
Output
2
0
2
 
      
可以直接暴力的上树链剖分,线段树维护区间乘积。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
const int low(int x) { return x&-x; }
const int N = 4e5 + 10;
int n, m, x, y, z;
int ft[N], nt[N], u[N], v[N], sz;
int ct[N], mx[N], fa[N];
int dep[N], top[N], g[N], G[N], F[N];
LL f[N << 1], a[N], c;
char s[N];

void dfs(int x, int f)
{
	ct[x] = 1; mx[x] = 0;
	dep[x] = dep[f] + 1; fa[x] = f;
	loop(i, ft[x], nt)
	{
		if (u[i] == f) continue;
		dfs(u[i], x);
		ct[x] += ct[u[i]];
		if (ct[u[i]] > ct[mx[x]]) mx[x] = u[i];
	}
}

void Dfs(int x, int t)
{
	top[x] = !t ? x : top[fa[x]];
	g[x] = ++sz;
	if (mx[x]) Dfs(mx[x], 1);
	loop(i, ft[x], nt)
	{
		if (u[i] == fa[x]) continue;
		if (u[i] == mx[x]) { G[v[i]] = g[u[i]]; continue; }
		Dfs(u[i], 0); G[v[i]] = g[u[i]];
	}
}

LL mul(LL x, LL y)
{
	if (1.0 * x * y > 1e18) return 0;
	return x * y;
}

void build(int x, int l, int r)
{
	if (l == r) { f[x] = a[F[l]]; return; }
	int mid = l + r >> 1;
	build(lson); build(rson);
	f[x] = mul(f[x << 1], f[x << 1 | 1]);
}

void change(int x, int l, int r, int u, LL v)
{
	if (l == r) { f[x] = v; return; }
	int mid = l + r >> 1;
	if (u <= mid) change(lson, u, v);
	else change(rson, u, v);
	f[x] = mul(f[x << 1], f[x << 1 | 1]);
}

LL sum(int x, int l, int r, int ll, int rr)
{
	if (ll <= l && r <= rr) return f[x];
	int mid = l + r >> 1;
	LL res = 1;
	if (ll <= mid) res = mul(res, sum(lson, ll, rr));
	if (rr > mid) res = mul(res, sum(rson, ll, rr));
	return res;
}

int main()
{
	while (intwo(n, m) != EOF)
	{
		rep(i, 1, n) ft[i] = -1;
		ct[0] = dep[0] = sz = 0;
		rep(i, 1, n - 1)
		{
			intwo(x, y); scanf("%lld", &a[i]);
			u[sz] = y; v[sz] = i; nt[sz] = ft[x]; ft[x] = sz++;
			u[sz] = x; v[sz] = i; nt[sz] = ft[y]; ft[y] = sz++;
		}
		dfs(1, sz = 0); Dfs(1, 0);
		rep(i, 1, n - 1) F[G[i]] = i;
		build(1, 1, n);
		while (m--)
		{
			inone(x);
			if (x == 1)
			{
				intwo(x, y); scanf("%lld", &c);
				if (x == y) { printf("%lld\n", c); continue; }
				LL res = 1;
				for (; top[x] != top[y]; x = fa[top[x]])
				{
					if (dep[top[x]] < dep[top[y]]) swap(x, y);
					res = mul(res, sum(1, 1, n, g[top[x]], g[x]));
				}
				if (dep[x] > dep[y]) swap(x, y);
				res = mul(res, sum(1, 1, n, g[x] + 1, g[y]));
				printf("%lld\n", res ? c / res : 0);
			}
			else
			{
				inone(x);  scanf("%lld", &c);
				change(1, 1, n, G[x], c);
			}
		}
	}
	return 0;
}
也可以利用题目中给出的一个隐性条件,递减的数字,一个数除于链上的值,下降速度一定是log级别的,除非链上的值是1,所以暴力计算答案,合并1的节点可以了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
const int low(int x) { return x&-x; }
const int N = 4e5 + 10;
int n, m, x, y, z;
int ft[N], nt[N], u[N], v[N], sz;
int fa[N], dep[N], g[N], top[N];
int L[N], R[N];
long long a[N], c;
char s[N];

void dfs(int x, int f)
{
	dep[x] = dep[f] + 1; fa[x] = f;
	L[x] = ++sz;
	loop(i, ft[x], nt)
	{
		if (u[i] == f) continue;
		g[u[i]] = v[i]; dfs(u[i], x);
	}
	R[x] = sz;
}

int get(int x)
{
	return x == top[x] ? x : top[x] = get(top[x]);
}

int main()
{
	while (intwo(n, m) != EOF)
	{
		rep(i, 1, n) ft[i] = -1, top[i] = i;
		g[0] = dep[0] = sz = 0; a[0] = 1;
		rep(i, 1, n - 1)
		{
			intwo(x, y); scanf("%lld", &a[i]);
			u[sz] = y; v[sz] = i; nt[sz] = ft[x]; ft[x] = sz++;
			u[sz] = x; v[sz] = i; nt[sz] = ft[y]; ft[y] = sz++;
		}
		dfs(1, sz = 0);
		while (m--)
		{
			inone(x);
			if (x == 1)
			{
				intwo(x, y); scanf("%lld", &c);
				while (top[x] != top[y] && c)
				{
					if (dep[x] < dep[y]) swap(x, y);
					x = get(x);
					if (a[g[x]] == 1) top[x] = fa[x];
					if (L[x] <= L[y] && R[x] >= R[y]) continue;
					c /= a[g[x]]; x = fa[x]; 
				}
				printf("%lld\n", c);
			}
			else
			{
				inone(x);  scanf("%lld", &a[x]);
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值