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) ...
随机推荐
- PHP常用函数大全。
php usleep() 函数延迟代码执行若干微秒. unpack() 函数从二进制字符串对数据进行解包. uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID. time_sleep_ ...
- python中random模块使用
- Linux内存性能指标、CPU性能指标
内存性能指标 内存基础概念 先执行一下 top 命令,看结果中关于内存的相关部分 # top 其中的 VIRT.RES.SWAP 都是什么呢? 分别是下面的3个概念: 物理内存 Resident - ...
- SQLServer系统监控
http://blog.sina.com.cn/s/blog_519d269c0100gx09.html http://blog.csdn.net/qxlwuyuhui0801/article/det ...
- 设计模式学习之中介者模式(Mediator,行为型模式)(18)
转载地址:http://www.cnblogs.com/zhili/p/MediatorPattern.html 一.引言 在现实生活中,有很多中介者模式的身影,例如QQ游戏平台,聊天室.QQ群和短信 ...
- I-number
以下是真坑爹题目: 此题必须输出前导零,否则永远a不了 I-number Time Limit: 5000MS Memory limit: 65536K 题目描述 The I-number of x ...
- GoLang搞一个基本的HTTP服务
慢慢和python的对应一下看看. package main import ( "fmt" "net/http" "strings" &qu ...
- C# DateTime时间格式转换为Unix时间戳格式
double ntime=dateTimeToUnixTimestamp(DateTime.Now); long g1 = GetUnixTimestamp(); long g2 = ConvertD ...
- 限制非安全IP访问
这个是一个检测ip是否非法的php函数,适应于白名单,黑名单功能开发,主要场景应用于:api来源限制,访问限制等. /** * 安全IP检测,支持IP段检测 * @param string $ip 要 ...
- C# Settings使用小结
本篇博客将介绍C#中Settings的使用. 首先介绍一个桌面程序中的例子,当我们新安装一个软件,软件启动后会有例如新手指导等窗体弹出来,每次都需要自己去关闭它.当然这些软件都会提供例如不再显示等功能 ...