D. Tree
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You are given a node of the tree with index 1 and with weight 0. Let cnt be the number of nodes in the tree at any instant (initially, cnt is set to 1). Support Q queries of following two types:

  •  Add a new node (index cnt + 1) with weight W and add edge between node R and this node.
  •  Output the maximum length of sequence of nodes which
    1. starts with R.
    2. Every node in the sequence is an ancestor of its predecessor.
    3. Sum of weight of nodes in sequence does not exceed X.
    4. For some nodes i, j that are consecutive in the sequence if i is an ancestor of j then w[i] ≥ w[j] and there should not exist a node k on simple path from i to j such that w[k] ≥ w[j]

The tree is rooted at node 1 at any instant.

Note that the queries are given in a modified way.

Input

First line containing the number of queries Q (1 ≤ Q ≤ 400000).

Let last be the answer for previous query of type 2 (initially last equals 0).

Each of the next Q lines contains a query of following form:

  • 1 p q (1 ≤ p, q ≤ 1018): This is query of first type where  and . It is guaranteed that 1 ≤ R ≤ cnt and0 ≤ W ≤ 109.
  • 2 p q (1 ≤ p, q ≤ 1018): This is query of second type where  and . It is guaranteed that 1 ≤ R ≤ cntand 0 ≤ X ≤ 1015.

 denotes bitwise XOR of a and b.

It is guaranteed that at least one query of type 2 exists.

Output

Output the answer to each query of second type in separate line.

Examples
input
6
1 1 1
2 2 0
2 2 1
1 3 0
2 2 0
2 2 2
output
0
1
1
2
input
6
1 1 0
2 2 0
2 0 3
1 0 2
2 1 3
2 1 6
output
2
2
3
2
input
7
1 1 2
1 2 3
2 3 3
1 0 0
1 5 1
2 5 0
2 4 0
output
1
1
2
input
7
1 1 3
1 2 3
2 3 4
1 2 0
1 5 3
2 5 5
2 7 22
output
1
2
3
Note

In the first example,

last = 0

- Query 1: 1 1 1, Node 2 with weight 1 is added to node 1.

- Query 2: 2 2 0, No sequence of nodes starting at 2 has weight less than or equal to 0. last = 0

- Query 3: 2 2 1, Answer is 1 as sequence will be {2}. last = 1

- Query 4: 1 2 1, Node 3 with weight 1 is added to node 2.

- Query 5: 2 3 1, Answer is 1 as sequence will be {3}. Node 2 cannot be added as sum of weights cannot be greater than 1. last = 1

- Query 6: 2 3 3, Answer is 2 as sequence will be {3, 2}. last = 2

题目大意:一棵树,每个点有点权,两种操作:1.新加一个点连接r,权值为w.  2.从一个点r开始往祖先上跳,每次跳到第一个值≥自身的祖先,将跳到的点的和加起来,不能大于w,问能跳几次.

分析:挺有意思的一道题.

   暴力算法就是一个一个往上跳着找喽,在树上往上跳有一种常用的优化方法--倍增.在这道题里面可以倍增地跳到≥自身权值的点.

   考虑怎么实现,fa数组就不能记录第2^i个祖先了,而要记录比自身权值大的第2^i个祖先.在加点的时候处理.可以发现,一旦处理出fa[i][0],就能够根据祖先节点的信息推出fa[i][j].

   如何处理fa[i][0]?如果r的权值比新加的点i的权值大或相等,则fa[i][0] = r,否则从r开始往上跳,如果w[fa[r][j]] < w[i],则往上跳,最后fa[i][0] = fa[r][0].

   因为最后要求和嘛,可以顺便维护一个sum数组,表示从i这个点跳到比i权值大的第2^j个祖先跳到的点的权值和为多少. 求出了这两个数组以后查询就很好办了,sum[i][j]是否≤w,是的话就往上跳,并且w -= sum[i][j].倍增的基础应用嘛.

   想清楚如何加速往祖先跳的过程,以及倍增应该维护什么东西这道题就能解决了.

   一些边界的值需要特殊考虑!

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
const ll maxn = ,inf = 1e18;
ll q,lastans,cnt = ,w[maxn];
ll fa[maxn][],sum[maxn][]; void add(ll x,ll v)
{
w[++cnt] = v;
if (w[cnt] <= w[x])
fa[cnt][] = x;
else
{
int y = x;
for (int i = ; i >= ; i--)
{
if (w[fa[y][i]] < w[cnt])
y = fa[y][i];
}
fa[cnt][] = fa[y][];
}
if (fa[cnt][] == )
sum[cnt][] = inf;
else
sum[cnt][] = w[fa[cnt][]];
for (int i = ; i <= ; i++)
{
fa[cnt][i] = fa[fa[cnt][i - ]][i - ];
if (fa[cnt][i] == )
sum[cnt][i] = inf;
else
sum[cnt][i] = sum[cnt][i - ] + sum[fa[cnt][i - ]][i - ];
}
} ll query(ll x,ll v)
{
if (w[x] > v)
return ;
v -= w[x];
ll res = ;
for (int i = ; i >= ; i--)
{
if (v >= sum[x][i])
{
v -= sum[x][i];
res += ( << i);
x = fa[x][i];
}
}
return res;
} int main()
{
w[] = inf;
for (int i = ; i <= ; i++)
sum[][i] = inf;
scanf("%I64d",&q);
while (q--)
{
int id;
ll a,b;
scanf("%d",&id);
scanf("%I64d%I64d",&a,&b);
a ^= lastans;
b ^= lastans;
if (id == )
add(a,b);
else
printf("%I64d\n",lastans = query(a,b));
}
}

