Codeforces Round #329 (Div. 2)

D. Happy Tree Party

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Bogdan has a birthday today and mom gave him a tree consisting of n vertecies. For every edge of the tree i, some number x**i 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 y**i, and two vertecies a**i and b**i. After that, he moves along the edges of the tree from vertex a**i to vertex b**i using 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 y**i by , that is, by the result of integer division y**i div x**j.
  2. Chooses some edge p**i and replaces the value written in it xpi by some positive integer c**i < 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 y**i for each i of the first type.

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 u**i, v**i and x**i (1 ≤ u**i, v**i ≤ n, u**i ≠ v**i, 1 ≤ x**i ≤ 1018), denoting an edge that connects vertecies u**i and v**i, with the number x**i 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 a**i b**i y**i corresponds to a guest, who chooses the operation of the first type.
  • 2 p**i c**i corresponds to a guests, who chooses the operation of the second type.

It is guaranteed that all the queries are correct, namely ,1 ≤ p**i ≤ n - 1 , wherexpirepresents a number written on edgep**iat this particular moment of time that is not necessarily equal to the initial valuexpi

Output

For each guest who chooses the operation of the first type, print the result of processing the value y**i through the path from a**i to b**i.

Examples

input

Copy

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

output

Copy

  1. 24203

input

Copy

  1. 5 41 2 71 3 33 4 23 5 51 4 2 1001 5 4 12 2 21 1 3 4

output

Copy

  1. 202

Note

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

题意:

可以去这个链接阅读中文题意:

https://vjudge.net/problem/CodeForces-593D#author=AwayWithCorrect

思路:

  • 1:边权转为点权建树:

    确定一个root后,在dfs过程中,把边权值放在深度较大的节点的点权上。

    这样建树的话,询问路径\(x->y\)的时候的边权sum和,实际求的过程中,点权只需要计算从(x到y路径的中的下一个节点z)到y节点的点权sum和。因为x的点权是root到x中的x上方的边权,并不在x到y的路径中。

  • 2:树链剖分,同时用线段树维护连续点权的累乘积。

  • 3:当线段树中的一个线段权值>1e18后,给该线段加个标记,或者权值定为0,因为询问是<=1e18的,那么如果询问包括了这个权值,答案一定是0.

  • 4:更新边的权值时,直接用线段树更新那条边中深度更大的点权即可。

  • 5:在树链剖分询问路径的过程中,别忘记1中讲到了去掉x节点点权,可以直接在最后的一个query中把id[x](x的dfs序)改为id[x]+1,因为一条链中dfs序时连续的。

判定x*y是否会超过1e18可以用这个函数的写法来求:

  1. long long mul(long long aaa,long long bbb)
  2. {
  3. if(aaa==0||bbb==0)
  4. return 0;
  5. if(INF/aaa<bbb)
  6. {
  7. return 0;
  8. }
  9. else
  10. return aaa*bbb;
  11. }

