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> ...
随机推荐
- React项目中使用less/scss&全局样式/变量
使用create-react-app脚手架搭建初始化项目 > npm install -g create-react-app > npx create-react-app my-app c ...
- 商户编号[Merchant Id]是什么
1. Merchant Id是什么 2. Merchant Id 是有哪几个部分构成的 2.1 收单机构代码 2.2 商户地区代码 2.3 Merchant Category Code(MCC) 本文 ...
- mysql复制表的两种方式
mysql复制表的两种方式. 第一.只复制表结构到新表 create table 新表 select * from 旧表 where 1=2 或者 create table 新表 like 旧表 第二 ...
- 【Redis】哨兵初始化和主观下线
在的redis启动函数main(server.c文件)中,对哨兵模式进行了检查,如果是哨兵模式,将调用initSentinelConfig和initSentinel进行初始化,initServer函数 ...
- 3D还原货拉拉女孩身亡真相,这一环值得反思!
货拉拉女孩跳车身亡的消息,让人惋惜又震惊.司机多次偏离原始路线,女孩最终选择跳车,结果不幸身亡. 货拉拉女孩跳车真相被3D还原 有人质疑平台监管不力,造成如此惨剧,有人吐槽企业压榨员工,司机绕路是不得 ...
- windows下安装和使用virtualenvwrapper-win
安装 pip安装 pip install virtualenv pip install virtualenvwrapper-win 修改默认创建环境的位置 创建环境变量 新建环境变量:WORKON_H ...
- Mac安装Brew包管理系统
Mac安装Brew包管理系统 前言 为什么需要安装brew 作为一个开发人员, 习惯了使用centos的yum和ubuntu的apt, 在mac中有没有这两个工具的平替? 有, 就是Brew. Bre ...
- Image-Text Matching
重要性和意义: Image-text matching has received a large amount of interest since it associates different mo ...
- MyBatis 映射文件
Mybatis映射文件简介 1) MyBatis 的真正强大在于它的映射语句.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉 ...
- C语言整形转字符串的方法
今天写力扣第九题,里面用到了这个,就做个笔记. 1. char *itoa( int value, char *string,int radix);(stdlib.h) Windows特有 ...