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)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- Python os.walk文件遍历
os.walk(top, topdown=True, onerror=None, followlinks=False) 可以得到一个三元tupple(dirpath, dirnames, filena ...
- idea 创建多模块时模块类无法引入
我的原因是类的位置方的不对,由于刚搭建的项目,本来只想做个测试,就直接在java下创建类,然而这居然是个深坑,模块引入了也无法引入这个模块的类. 解决方法:创建com.***.***包后的类可以正常引 ...
- eclipse中编写代码时如何自动提示变量名?
打开 Eclipse -> Window -> Perferences -> Java -> Editor -> Content Assist,在右边最下面一栏找到 a ...
- 图论:Prufer编码-Cayley定理
BZOJ1430:运用Cayley定理解决树的形态统计问题 由Prufer编码可以引申出来一个定理:Cayley 内容是不同的n结点标号的树的数量为n^(n-2) 换一种说法就是一棵无根树,当知道结点 ...
- [洛谷P4768] [NOI2018]归程 (kruskal重构树模板讲解)
洛谷题目链接:[NOI2018]归程 因为题面复制过来有点炸格式,所以要看题目就点一下链接吧\(qwq\) 题意: 在一张无向图上,每一条边都有一个长度和海拔高度,小\(Y\)的家在\(1\)节点,并 ...
- mysql创建用户,并授权
1.创建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; host分下列3种情况:'%' - 所有情况都能访问‘localhost’ ...
- 阿里云服务器部署笔记二(python3、Flask、uWSGI、Nginx)
从git上把项目拉到服务器,项目可以在服务器上运行后,就只需要配置uwsgi和nginx了.它们的逻辑关系是:外部请求->nginx->uwsgi->项目实例. 一.配置uwsgi ...
- URI设计原则
以下是与 REST API 相关的重要术语: 资源(Resource) 是一个对象或对某物的表示.它有一些相关联的数据,并有一组方法进行操作. 例如:动物,学校和员工是资源.这些资源都有着删除,添加, ...
- GCC在C语言中内嵌汇编 asm __volatile__ 【转】
转自:http://blog.csdn.net/pbymw8iwm/article/details/8227839 在内嵌汇编中,可以将C语言表达式指定为汇编指令的操作数,而且不用去管如何将C语言表达 ...
- GDB实战
程序中除了一目了然的Bug之外都需要一定的调试手段来分析到底错在哪.到目前为止我们的调试手段只有一种:根据程序执行时的出错现象假设错误原因,然后在代码中适当的位置插入 printf ,执行程序并分析打 ...