【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 ...
随机推荐
- AbstractQueuedSynchronizer中CAS的疑惑
这段代码是AQS框架中将当前节点入队的操作. Node pred = tail; if (pred != null) { node.prev = pred; if (compareAndSetTail ...
- swift语言点评十八-异常与错误
1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...
- 前端那些事之----jQuery
1.jquery是什么 一个js的框架,可以方便的使用js 2 什么是jQuery对象 是由jQuery封装后的DOM对象 注意:与DOM对象的方法不同,不可以混用,但是可以相 ...
- C语言传参的类型匹配
有一个这样的问题: 形参const char *p和实参char *c可以匹配 形参const char**p和实参char**c不可以匹配 注:argument和parameter:严格而言,par ...
- ActiveMQ客户端配置使用
一.通过JNDI来使用ActiveMQ 1.jndi配置JMS对象 java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQIni ...
- ubuntu 16.04 安装KVM-多系统
为了使用QQ 只能再跑一个Windows了
- 【转载】python学习之 字符串前'r'的用法
文章转载:https://www.cnblogs.com/cyiner/archive/2011/09/18/2180729.html 在打开文件的时候open(r'c:\....') 加r和不加'' ...
- Uart,IIC和SPI的区别
1.UART, SPI, IIC的详解 UART.SPI.IIC是经常用到的几个数据传输标准,下面分别总结一下: UART(Universal Asynchronous Receive Transmi ...
- Java基础学习总结(46)——JAVA注解快速入门
各位开发童鞋,注解这个东西我们肯定每天都能看见,也许有时候看的太多了到是会忽略注解这东西具体是如何工作的.今天在这里用最短的篇幅快速讲解下注解的原理,对这块记的不太清楚的同学也可以再次看看,下次有人详 ...
- Linux 设备驱动之 UIO 机制(基本概念)
一个设备驱动的主要任务有两个: 1. 存取设备的内存 2. 处理设备产生的中断 对于第一个任务.UIO 核心实现了mmap()能够处理物理内存(physical memory),逻辑内存(logica ...