【主席树】bzoj2588 Spoj 10628. Count on a tree
每个点的主席树的root是从其父转移来的。询问的时候用U+V-LCA-FA(LCA)即可。
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100001
int v[N<<1],first[N],next[N<<1],en,Ans;
void AddEdge(int U,int V)
{
v[++en]=V;
next[en]=first[U];
first[U]=en;
}
struct Point{int v,p;}t[N];
bool operator < (Point a,Point b){return a.v<b.v;}
int n,m,ma[N],a[N],zy;
struct Node{int v,lc,rc;}T[N*24];
int root[N],e=1;
void BuildTree(int cur,int l,int r)
{
if(l==r) return;
int m=(l+r>>1);
T[cur].lc=++e;
BuildTree(T[cur].lc,l,m);
T[cur].rc=++e;
BuildTree(T[cur].rc,m+1,r);
}
void Insert(int pre,int cur,int p,int l,int r)
{
if(l==r)
{
T[cur].v=T[pre].v+1;
return;
}
int m=(l+r>>1);
if(p<=m)
{
T[cur].lc=++e;
T[cur].rc=T[pre].rc;
Insert(T[pre].lc,T[cur].lc,p,l,m);
}
else
{
T[cur].rc=++e;
T[cur].lc=T[pre].lc;
Insert(T[pre].rc,T[cur].rc,p,m+1,r);
}
T[cur].v=T[T[cur].lc].v+T[T[cur].rc].v;
}
int top[N],siz[N],son[N],fa[N],dep[N];
void df1(int U)
{
siz[U]=1;
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
{
fa[v[i]]=U;
dep[v[i]]=dep[U]+1;
df1(v[i]);
siz[U]+=siz[v[i]];
if(siz[v[i]]>siz[son[U]])
son[U]=v[i];
}
}
void df2(int U)
{
if(son[U])
{
top[son[U]]=top[U];
df2(son[U]);
}
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U]&&v[i]!=son[U])
{
top[v[i]]=v[i];
df2(v[i]);
}
}
int lca(int U,int V)
{
while(top[U]!=top[V])
{
if(dep[top[U]]<dep[top[V]])
swap(U,V);
U=fa[top[U]];
}
if(dep[U]>dep[V])
swap(U,V);
return U;
}
void dfs(int U)
{
root[U]=++e;
Insert(root[fa[U]],root[U],a[U],1,zy);
for(int i=first[U];i;i=next[i])
if(v[i]!=fa[U])
dfs(v[i]);
}
int Kth(int ql,int qr,int LCA,int FLCA,int K,int l,int r)
{
if(l==r) return l;
int m=(l+r>>1);
if(T[T[ql].lc].v+T[T[qr].lc].v-T[T[LCA].lc].v-T[T[FLCA].lc].v>=K)
return Kth(T[ql].lc,T[qr].lc,T[LCA].lc,T[FLCA].lc,K,l,m);
else
return Kth(T[ql].rc,T[qr].rc,T[LCA].rc,T[FLCA].rc,K-(T[T[ql].lc].v+T[T[qr].lc].v-T[T[LCA].lc].v-T[T[FLCA].lc].v),m+1,r);
}
int main()
{
int X,Y,W;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
{
scanf("%d",&t[i].v);
t[i].p=i;
}
sort(t+1,t+n+1);
a[t[1].p]=++zy;
ma[zy]=t[1].v;
for(int i=2;i<=n;++i)
{
if(t[i].v!=t[i-1].v) ++zy;
a[t[i].p]=zy;
ma[zy]=t[i].v;
}
for(int i=1;i<n;++i)
{
scanf("%d%d",&X,&Y);
AddEdge(X,Y);
AddEdge(Y,X);
}
root[0]=1;
BuildTree(root[0],1,zy);
df1(1);
top[1]=1;
df2(1);
dfs(1);
for(;m;--m)
{
scanf("%d%d%d",&X,&Y,&W); X^=Ans;
int t=lca(X,Y);
printf("%d",Ans=ma[Kth(root[X],root[Y],root[t],root[fa[t]],W,1,zy)]);
if(m!=1) puts("");
}
return 0;
}
【主席树】bzoj2588 Spoj 10628. Count on a tree的更多相关文章
- BZOJ2588: Spoj 10628. Count on a tree
		
传送门 刚开始看错题以为是dfs序瞎搞.. 后来看清题了开始想用树剖瞎搞... 感觉要滚粗啊.. 对于每个点到根的路径建立线段树,暴力建MLE没跑,上主席树,然后$(x,y)$的路径就可以先求出来$L ...
 - 主席树初探--BZOJ2588: Spoj 10628. Count on a tree
		
n<=100000的点权树,有m<=100000个询问,每次问两个点间的第k小点权,保证有解,强制在线. 主席上树啦!类似于之前的序列不带修改询问的前缀表示法,现在只要把前缀当成某点到根的 ...
 - bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)
		
每个节点继承父节点的树,则答案为query(root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]]) #include<iostream> #i ...
 - 【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 [树上主席树]
		
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
		
题目: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+主席树)
		
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( LCA + 主席树 )
		
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
 
随机推荐
- HDU1166 敌兵布阵(树状数组实现
			
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
 - elk,centos7,filebeat,elasticsearch-head集成搭建
			
1.安装 elasticsearch-5.2.2.tar.gz cd elasticsearch-5.2.2/bin ./elasticsearch -Ecluster.name=my_cluster ...
 - maven工程开启jetty调试
			
转摘自:http://czj4451.iteye.com/blog/1942437 准备工作: a. 在pom.xml中配置jetty插件: <plugins> <plugin> ...
 - ViewPager使用--文章集锦
			
viewpager中彻底性动态添加.删除Fragment Android ViewPager使用详解 fragment中嵌套viewpager,vierpager中有多个fragment,不显示 .. ...
 - [POI2014] KUR-Couriers(洛谷P3567)
			
洛谷题目链接:[POI2014]KUR-Couriers 题目描述 Byteasar works for the BAJ company, which sells computer games. Th ...
 - 最适合初学者学习的idea教程
			
https://github.com/judasn/IntelliJ-IDEA-Tutorial
 - [object-c 2.0 程序设计]object-c review (一)
			
// // main.m // cmdTry // // Created by Calos Chen on 2017/8/21. // Copyright © 2017年 Calos Chen. Al ...
 - bzoj 1301 后缀数组
			
比较裸的后缀数组. /************************************************************** Problem: User: BLADEVIL La ...
 - bzoj 2668 费用流
			
我们可以把初始状态转化为目标状态这一约束转化为将黑子移动到目标状态所需要的最少步数. 除了初始点和目标点之外,剩下的点如果被经过那么就会被交换两次,所以我们将一个点拆成3个点,a,b,c,新建附加源点 ...
 - python函数对象和闭包
			
关于函数对象和闭包 闭包(closure)是函数式编程的重要的语法结构.不同的语言实现闭包的方式不同.Python以函数对象为基础,为闭包这一语法结构提供支持的 (我们在特殊方法与多范式中,已经多次看 ...