[CF932D]Tree
题目大意:两种操作:
- $1\;u\;w:$把下一个点挂在$u$下,权值为$w$。
- $2\;u\;w:$询问从$u$开始的序列的最长长度。序列为从$u$开始的祖先序列中的不严格上升序列
题解:可以把一个点的父亲设为它祖先中第一个比它大的,倍增即可
卡点:跳父亲语句写在更新答案之前,然后锅锅
C++ Code:
#include <cstdio>
#define maxn 400010
#define N 20
int cnt = 1, n, fa[N][maxn];
long long sum[N][maxn], w[maxn], pw[maxn];
long long last;
int main() {
scanf("%d", &n);
pw[0] = 1; for (int i = 1; i < N; i++) pw[i] = pw[i - 1] << 1;
for (int i = 1; i <= n; i++) {
int op; long long u, W;
scanf("%d%lld%lld", &op, &u, &W);
u ^= last, W ^= last;
if (op == 1) {
w[++cnt] = W;
int now = u;
if (W > w[u]) {
for (int j = N - 1; ~j; j--) if (fa[j][now] && W > w[fa[j][now]]) now = fa[j][now];
now = fa[0][now];
}
fa[0][cnt] = now;
sum[0][cnt] = w[now];
for (int j = 1; j < N; j++) {
sum[j][cnt] = sum[j - 1][cnt] + sum[j - 1][fa[j - 1][cnt]];
fa[j][cnt] = fa[j - 1][fa[j - 1][cnt]];
}
} else {
last = 0;
if (w[u] > W) {
puts("0");
continue;
}
long long S = w[u];
for (int j = N - 1; ~j; j--) if (fa[j][u] && S + sum[j][u] <= W) {
S += sum[j][u];
u = fa[j][u];
last += pw[j];
}
printf("%lld\n", last += 1);
}
}
return 0;
}
[CF932D]Tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- 1237: [SCOI2008]配对
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1789 Solved: 715[Submit][Status][Discuss] Descripti ...
- 【学时总结】◆学时·VII◆ 高维DP
◆学时·VII◆ 高维DP 自学之余,偶遇DP…… ◇ 算法概述 顾名思义——一种处理多方面状态的DP,这种DP特点是……每一维的大小都不算太大(不然用dp数组存储下来内存会炸),而且枚举时容易超时… ...
- linux环境下nginx配置
1.反向代理配置 # nginx/conf/nginx.conf
- django+xadmin在线教育平台(十)
剩余app model注册 courses注册 新建courses/adminx.py: # encoding: utf-8 __author__ = 'mtianyan' __date__ = '2 ...
- 如何在maven中的项目使用tomcat插件
在pom.xml中引入tomcat7插件,具体示例代码如下: <project> <build> <plugins> <plugin> <grou ...
- HDU 3364 高斯消元
Lanterns Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- POJ 2441 状压DP
Arrange the Bulls Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 5289 Accepted: 2033 ...
- 36-应用Jwtbearer Authentication
新建.net core webapi项目 E:\coding\netcore>dotnet new webapi --name JwtAuthSample 创建需要用到的实体对象类 namesp ...
- 29-自己动手构建RequestDelegate管道
1-使用vsCode新建个项目 2-新建RequestDelegate和Context public delegate Task RequestDelegate(Context context); p ...
- struts2官方 中文教程 系列三:使用struts2 标签 tag
避免被爬,先贴上本帖地址:struts2 官方系列教程一:使用struts2 标签 tag http://www.cnblogs.com/linghaoxinpian/p/6901316.html 本 ...