您的位置:首页 > 其它

2588: Spoj 10628. Count on a tree (可持久化线段树)

2014-07-26 21:53 537 查看
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
#define inf 0x7fffffff
#define rd(x) x=read()
#define clr(a,b) memset(a,b,sizeof(a));
#define rep(i,n) for(int i=1;i<=int(n);++i)
#define dwn(i,n) for(int i=int(n);i>=1;--i)
#define rep0(i,n) for(int i=0;i<=int(n);++i)
#define dwn0(i,n) for(int i=int(n);i>=0;--i)
#define erep(i,x) for(int i=head[x];i;i=e[i].next)
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N = 100005, M = 2000005;
int n,m,tot,sz,cnt,ind,last;
int num
,pos
;
int v
,tmp
,hash
,root
;
int ls[M],rs[M],sum[M];
int deep
,fa
[17];
struct edge{int to,next;}e[200005];int head
;
inline void ins(int u,int v){
e[++cnt]=(edge){v,head[u]};head[u]=cnt;
e[++cnt]=(edge){u,head[v]};head[v]=cnt;
}
inline int find(int x){
int l=1,r=tot;
while(l<=r){
int mid=(l+r)>>1;
if(hash[mid]<x)l=mid+1;
else if(hash[mid]==x)return mid;
else r=mid-1;
}

}
inline void dfs(int x){
pos[x]=++ind;num[ind]=x;
rep(i,16)if((1<<i)>deep[x])break;
else fa[x][i]=fa[fa[x][i-1]][i-1];
erep(i,x)if(fa[x][0]!=e[i].to){
deep[e[i].to]=deep[x]+1;
fa[e[i].to][0]=x;
dfs(e[i].to);
}
}
inline int lca(int x,int y){
if(deep[x]<deep[y])swap(x,y);
int d=deep[x]-deep[y];
rep0(i,16)if((1<<i)&d)x=fa[x][i];
dwn0(i,16)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
if(x==y)return x;return fa[x][0];
}
inline void update(int l,int r,int x,int &y,int num){
sum[y=++sz]=sum[x]+1;
if(l==r)return;
ls[y]=ls[x];rs[y]=rs[x];
int mid=(l+r)>>1;
if(num<=mid)update(l,mid,ls[x],ls[y],num);
else update(mid+1,r,rs[x],rs[y],num);
}
inline int que(int x,int y,int rk){
int a=x,b=y,c=lca(x,y),d=fa[c][0];
a=root[pos[a]],b=root[pos[b]],c=root[pos[c]],d=root[pos[d]];
int l=1,r=tot;
while(l<r){
int mid=(l+r)>>1;
int tmp=sum[ls[a]]+sum[ls[b]]-sum[ls[c]]-sum[ls[d]];
if(tmp>=rk)r=mid,a=ls[a],b=ls[b],c=ls[c],d=ls[d];
else rk-=tmp,l=mid+1,a=rs[a],b=rs[b],c=rs[c],d=rs[d];
}
return hash[l];
}
int main(){
rd(n);rd(m);
rep(i,n)rd(v[i]),tmp[i]=v[i];
sort(tmp+1,tmp+n+1);
hash[++tot]=tmp[1];
for(int i=2;i<=n;i++)
if(tmp[i]!=tmp[i-1])hash[++tot]=tmp[i];
rep(i,n)v[i]=find(v[i]);
for(int i=1;i<n;i++){
int u=read(),v=read();
ins(u,v);
}
dfs(1);
rep(i,n){
int t=num[i];
update(1,tot,root[pos[fa[t][0]]],root[i],v[t]);
}
rep(i,m){
int x=read(),y=read(),rk=read();
x^=last;
last=que(x,y,rk);
printf("%d",last);
if(i!=m)printf("\n");
}
return 0;
}

http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2131
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2130
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2129
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2128
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2127
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2126
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2125
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2124
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2123
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2122
http://www.chinayjzb.com/index.php?homepage=quanquan&file=news&itemid=2121


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: