HDU - 6394 Tree(树分块+倍增)
http://acm.hdu.edu.cn/showproblem.php?pid=6394
题意
给出一棵树,然后每个节点有一个权值,代表这个点可以往上面跳多远,问最少需要多少次可以跳出这颗树
分析
先dfs一次得到dfs序,然后按dfs序分块。倍增计算从某点跳x到哪个点,用cn保存它跳出这一块需要的次数,ne保存跳出这块会去的点。然后块内就暴力修改了。复杂度nsqrt(n);
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + ;
const int maxm = + ;
const int mod = ; int w[maxn],fa[][maxn],n,m;
int head[maxn],nxt[maxn],to[maxn],tot;
int id[maxn],cnt;//dfs序
int num,block,belong[maxn],l[maxn],r[maxn];//块的数目,快的大小,属于那块,每块的左右边界
int cn[maxn],ne[maxn],pre[maxn];//跳出本块的次数,跳向的下一块的点,这个点会跳向的位置 void init(){
memset(head,-,sizeof(head));
cnt=tot=;
}
void addedge(int u,int v){
to[tot]=v;
nxt[tot]=head[u];
head[u]=tot++;
}
void dfs1(int u,int f){//dfs一次,得到dfs序和每一个点的父亲
fa[][u]=f;
id[u]=++cnt;
for(int i=head[u];~i;i=nxt[i]){
dfs1(to[i],u);
}
}
void build(){//分块
block=sqrt(n);
num=(n+block-)/block;
for(int i=;i<=n;i++){
belong[i]=(i-)/block+;
}
for(int i=;i<=num;i++){
l[i]=(i-)*block+;
r[i]=i*block;
}
r[num]=n;
}
int find(int u,int l){
for(int i=;i>=;i--){
if((l>>i)&){
u=fa[i][u];
}
}
return u;
}
void dfs2(int u){//再次dfs得到cn[N],net[N],pre[N]
int f=find(u,w[u]);
pre[id[u]]=id[f];
if(id[f]<l[belong[id[u]]]) cn[id[u]]=,ne[id[u]]=id[f];
else cn[id[u]]=cn[id[f]]+,ne[id[u]]=ne[id[f]];
for(int i=head[u];~i;i=nxt[i]){
dfs2(to[i]);
}
}
int query(int u){
int ans=;
while(u>){
ans+=cn[u];
u=ne[u];
}
return ans;
}
void update(int u,int val){
int f=find(u,val);
w[u]=val;
pre[id[u]]=id[f];
if(id[f]<l[belong[id[u]]]) cn[id[u]]=,ne[id[u]]=id[f];
else cn[id[u]]=cn[id[f]]+,ne[id[u]]=ne[id[f]];
for(int i=id[u]+;i<=r[belong[id[u]]];i++){//更新这块内后面的点
if(pre[i]>=l[belong[i]]){
cn[i]=cn[pre[i]]+;
ne[i]=ne[pre[i]];
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int T;
scanf("%d",&T);
while(T--){
init();
scanf("%d",&n);
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
addedge(x,i);
}
for(int i=;i<=n;i++){
scanf("%d",&w[i]);
}
dfs1(,);
for(int i=;i<;i++){
for(int j=;j<=n;j++){
fa[i][j]=fa[i-][fa[i-][j]];
}
}
build();
dfs2();
scanf("%d",&m);
while(m--){
int op,x,y;
scanf("%d%d",&op,&x);
if(op==){
printf("%d\n",query(id[x]));
}else{
scanf("%d",&y);
update(x,y);
}
}
}
return ;
}
HDU - 6394 Tree(树分块+倍增)的更多相关文章
- hdu 6394 Tree (2018 Multi-University Training Contest 7 1009) (树分块+倍增)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=6394 思路:用dfs序处理下树,在用分块,我们只需要维护当前这个点要跳出这个块需要的步数和他跳出这个块去 ...
- HDU 6394 Tree 分块 || lct
Tree 题意: 给你一颗树, 每一个节点都有一个权值, 如果一个石头落在某个节点上, 他就会往上跳这个的点的权值步. 现在有2种操作, 1 把一个石头放在 x 的位置 询问有跳几次才跳出这棵树, 2 ...
- HDU 5044 Tree 树链剖分+区间标记
Tree Problem Description You are given a tree (an acyclic undirected connected graph) with N nodes. ...
- HDU 5044 Tree --树链剖分
题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...
- hdu 4366 Successor - CDQ分治 - 线段树 - 树分块
Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty an ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- HDU 5044 Tree(树链剖分)
HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- 洛谷 P6177 - Count on a tree II/【模板】树分块(树分块)
洛谷题面传送门 好家伙,在做这道题之前我甚至不知道有个东西叫树分块 树分块,说白了就是像对序列分块一样设一个阈值 \(B\),然后在树上随机撒 \(\dfrac{n}{B}\) 个关键点,满足任意一个 ...
随机推荐
- HDOJ5547 SudoKu
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547 题目大意:填数独... 思路:爆搜 #include <stdio.h> #incl ...
- MT【256】2016四川高考解答压轴题
(2016四川高考数学解答压轴题)设函数$f(x)=ax^2-a-\ln x,a\in R$. 1)讨论$f(x)$的单调性;2)确定$a$的所有可能值,使得$f(x)>\dfrac{1}{x} ...
- mailkit库收发邮件
mailkit库用于收发邮件.这个库可以替代C#自带的发邮件库 环境 W10 / VS2017CMMT / MailKit version="2.0.3" "net46 ...
- bzoj1014 火星人 (hash+splay+二分答案)
求公共前缀的问题可以用hash+二分来解决,但这个是动态的,所以我们用平衡树来维护区间的hash值 复杂度$O(mlog^2n)$ #include<bits/stdc++.h> #def ...
- Spring注解方式实现任务调度
原文:http://docs.spring.io/spring/docs/4.0.1.BUILD-SNAPSHOT/javadoc-api/ 注解类型:EnableScheduling @Target ...
- [BJOI2017]开车
[BJOI2017]开车 直接做要用栈 修改?难以直接维护 统计边的贡献! len*abs(pre)pre表示前缀car-stop 修改时候,整个区间的pre+1或者-1 分块,块内对pre排序并打标 ...
- B1016. 部分A+B
正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...
- Flask block继承和include包含
继承(Block)的本质是代码替换,继承我认为就是把完整的html文件继承到一个不完整的html文件里. 被继承html文件: <!DOCTYPE html> <html lang= ...
- k短路(A*)
http://poj.org/problem?id=2449 #include <cstdio> #include <cstdlib> #include <cstring ...
- 【译】10. Java反射——数组
原文地址:http://tutorials.jenkov.com/java-reflection/arrays.html ======================================= ...