【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) D】Tree
【链接】 我是链接,点我呀:)
【题意】
让你在树上找一个序列。
这个序列中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的更多相关文章
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大 ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) B】Recursive Queries
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个记忆化搜索. 接近O(n)的复杂度吧 [代码] #include <bits/stdc++.h> using nam ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A】 Palindromic Supersequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 字符串倒着加到原串右边就好 [代码] #include <bits/stdc++.h> using namespace ...
- 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 ----------------------------------------------------------我是分 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- POJ 3320 Jessica's Reading Problem (尺取法,时间复杂度O(n logn))
题目: 解法:定义左索引和右索引 1.先让右索引往右移,直到得到所有知识点为止: 2.然后让左索引向右移,直到刚刚能够得到所有知识点: 3.用右索引减去左索引更新答案,因为这是满足要求的子串. 4.不 ...
- SQL 的stuff函数
1.作用 删除指定长度的字符,并在指定的起点处插入另一组字符. 2.语法 STUFF ( character_expression , start , length ,character_expres ...
- NYOJ 16 矩形嵌套【DP】
解题思路:呃,是看的紫书上面的做法,一个矩形和另一个矩形之间的关系就只有两种,(因为它自己是不能嵌套自己的),可嵌套,不可嵌套,是一个二元关系,如果可嵌套的话,则记为1,如果不可嵌套的话则记为0,就可 ...
- 搭建appium环境
1.下载jdk1.8 配置环境变量 JAVA_HOME---------->你的jdk路径 path---------------------->%JAVA_HOME%\bin;%JAV ...
- hadoop从wordCount开始
最近一段时间大数据很火,我有稍微有点java基础,自然选择了由java编写的hadoop框架,wordCount是hadoop中类似于java中helloWorld的存在,自然不能错过. packag ...
- Iterator(迭代器) 和generator
数组是可迭代的 var a = []; console.dir(a); 发现这里有一个Symbol.iterator ,说明它是可迭代的. object 是不可以迭代的 var a = {} cons ...
- apicloud 上传/更新App版本到 ios store 流程步骤
app更新 上传APP的地址: https://itunesconnect.apple.com/login 苹果开发者中心: https://developer.apple.com/ app正式包更新 ...
- JSON.stringify(),JSON.parse(),eval(string)
JSON.stringify()用于从一个对象解析出字符串 : var obj = {"name":"week","age":" ...
- AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件
个人使用记录 1:KEY 授权 chmod 400 VPN.pem 2:连接 ssh -i "VPN.pem" ubuntu@ec2-54-183-119-93.us-west-1 ...
- 在使用shape的同一时候,用代码改动shape的颜色属性
Android里面常常会使用shape来定制一些View的背景 能够改动View的背景颜色.形状等属性 普通情况下.shape都是在xml文件中面写死了.今天遇到一个需求,View的形状是圆角的,可是 ...