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) ...
随机推荐
- 【C#】Json数据 排版算法
我从服务器上取得一串Json数据,然后想表示到画面上.不过服务器上取下的Json数据肯定是经过压缩的,空格和换行都没有.如果直接看,可读性非常差. 由于我这个软件是内部管理用的,使用者既能直接看懂Js ...
- 设计模式学习之代理模式(Proxy,结构型模式)(11)
参考地址:http://www.cnblogs.com/zhili/p/ProxyPattern.html 一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访 ...
- c# 扩展方法奇思妙用基础篇八:Distinct 扩展(转载)
转载地址:http://www.cnblogs.com/ldp615/archive/2011/08/01/distinct-entension.html 刚看了篇文章 <Linq的Distin ...
- Pyqt清空Win回收站
Pyqt清空回收站其实的调用Python的第三方库,通过第三方库调用windows的api删除回收站的数据 一. 准备工作 先下载第三方库winshell 下载地址: https://github.c ...
- Jquery自定义扩展方法(一)
jquery是一款流行的JS框架,自定义JS方法,封装到Jquery中,调用起来也挺方便的,怎么写Jquery扩展方法那,网上翻阅了一部分代码,其实也挺简单的: 方式一: (jQuery.fn.set ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- RTCP资料详解
转自:http://www.360doc.com/content/13/0606/10/1317564_290865866.shtml RTCP RTCP协议将控制包周期发送给所有连接者,应用与数据包 ...
- How to install the zsh shell in Linux && how to set it as a default login shell
Z shell’s (zsh) popularity has increased in the last years. I have not moved too zsh yet, but I am g ...
- POJ 1830 高斯消元
开关问题 Description 有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为 ...
- 创建对象为什么要 init?
self 为什么要赋值为[super init]:”,当程序进入到init这个方法的时候,系统已经生成了对象并分配了存储空间,在调用[super init]是为了初始化父类对象,在父类对象初始化过程序 ...