AC代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <iomanip>
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define sz(a) int(a.size())
  14. #define all(a) a.begin(), a.end()
  15. #define rep(i,x,n) for(int i=x;i<n;i++)
  16. #define repd(i,x,n) for(int i=x;i<=n;i++)
  17. #define pii pair<int,int>
  18. #define pll pair<long long ,long long>
  19. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  20. #define MS0(X) memset((X), 0, sizeof((X)))
  21. #define MSC0(X) memset((X), '\0', sizeof((X)))
  22. #define pb push_back
  23. #define mp make_pair
  24. #define fi first
  25. #define se second
  26. #define eps 1e-6
  27. #define gg(x) getInt(&x)
  28. #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
  29. using namespace std;
  30. typedef long long ll;
  31. ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
  32. ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
  33. ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
  34. inline void getInt(int* p);
  35. const int maxn = 600010;
  36. const int inf = 0x3f3f3f3f;
  37. /*** TEMPLATE CODE * * STARTS HERE ***/
  38. int n, m;
  39. int root;
  40. ll a[maxn];// 初始点权
  41. ll wt[maxn];// 新建编号点权。
  42. int cnt;// 编号用的变量
  43. int top[maxn];// 所在重链的顶点编号
  44. int id[maxn];//节点的新编号。
  45. typedef pair<int, ll> pil;
  46. std::vector<pil> son[maxn];
  47. int SZ[maxn];// 子数大小
  48. int wson[maxn];// 重儿子
  49. int fa[maxn];// 父节点
  50. int dep[maxn];// 节点的深度
  51. void dfs1(int id, int pre, int step) // 维护出sz,wson,fa,dep
  52. {
  53. dep[id] = step;
  54. fa[id] = pre;
  55. SZ[id] = 1;
  56. int maxson = -1;
  57. for (auto x : son[id])
  58. {
  59. if (x.fi != pre)
  60. {
  61. a[x.fi] = x.se;
  62. dfs1(x.fi, id, step + 1);
  63. SZ[id] += SZ[x.fi];
  64. if (SZ[x.fi] > maxson)
  65. {
  66. maxson = SZ[x.fi];
  67. wson[id] = x.fi;
  68. }
  69. }
  70. }
  71. }
  72. //处理出top[],wt[],id[]
  73. void dfs2(int u, int topf)
  74. {
  75. id[u] = ++cnt;
  76. wt[cnt] = a[u];
  77. top[u] = topf;
  78. if (!wson[u]) // 没儿子时直接结束
  79. {
  80. return ;
  81. }
  82. dfs2(wson[u], topf); // 先处理重儿子
  83. for (auto x : son[u])
  84. {
  85. if (x.fi == wson[u] || x.fi == fa[u]) //只处理轻儿子
  86. {
  87. continue;
  88. }
  89. dfs2(x.fi, x.fi); // 每个轻儿子以自己为top
  90. }
  91. }
  92. struct node
  93. {
  94. int l, r;
  95. ll sum;
  96. } segment_tree[maxn << 2];
  97. int getwei(ll x)
  98. {
  99. int res = 0;
  100. while (x)
  101. {
  102. res++;
  103. x /= 10;
  104. }
  105. return res;
  106. }
  107. ll num_1e18 = 1e18;
  108. ll getcheng(ll v1, ll v2)
  109. {
  110. if (v1 == 0ll || v2 == 0ll)
  111. {
  112. return 0ll;
  113. }
  114. int x1 = getwei(v1);
  115. int x2 = getwei(v2);
  116. ll res;
  117. if (x1 + x2 > 20)
  118. {
  119. res = 0ll;
  120. } else if (x1 + x2 == 20 && num_1e18 / v2 == v2)
  121. {
  122. res = 0ll;
  123. } else
  124. {
  125. res = (v1 * v2);
  126. }
  127. return res;
  128. }
  129. void pushup(int rt)
  130. {
  131. segment_tree[rt].sum = getcheng(segment_tree[rt << 1].sum, segment_tree[rt << 1 | 1].sum);
  132. }
  133. void build(int rt, int l, int r)
  134. {
  135. segment_tree[rt].l = l;
  136. segment_tree[rt].r = r;
  137. if (l == r)
  138. {
  139. segment_tree[rt].sum = wt[l];
  140. return;
  141. }
  142. int mid = (l + r) >> 1;
  143. build(rt << 1, l, mid);
  144. build(rt << 1 | 1, mid + 1, r);
  145. pushup(rt);
  146. }
  147. void update(int rt, int pos, ll val)
  148. {
  149. if (segment_tree[rt].l == pos && segment_tree[rt].r == pos)
  150. {
  151. segment_tree[rt].sum = min(segment_tree[rt].sum, val);
  152. return ;
  153. }
  154. int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
  155. if (mid >= pos)
  156. {
  157. update(rt << 1, pos, val);
  158. }
  159. if (mid < pos)
  160. {
  161. update(rt << 1 | 1, pos, val);
  162. }
  163. pushup(rt);
  164. }
  165. ll query(int rt, int l, int r)
  166. {
  167. if (l > r)
  168. {
  169. return 1ll;
  170. }
  171. if (segment_tree[rt].l >= l && segment_tree[rt].r <= r)
  172. {
  173. return segment_tree[rt].sum;
  174. }
  175. int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
  176. ll res = 1ll;
  177. if (mid >= l)
  178. {
  179. res = getcheng(res, query(rt << 1, l, r));
  180. }
  181. if (mid < r)
  182. {
  183. res = getcheng(res, query(rt << 1 | 1, l, r));
  184. }
  185. return res;
  186. }
  187. void uprange(int x, int y, ll k)
  188. {
  189. if (dep[x] < dep[y]) // 使x的top深度较大
  190. {
  191. swap(x, y);
  192. }
  193. update(1, id[x], k);
  194. }
  195. ll qrange(int x, int y)
  196. {
  197. ll ans = 1ll;
  198. while (top[x] != top[y])
  199. {
  200. if (dep[top[x]] < dep[top[y]])
  201. {
  202. swap(x, y);
  203. }
  204. ans = getcheng(ans, query(1, id[top[x]], id[x]));
  205. x = fa[top[x]];
  206. }
  207. if (dep[x] > dep[y])
  208. swap(x, y);
  209. ans = getcheng(ans, query(1, id[x] + 1, id[y]));
  210. return ans;
  211. }
  212. pii info[maxn];
  213. int main()
  214. {
  215. // freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
  216. // freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
  217. gbtb;
  218. // chu(getwei(1e9));
  219. cin >> n >> m;
  220. root = 1;
  221. int u, v;
  222. ll val;
  223. repd(i, 1, n - 1)
  224. {
  225. cin >> u >> v >> val;
  226. son[u].pb(mp(v, val));
  227. son[v].pb(mp(u, val));
  228. info[i] = mp(u, v);
  229. }
  230. dfs1(root, 0, 1);
  231. dfs2(root, root);
  232. build(1, 1, n);
  233. int op, x, y;
  234. ll z;
  235. int isok = 0;
  236. if (info[1].fi == 7610 && info[1].se == 132654)
  237. {
  238. isok = 1;
  239. }
  240. while (m--)
  241. {
  242. cin >> op;
  243. if (op == 1)
  244. {
  245. cin >> x >> y >> z;
  246. val = qrange(x, y);
  247. // if (isok)
  248. // {
  249. // cout << " 1: " << val << endl;
  250. // }
  251. if (val == 0)
  252. {
  253. cout << val << endl;
  254. } else
  255. {
  256. cout << z / val << endl;
  257. }
  258. } else if (op == 2)
  259. {
  260. cin >> x >> z;
  261. uprange(info[x].fi, info[x].se, z);
  262. }
  263. }
  264. return 0;
  265. }
  266. inline void getInt(int* p) {
  267. char ch;
  268. do {
  269. ch = getchar();
  270. } while (ch == ' ' || ch == '\n');
  271. if (ch == '-') {
  272. *p = -(getchar() - '0');
  273. while ((ch = getchar()) >= '0' && ch <= '9') {
  274. *p = *p * 10 - ch + '0';
  275. }
  276. }
  277. else {
  278. *p = ch - '0';
  279. while ((ch = getchar()) >= '0' && ch <= '9') {
  280. *p = *p * 10 + ch - '0';
  281. }
  282. }
  283. }
  284.  

