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的更多相关文章

  1. Tree CodeForces -932D

    错误记录:如下注释语句 #include<cstdio> #include<algorithm> using namespace std; typedef long long ...

  2. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

  3. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  4. Codeforces 570D - Tree Requests【树形转线性,前缀和】

    http://codeforces.com/contest/570/problem/D 给一棵有根树(50w个点)(指定根是1号节点),每个点上有一个小写字母,然后有最多50w个询问,每个询问给出x和 ...

  5. Codeforces 23E Tree

    http://codeforces.com/problemset/problem/23/E 题意:给一个树,求砍断某些边,使得所有联通块大小的乘积最大.思路:f[i][j]代表当前把j个贡献给i的父亲 ...

  6. Codeforces 1092F Tree with Maximum Cost(树形DP)

    题目链接:Tree with Maximum Cost 题意:给定一棵树,树上每个顶点都有属性值ai,树的边权为1,求$\sum\limits_{i = 1}^{n} dist(i, v) \cdot ...

  7. [Educational Round 17][Codeforces 762F. Tree nesting]

    题目连接:678F - Lena and Queries 题目大意:给出两个树\(S,T\),问\(S\)中有多少连通子图与\(T\)同构.\(|S|\leq 1000,|T|\leq 12\) 题解 ...

  8. Codeforces 911F Tree Destruction

    Tree Destruction 先把直径扣出来, 然后每个点都和直径的其中一端组合, 这样可以保证是最优的. #include<bits/stdc++.h> #define LL lon ...

  9. CodeForces 570D - Tree Requests - [DFS序+二分]

    题目链接:https://codeforces.com/problemset/problem/570/D 题解: 这种题,基本上容易想到DFS序. 然后,我们如果再把所有节点分层存下来,那么显然可以根 ...

随机推荐

  1. P5290 [十二省联考2019]春节十二响(堆+启发式合并)

    P5290 [十二省联考2019]春节十二响 从特殊到一般 我们先看链的情况. 我们把点$1$左右的两条子链分别扔入堆里 每次取出两个堆的最大值,把答案累加上更大的那个(另一堆为空则直接加上去). 那 ...

  2. 超详细 Nginx 极简教程,傻瓜一看也会!

    什么是Nginx? Nginx (engine x) 是一款轻量级的Web 服务器 .反向代理服务器及电子邮件(IMAP/POP3)代理服务器. 什么是反向代理? 反向代理(Reverse Proxy ...

  3. 八数码问题 Eight Digital Problem

    八数码问题 利用启发式搜索,找出以下问题的最优解. #include <iostream> #include <vector> #include <algorithm&g ...

  4. 两步完成ssh免密码登录

    1.生成公钥/私钥 ssh-keygen -N '' 生成公钥在/root/.ssh目录下. 2.分发公钥 ssh-copy-id root@192.168.137.141 192.168.137.1 ...

  5. 2018-2019-2 《网络对抗技术》Exp2 后门原理与应用 20165211

    目录 后门原理与应用 实验内容 基础问题回答 常用后门工具 Netcat Socat MSFmeterpreter 实践过程记录 1.后门工具熟悉 2.使用netcat获取主机操作Shell,cron ...

  6. discuz 不能上传头像提示can not write to the data/tmp folder

    # discuz 不能上传头像提示can not write to the data/tmp folder 解释: disucz头像上传不成功,提示data/tmp目录没有写入权限,这里的data/t ...

  7. P3412 仓鼠找sugar II

    思路 挺神的概率期望.. 好吧是我太弱了,完全没有往那里想 注意期望是具有线性性的,一条路径的期望可以变成每条边的期望求和 概率是某件事发生的可能性,期望是某件事确定发生的代价 首先没有终点的条件并不 ...

  8. P3121 [USACO15FEB]审查(黄金)Censoring (Gold)

    吐槽 数据太水了吧,我AC自动机的trie建错了结果只是RE了两个点,还以为数组开小了改了好久 思路 看到多模板串,字符串匹配,且模板串总长度不长,就想到AC自动机 然后用栈维护当前的字符串位置,如果 ...

  9. docker 安装redis

    https://blog.csdn.net/chenjianandiyi/article/details/78962709 运行镜像redis:  docker run --name redis1 - ...

  10. facebook api之Access Tokens

    Access Tokens When someone connects with an app using Facebook Login and approves the reqest for per ...