Spoj 10628. Count on a tree 题解
题目大意:
给定一棵n个点的树,每个点有一个权值,m个询问,每次询问树上点x到点y的路径上的第k小数。
思路:
dfs后给每个节点一个dfs序,以每个点在他父亲的基础上建立主席树,询问时用(点x+点y-点lca(x,y)-点dad[lca(x,y)])即可得到x到y的链,在上面查询即可。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 200009
using namespace std; int tot=,dfn,num,cnt,pa[N][],to[N],next[N],head[N],lc[N*],rc[N*],deep[N],sum[N*],id[N],pos[N],root[N],a[N],b[N]; int read()
{
int x=,y=;char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') y=-;ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-;ch=getchar();}
return x*y;
} void add(int x,int y)
{
to[++cnt]=y,next[cnt]=head[x],head[x]=cnt;
} void dfs(int x)
{
int i;id[x]=++dfn,pos[dfn]=x;
for (i=;i<=;i++)
if ((<<i)<=deep[x]) pa[x][i]=pa[pa[x][i-]][i-];
else break;
for (i=head[x];i;i=next[i])
if (pa[x][]!=to[i])
{
deep[to[i]]=deep[x]+;
pa[to[i]][]=x;
dfs(to[i]);
}
} void change(int l,int r,int x,int &cur,int _cur)
{
cur=++num;
lc[cur]=lc[_cur];
rc[cur]=rc[_cur];
sum[cur]=sum[_cur]+;
if (l==r) return;
int mid=l+r>>;
if (x<=b[mid]) change(l,mid,x,lc[cur],lc[_cur]);
else change(mid+,r,x,rc[cur],rc[_cur]);
} int LCA(int x,int y)
{
if (deep[x]<deep[y]) swap(x,y);
int i,t=deep[x]-deep[y];
for (i=;i<=;i++)
if ((<<i)&t) x=pa[x][i];
for (i=;i>=;i--)
if (pa[x][i]!=pa[y][i]) x=pa[x][i],y=pa[y][i];
if (x==y) return x;
return pa[x][];
} int ask(int x,int y,int k)
{
int a=root[id[x]],b=root[id[y]],c=LCA(x,y),d=pa[c][],l=,r=tot;
c=root[id[c]],d=root[id[d]];
while (l<r)
{
int t=sum[lc[a]]+sum[lc[b]]-sum[lc[c]]-sum[lc[d]],mid=l+r>>;
if (t>=k) a=lc[a],b=lc[b],c=lc[c],d=lc[d],r=mid;
else a=rc[a],b=rc[b],c=rc[c],d=rc[d],l=mid+,k-=t;
}
return l;
} int main()
{
int n=read(),m=read(),i,x,y,ans=,k;
for (i=;i<=n;i++) a[i]=read(),b[i]=a[i];
for (i=;i<n;i++) x=read(),y=read(),add(x,y),add(y,x);
dfs(),sort(b+,b+n+);
for (i=;i<=n;i++)
if (b[tot]!=b[i]) b[++tot]=b[i];
for(i=;i<=n;i++) change(,tot,a[pos[i]],root[i],root[id[pa[pos[i]][]]]);
for (i=;i<=m;i++)
{
x=read(),y=read(),k=read(),x^=ans;
printf("%d",ans=b[ask(x,y,k)]);
if (i<m) printf("\n");
}
return ;
}
Spoj 10628. Count on a tree 题解的更多相关文章
- 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 ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 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 ...
- 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( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- 2588: Spoj 10628. Count on a tree
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5766 Solved: 1374 ...
- 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个节点的树,每个点 ...
- 主席树 || 可持久化线段树 || 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) ...
随机推荐
- JS添加MD5,JS提示框
http://pan.baidu.com/s/1kTmSp9t
- SQL中的多表查询,以及JOIN的顺序重要么?
说法是,一般来说,JOIN的顺序不重要,除非你要自己定制driving table. 示例: SELECT a.account_id, c.fed_id, e.fname, e.lname -> ...
- git branch用法总结
git branch git branch 不带参数:列出本地已经存在的分支,并且在当前分支的前面加“*”号标记,例如: #git branch* master newbranch ...
- 攻城狮在路上(叁)Linux(三十)--- 光盘写入工具
一.基本步骤: 1.用mkisofs命令将所需备份的数据构建成镜像文件. 2.用cdrecord命令将镜像文件刻录至光盘或者DVD中. 二.mkisofs:新建镜像文件 mkisofs [-0 镜像文 ...
- php mysql PDO基本操作
<?php $dbh = new PDO('mysql:host=localhost;dbname=localhost', 'root', ''); $dbh->setAttribute( ...
- JSON资料汇总
网络入门学习资料 1.W3School的JSON教程:http://www.w3school.com.cn/json/index.asp 2.Introducing JSON[介绍JSON]:http ...
- ObCallback回调钩子检测
ObCallback回调钩子检测 2013-12-20 Nie.Meining Ring0 在 PatchGuard 的摧残下,通过 ObRegisterCallbacks 函数注册回调钩子已经成了 ...
- Arduino101学习笔记(五)—— 模拟IO
1.配置IO管脚 //***************************************************************************************** ...
- Asp.Net Mvc 控制器与视图的数据传递
数据传递也就是控制器和视图之间的交互,比如在视图中提交的数据,在控制器怎么获取,或者控制器从业务层获得一些数据,怎么传递到视图中,让视图显示在客户端呢?带着这些疑问,我们接着看.. 下面 ...
- a标签 打电话 发邮件
打电话<a href=”tel:010-13220163333″>13220163333</a> 发邮件<a href=”mailto:sb@you.com”>发送 ...