【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 ...
随机推荐
- 是否可以从一个static方法内部发出对非static方法的调用
不可以.因为非static方法是要与对象关联在一起的,必须创建一个对象后,才可以在该对象上进行方 法调用,而static方法调用时不需要创建对象,可以直接调用.也就是说,当一个static方法被调用时 ...
- camke 参数
cmake -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \ -DMYSQL_DATADIR=/application/mysql-5.5.32 ...
- [SDOI2008]郁闷的小J(分块)
[SDOI2008]郁闷的小J 题目描述 小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他面临着被解雇的危 ...
- HDU 4945 2048 DP 组合
思路: 这个题写了一个背包的解法,超时了.搜了下题解才发现我根本不会做. 思路参见这个: 其实我们可以这样来考虑,求补集,用全集减掉不能组成2048的集合就是答案了. 因为只要达到2048就可以了,所 ...
- Kneser猜想与相关推广
本文本来是想放在Borsuk-Ulam定理的应用这篇文章当中.但是这个文章实在是太长,导致有喧宾夺主之嫌,从而独立出为一篇文章,仅供参考.$\newcommand{\di}{\mathrm{dist} ...
- 【codeforces 466D】Increase Sequence
[题目链接]:http://codeforces.com/problemset/problem/466/D [题意] 给你n个数字; 让你选择若干个区间; 且这些区间[li,ri]; 左端点不能一样; ...
- Qt之QNetworkProxy(网络代理)
简述 QNetworkProxy类提供了一个网络层代理. QNetworkProxy提供了配置网络层代理支持Qt网络类的方法.目前支持的类有QAbstractSocket.QTcpSocket.QUd ...
- reactor模式与java nio
Reactor是由Schmidt, Douglas C提出的一种模式,在高并发server实现中广泛採用. 改模式採用事件驱动方式,当事件出现时,后调用对应的事件处理代码(Event Handl ...
- spring注解中@component是什么意思
@Component("userManager") public class UserManagerImpl implements UserManager { private Us ...
- 图像切割—基于图的图像切割(Graph-Based Image Segmentation)
图像切割-基于图的图像切割(Graph-Based Image Segmentation) Reference: Efficient Graph-Based Image Segmentation ...