Codeforces 932.D Tree的更多相关文章

  1. Problem - D - Codeforces Fix a Tree

    Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...

  2. Codeforces 765 E. Tree Folding

    题目链接:http://codeforces.com/problemset/problem/765/E $DFS子$树进行$DP$ 大概分以下几种情况: 1.为叶子,直接返回. 2.长度不同的路径长度 ...

  3. Codeforces 932 E. Team Work(组合数学)

    http://codeforces.com/contest/932/problem/E 题意:   可以看做 有n种小球,每种小球有无限个,先从中选出x种,再在这x种小球中任选k个小球的方案数 选出的 ...

  4. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  5. CodeForces 383C Propagating tree

    Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  6. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  9. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

随机推荐

  1. Ubuntu16.04使用Tarball安装ntp

    最近在学习linux,看书上例子(鸟哥的linux私房菜 P674),使用Tarball来安装ntp,出了点问题,提示错误,使用 ./configure 来检测程序时,出现如下提示: 提示少了 ope ...

  2. Python学习之web框架 Flask

    一.通过PIP 安装Flask 1.1 Windows环境安装pip A.首先PIP进入官网(https://pypi.python.org/pypi/pip)下载gz包 B.对gz压缩包进行解压,解 ...

  3. Blockchain For Dummies(IBM Limited Edition

    Blockchain For Dummies(IBM Limited Edition)笔记 该系列内容主要介绍用于商业的区块链,有人说区块链之于贸易,犹如因特网之于信息.在商业领域区块链可以用于交易任 ...

  4. dp算法之硬币找零问题

    题目:硬币找零 题目介绍:现在有面值1.3.5元三种硬币无限个,问组成n元的硬币的最小数目? 分析:现在假设n=10,画出状态分布图: 硬币编号 硬币面值p 1 1 2 3 3 5 编号i/n总数j ...

  5. Android 7.1.1 又出幺蛾子了 —— 再谈 Android 上的 Wifi 连接

    在之前的博客文章中,我写了点在 Android 6 系统中连接到指定名称的 Wifi 的体验.然而,在 Android 7 中,有一些东西又变化了.另外就是在那篇文章中我说要提供代码,结果拖到这篇文章 ...

  6. eclipse建包的一些细节

    com.a :com.b 等会先在com文件夹下在 建立 a,b两个子文件夹,引用路径时 不可"*\\com.a\\*"而是"*\\com\\a\\*"这点基础 ...

  7. rabbitmqctl 的常用命令

    # 查看服务器的状态 rabbitmqctl status   # 查看环境变量 rabbitmqctl environment   # 停止rabbitmq的应用 rabbitmqctl stop_ ...

  8. 3dContactPointAnnotationTool开发日志(三一)

      在玩的时候遇到了一个python的问题: Traceback (most recent call last): File ".\convert.py", line 13, in ...

  9. 【第二周】Java实现英语文章词频统计

    1.需求:对于给定的英文文章进行单词频率的统计 2.分析: (1)建立一个如下图所示的数据库表word_frequency用来存放单词和其对应数量 (2)Scanner输入要查询的英文文章存入Stri ...

  10. 【第八周】【新蜂】新NABCD

    由小组成员宫成荣撰写 一.小组项目申请时提交的NABCD: 痛点:普通的俄罗斯方块是不现实距离下一级有多远的,我们的游戏能显示距离下一等级游戏有多远.方便玩家体验. nabc: n:能满足大多数玩家的 ...