Codeforces 932D - Tree
思路:
树上倍增
anc[i][u]:u的2^i祖先
mx[i][u]:u到它的2^i祖先之间的最大值,不包括u
pre[i][u]:以u开始的递增序列的2^i祖先
sum[i][u]:以u开始递增序列从u到2^i祖先的和,不包括u
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int N=4e5+;
const ll INF=1e15;
int anc[][N],pre[][N];
ll sum[][N],w[N],mx[][N];
int main(){
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int q,cnt=,op;
ll l,r,last=;
w[]=INF;
for(int i=;i<;i++)sum[i][]=sum[i][]=mx[i][]=mx[i][]=INF;
cin>>q;
while(q--){
cin>>op>>l>>r;
l^=last;
r^=last;
if(op==){
anc[][++cnt]=l;
w[cnt]=r;
mx[][cnt]=w[l];
for(int i=;i<;i++){
anc[i][cnt]=anc[i-][anc[i-][cnt]];
mx[i][cnt]=max(mx[i-][cnt],mx[i-][anc[i-][cnt]]);
}
int t=cnt;
for(int i=;i>=;i--){
if(mx[i][t]<w[cnt]){
t=anc[i][t];
}
}
pre[][cnt]=anc[][t];
sum[][cnt]=w[anc[][t]];
for(int i=;i<;i++){
pre[i][cnt]=pre[i-][pre[i-][cnt]];
sum[i][cnt]=sum[i-][cnt]+sum[i-][pre[i-][cnt]];
}
}
else{
int ans=;
if(w[l]<=r){
ans=;
r-=w[l];
for(int i=;i>=;i--){
if(sum[i][l]<=r){
r-=sum[i][l];
l=pre[i][l];
ans+=<<i;
}
}
}
last=ans;
cout<<ans<<endl;
}
}
return ;
}
Codeforces 932D - Tree的更多相关文章
- Tree CodeForces -932D
错误记录:如下注释语句 #include<cstdio> #include<algorithm> using namespace std; typedef long long ...
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- Codeforces 570D - Tree Requests【树形转线性,前缀和】
http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...
- Codeforces 23E Tree
http://codeforces.com/problemset/problem/23/E 题意:给一个树,求砍断某些边,使得所有联通块大小的乘积最大.思路:f[i][j]代表当前把j个贡献给i的父亲 ...
- Codeforces 1092F Tree with Maximum Cost(树形DP)
题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...
- [Educational Round 17][Codeforces 762F. Tree nesting]
题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...
- Codeforces 911F Tree Destruction
Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的. #include<bits/stdc++.h> #define LL lon ...
- CodeForces 570D - Tree Requests - [DFS序+二分]
题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...
随机推荐
- 错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法
晚上花几分钟在windows下测了下pthread的用法,出现错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 ...
- devexpress 之 ChartControl
\}5#~mV#cr(/k1yIZ7.Lg
- 实现 AD 采样,使用 LCD1602 显示 AD 数值
实现 AD 采样,使用 LCD1602 显示 AD 数值 写在前面 单片机内集成的A/D转换,一般都有相应的特殊功能寄存器来设置A/D的使能标志,参考电压,转换频率,通道选择,A/D输入口的属性(模拟 ...
- win-Linux文件脚本迁移过程中的问题 syntax error: unexpected end of file
问题: 在win下写好的shell脚本,放到Linux上测试sh -n报错如下 ORA_check.sh: line 251: syntax error: unexpected end of fil ...
- docker版本升级
docker的版本变化: Docker从1.13.x版本开始,版本分为企业版EE和社区版CE,版本号也改为按照时间线来发布,比如17.03就是2017年3月,有点类似于ubuntu的版本发布方式. 企 ...
- (转)ElasticSearch学习
(二期)21.全文搜索引擎Elasticsearch [课程21]elasticsearch.xmind82.1KB [课程21]lucene.xmind0.8MB [课程21]基本用法....api ...
- javbus爬虫-老司机你值得拥有
# 起因 有个朋友叫我帮忙写个爬虫,爬取javbus5上面所有的详情页链接,也就是所有的https://www.javbus5.com/SRS-055这种链接, 我一看,嘿呀,这是司机的活儿啊,我绝对 ...
- 操作系统04_IO管理
输入输出系统 IO系统的层次结构 用户层IO软件 设备独立性软件 设备驱动程序 中断处理程序 对IO设备的控制方式 使用轮询的可编程IO方式 cpu不停地检查设备的状态,以字节为单位,非中断方式,利用 ...
- 论文阅读:Prominent Object Detection and Recognition: A Saliency-based Pipeline
论文阅读:Prominent Object Detection and Recognition: A Saliency-based Pipeline 如上图所示,本文旨在解决一个问题:给定一张图像, ...
- 函数嵌套函数传递this值
<button onclick="demo()(this)">test</button> function demo(){ return function ...