题面

题解

我们发现,对于除法有效的xi最小为2,yi最多除log次就会变成0,所以我们可以每次找路径上下一个>=2的xi,暴力除,当发现y=0时就停止

于是我们维护每个点向上走一直走到根最近的一条数字大于1的边,存下该边的下端点,每当有一条边数字大于1,就要更新它的下端点子树中每一个的最近边,这个可以把点按照dfs序排序后用线段树做区间修改,单点查询

但是题目中的修改操作很特殊,每次会将一条边上的数字变小,后面一旦变为1了就不好维护,所以我决定倒着来(感性理解,把进度条从后往前拖),先把操作输入完,把每条边的数字先变为最小,从后往前处理询问,这样就相当于只有把xi变大的操作,就很好维护了。

(线段树一个运算符打错了,调了一晚上QAQ

CODE

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#define MAXN 200005
#define MAXM 200005
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x)&(x))
//#define int LL
//#pragma GCC optimize(2)
using namespace std;
inline LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + (s - '0');s = getchar();}
return x * f;
}
const int jzm = 1000000007;
int n,m,i,j,s,o,k,M;
int u[MAXN],v[MAXN];
LL x[MAXN];
struct cz{
int k,u,v;
LL y;
}q[MAXN];
vector<int> g[MAXN];
int d[MAXN],f[MAXN][20],dfn[MAXN],id[MAXN],rr[MAXN],cnt;
LL fe[MAXN];
int tre[MAXN<<2];
int bing(int a,int b) {return d[a] > d[b] ? a : b;}
void maketree(int n) {M = 1;while(M < n + 2) M <<= 1;}
void addtree(int l,int r,int y) {
int s = M + l - 1,t = M + r + 1;
while(s || t) {
if((s>>1) ^ (t>>1)) {
if(!(s & 1)) tre[s ^ 1] = bing(tre[s ^ 1],y);
if(t & 1) tre[t ^ 1] = bing(tre[t ^ 1],y);
}
else break;
s >>= 1;t >>= 1;
}
return ;
}
int findt(int x) {
int s = M + x;int as = 0;
while(s) as = bing(as,tre[s]),s >>= 1;
return as;
}
void dfs(int x,int fa) {
d[x] = d[fa] + 1;
f[x][0] = fa;
dfn[x] = ++ cnt;
id[cnt] = x;
for(int i = 1;i <= 18;i ++) f[x][i] = f[f[x][i-1]][i-1];
for(int i = 0;i < g[x].size();i ++) {
if(g[x][i] != fa)
dfs(g[x][i],x);
}
rr[x] = cnt;
return ;
}
int lca(int a,int b) {
if(d[b] > d[a]) swap(a,b);
if(d[a] > d[b]) {
for(int i = 18;i >= 0;i --) {
if(d[f[a][i]] >= d[b]) {
a = f[a][i];
}
}
}
if(a == b) return a;
for(int i = 18;i >= 0;i --) {
if(f[a][i] != f[b][i]) {
a = f[a][i],b = f[b][i];
}
}
return f[a][0];
}
signed main() {
n = read();m = read();
for(int i = 1;i < n;i ++) {
s = read();o = read();
g[s].push_back(o);
g[o].push_back(s);
u[i] = s;v[i] = o;
x[i] = read();
}
dfs(1,0);
for(int i = 1;i < n;i ++) if(d[u[i]] < d[v[i]]) swap(u[i],v[i]);
for(int i = 1;i <= m;i ++) {
q[i].k = read();
if(q[i].k == 1) {
q[i].u = read();
q[i].v = read();
q[i].y = read();
}
else {
q[i].u = read();
LL ss = read();
q[i].y = x[q[i].u];
x[q[i].u] = ss;
}
}
maketree(cnt);
addtree(1,cnt,1);
fe[1] = 1ll;
for(int i = 1;i < n;i ++) {
fe[u[i]] = x[i];
if(x[i] > 1) {
addtree(dfn[u[i]],rr[u[i]],u[i]);
}
}
stack<LL> as;
for(int i = m;i > 0;i --) {
if(q[i].k == 2) {
int j = q[i].u;
if(x[j] <= 1ll && q[i].y > 1ll) addtree(dfn[u[j]],rr[u[j]],u[j]);
fe[u[j]] = x[j] = q[i].y;
}
else {
// for(int j = 1;j < n;j ++) printf("%lld ",x[j]);ENDL;
s = q[i].u,o = q[i].v;
LL y = q[i].y;
int lc = lca(s,o);
while(d[s] > d[lc]) {
if(y <= 0) break;
y /= fe[s];
s = findt(dfn[f[s][0]]);
}
stack<LL> no;
LL sum = 1ll;
while(d[o] > d[lc]) {
if(sum > y) break;
sum *= fe[o];
no.push(fe[o]);
o = findt(dfn[f[o][0]]);
}
while(!no.empty()) y /= no.top(),no.pop();
as.push(y);
}
}
while(!as.empty()) printf("%lld\n",as.top()),as.pop();
return 0;
}

