Codeforces932D. Tree
n<=400000个在线操作:树上插入一个某点权、父亲为某点的点;查询这样的最长点序列:序列的某个数必须是上一个数的祖先之一;序列的点权和不能超过x;序列的某个点的点权必须不小于上一个,且相邻两个点之间不存在点权大于等于深度大的那个点的点权的点。
说白了,就是每个点找他祖先中第一个点权大于等于他的点,然后建一棵假的树,树边表示某点如果要构造序列,序列的下一个数字就是他祖先。
分别处理:原树父亲、祖先点权最大值、假的树的父亲、假的树的祖先点权和,四个st表一个推一个即可。
//#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
//#include<map>
#include<math.h>
//#include<time.h>
//#include<complex>
#include<algorithm>
using namespace std; int n;
#define maxn 400011
#define LL long long
LL fa[maxn][],val[maxn],M[maxn][],pp[maxn][],sum[maxn][],tot=; int main()
{
scanf("%d",&n);
int last=; LL x,y,z;
for (int i=;i<=n;i++)
{
scanf("%lld%lld%lld",&x,&y,&z); y^=last; z^=last;
if (x==)
{
val[++tot]=z;
fa[tot][]=y; for (int i=;i<=;i++) fa[tot][i]=fa[fa[tot][i-]][i-];
M[tot][]=max(val[y],z); for (int i=;i<=;i++) M[tot][i]=max(M[tot][i-],M[fa[tot][i-]][i-]);
if (val[y]>=z) pp[tot][]=y;
else
{
int t=y; for (int i=;i>=;i--) if (fa[t][i] && M[t][i]<z) t=fa[t][i]; t=fa[t][];
pp[tot][]=t;
}
for (int i=;i<=;i++) pp[tot][i]=pp[pp[tot][i-]][i-];
sum[tot][]=val[tot]+val[pp[tot][]];
for (int i=;i<=;i++) sum[tot][i]=sum[tot][i-]+sum[pp[tot][i-]][i-]-val[pp[tot][i-]];
}
else
{
if (val[y]>z) {printf("%d\n",(last=)); continue;}
int t=y,ans=; for (int i=;i>=;i--) if (pp[t][i] && sum[t][i]<=z) ans+=(<<i),z-=sum[t][i]-val[pp[t][i]],t=pp[t][i];
printf("%d\n",(last=ans));
}
}
return ;
}
Codeforces932D. 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 ...
随机推荐
- 通过流传入excel解析的问题
做了个excel文件导入的功能,接收excel文件流,先读取文件流头部信息确定文件类型,然后调用poi方法 OPCPackage pkg = OPCPackage.open(inputStream) ...
- this的试题
1.var x=12; function test(){ console.log(this.x) } test() //主体是window 2.var x=12; function test ...
- DOM编程练习(慕课网题目)
编程练习 制作一个表格,显示班级的学生信息. 要求: 1. 鼠标移到不同行上时背景色改为色值为 #f2f2f2,移开鼠标时则恢复为原背景色 #fff 2. 点击添加按钮,能动态在最后添加一行 3. 点 ...
- [Python學習筆記] 利用 Python在Excel 插入註解
用Python 來處理excel 檔 用過了 openpyxl 還有 pyexcel目前覺得除了讀寫如果還要使用另外的功能 (像是讀取格子裡的公式)可以用 xlwings 他的首頁標題 " ...
- vSphere Client用户名密码保存记录
vSphere Client在访问ESXi主机或vCenter后是默认不保存登录用户名和密码的,不过可以通过修改配置文件来保存,方便访问连接. 方法如下: 打开配置文件路径(实际安装路径):D:\Pr ...
- 原创Couldn't read packet: Connection reset by peer 错误排查思路(推荐)
作为一个运维 不是你懂多少知识才是你的价值 你有幸能遇到多少错误才是你的最大的价值 知识 你有我有大家有 错误我有你没有 这便是我的价值 我遇到一个错误 蛮难遇到的一个错误 所以想分享给大家 下面我 ...
- 【C++】cerr,cout,clog
http://stackoverflow.com/questions/16772842/what-is-the-difference-between-cout-cerr-clog-of-iostrea ...
- DedeCMS文章标题长度最全修改方法
有时候DedeCMS首页或者其他页面不能全部展示文章标题,造成读者阅读体验差.一般来说标题精简.概括性强.有本文关键词就是一个好标题.写软文不比写“作文”,也不是论坛的标题党,软文是用来做排名的,主要 ...
- Android项目源码分享
http://blog.csdn.net/gao_chun/article/details/47263063 Android项目源码分享 给大家分享几个Android开发项目源码,大部分功能相信可以在 ...
- Android N requires the IDE to be running with Java 1.8 or later
Android Studio需要两个JDK: ide jdk和project jdk: 前者是IDE本身运行使用的JDK. 后者用于编译Java代码 Project JDK 可以通过file-&g ...