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

【题意】

让你在树上找一个序列。
这个序列中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. ActiveMQ学习笔记(16)----Message Dispatch高级特性(二)

    1. Optimized Acknowledgetment ActiveMQ缺省支持批量确认消息,由于批量确认会提高性能,如果希望在应用程序中禁止经过优化的确认方式,可以采用以下几种方式: 1. 在C ...

  2. [洛谷P3939]数颜色

    题目大意:有n个物品,每个物品有一个颜色.现在有两种操作:1.查询l-r内有多少颜色为c的物品并输出.2.将第x个物品和第x+1个交换.现在让你实现这些操作. 解题思路:首先一共有300000种颜色, ...

  3. python做的 QQ未读消息图像

    #!/usr/bin/pythonfrom PIL import Image ,ImageDraw, ImageFont#打开所在的文件im=Image.open('test.jpg')#获取图片对象 ...

  4. Ubuntu 16.04 Chrome浏览器安装flash player插件

    1:官网下载插件  flash palyer lash_player_npapi_linux_debug.x86_64.tar.gz 2:解压 提取 libpepflashplayer.so 3:手动 ...

  5. Regular Expressions Syntax

    https://support.syslogwatcher.com/support/solutions/articles/8000033627-regular-expressions-syntax

  6. Unix发展史

    简述 了解过去,我们才能知其然,更知所以然.总结过去,我们才会知道明天该何去何从.在时间的滚轮中,许许多多的东西就像流星一样一闪而逝,而有些东西却能经受着时间的考验散发着经久的魅力,让人津津乐道.流传 ...

  7. Qt之字体文件(TTF)

    简述 TTF(TrueTypeFont)是Apple公司和Microsoft公司共同推出的字体文件格式,随着windows的流行,已经变成最常用的一种字体文件表示方式. 在一些特殊的场合,系统字符集不 ...

  8. What&#39;s Wrong With Hue Oozie Editor?

    本文原文出处: http://blog.csdn.net/bluishglc/article/details/47021019 严禁不论什么形式的转载,否则将托付CSDN官方维护权益! First, ...

  9. vijos - P1732能量採集 (状态转移)

    P1732能量採集 Accepted 标签:NOI2010[显示标签] 背景 描写叙述 栋栋有一块长方形的地.他在地上种了一种能量植物,这样的植物能够採集太阳光的能量. 在这些植物採集能量后,栋栋再使 ...

  10. Dictionaries

    A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dict ...