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) ...
随机推荐
- SAE云平台上传图片和发送邮件
1.远程图片保存至Storage 其中public是Storage中的容器名,"目录1/目录2/"是容器下的路径 $file_content 是得到的文件数据 $s = new S ...
- JS window.open()属性
一. Window 对象 Window 对象是 JavaScript 层级中的顶层对象. Window 对象代表一个浏览器窗口或一个框架. Window 对象会在 <body> 或 < ...
- 写了个简单的pdo的封装类
<?php class PD { //造对象 public $dsn = "mysql:dbname=test2;host=localhost"; //数据库类型,数据库名和 ...
- Android自学指导
如果想自学Android,以下的文章可以作为参考: 如何自学Android(Gityuan) 那两年炼就的Android内功修养(老罗的Android之旅)
- kvm 下运行的 WINWS7磁盘空间不足 增加磁盘 实战(这个有问题,还未解决)
创建一个新硬盘: [root@NB vhost]# qemu-img create -f qcow2 add_win_desk.img 5G Formatting encryption=off clu ...
- 为 ASP.NET Web API 创建帮助页面(转载)
转载地址:http://www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages 当创建web API 时,经常要创 ...
- Algorithms, Part I by Kevin Wayne, Robert Sedgewick
Welcome to Algorithms, Part I 前言 昨天在突然看到了Coursera上Robert Sedgewick讲的Algorithms,Part II看了一些,甚是爽快,所以又去 ...
- python实现学生选课系统 面向对象的应用:
一.要求: 选课系统 管理员: 创建老师:姓名.性别.年龄.资产 创建课程:课程名称.上课时间.课时费.关联老师 使用pickle保存在文件 学生: 学生:用户名.密码.性别.年龄.选课列表[].上课 ...
- 【JAVA集合框架之List】
一.List接口概述. List有个很大的特点就是可以操作角标. 下面开始介绍List接口中相对于Collection接口比较特别的方法.在Collection接口中已经介绍的方法此处就不再赘述. 1 ...
- Win10 for Phone 裁剪控件
<Page.BottomAppBar> <CommandBar x:Name="appBar"> <AppBarButton Label=" ...