Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分
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.
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 in the order they appear in the input.
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 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
2
4
20
3
2 x y,把第x条边的边权改为y
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 5e5+, inf = 2e9, mod = ; LL c[N];
int head[N],t = , fv[N], f[N], deep[N], n, m, pa[N];
struct edge{int to,next,id;}e[N * ];
struct Line {
int x,y;
LL z;
Line(int x = , int y = , int z = ) : x (x), y (y), z (z) {}
}L[N];
void add(int u,int v,int id) {e[t].next = head[u];e[t].to = v;e[t].id = id; head[u] = t++; } int finds(int x) {return x == pa[x]? pa[x]:pa[x] = finds(pa[x]);} void update(int u,int to) {
fv[to] = fv[u];
f[to] = f[u];
}
void dfs(int u,int fa) {
deep[u] = deep[fa] + ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == fa) continue;
fv[to] = e[i].id;
f[to] = u;
if(c[e[i].id] == ) {
pa[to] = finds(u);
update(u,to);
}
dfs(to,u);
}
} LL Lca(int u,int v,LL res) {
u = finds(u);
v = finds(v);
while(u != v) {
if(deep[u] < deep[v]) swap(u,v);
// cout<<deep[u]<<" "<<deep[v]<<" "<<fv[u]<<" "<<c[fv[u]]<<endl;
res /= c[fv[u]];
if(res == ) return res;
u = finds(f[u]);
}
return res;
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ; i <= n; ++i) pa[i] = i;
for(int i = ; i < n; ++i) {
int a,b;
scanf("%d%d%I64d",&a,&b,&c[i]);
add(a,b,i);add(b,a,i);
L[i] = Line(a,b,c[i]);
} c[] = ;
fv[] = ;
f[] = ; dfs(,);
while(m --) {
int op, x;
LL z,y;
scanf("%d%d%I64d",&op,&x,&y);
if(op == ) {
scanf("%I64d",&z);
LL res = Lca(x,y,z);
printf("%I64d\n",res);
}
else {
c[x] = y;
int u = L[x].x;
int v = L[x].y;
if(deep[u] < deep[v]) swap(u,v);
if(c[x] == ) {
pa[u] = finds(v);
update(v,u);
}
}
}
}
树链剖分
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define root 1,2,n
#define lson ls , ll , mid
#define rson rs , mid + 1 , rr #define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+200LL;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 5e5+, inf = 2e9, mod = ; LL sum[N << ],val[N << ];
int head[N], t = , tot, deep[N], top[N], f[N], siz[N], son[N], pos[N << ];
int n,m;
struct edge{int to,next;LL value;}e[N * ];
struct Line{
int x,y;LL z;
Line(int x = , int y = , LL z = ) : x(x), y(y), z(z) {}
}L[N];
void add(int u,int v) {e[t].next=head[u];e[t].to=v;head[u]=t++;}
void dfs1(int u,int fa) {
siz[u] = ;son[u] = ;
deep[u] = deep[fa] + ;
f[u] = fa;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == fa) continue;
dfs1(to,u);
siz[u] += siz[to];
if(siz[to] > siz[son[u]]) son[u] = to;
}
}
void dfs2(int u,int chan) {
top[u] = son[chan] == u? top[chan] : u;
pos[u] = ++tot;
if(son[u]) dfs2(son[u],u);
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == son[u] || to == chan) continue;
dfs2(to,u);
}
}
void push_up(int i) {
if(log(sum[ls]*1.0) + log(sum[rs] * 1.0) > log(INF * 1.0))
sum[i] = INF;
else sum[i] = sum[ls] * sum[rs];
}
void build(int i,int ll,int rr) {
if(ll == rr) {
sum[i] = val[ll];
return ;
}
build(lson), build(rson);
push_up(i);
}
void update(int i,int ll,int rr,int x,LL c)
{
if(ll == x && rr == x) {
sum[i] = c;
return ;
}
if(x <= mid) update(lson,x,c);
else update(rson,x,c);
push_up(i);
} LL query(int i,int ll,int rr,int x,int y) {
if(ll == x && rr == y) return sum[i];
if(y <= mid) return query(lson,x,y);
else if(x > mid) return query(rson,x,y);
else {
LL fi = query(lson,x,mid);
LL se = query(rson,mid+,y);
if(log(fi*1.0) + log(se * 1.0) > log(INF * 1.0)) {
return INF;
}
else return fi*se;
}
} LL sub_query(int x,int y,LL res) {
while(top[x] != top[y]) {
if(deep[top[x]] < deep[top[y]]) swap(x,y);
// cout<<pos[top[x]]<<" "<<pos[x]<<endl;
res /= query(root,pos[top[x]],pos[x]);
if(res == ) return ;
x = f[top[x]];
}
if(x == y) return res;
if(deep[x] < deep[y]) swap(x,y);
// cout<<query(root,pos[y]+1,pos[x])<<endl;
return res / query(root,pos[y]+,pos[x]);
} int main () {
scanf("%d%d",&n,&m);
for(int i = ; i <= n-; ++i) {
int a,b;
LL c;
scanf("%d%d%I64d",&a,&b,&c);
add(a,b);
add(b,a);
L[i] = Line(a,b,c);
}
dfs1(,);
dfs2(,);
for(int i = ; i < n; ++i) {
if(deep[L[i].x] < deep[L[i].y]) swap(L[i].x,L[i].y);
val[pos[L[i].x]] = L[i].z;
}
build(root);
while(m--) {
int op,x;
LL y,z;
scanf("%d%d%I64d",&op,&x,&y);
if(op == ) {
scanf("%I64d",&z);
printf("%I64d\n",sub_query(x,y,z));
}else {
update(root,pos[L[x].x],y);
}
}
}
Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分的更多相关文章
- Codeforces Round #329 (Div. 2) D. Happy Tree Party(LCA+并查集)
题目链接 题意:就是给你一颗这样的树,用一个$y$来除以两点之间每条边的权值,比如$3->7$,问最后的y的是多少,修改操作是把权值变成更小的. 这个$(y<=10^{18})$除的权值如 ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式)
Count on a tree SPOJ 10628 主席树+LCA(树链剖分实现)(两种存图方式) 题外话,这是我第40篇随笔,纪念一下.<( ̄︶ ̄)↗[GO!] 题意 是说有棵树,每个节点上 ...
- Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序
Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...
- Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树
D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #200 (Div. 1)D. Water Tree
简单的树链剖分+线段树 #include<bits\stdc++.h> using namespace std; #define pb push_back #define lson roo ...
- CF 504E Misha and LCP on Tree——后缀数组+树链剖分
题目:http://codeforces.com/contest/504/problem/E 树链剖分,把重链都接起来,且把每条重链的另一种方向的也都接上,在这个 2*n 的序列上跑后缀数组. 对于询 ...
随机推荐
- struts2 配置 struts.xml 提示
1.这个提示通常是在 连网络的时候才可以看到 2.当没有网路的时候我们该如何配置呢? window -->preferences -->xml catelog -->user.... ...
- Unity3d NavMesh获得地面高度
UnityPro内置的NavMesh有几个API很有用 NavMesh.SamplePosition 根据给的点进行采样,可传入最大距离,返回true说明采样到了点,否则采样失败(可以用来获得地形高度 ...
- Linux下如何移除同时在线的用户
Linux下移除同时在线的用户太多时,shell操作会变得比较卡,很多时候经常是直接关闭终端导致不正常退出,一般要等上一段时间才会退出,这个时候主动结束用户进程使用户下线是比较好的方式,方法如下: 使 ...
- [转]Handler MessageQueue Looper消息循环原理分析
Handler MessageQueue Looper消息循环原理分析 Handler概述 Handler在Android开发中非常重要,最常见的使用场景就是在子线程需要更新UI,用Handler ...
- FZU 2165 v11(最小重复覆盖)+ codeforces 417D Cunning Gena
告诉你若干个(<=100)武器的花费以及武器能消灭的怪物编号,问消灭所有怪物(<=100)的最小花费...当然每个武器可以无限次使用,不然这题就太水了╮(╯▽╰)╭ 这题当时比赛的时候连题 ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode】Two Sum (easy)
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...
- php策略模式的研究
<?php abstract class Moshi{ private $num; public $price; const Ted=1; const Sed=2 ...
- August 22nd 2016 Week 35th Monday
Have you ever given any thought to your future? 你有没有为将来打算过呢? Have you ever given any thought to your ...