【13.91%】【codeforces 593D】Happy Tree Party
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.
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.
Examples
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
output
2
4
20
3
input
5 4
1 2 7
1 3 3
3 4 2
3 5 5
1 4 2 100
1 5 4 1
2 2 2
1 1 3 4
output
2
0
2
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
【题目链接】:http://codeforces.com/contest/593/problem/D
【题解】
两个点之间的最短路径->LCA;
两个点同时往上走;
在走的过程中除边就可以了(哪条边先除是一样的);
然后如果边的边权大于1,即最少为2;则复杂度是log2n;->nlog2n这是可以接受的;->变成0 就可以直接退出了所以是log2n;
但是就怕数据给你一大段边权全是1的情况。这样你再除一下就可能退化成O(n)了;->n^2
而我们注意到在修改边权的时候那些边权只会变小;所以最后变成1的点肯定不会再增大了;
则我们可以跳过这些点->并查集(除1的话还是不变,不管除几个1都是一样的);
LCA的话一步一步往上走就好,遇到边大于1就直接除;如果等于1就一直往上跳(并查集);
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
const int MAXN = 200000+100;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
int n,m;
int f[MAXN],dep[MAXN];
LL w[MAXN];
vector <pair<int,int> > a[MAXN];
pair<int,int> pre[MAXN];
void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
}
void dfs(int x,int fa)
{
dep[x] = dep[fa]+1;
int len = a[x].size();
for (int i = 0;i <= len-1;i++)
{
int y = a[x][i].first,id = a[x][i].second;
if (fa == y)
continue;
pre[y] = make_pair(x,id);
dfs(y,x);
}
}
int ff(int x)
{
int id = pre[x].second;
if (w[id]!=1)
return x;
if (f[x] == x)
return f[x] = ff(pre[x].first);
return f[x] = ff(f[x]);
}
int up(int x,LL &z)
{
int prex = pre[x].first,id = pre[x].second;
if (w[id] > 1)
{
z/=w[id];
return prex;
}
return f[x] = ff(f[x]);
}
LL solve(int x,int y,LL z)
{
while (z && x!=y)
{
if (dep[x] < dep[y])
swap(x,y);
x = up(x,z);
}
return z;
}
int main()
{
rei(n);rei(m);
for (int i = 1;i <= n-1;i++)
{
int x,y;
rei(x);rei(y);rel(w[i]);
a[x].push_back(make_pair(y,i));
a[y].push_back(make_pair(x,i));
}
dfs(1,0);
for (int i = 1;i <= n;i++)
f[i] = i;
for (int i = 1;i <= m;i++)
{
int op;
rei(op);
if (op == 1)
{
int x,y;
LL z;
rei(x);rei(y);rel(z);
printf("%I64d\n",solve(x,y,z));
}
else
{
int x;LL y;
rei(x);rel(y);
w[x] = y;
}
}
return 0;
}
【13.91%】【codeforces 593D】Happy Tree Party的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 793D】Presents in Bankopolis
[题目链接]:http://codeforces.com/contest/793/problem/D [题意] 给你n个点, 这n个点 从左到右1..n依序排; 然后给你m条有向边; 然后让你从中选出 ...
- 【57.97%】【codeforces Round #380A】Interview with Oleg
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【42.86%】【Codeforces Round #380D】Sea Battle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.78%】【codeforces 625C】K-special Tables
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【41.43%】【codeforces 560C】Gerald's Hexagon
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【24.34%】【codeforces 560D】Equivalent Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- C#调用天气预报网络服务
本程序通过调用网络上公开的天气预报网络服务来显示某个地区三天的天气,使用到的网络服务地址:http://www.webxml.com.cn/WebServices/WeatherWebService. ...
- 硬件——STM32,ADC篇
未完,待续...... 也就是stm32f10X系列的adc采集出来的结果是12位的 stm32f10X系列有两个16位adc 关于程序的编写方法:一般 “某某.c文件”:都是用来设置“某某”的一些 ...
- 低成本开始互联网创业:探讨域名、服务器、CDN、邮箱等节流之道
互联网创业一直是个热门话题,对这个问题我也有不断的思考. 今天,探讨下如何低成本开始互联网创业. 背景 愿意冒险去创业的同志,大多是"屌丝"而非"高富帅",大多 ...
- iOS开发- iOS7显示偏差(UITableView下移)解决的方法
之前碰到过一个问题. 就是利用storyboard拖动出来的控件, 在iOS7上跑老是莫名的下移. 比方这样(红色区域为多余的) 解决的方法: iOS7在Conttoller中新增了这个属性: aut ...
- Android 中AIDL的使用与理解
AIDL的使用: 最常见的aidl的使用就是Service的跨进程通信了,那么我们就写一个Activity和Service的跨进程通信吧. 首先,我们就在AS里面新建一个aidl文件(ps:现在AS建 ...
- Cmake 实现debug和release lib依赖项处理
一.说明 最近用cmake开发东西,编译vs时候,发现debug和release版本的lib库的依赖项问题,故此小结一下.若有不对之处,还请看官多多指教. 使用的工程有自己编写的工程,也有借用第三方库 ...
- [Ramda] Pluck & Props: Get the prop(s) from object array
Pluck: Get one prop from the object array: R.pluck(}, {a: }]); //=> [1, 2] R.pluck()([[, ], [, ]] ...
- Java 开源博客——B3log Solo 0.6.7 正式版发布了!
Java 开源博客 -- B3log Solo 0.6.7 正式版发布了!欢迎大家下载. 另外,欢迎观摩 B3log 团队的新项目:Wide,也非常欢迎大家参与进来 :-) 特性 基于标签的文章分类 ...
- HDU 1408 盐水的故事 数学水题
http://acm.hdu.edu.cn/showproblem.php?pid=1408 题目: 挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下:然后滴二滴,停一下:再滴三滴,停一下...,现 ...
- POJ 3624 Charm Bracelet 0-1背包
传送门:http://poj.org/problem?id=3624 题目大意:XXX去珠宝店,她需要N件首饰,能带的首饰总重量不超过M,要求不超过M的情况下,使首饰的魔力值(D)最大. 0-1背包入 ...