BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree
Time Limit: 12 Sec Memory Limit: 128 MB
Submit: 9280 Solved: 2421
[Submit][Status][Discuss]
Description
Input
Output
M行,表示每个询问的答案。最后一个询问不输出换行符
Sample Input
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2
Sample Output
8
9
105
7
HINT
//BZOJ 2588 可持久化线段树+LCA
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m
#define rson m+1,r int ans,tot,cnt,lca;
int n,d,q;
int a[maxn],b[maxn],h[maxn],L[maxn<<],R[maxn<<];
int to[maxn<<],head[maxn],Next[maxn<<];//存图
int sum[maxn<<],root[maxn],dep[maxn];
int fa[maxn][]; void add(int x,int y)//存图
{
tot++;
Next[tot]=head[x];
head[x]=tot;
to[tot]=y;
} int LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
for(int i=;i<=;i++) if(((dep[x]-dep[y])&(<<i))!=) x=fa[x][i];//不知道为什么我注释掉的那种写法为什么不对。。。
//for(int i=0;i<=19;i++) if(dep[x]==dep[y]-(1<<i)) x=fa[x][i];
if(x==y) return x;
for(int i=;i>=;i--){
if(fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
}
return fa[x][];
} void update(int pre,int &rt,int l,int r,int k)
{
rt=++cnt;sum[rt]=sum[pre]+;
L[rt]=L[pre];R[rt]=R[pre];
if(l==r){
return ;
} int m=(l+r)>>;
if(k<=m) update(L[pre],L[rt],lson,k);
else update(R[pre],R[rt],rson,k);
} int query(int x,int y,int lca,int fa,int l,int r,int k)
{
if(l==r){
return h[l];
} int m=(l+r)>>;
int now=sum[L[x]]+sum[L[y]]-sum[L[lca]]-sum[L[fa]];
if(now>=k) return query(L[x],L[y],L[lca],L[fa],lson,k);
else return query(R[x],R[y],R[lca],R[fa],rson,k-now);
} void dfs(int x,int fath)//按照dfs进行更新
{
dep[x]=dep[fath]+;
int k=lower_bound(b+,b++d,a[x])-b;
h[k]=a[x];
update(root[fath],root[x],,n,k);
for(int i=;i<=;i++){
fa[x][i]=fa[fa[x][i-]][i-];
}
for(int i=head[x];i;i=Next[i]){
if(to[i]!=fath){
fa[to[i]][]=x;
dfs(to[i],x);
}
}
} int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
d=unique(b+,b++n)-(b+);//去重建立权值线段树
for(int i=;i<n;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(,);
while(q--){
int x,y,k;
scanf("%d%d%d",&x,&y,&k);
x=x^ans;
lca=LCA(x,y);
ans=query(root[x],root[y],root[lca],root[fa[lca][]],,n,k);//按照LCA,从x到lca,然后从lca到y,去掉lca的爸爸。
printf("%d\n",ans);
}
}
BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)的更多相关文章
- BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)
题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submi ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- ●BZOJ 2588 Spoj 10628. Count on a tree
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- cin.get()、流和缓冲区
大家好,这是我在CSDN的第一篇博客.我是一名学习GIS专业的大学生.我从小开始喜欢编程,可是到现在编程水平却长进不大,依然是菜鸟一个.究其原因,虽然这些年乱七八糟的东西学过不少,但是总的来说还是基础 ...
- 51Nod 1094 和为k的连续区间 | 水
Input示例 6 10 1 2 3 4 5 6 Output示例 1 4 #include "cstdio" #include "algorithm" #in ...
- jedis,spring-redis-data 整合使用,版本问题异常
jedis,spring-redis-data 整合使用,版本不匹配的时候经常会报一些异常,例如1: java.lang.NoClassDefFoundError: org/springframewo ...
- PHP扩展--APC缓存安装与使用
apc安装 wget http://pecl.php.net/get/APC-3.1.13.tgz tar zxvf APC-3.1.13.tgz cd APC-3.1.13 /usr/local/p ...
- noip2012~2015刷题小记录
2012d1t1 密码 模拟题 #include<cstdio> #include<cstdlib> #include<cstring> #include<c ...
- 【BZOJ】1270 [BeijingWc2008]雷涛的小猫
[算法]DP [题解]f1[i]表示第i棵树当前高度能得到的最多果子数 f2[i]表示高度i能得到的最多果子数. 于是有: f1[j]=max(f1[j],f2[i+delta])+mp[j][i]; ...
- 【洛谷 P5110】 块速递推(矩阵加速,分块打表)
题目链接 掌握了分块打表法了.原来以前一直想错了... 块的大小\(size=\sqrt n\),每隔\(size\)个数打一个表,还要在\(0\text{~}size-1\)每个数打一个表. 然后就 ...
- java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 找不到jar包的问题,路径问题
1.参考连接: https://blog.csdn.net/huangbiao86/article/details/6428608 折腾了一上午,找到了这错误的原因.哎……悲剧! 确认包已经被导入we ...
- C# IEqualityComparer 使用方法 Linq Distinct使用方法
创建 IEqualityComparer的接口类必须实现Equals和GetHashCode方法 public class TipComparer : IEqualityComparer<Tip ...
- vue路由-动态路由和嵌套路由
一.动态路由 我们经常需要把某种模式匹配到的所有路由,全都映射到同个组件.例如,我们有一个 User 组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染.那么,我们可以在 vue-route ...