CF593D Happy Tree Party(不用树剖)
题面
题解
我们发现,对于除法有效的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(不用树剖)的更多相关文章
- SPOJ375Query on a tree I(树剖+线段树)(询问边)
ιYou are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 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, ...
- BZOJ 4034 [HAOI2015]树上操作 线段树+树剖或dfs
题意 直接照搬原题面 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所 ...
- SP375 QTREE - Query on a tree (树剖)
题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...
- 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 ...
- 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 ...
- 2019.01.19 codeforces343D.Water Tree(树剖+ODT)
传送门 ODTODTODT板子题. 支持子树01覆盖,路径01覆盖,询问一个点的值. 思路:当然可以用树剖+线段树,不过树剖+ODTODTODT也可以很好的水过去. 注意修改路径时每次跳重链都要修改. ...
- CF 504 E —— Misha and LCP on Tree —— 树剖+后缀数组
题目:http://codeforces.com/contest/504/problem/E 快速查询LCP,可以用后缀数组,但树上的字符串不是一个序列: 所以考虑转化成序列—— dfs 序! 普通的 ...
- BZOJ4353 Play with tree[树剖]
复习几乎考不到的树剖.维护min以及min个数,打set和add标记即可,注意set优先级优于add. #include<iostream> #include<cstdio> ...
随机推荐
- .NET中的 Count()、Count、Length 有什么不同
更新记录 2022年4月16日:本文迁移自Panda666原博客,原发布时间:2021年7月15日. Count().Count.Length,都用于获得序列长度或者说元素的个数,但它们有什么明确的区 ...
- 【Redis】客观下线
在sentinelHandleRedisInstance函数中,如果是主节点,需要做如下处理: void sentinelHandleRedisInstance(sentinelRedisInstan ...
- 《Java基础——IO流》
Java基础--IO流 一.字节流: 1.输入流 (InputStream) 规则: 此处用于读取txt文件中的内容. 代码: import java.io.*; public c ...
- kali 无线网络渗透测试
一.无线网络渗透嗅探工具Kismet 如果要进行无线网络渗透测试,则必须先扫描所有有效的无线接入点.在Kali linux中,提供了一款嗅探无线网络工具Kismet.使用该工具可以测量周围的无线信号, ...
- OpenLayers入门(一)
OpenLayers简介 OpenLayers(https://openlayers.org/)是一个用来帮助开发Web地图应用的高性能的.功能丰富的JavaScript类库,可以满足几乎所有的地图开 ...
- Tensorflow2 深度学习十必知
博主根据自身多年的深度学习算法研发经验,整理分享以下十条必知. 含参考资料链接,部分附上相关代码实现. 独乐乐不如众乐乐,希望对各位看客有所帮助. 待回头有时间再展开细节说一说深度学习里的那些道道. ...
- Visio Professional之活动图
1 什么叫活动图? 活动图在本质上是一种流程图. 活动图(Activity diagram)是UML用于对系统的动态行为建模的一种常用工具,它描述活动的顺序,表示一个活动到另一个活动的控制流. 2.活 ...
- C++简单工厂模式的学习
我们先从最常见的C++类的一个实现开始说起, class API { public: virtual test(std::string s)=0; protected: API(){}; }; cla ...
- 《ASP.NET Core 6框架揭秘》样章发布[200页/5章]
作为<ASP.NET Core 3 框架揭秘>的升级版,<ASP.NET Core 6框架揭秘>不仅针对ASP.NET Core 6的新特性进行了修订,并添加了若干原来没有的内 ...
- MongoDB慢查询与索引
MongoDB慢查询 慢查询分析 开启内置的慢查询分析器 db.setProfilingLevel(n,m),n的取值可选0,1,2 0:表示不记录 1:表示记录慢速操作,如果值为1,m需要传慢查询的 ...