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/problem/D
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:
- 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 bi 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 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.
⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.
⋅2. Ignoring the buoys and relying on dogfighting to get point.
If you and your opponent meet in the same position, you can try to
fight with your opponent to score one point. For the proposal of game
balance, two players are not allowed to fight before buoy #2 is touched by anybody.
There are three types of players.
Speeder:
As a player specializing in high speed movement, he/she tries to avoid
dogfighting while attempting to gain points by touching buoys.
Fighter:
As a player specializing in dogfighting, he/she always tries to fight
with the opponent to score points. Since a fighter is slower than a
speeder, it's difficult for him/her to score points by touching buoys
when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.
There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.
Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting.
Since Asuka is slower than Shion, she decides to fight with Shion for
only one time during the match. It is also assumed that if Asuka and
Shion touch the buoy in the same time, the point will be given to Asuka
and Asuka could also fight with Shion at the buoy. We assume that in
such scenario, the dogfighting must happen after the buoy is touched by
Asuka or Shion.
The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?
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 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 xpi represents 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.
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
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
Sample Output
2
4
20
3
HINT
题意
给你一颗树,然后有两个操作
1 x y z,假设x到y的链上边权分别为x1,x2,x3....,那么输出 [[[z/x1]/x2]/x3]
2 x y,把第x条边的边权改为y
题解:
有一个结论 [[[z/x1]/x2]/x3] = [z/(x1*x2*x3)]
那么我们只要维护区间乘积就好了,树链剖分裸题
但是区间乘积很容易爆LL,那么我们就用一个log来判断,如果爆了ll,那就直接把他置为1e18
然后答案直接输出0就好了
代码
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
const int N=;
const int INF=<<; int n,tim; long long num[N];
int siz[N],top[N],son[N];
int dep[N],tid[N],Rank[N],fa[N];
int head[N],to[*N],Next[*N],w[*N],edge;
int flag = ;
struct Edge
{
int u,v;
long long c;
};
Edge tmp[*N]; void Init()
{
memset(head,-,sizeof(head));
memset(son,-,sizeof(son));
tim=;
edge=;
} void addedge(int u,int v,int c)
{
to[edge]=v,w[edge]=c,Next[edge]=head[u],head[u]=edge++;
to[edge]=u,w[edge]=c,Next[edge]=head[v],head[v]=edge++;
} //树链剖分部分
void dfs1(int u,int father,int d)
{
dep[u]=d;
fa[u]=father;
siz[u]=;
for(int i=head[u]; ~i; i=Next[i])
{
int v=to[i];
if(v!=father)
{
dfs1(v,u,d+);
siz[u]+=siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u]=v;
}
}
} void dfs2(int u,int tp)
{
top[u]=tp;
tid[u]=++tim;
Rank[tid[u]]=u;
if(son[u]==-) return;
dfs2(son[u],tp);
for(int i=head[u]; ~i; i=Next[i])
{
int v=to[i];
if(v!=son[u]&&v!=fa[u])
dfs2(v,v);
}
} //线段树部分
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const long long kkk = 1e18+;
long long MAX[*N]; void PushUP(int rt)
{
if(MAX[rt<<]==)
MAX[rt]=;
if(MAX[rt<<|]==)
MAX[rt<<|]=;
double ppp1 = MAX[rt<<];
double ppp2 = MAX[rt<<|];
if(log(ppp1) + log(ppp2) > log(kkk * 1.0))
MAX[rt]=kkk+;
else
MAX[rt]=MAX[rt<<]*MAX[rt<<|];
} void Build(int l,int r,int rt)
{
if(l==r)
{
MAX[rt]=num[l];
return;
}
int mid=(l+r)>>;
Build(lson);
Build(rson);
PushUP(rt);
} void Update(int l,int r,int rt,int p,long long val)
{
if(l==r)
{
MAX[rt]=val;
return;
}
int mid=(l+r)>>;
if(p<=mid)
Update(lson,p,val);
else
Update(rson,p,val);
PushUP(rt);
} long long Query(int l,int r,int rt,int L,int R)
{
if(L<=l&&R>=r)
return MAX[rt];
int mid=(l+r)>>;
long long ans=;
if(L<=mid)
{
long long kkkk = Query(lson,L,R);
if(kkkk==-)
flag = ;
if(log(ans*1.0) + log(kkkk*1.0) > log(kkk*1.0))
ans = kkk+;
else
ans*=kkkk;
}
if(R>mid)
{
long long kkkk = Query(rson,L,R);
if(kkkk==-)
flag = ;
if(log(ans*1.0) + log(kkkk*1.0) > log(kkk*1.0))
ans = kkk+;
else
ans*=kkkk;
}
return ans;
} void Change(int x,long long val)
{
if(dep[tmp[x].u]>dep[tmp[x].v])
Update(,n,,tid[tmp[x].u],val);
else
Update(,n,,tid[tmp[x].v],val);
} long long query(int x,int y)
{
long long ans=;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
long long kkkk = Query(,n,,tid[top[x]],tid[x]);
if(log(ans*1.0) + log(kkkk*1.0) > log(kkk*1.0))
ans = kkk+;
else
ans*=kkkk;
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
if(x!=y)
{
long long kkkk =Query(,n,,tid[x]+,tid[y]);
if(log(ans*1.0) + log(kkkk*1.0) > log(kkk*1.0))
ans = kkk+;
else
ans*=kkkk;
}
return ans;
}
int MM;
int main()
{
char oper[];
int a,b;
long long c;
Init();
scanf("%d%d",&n,&MM);
for(int i=; i<n; i++)
{
scanf("%d%d%lld",&a,&b,&c);
tmp[i].u=a;tmp[i].v=b;tmp[i].c=c;
addedge(a,b,c);
}
dfs1(,,);
dfs2(,);
//用边的孩子节点来表示该边
for(int i=;i<n;i++)
{
if(dep[tmp[i].u]>dep[tmp[i].v])
num[tid[tmp[i].u]]=tmp[i].c;
else
num[tid[tmp[i].v]]=tmp[i].c;
}
Build(,n,);
while(MM--)
{
scanf("%s",oper);
if(oper[]=='')
{
long long ppp;
scanf("%d%d",&a,&b);
scanf("%lld",&ppp);
flag = ;
long long dddd = query(a,b);
printf("%lld\n",ppp/dddd);
}
else
{
long long ppp;
scanf("%d%lld",&a,&ppp);
Change(a,ppp);
}
}
return ;
}
Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分的更多相关文章
- Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分
D. Happy Tree Party Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party(LCA+并查集)
题目链接 题意:就是给你一颗这样的树,用一个$y$来除以两点之间每条边的权值,比如$3->7$,问最后的y的是多少,修改操作是把权值变成更小的. 这个$(y<=10^{18})$除的权值如 ...
- 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 #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 ...
- 【POJ3237】Tree(树链剖分)
题意:在一棵N个节点,有边权的树上维护以下操作: 1:单边修改,将第X条边的边权修改成Y 2:区间取反,将点X与Y在树上路径中的所有边边权取反 3:区间询问最大值,询问X到Y树上路径中边权最大值 n& ...
- HDU 4718 The LCIS on the Tree(树链剖分)
Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i < ...
- POJ 3237:Tree(树链剖分)
http://poj.org/problem?id=3237 题意:树链剖分.操作有三种:改变一条边的边权,将 a 到 b 的每条边的边权都翻转(即 w[i] = -w[i]),询问 a 到 b 的最 ...
随机推荐
- 一天一个Java基础——对象和类
1.在Java中你所做的全部工作就是定义类,产生那些类的对象,以及发送消息给这些对象 2.可以在类中设置两种类型的元素:字段(也被称作数据成员)和方法(也被称作成员函数) 3.字段可以是任何类型的对象 ...
- Java基础——I/O续
目录 二进制I/O类 文件导航和I/O 二进制I/O类 FileInputStream类和FileOutputStream类 *FileOutputStream(file: File) *FileOu ...
- 【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)
[概念] 转载连接:树状数组 讲的挺好. 这两题非常的相似,查询区间的累加和.更新结点.Add(x,d) 与 Query(L,R) 的操作 [题目链接:candy] 唉,也是现在才发现这题用了这个知识 ...
- Javascript Utils.js
\ var Utils ={//字符串格式化StringFormat: function () {if (arguments.length == 0)return null;var str = arg ...
- How to Download APK Files from Google Play Store
Evozi, an Android app developer, offers a one-click online APK download app that lets you download t ...
- java web 学习七(HttpServletResponse对象1)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- Android ViewPager欢迎页+引导页+进入首页
import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences ...
- [视频监控]用状态机图展示Layout切换关系
监控系统通常会提供多种Layout给用户,用于满足不同需求,如:高清显示单路视频或者同时观察多路监控情况. 文中系统只提供了单路.2x2(2行2列共4路).8路(4行4列布局,从左上角算起,有个核心显 ...
- Mondriaan's Dream(POJ 2411状态压缩dp)
题意:用1*2的方格填充m*n的方格不能重叠,问有多少种填充方法 分析:dp[i][j]表示i行状态为j时的方案数,对于j,0表示该列竖放(影响下一行的该列),1表示横放成功(影响下一列)或上一列竖放 ...
- 《Python基础教程(第二版)》学习笔记 -> 第十一章 文件和素材
打开文件 open函数用来打开文件,语句如下: open(name[,mode[,buffering]]) open函数使用一个文件名作为唯一的强制参数,然后后返回一个文件对象.模式(mode)和缓冲 ...