天天看點

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

Input

The first line of the input contains integers, n and m (2 ≤ n ≤ 200 000, 1 ≤ 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 ui, vi and xi (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ xi ≤ 1018), denoting an edge that connects vertecies ui and vi, with the number xi

The following m

  • 1 ai bi yi
  • 2 pi ci

It is guaranteed that all the queries are correct, namely 

1 ≤ ai, bi ≤ n, 

1 ≤ pi ≤ n - 1, 

1 ≤ 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

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;
}