【链接】 我是链接,点我呀:)

【题意】

让你在树上找一个序列。
这个序列中a[1]=R
然后a[2],a[3]..a[d]它们满足a[2]是a[1]的祖先,a[3]是a[2]的祖先。。。
且w[a[1]]

【题解】

考虑一个naive的思路。
定义一个next[i]数组,表示i往上最近的权值大于i的节点所在的位置。
则我们每次输入2 R X的时候
可以从R到next[R],再到next[next[R]]
尽可能地累加和。
获取最大序列就好。

但显然会超时。

注意到我们这个next数组最后会把一些节点连在一起。

且越往上的话,权值越大。

则我们可以写一个树上倍增。

求出一个f[i][j]

表示i节点往上做2^j次next[i]操作得到的节点是什么。

以及sum[i][j]

表示i节点往上做2^j次next[i]操作的所有节点的权值和。

输入1 R W的时候。

看看w[R]是不是大于等于W

是的话f[++cnt][0] = R

否则

在R的f数组中往上走找到第一个权值大于W的节点。

(因为越往上权值越大,显然有单调性

作为f[++cnt][0]的值。

然后根据f[cnt][0]的值,以及因为cnt上面的节点的f数组都已经求出来了。

所以可以求出f[cnt][0..20]的值了。

再用类似的方法求出sum数组。

求最长路径的时候,用sum数组和f数组尽量往上走就可以了

注意边界。

加一些INF的值。

因为可能上面已经没有大于w的值了。

就会指向0了。

【代码】

#include <bits/stdc++.h>
#define ll long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1
using namespace std; const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int N = 4e5;
const int M = 20;
const ll INF = 1e16; int Q;
ll last,W[N+10],sum[N+10][M+10];
int f[N+10][M+10],cnt = 1; int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
cin >> Q;
for (int i = 0;i <= 20;i++) sum[1][i] = sum[0][i] = INF;
W[0] = INF;
rep1(i,1,Q){
ll ope,p,q;
cin >> ope >> p >> q;
if (ope==1){
ll R = p^last,w = q^last;
cnt++;
W[cnt]=w;
if (W[R]>=w){
f[cnt][0] = R;
}else{
ll now = R;
for (int i = 20;i >= 0;i--)
if (W[f[now][i]]<w){
now = f[now][i];
}
f[cnt][0] = f[now][0];
}
for (int i = 1;i <= 20;i++)
f[cnt][i] = f[f[cnt][i-1]][i-1]; sum[cnt][0] = W[f[cnt][0]]; for (int i = 1;i <= 20;i++){
if (f[cnt][i]==0)
sum[cnt][i] = INF;
else
sum[cnt][i] = sum[cnt][i-1]+sum[f[cnt][i-1]][i-1];
}
}else{
ll R = p^last,X = q^last;
int len = 0;
if (X<W[R]){
cout<<0<<endl;
last=0;
continue;
}
X-=W[R];
len++;
for (int i = 20;i >= 0;i--){
if (X>=sum[R][i]){
X-=sum[R][i];
R = f[R][i];
len+=(1<<i);
}
}
cout<<len<<endl;
last=len;
}
}
return 0;
}

【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) D】Tree的更多相关文章

  1. 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大 ...

  2. 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) B】Recursive Queries

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个记忆化搜索. 接近O(n)的复杂度吧 [代码] #include <bits/stdc++.h> using nam ...

  3. 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A】 Palindromic Supersequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 字符串倒着加到原串右边就好 [代码] #include <bits/stdc++.h> using namespace ...

  4. Codeforces 932 A.Palindromic Supersequence (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    占坑,明天写,想把D补出来一起写.2/20/2018 11:17:00 PM ----------------------------------------------------------我是分 ...

  5. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A

    2018-02-19 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 mega ...

  6. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined)

    靠这把上了蓝 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 megabyte ...

  7. Codeforces 932 C.Permutation Cycle-数学 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    C. Permutation Cycle   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. Codeforces 932 B.Recursive Queries-前缀和 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    B. Recursive Queries   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀

    A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  2. 洛谷 P2633 Count on a tree 主席树

    在一棵树上,我们要求点 $(u,v)$ 之间路径的第$k$大数. 对于点 $i$  ,建立 $i$  到根节点的一棵前缀主席树. 简单容斥后不难得出结果为$sumv[u]+sumv[v]−sumv[l ...

  3. 4----COM:a Generative Model for group recommendation(组推荐的一种生成模型)

    1.摘要: 组推荐的一个挑战性问题:因为不同组的成员就有不同的偏好,如何平衡这些组员的偏好是一个难以解决的问题. 在本文中,作者提出了一个COM的概率模型来建立组活动生成过程. 直觉上: 一个组中的用 ...

  4. python list set dict的简单应用示例

    list.count(x):返回指定元素x在列表中出现的次数 set(list):将list类型变量转换为set类型,去除重复值 dick:保存键值对 x=[1,2,2,3,3] s1=set(x) ...

  5. Linux的硬链接、软连接与拷贝

    Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).硬链接:创建一个与原文件任何信息都相同的目标文件(文件名可能不同,自由设定).硬连接的 ...

  6. 配置oh-my-zsh

    1. 当使用zsh进入庞大的git工程目录下时,会发生cd命令很慢的情况 可以把~/.oh-my-zsh/lib/git.zsh里面的git_prompt_info函数替换为 function git ...

  7. 没用私服,自己安装下本地jar包,制作坐标

    没用私服的话,自己安装下本地jar包吧.执行下面命令即可. -Dfile 代表jar存在路径.mvn install:install-file -DgroupId=alipay -DartifactI ...

  8. js模拟支付宝提交表单

    弄过支付宝的程序猿可能都知道,里面有非常多地方都用到了自提交表单的方式,支付宝的接口通过请求API的形式取得server返回的表单字符串,使用out.print("表单字符串")在 ...

  9. 适配 iOS 8 时遇到的问题两则:远程推送和 Unwind Segue

    原文:http://imtx.me/archives/1910.html 昨天我在微博上吐槽:iOS 8 / Xcode 6 真是史上对开发人员最糟糕的版本号了.收到非常多朋友表达同感. 之所以这么说 ...

  10. Python图像处理库PIL的ImageStat模块介绍

    ImageStat模块用于计算整个图像或者图像的一个区域的统计数据. 一.ImageStat模块的函数 1.  Stat 定义1:ImageStat.Stat(image)⇒ Stat instanc ...