D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】
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:
- 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. - 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
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
24203
input
Copy
5 41 2 71 3 33 4 23 5 51 4 2 1001 5 4 12 2 21 1 3 4
output
Copy
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可以用这个函数的写法来求:
long long mul(long long aaa,long long bbb)
{
if(aaa==0||bbb==0)
return 0;
if(INF/aaa<bbb)
{
return 0;
}
else
return aaa*bbb;
}
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
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;}
inline void getInt(int* p);
const int maxn = 600010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n, m;
int root;
ll a[maxn];// 初始点权
ll wt[maxn];// 新建编号点权。
int cnt;// 编号用的变量
int top[maxn];// 所在重链的顶点编号
int id[maxn];//节点的新编号。
typedef pair<int, ll> pil;
std::vector<pil> son[maxn];
int SZ[maxn];// 子数大小
int wson[maxn];// 重儿子
int fa[maxn];// 父节点
int dep[maxn];// 节点的深度
void dfs1(int id, int pre, int step) // 维护出sz,wson,fa,dep
{
dep[id] = step;
fa[id] = pre;
SZ[id] = 1;
int maxson = -1;
for (auto x : son[id])
{
if (x.fi != pre)
{
a[x.fi] = x.se;
dfs1(x.fi, id, step + 1);
SZ[id] += SZ[x.fi];
if (SZ[x.fi] > maxson)
{
maxson = SZ[x.fi];
wson[id] = x.fi;
}
}
}
}
//处理出top[],wt[],id[]
void dfs2(int u, int topf)
{
id[u] = ++cnt;
wt[cnt] = a[u];
top[u] = topf;
if (!wson[u]) // 没儿子时直接结束
{
return ;
}
dfs2(wson[u], topf); // 先处理重儿子
for (auto x : son[u])
{
if (x.fi == wson[u] || x.fi == fa[u]) //只处理轻儿子
{
continue;
}
dfs2(x.fi, x.fi); // 每个轻儿子以自己为top
}
}
struct node
{
int l, r;
ll sum;
} segment_tree[maxn << 2];
int getwei(ll x)
{
int res = 0;
while (x)
{
res++;
x /= 10;
}
return res;
}
ll num_1e18 = 1e18;
ll getcheng(ll v1, ll v2)
{
if (v1 == 0ll || v2 == 0ll)
{
return 0ll;
}
int x1 = getwei(v1);
int x2 = getwei(v2);
ll res;
if (x1 + x2 > 20)
{
res = 0ll;
} else if (x1 + x2 == 20 && num_1e18 / v2 == v2)
{
res = 0ll;
} else
{
res = (v1 * v2);
}
return res;
}
void pushup(int rt)
{
segment_tree[rt].sum = getcheng(segment_tree[rt << 1].sum, segment_tree[rt << 1 | 1].sum);
}
void build(int rt, int l, int r)
{
segment_tree[rt].l = l;
segment_tree[rt].r = r;
if (l == r)
{
segment_tree[rt].sum = wt[l];
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void update(int rt, int pos, ll val)
{
if (segment_tree[rt].l == pos && segment_tree[rt].r == pos)
{
segment_tree[rt].sum = min(segment_tree[rt].sum, val);
return ;
}
int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
if (mid >= pos)
{
update(rt << 1, pos, val);
}
if (mid < pos)
{
update(rt << 1 | 1, pos, val);
}
pushup(rt);
}
ll query(int rt, int l, int r)
{
if (l > r)
{
return 1ll;
}
if (segment_tree[rt].l >= l && segment_tree[rt].r <= r)
{
return segment_tree[rt].sum;
}
int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
ll res = 1ll;
if (mid >= l)
{
res = getcheng(res, query(rt << 1, l, r));
}
if (mid < r)
{
res = getcheng(res, query(rt << 1 | 1, l, r));
}
return res;
}
void uprange(int x, int y, ll k)
{
if (dep[x] < dep[y]) // 使x的top深度较大
{
swap(x, y);
}
update(1, id[x], k);
}
ll qrange(int x, int y)
{
ll ans = 1ll;
while (top[x] != top[y])
{
if (dep[top[x]] < dep[top[y]])
{
swap(x, y);
}
ans = getcheng(ans, query(1, id[top[x]], id[x]));
x = fa[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
ans = getcheng(ans, query(1, id[x] + 1, id[y]));
return ans;
}
pii info[maxn];
int main()
{
// freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
// freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
// chu(getwei(1e9));
cin >> n >> m;
root = 1;
int u, v;
ll val;
repd(i, 1, n - 1)
{
cin >> u >> v >> val;
son[u].pb(mp(v, val));
son[v].pb(mp(u, val));
info[i] = mp(u, v);
}
dfs1(root, 0, 1);
dfs2(root, root);
build(1, 1, n);
int op, x, y;
ll z;
int isok = 0;
if (info[1].fi == 7610 && info[1].se == 132654)
{
isok = 1;
}
while (m--)
{
cin >> op;
if (op == 1)
{
cin >> x >> y >> z;
val = qrange(x, y);
// if (isok)
// {
// cout << " 1: " << val << endl;
// }
if (val == 0)
{
cout << val << endl;
} else
{
cout << z / val << endl;
}
} else if (op == 2)
{
cin >> x >> z;
uprange(info[x].fi, info[x].se, z);
}
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】的更多相关文章
- 4.12 省选模拟赛 LCA on tree 树链剖分 树状数组 分析答案变化量
LINK:duoxiao OJ LCA on Tree 题目: 一道树链剖分+树状数组的神题. (直接nQ的暴力有50. 其实对于树随机的时候不难想到一个算法 对于x的修改 暴力修改到根. 对于儿子的 ...
- hdu 3966 Aragorn's Story(树链剖分+树状数组/线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: 给出一棵树,并给定各个点权的值,然后有3种操作: I C1 C2 K: 把C1与C2的路 ...
- Aragorn's Story 树链剖分+线段树 && 树链剖分+树状数组
Aragorn's Story 来源:http://www.fjutacm.com/Problem.jsp?pid=2710来源:http://acm.hdu.edu.cn/showproblem.p ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- 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 ...
- Codeforces Round #425 (Div. 2) D 树链剖分 + 树状数组维护区间
一看就知道 可以LCA判断做 也可以树链剖分拿头暴力 然而快速读入和线段树维护区间会T70 于是只能LCA? 线段树的常数不小 于是需要另外一种办法来进行区间加减和查询区间和 就是使用树状数组 这个题 ...
- dsu+树链剖分+树分治
dsu,对于无修改子树信息查询,并且操作支持undo的问题 暴力dfs,对于每个节点,对所有轻儿子dfs下去,然后再消除轻儿子的影响 dfs重儿子,然后dfs暴力恢复轻儿子们的影响,再把当前节点影响算 ...
- HDU 3966 /// 树链剖分+树状数组
题意: http://acm.hdu.edu.cn/showproblem.php?pid=3966 给一棵树,并给定各个点权的值,然后有3种操作: I x y z : 把x到y的路径上的所有点权值加 ...
- 7.18 NOI模拟赛 树论 线段树 树链剖分 树的直径的中心 SG函数 换根
LINK:树论 不愧是我认识的出题人 出的题就是牛掰 == 他好像不认识我 考试的时候 只会写42 还有两个subtask写挂了 拿了37 确实两个subtask合起来只有5分的好成绩 父亲能转移到自 ...
随机推荐
- whereis which type find
1. whereis name whereis命令只能用于搜索程序名,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 2. wh ...
- NMS(Non-Maximum Suppression) 非极大值抑制
NMS 非极大值抑制:找到局部最大值,并删除邻域内其他的值. 简单说一下流程: 首先剔除背景(背景无需NMS),假设有6个边界框,根据分类置信度对这6个边界框做降序排列,假设顺序为A.B.C.D.E ...
- Java学习笔记-抽象类与接口
抽象类用于在类中不用具体实现,而在子类中去实现的类 抽象类 抽象类概述 抽象定义:抽象就是从多个事物中将共性的,本质的内容抽取出来 抽象类:Java中可以定义没有方法体的方法,该方法的具体实现由子类完 ...
- bootstrap基础学习【网格系统】(三)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 前端ajax中运用post请求和get请求之于session验证
首先我们来看下ajax两种请求的区别: Ajax中POST和GET的区别Get和Post都是向服务器发送的一种请求,只是发送机制不同. 1. GET请求会将参数跟在URL后进行传递,而POST请求则是 ...
- c# 所有类型都是从object继承,那么值类型默认也有装箱吗?
我们知道,c#所有类型都是从System.Object继承,int等值类型也逃脱不了这种命运,那难道值类型默认有装箱操作吗?答案是否,在CLR via这本书中有简短的解释说明: 1.值类型从Syste ...
- SQL SERVER 字符串函数 PATINDEX()
定义: PATINDEX()返回模式在指定表达式中第一次出现的起始位置:如果在所有有效的文本和字符数据类型中都找不到该模式,则返回零. 语法: PATINDEX ( '%pattern%' , exp ...
- 第二章、http协议及嗅探抓包--http协议详解
初识http协议 hypertext trandfer protocol 超文本传输协议,是一种分布式,合作式,多媒体信息系统服务,面向应用层的协议.使用最广泛的应用层协议,基于传输层的TCP协 ...
- nginx反向代理服务器以及负载均衡,从安装到配置
nginx的具体作用不用细说,很强大,做负载均衡.反向代理服务器解决前端跨域问题等等.下面是nginx的安装过程 首先nginx主要的依赖: pcre. pcre-devel zlib zlib-de ...
- LeetCode 866. Prime Palindrome
866. Prime Palindrome(回文素数) 题目: 求出大于或等于 N 的最小回文素数. 回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数. 例如,2,3,5,7 ...