错误记录:如下注释语句

 #include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
LL log2n=,cnt=;
LL anc[][],maxv[][],v[];
LL anc2[][],sum[][];
LL pow2[]={,,,,,,,,,,,,,,,,,,,};
//maxv[i][j]指i到其2^j级祖先的路径上点权最大值,含2^j级祖先,不含i
//sum[i][j]指i到其2^j级祖先的路径上点权之和,含祖先,不含i
LL Q,last;
int main()
{
LL i,idx,p,q,t,ans;
v[]=1e15+;
for(i=;i<=log2n;i++) maxv[][i]=maxv[][i]=sum[][i]=sum[][i]=1e15+;
scanf("%lld",&Q);
while(Q--)
{
scanf("%lld%lld%lld",&idx,&p,&q);
p^=last;q^=last;
//printf("%lld %lld\n",p,q);
if(idx==)
{
v[++cnt]=q;anc[cnt][]=p;maxv[cnt][]=v[anc[cnt][]];
//putchar('a');printf("%d %d ",0,maxv[cnt][0]);
for(i=;i<=log2n;i++)
{
anc[cnt][i]=anc[anc[cnt][i-]][i-];
maxv[cnt][i]=max(maxv[cnt][i-],maxv[anc[cnt][i-]][i-]);
//printf("%d %d ",i,maxv[cnt][i]);
}
//putchar('b');
for(t=cnt,i=log2n;i>=;i--)
if(maxv[t][i]<v[cnt])
t=anc[t][i];
anc2[cnt][]=anc[t][];sum[cnt][]=v[anc2[cnt][]];
//printf("%d %d\n",cnt,nxt[cnt]);
//printf("%lld %lld ",0LL,sum[cnt][0]);
for(i=;i<=log2n;i++)
{
anc2[cnt][i]=anc2[anc2[cnt][i-]][i-];
sum[cnt][i]=sum[cnt][i-]+sum[anc2[cnt][i-]][i-];
//printf("%lld %lld ",i,sum[cnt][i]);
}
//putchar('c');
}
else
{
q-=v[p];ans=;if(q<){ans=;goto xxx;}
for(t=p,i=log2n;i>=;i--)
if(sum[t][i]<=q)
{
//t=anc2[t][i];
q-=sum[t][i];ans+=pow2[i];//ans++
t=anc2[t][i];
}
xxx:last=ans;
printf("%lld\n",ans);
}
}
return ;
}

另外,不能看到要加点就想动态树(不会)什么的,比如这道题,虽然要加点,但是仅此而已,用不到那些东西,只需要加入点时倍增求出要维护的值就行了。

对于原树,每加入一个节点u求出maxv(含义见程序),然后由这个新节点u向上倍增找到其祖先节点v,使得u到v的路径上所有点(不含u,含v)权值的最大值小于u的权值,且v深度最小。那么v的父节点就是u向上一级一级跳时第一个能访问到的权值大于等于u的节点,记为nxt[u](程序中省略,直接记为anc2[u][0])。

这样子,对于每个节点u(除了1号)都能得到nxt[u],把nxt[u]当做u的父节点,形成一棵新的树,那么查询转化为给定一个节点u,在新树上找到u的祖先节点v,使得u到v的路径上所有点(含u和v)权值之和小于等于x,且v的深度最小,输出u到v的路径上点数(含u和v)。(当然也可能u的权值就超过x了,那么输出0,要特判)。仍然倍增解决即可。

 #include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
LL log2n=,cnt=;
LL anc[][],maxv[][],v[];
LL anc2[][],sum[][];
//maxv[i][j]指i到其2^j级祖先的路径上点权最大值,含2^j级祖先,不含i
//sum[i][j]指i到其2^j级祖先的路径上点权之和,含祖先,不含i
LL Q,last;
int main()
{
LL i,idx,p,q,t,ans;
v[]=1e15+;
for(i=;i<=log2n;i++) maxv[][i]=maxv[][i]=sum[][i]=sum[][i]=1e15+;
scanf("%lld",&Q);
while(Q--)
{
scanf("%lld%lld%lld",&idx,&p,&q);
p^=last;q^=last;
if(idx==)
{
v[++cnt]=q;anc[cnt][]=p;maxv[cnt][]=v[anc[cnt][]];
for(i=;i<=log2n;i++)
{
anc[cnt][i]=anc[anc[cnt][i-]][i-];
maxv[cnt][i]=max(maxv[cnt][i-],maxv[anc[cnt][i-]][i-]);
}
for(t=cnt,i=log2n;i>=;i--)
if(maxv[t][i]<v[cnt])
t=anc[t][i];
anc2[cnt][]=anc[t][];sum[cnt][]=v[anc2[cnt][]];
for(i=;i<=log2n;i++)
{
anc2[cnt][i]=anc2[anc2[cnt][i-]][i-];
sum[cnt][i]=sum[cnt][i-]+sum[anc2[cnt][i-]][i-];
}
}
else
{
q-=v[p];ans=;if(q<){ans=;goto xxx;}
for(t=p,i=log2n;i>=;i--)
if(sum[t][i]<=q)
{
q-=sum[t][i];ans+=(1LL<<i);
t=anc2[t][i];
}
xxx:last=ans;
printf("%lld\n",ans);
}
}
return ;
}