D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】的更多相关文章

  1. 4.12 省选模拟赛 LCA on tree 树链剖分 树状数组 分析答案变化量

    LINK:duoxiao OJ LCA on Tree 题目: 一道树链剖分+树状数组的神题. (直接nQ的暴力有50. 其实对于树随机的时候不难想到一个算法 对于x的修改 暴力修改到根. 对于儿子的 ...

  2. hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...

  3. Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组

    Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...

  4. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释

    P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...

  5. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  6. Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  7. Codeforces Round #425 (Div. 2) D 树链剖分 + 树状数组维护区间

    一看就知道 可以LCA判断做 也可以树链剖分拿头暴力 然而快速读入和线段树维护区间会T70 于是只能LCA? 线段树的常数不小 于是需要另外一种办法来进行区间加减和查询区间和 就是使用树状数组 这个题 ...

  8. dsu+树链剖分+树分治

    dsu,对于无修改子树信息查询,并且操作支持undo的问题 暴力dfs,对于每个节点,对所有轻儿子dfs下去,然后再消除轻儿子的影响 dfs重儿子,然后dfs暴力恢复轻儿子们的影响,再把当前节点影响算 ...

  9. HDU 3966 /// 树链剖分+树状数组

    题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...

  10. 7.18 NOI模拟赛 树论 线段树 树链剖分 树的直径的中心 SG函数 换根

    LINK:树论 不愧是我认识的出题人 出的题就是牛掰 == 他好像不认识我 考试的时候 只会写42 还有两个subtask写挂了 拿了37 确实两个subtask合起来只有5分的好成绩 父亲能转移到自 ...

随机推荐

  1. python-Web-flask-视图内容和模板

    2 视图内容和模板: 基本使用 #设置cookie值 @app.route('/set_cookie') def set_cookie(): response = make_response(&quo ...

  2. 【Qt开发】【Gstreamer开发】Qt error: glibconfig.h: No such file or directory #include

    今天遇到一个问题如题 但是明明安装了 glib2.0和gtk,但是仍然找不到glibconfig.h,自己在/usr/include下找来也确实没有,然后只能在全盘搜啦 位置在: /usr/lib/x ...

  3. [学习笔记] 在Eclips 中导出项目

    有时候需要将自己完成的项目分享给别人,可以按如下步骤操作: 选择要导出的内容,设置导出的文件名. 然后点击,Finish 即可. 然后到d:\tmp 会看到文件:Hibernat_demo_001.z ...

  4. Mybatis插件之Mybatis-Plus的CRUD方法

    使用Mybatis-plus进行基本的CRUD(增查改删)操作. 实体类(User)代码: import com.baomidou.mybatisplus.annotation.IdType; imp ...

  5. [Agc028A]Two Abbreviations_数学

    Two Abbreviations 题目链接:https://atcoder.jp/contests/agc028/tasks/agc028_a 数据范围:略. 题解: 题目中的位置非常不利于思考,我 ...

  6. SQL SERVER CONVERT函数

    定义: CONVERT函数返回 转换了数据类型的数据. 语法: CONVERT(target_type,expression,date_style smallint) 参数: ①target_type ...

  7. java源码--ArrayList

    1.1.ArrayList概述 1)ArrayList是可以动态增长和缩减的索引序列,它是基于数组实现的List类. 2)该类封装了一个动态再分配的Object[]数组,每一个类对象都有一个capac ...

  8. Python之random.seed()用法

    import random # 随机数不一样 random.seed() print('随机数1:',random.random()) random.seed() print('随机数2:',rand ...

  9. CCF - CCSP 2018-01 绝地求生 BFS

    BFS从安全地区方向搞一下就好了 1.还是注意每回合清空 2.posx居然开小了,可不能犯这种错误 3.地图用a和节点的dis重名了,建议其他变量禁止用a命名 4.在输入数据之前continue了,这 ...

  10. hdu 2189还是dp..

    题目的意思比较简单,类似计数dp. 一开始我想让dp[i]+=dp[i-prime] 每次遍历比i小的所有素数,然后发现有重叠的 比如 2+3 3+2 就导致错误.看了其他人的填充方式,发现定下pri ...