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(树分块+倍增)的更多相关文章

  1. hdu 6394 Tree (2018 Multi-University Training Contest 7 1009) (树分块+倍增)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=6394 思路:用dfs序处理下树,在用分块,我们只需要维护当前这个点要跳出这个块需要的步数和他跳出这个块去 ...

  2. HDU 6394 Tree 分块 || lct

    Tree 题意: 给你一颗树, 每一个节点都有一个权值, 如果一个石头落在某个节点上, 他就会往上跳这个的点的权值步. 现在有2种操作, 1 把一个石头放在 x 的位置 询问有跳几次才跳出这棵树, 2 ...

  3. HDU 5044 Tree 树链剖分+区间标记

    Tree Problem Description You are given a tree (an acyclic undirected connected graph) with N nodes. ...

  4. HDU 5044 Tree --树链剖分

    题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...

  5. 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 ...

  6. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  7. HDU 5044 Tree(树链剖分)

    HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...

  8. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  9. 洛谷 P6177 - Count on a tree II/【模板】树分块(树分块)

    洛谷题面传送门 好家伙,在做这道题之前我甚至不知道有个东西叫树分块 树分块,说白了就是像对序列分块一样设一个阈值 \(B\),然后在树上随机撒 \(\dfrac{n}{B}\) 个关键点,满足任意一个 ...

随机推荐

  1. Marriage Match II HDU - 3081(二分权值建边)

    题意: 有编号为1~n的女生和1~n的男生配对 首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 然后输入f组,c,d表示编号为c的女生和编号为d的女生是朋友 进行配对的要求满足其一即 ...

  2. Verilog定义计算位宽的函数clogb2

    在很多情况下要计算输入输出的位宽,比如你写一个8*8的ram,那么地址需要三位去表示,那么这个函数的方便就体现出来了,你需要使用函数定义就好了,如果对于多文件可以包含定义的文件: 如果你的DEPTH是 ...

  3. Python_sys.argv 命令行参数获取使用方法

    import sys print(sys.argv) """ 获取命令行参数 输入 python3 sys.argv_demo.py 输出: ['argv.py'] 输入 ...

  4. EMM386和UMBPCI 区别

    EMM386和UMBPCI 区别 1,SupportCD-ROM[HIMEM+EMM386NOEMS].支持光驱(EMM386模式)2,SupportCD-ROM[HIMEM+UMBPCI].支持光驱 ...

  5. 【cf789C】Functions again(最大子序列和)

    C.Functions again 题意 给你一个数组a[1..n].有一个函数\(f(l,r)=\sum_{i=l}^{r-1}\left| a[i]-a[i+1]\right| (-1)^{l-i ...

  6. 《App后台开发运维与架构实践》第2章 App后台基础技术

    2.1 從App業務邏輯中提煉API接口 業務邏輯思維導圖 功能-業務邏輯思維導圖 基本功能模塊關系 功能模塊接口UML(設計出API) 在設計稿標注API 編寫API文檔 2.2 設計API的要點 ...

  7. Hdoj 2501.Tiling_easy version 题解

    Problem Description 有一个大小是 2 x n 的网格,现在需要用2种规格的骨牌铺满,骨牌规格分别是 2 x 1 和 2 x 2,请计算一共有多少种铺设的方法. Input 输入的第 ...

  8. [NOI2014]购票(斜率优化+线段树)

    题目描述 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市与它的父亲用道路连接 ...

  9. Flask 自定义过滤器多个参数传入

    非完整HTML文件: <div class="container" style="margin-top:50px;"> <div class= ...

  10. 【【洛谷P2678 跳石头】——%%%ShawnZhou大佬】

    {dalao传送门} 这道题如果要使用暴力搜索直接求解会严重超时.实际上,我们可以发现,这个所谓的最短跳跃距离显然不能超过一个范围,而这个范围题目上已经给了出来.也就是说,答案是有一个确定的范围限制的 ...