Tree CodeForces -932D的更多相关文章

  1. Vasya and a Tree CodeForces - 1076E(线段树+dfs)

    I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...

  2. Distance in Tree CodeForces - 161D

    Distance in Tree CodeForces - 161D 题意:给一棵n个结点的树,任意两点之间的距离为1,现在有点u.v,且u与v的最短距离为k,求这样的点对(u,v)的个数((u,v) ...

  3. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  4. Z - New Year Tree CodeForces - 620E 线段树 区间种类 bitset

    Z - New Year Tree CodeForces - 620E 这个题目还没有写,先想想思路,我觉得这个题目应该可以用bitset, 首先这个肯定是用dfs序把这个树转化成线段树,也就是二叉树 ...

  5. Codeforces 932D - Tree

    932D - Tree 思路: 树上倍增 anc[i][u]:u的2^i祖先 mx[i][u]:u到它的2^i祖先之间的最大值,不包括u pre[i][u]:以u开始的递增序列的2^i祖先 sum[i ...

  6. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

  7. AC日记——Propagating tree Codeforces 383c

    C. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. B. Grow The Tree Codeforces Round #594 (Div. 2)

    Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on t ...

  9. Vasya and a Tree CodeForces - 1076E (线段树 + dfs)

    题面 Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 writ ...

随机推荐

  1. 编译iOS使用的.a库文件

    首先是须要编译成.a的源文件 hello.h: #ifndef __INCLUDE_HELLO_H__ #define __INCLUDE_HELLO_H__ void hello(const cha ...

  2. poj 1426 Find The Multiple ( BFS+同余模定理)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 744 ...

  3. easyUI 验证控件应用、自己定义、扩展验证 手机号码或电话话码格式

    easyUI 验证控件应用.自己定义.扩展验证 手机号码或电话话码格式 在API中   发现给的demo 中没有这个验证,所以就研究了下. 相关介绍省略,直接上代码吧! watermark/2/tex ...

  4. soapUI系列之—-07 调用JIRA Rest API接口【例】

    一.调用JIRA接口------实现过滤器搜索问题 1. 在SoapUI中新建 REST Project, 在URI 中输入登录接口的 url (任意一个 Rest 接口的 url 都可以): 2. ...

  5. 【iOS进阶】UIWebview加载搜狐视频,自动跳回客户端 问题解决

    UIWebview加载搜狐视频,自动跳回搜狐客户端 问题解决 当我们用UIWebview(iOS端)加载网页视频的时候,会发现,当真机上有搜狐客户端的时候,会自动跳转到搜狐客户端进行播放,这样的体验对 ...

  6. Java中有多少种设计模式?请简单画一下三种常见设计模式的类图?

    转载:http://blog.csdn.net/longyulu/article/details/9159589 一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽 ...

  7. 超线程技术——超线程技术让(P4)处理器增加5%的裸晶面积,就可以换来15%~30%的效能提升,本质单核模拟双核!和异步编程的思想无异。

    超线程是Intel 所研发的一种技术,于2002年发布.超线程的英文是HT技术,全名为Hyper-Threading,中文又名超线程.超线程技术原先只应用于Intel Xeon处理器中,当时称为Sup ...

  8. 连接mysql时报:message from server: "Host '192.168.76.89' is not allowed to connect to this MySQL server

    处理方案: 1.先用localhost方式连接到MySQL数据库,然后使用MySQL自带的数据库mysql; use mysql: 2.执行:select host from user where u ...

  9. 细数vue爬坑之路<坑路大集合>

    坑爹集锦一: npm出现Newline required at end of file but not found错误 原因:eslint语法错误(vue为后缀名的组件结尾没有换行) 解决办法:在结尾 ...

  10. AutoIT:为文件夹下面的文件批量改名

    以前用Ruby脚本,对于中文,数字结合的文件名,修改名字也不是非常简单,需要修改字符集,可是用autoit来实现,也挺简单的,并且可以替换已有文件名中的汉字部分. $filepath = " ...