CF593D Happy Tree Party(不用树剖)的更多相关文章

  1. SPOJ375Query on a tree I(树剖+线段树)(询问边)

    ιYou are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  2. SPOJ Query on a tree II (树剖||倍增LCA)(占位)

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  3. BZOJ 4034 [HAOI2015]树上操作 线段树+树剖或dfs

    题意 直接照搬原题面 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所 ...

  4. SP375 QTREE - Query on a tree (树剖)

    题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...

  5. SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)

    You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...

  6. POJ3237 Tree(树剖+线段树+lazy标记)

    You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbe ...

  7. 2019.01.19 codeforces343D.Water Tree(树剖+ODT)

    传送门 ODTODTODT板子题. 支持子树01覆盖,路径01覆盖,询问一个点的值. 思路:当然可以用树剖+线段树,不过树剖+ODTODTODT也可以很好的水过去. 注意修改路径时每次跳重链都要修改. ...

  8. CF 504 E —— Misha and LCP on Tree —— 树剖+后缀数组

    题目:http://codeforces.com/contest/504/problem/E 快速查询LCP,可以用后缀数组,但树上的字符串不是一个序列: 所以考虑转化成序列—— dfs 序! 普通的 ...

  9. BZOJ4353 Play with tree[树剖]

    复习几乎考不到的树剖.维护min以及min个数,打set和add标记即可,注意set优先级优于add. #include<iostream> #include<cstdio> ...

随机推荐

  1. ExtJS 布局-HBox 布局(HBox layout)

    更新记录: 2022年6月11日 更新文章结构. 2022年6月8日 发布. 2022年6月1日 开始. 1.说明 hbox布局与column布局几乎相同,但hbox允许拉伸列的高度. 既可以在水平方 ...

  2. 【Java集合】ArrayDeque源码解读

    简介 双端队列是一种特殊的队列,它的两端都可以进出元素,故而得名双端队列. ArrayDeque是一种以循环数组方式实现的双端队列,它是非线程安全的. 它既可以作为队列也可以作为栈. 继承体系 Arr ...

  3. 用QT制作3D点云显示器——QtDataVisualization

    因为QT的三维显示模块QtDataVisualization已经对个人开发免费开放了,所以在制作点云,地图,表格之类的东西的时候,其实我们都不需要使用QtCharts或者QOpenGL模块了.直接使用 ...

  4. 左右手切换工具xmouse v1.2版本发布

    Xmouse 方便的切换鼠标左右键,因为功能非常简单,所以支持.net framework 2.0及以上 windows环境就可以了,目前已测试win7.win10可用. 关于为什么做这么个东西,那是 ...

  5. centos7 离线升级/在线升级操作系统内核

    目录 一.前言 二.系统环境 三.系统内核下载网址 四.centos7离线升级系统内核 1.先查看系统环境 2.离线升级系统内核 五.在线升级系统内核 一.前言 CentOS(Community EN ...

  6. 深入理解 volatile 关键字

    volatile 关键字是 Java 语言的高级特性,但要弄清楚其工作原理,需要先弄懂 Java 内存模型.如果你之前没了解过 Java 内存模型,那可以先看看之前我写过的一篇「深入理解 Java 内 ...

  7. 正在运行中的docker容器设置自动重启

    # demo : 你的容器名称 docker update –-restart=always demo

  8. Git下载(快速快速快速下载!!)

    在安装Git环境的时候,需要下载Git的安装包,但是官网的下载网速实在是太慢的(几十M的安装包,下载速度只有几十K) (所以可以在镜像中下载,速度超快) Git镜像下载链接-------------- ...

  9. pytest多进程/多线程执行测试用例

    前言: 实际项目中的用例数量会非常多,几百上千:如果采用单进程串行执行的话会非常耗费时间.假设每条用例耗时2s,1000条就需要2000s $\approx$ 33min:还要加上用例加载.测试前/后 ...

  10. js导入excel&导出excel

    Excel导入 html代码 <button style={{ color: '#1890ff', fontSize: '14px', cursor: 'pointer' }} onClick= ...