Codeforces 593D-Happy Tree Party
来源:互联网 发布:求暖床软件怎么样 编辑:程序博客网 时间:2024/09/21 09:29
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:
- 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.
- 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.
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 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.
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.
6 61 2 11 3 71 4 42 5 52 6 21 4 6 172 3 21 4 6 171 5 5 202 4 11 5 1 3
24203
5 41 2 71 3 33 4 23 5 51 4 2 1001 5 4 12 2 21 1 3 4
202
Initially the tree looks like this:
The response to the first query is: = 2
After the third edge is changed, the tree looks like this:
The response to the second query is: = 4
In the third query the initial and final vertex coincide, that is, the answer will be the initial number 20.
After the change in the fourth edge the tree looks like this:
In the last query the answer will be: = 3
题意:给你一棵树,每条边上有权值。有两个操作
1. u v p 从u~v的路径上用p除每条边的权值,向下取整
2. u p 将第u条边的权值改为p
解题思路:树链剖分
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int n, q, x, y, p;int top[200009], g[200009], G[200009], G1[200009];int s[200009], nt[400009], e[400009], l[400009], cnt;int ct[200009], mx[200009], fa[200009], dep[200009];LL a[200009], f[200009 * 4],z;void dfs(int k, int f){dep[k] = dep[f] + 1;fa[k] = f, ct[k] = 1, mx[k] = 0;for (int i = s[k]; ~i; i = nt[i]){if (e[i] == f) continue;dfs(e[i], k);ct[k] += ct[e[i]];if (ct[e[i]] > ct[mx[k]]) mx[k] = e[i];}}void Dfs(int k, int t){top[k] = !t ? k : top[fa[k]];g[k] = ++cnt;if (mx[k]) Dfs(mx[k], 1);for (int i = s[k]; ~i; i = nt[i]){if (e[i] == fa[k]) continue;if (e[i] == mx[k]) { G[l[i]] = g[e[i]]; G1[g[e[i]]] = l[i]; continue; }Dfs(e[i], 0);G[l[i]] = g[e[i]];G1[g[e[i]]] = l[i];}}LL mul(LL x, LL y){return 1.0*x*y > 1e18 ? 0 : x*y;}void build(int k, int l, int r){if (l == r) { f[k] = a[G1[l]]; return; }int mid = (l + r) >> 1;build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r);f[k] = mul(f[k << 1], f[k << 1 | 1]);}void update(int k, int l, int r, int p, LL val){if (l == r) { f[k] = val; return; }int mid = (l + r) >> 1;if (p <= mid) update(k << 1, l, mid, p, val);else update(k << 1 | 1, mid + 1, r, p, val);f[k] = mul(f[k << 1], f[k << 1 | 1]);}LL query(int k, int l, int r, int ll, int rr){if (l >= ll&&r <= rr) return f[k];int mid = (l + r) >> 1;LL ans = 1;if(ll<=mid) ans= mul(ans, query(k<<1,l,mid,ll,rr));if (rr > mid) ans = mul(ans, query(k << 1 | 1, mid + 1, r, ll, rr));return ans;}void solve(int x, int y, LL z){LL ans = 1;while (top[x] != top[y]){if (dep[top[x]] < dep[top[y]]) swap(x, y);ans=mul(ans,query(1, 1, n, g[top[x]], g[x])); x = fa[top[x]];}if (dep[x] > dep[y]) swap(x, y);ans = mul(ans, query(1, 1, n, g[x] + 1, g[y]));printf("%lld\n", ans ? z / ans : ans);}int main(){//freopen("input.txt", "r", stdin);//freopen("output.txt", "w", stdout);while (~scanf("%d%d", &n, &q)){memset(s, -1, sizeof s);dep[0] = ct[0] = cnt = 0;for (int i = 1; i < n; i++){scanf("%d%d%lld", &x, &y, &a[i]);nt[cnt] = s[x], s[x] = cnt, l[cnt] = i, e[cnt++] = y;nt[cnt] = s[y], s[y] = cnt, l[cnt] = i, e[cnt++] = x;}dfs(1, 0);Dfs(1, cnt = 0);build(1, 1, n);while (q--){scanf("%d", &p);if (p == 2){scanf("%d%lld", &x, &z);update(1, 1, n, G[x], z);}else{scanf("%d%d%lld", &x, &y, &z);if (x == y) { printf("%lld\n", z); continue; }solve(x, y, z);}}}return 0;}
- 【13.91%】【codeforces 593D】Happy Tree Party
- CodeForces 593D Happy Tree Party
- Codeforces 593D-Happy Tree Party
- codeforces 593 D. Happy Tree Party (LCA + 并查集)
- codeforces 593D Happy Tree Party Lca+并查集
- Codeforces 593D - Happy Tree Party(树链剖分)
- [CodeForces 593D]Happy Tree Party[LCA][路径压缩]
- Codeforces Round #329 D Happy Tree Party(树链剖分)
- Codeforces Round #329 (Div. 2) D. Happy Tree Party
- Codeforces Round #329 (Div. 2) 593D. Happy Tree Party(树链剖分)
- CodeForces 593D Happy Tree Party(树链剖分(边权) or LCA+并查集)
- codeforces #329 D. Happy Tree Party (LCA+并查集 || 树链剖分)
- Codeforces Round #329 (Div. 2) D. Happy Tree Party(树链剖分)
- [树链剖分] CF593D. Happy Tree Party
- 【Codeforces Round 329 (Div 2) D】【LCA+并查集路径压缩】Happy Tree Party 除上两点间路径全部权值
- CodeForces Gym 100646D Party Party Party
- LCA + 并查集 Happy Tree Party codeforces593D
- codeforces D. Distance in Tree
- CVPR 2017论文:基于网格的运动统计,用于快速、超鲁棒的特征匹配(附大神解读)
- JavaScript中typeof的使用
- sql注入原理及解决方案
- Android通用流行框架大全
- ScrollView中Spinner问题
- Codeforces 593D-Happy Tree Party
- react用PropTypes检测类型
- 170914_算法导论学习(二)_2.2 分析算法
- DFS:257. Binary Tree Paths
- 运维自动化之ansible的安装与使用
- javamail 邮件发送
- eclipse 搭建Android开发环境 安装ADT 24.0.2
- C++友元函数访问类的私有成员
- android adb connnect 连接失败问题