bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】
算是板子,把值离散化,每个点到跟上做主席树,然后查询的时候主席树上用u+v-lca-fa[lca]的值二分
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
const int N=100005;
int n,m,h[N],cnt,tot,la,a[N],ha[N],b[N],has,f[N][30],rt[N],ind,po[N],nu[N],de[N];
map<int,int>mp;
struct qwe
{
int ne,to;
}e[N<<1];
struct zhuxishu
{
int ls,rs,sum;
}t[2200005];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
void dfs(int u,int fat)
{
f[u][0]=fat;
nu[++ind]=u;
po[u]=ind;
de[u]=de[fat]+1;
for(int i=h[u];i;i=e[i].ne)
if(e[i].to!=fat)
dfs(e[i].to,u);
}
void update(int l,int r,int pr,int &ro,int w)
{
ro=++tot;
t[ro].sum=t[pr].sum+1;
if(l==r)
return;
t[ro].ls=t[pr].ls;
t[ro].rs=t[pr].rs;
int mid=(l+r)>>1;
if(w<=mid)
update(l,mid,t[pr].ls,t[ro].ls,w);
else
update(mid+1,r,t[pr].rs,t[ro].rs,w);
}
int lca(int x,int y)
{
if(de[x]<de[y])
swap(x,y);
for(int i=16;i>=0;i--)
if((1<<i)&(de[x]-de[y]))
x=f[x][i];
for(int i=16;i>=0;i--)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return x==y?x:f[x][0];
}
int ques(int x,int y,int k)
{
int a=x,b=y,c=lca(a,b),d=f[c][0];
a=rt[po[a]],b=rt[po[b]],c=rt[po[c]],d=rt[po[d]];
int l=1,r=has;
while(l<r)
{
int mid=(l+r)>>1;
int now=t[t[a].ls].sum+t[t[b].ls].sum-t[t[c].ls].sum-t[t[d].ls].sum;
if(now>=k)
{
r=mid;
a=t[a].ls,b=t[b].ls,c=t[c].ls,d=t[d].ls;
}
else
{
k-=now;
l=mid+1;
a=t[a].rs,b=t[b].rs,c=t[c].rs,d=t[d].rs;
}
}
return ha[l];
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i]=read(),b[i]=a[i];
sort(b+1,b+1+n);
for(int i=1;i<=n;i++)
if(i==1||b[i]!=b[i-1])
mp[b[i]]=++has,ha[has]=b[i];
for(int i=1;i<=n;i++)
a[i]=mp[a[i]];
for(int i=1;i<n;i++)
{
int x=read(),y=read();
add(x,y);add(y,x);
}
dfs(1,0);
for(int j=1;j<=16;j++)
for(int i=1;i<=n;i++)
f[i][j]=f[f[i][j-1]][j-1];
for(int i=1;i<=n;i++)
update(1,has,rt[po[f[nu[i]][0]]],rt[i],a[nu[i]]);
for(int i=1;i<=m;i++)
{
int x=read(),y=read(),k=read();
x^=la;
la=ques(x,y,k);
printf("%d",la);
if(i!=m)
puts("");
}
return 0;
}
bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】的更多相关文章
- 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
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- 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 + 主席树 )
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 ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- 主席树 || 可持久化线段树 || 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)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
随机推荐
- Effective C++ 43,44
43.明智地使用多继承. 多继承带来了极大的复杂性.最主要的一条就是二义性. 当派生类为多继承时,其多个基类有同名的成员时,就会出现二义性.通常要明白其使用哪个成员的.显式地限制修饰成员不仅非常笨拙, ...
- VirtualBox 虚拟Ubuntu系统与主机互ping
互ping的前提是主机和虚拟机的ip地址在同一波段[eg:主机为:192.168.1.10虚拟Linux:192.168.1.11] 1.设置主机ip: ...
- ScrollView白边问题
在Android开发所使用的ScrollView中..兼容比較低的版本号的时候(比方14)会出现难看的白边.这时假设使用的是xml布局文件话设置ScrollView的android:fadingEdg ...
- Java多线程面试题归纳
1.多线程有哪几种实现方法?举个样例说明下线程的同步. (1)Java多线程有两种实现方式:继承Thread类和实现Runnable接口,Thread就是实现了Runnable接口. 两个最简单的线程 ...
- [Pyhton]weakref 弱引用
文档中的解释: https://docs.python.org/2/library/weakref.html wiki 中的解释: 在计算机程序设计中,弱引用.与强引用相对.是指不能确保其引用的对象不 ...
- Deep Learning for Robotics 资源汇总
1 前言 在最新Nature的Machine Intelligence 中Lecun.Hinton和Bengio三位大牛的Review文章Deep Learning中.最后谈The Future Of ...
- VMWare 14 Workstation Pro 下载与安装
1.双击安装运行 2.下一步 3.接受 下一步 4.自定义安装路径,下一步 5.下一步,取消勾选加入vmware客户体验 6.下一步 7.安装 8.安装中 9.完成 10.点击许可证安装 输入:FF3 ...
- shell学习五十六天----延迟进程调度
延迟进程调度 前言:大部分时候,我们都希望进程快点開始,开点结束,别卡.而shell的运行,也是在前一个命令后,立即接着运行下一个命令.命令完毕的速度是与资源的限制有关,且不在shell的权限下. 在 ...
- windows下使用F2PY编译fortran文件的问题
在windo系统下F2PY不支持gcc+gfortran的组合,解决的办法: 1.安装mingw和msys,在msys环境下使用F2PY调用gcc+gfortran进行编译 2.放弃F2PY,直接gf ...
- 使用Apache Ant合并多个jar
Apache Ant下载地址 下载解压后进入bin目录,并在此目录打开cmd 在cmd中运行ant,运行结果为: Buildfile: build.xml does not exist! Build ...