您的位置:首页 > 其它

[BZOJ4539][HNOI2016]树(主席树)

2018-01-19 21:21 344 查看

 

4539: [Hnoi2016]树

Time Limit: 40 Sec  Memory Limit: 256 MB
Submit: 746  Solved: 292
[Submit][Status][Discuss]

 

Description

 

  小A想做一棵很大的树,但是他手上的材料有限,只好用点小技巧了。开始,小A只有一棵结点数为N的树,结
点的编号为1,2,…,N,其中结点1为根;我们称这颗树为模板树。小A决定通过这棵模板树来构建一颗大树。构建过
程如下:(1)将模板树复制为初始的大树。(2)以下(2.1)(2.2)(2.3)步循环执行M次(2.1)选择两个数字a,b,
其中1<=a<=N,1<=b<=当前大树的结点数。(2.2)将模板树中以结点a为根的子树复制一遍,挂到大树中结点b的下
方(也就是说,模板树中的结点a为根的子树复制到大树中后,将成为大树中结点b的子树)。(2.3)将新加入大树
的结点按照在模板树中编号的顺序重新编号。例如,假设在进行2.2步之前大树有L个结点,模板树中以a为根的子
树共有C个结点,那么新加入模板树的C个结点在大树中的编号将是L+1,L+2,…,L+C;大树中这C个结点编号的大小
顺序和模板树中对应的C个结点的大小顺序是一致的。下面给出一个实例。假设模板树如下图:


根据第(1)步,初始的大树与模板树是相同的。在(2.1)步,假设选择了a=4,b=3。运行(2.2)和(2.3)后,得到新的
大树如下图所示

现在他想问你,树中一些结点对的距离是多少。

 

Input

 

  第一行三个整数:N,M,Q,以空格隔开,N表示模板树结点数,M表示第(2)中的循环操作的次数,Q 表示询问数
量。接下来N-1行,每行两个整数 fr,to,表示模板树中的一条树边。再接下来M行,每行两个整数x,to,表示将模
板树中 x 为根的子树复制到大树中成为结点to的子树的一次操作。再接下来Q行,每行两个整数fr,to,表示询问
大树中结点 fr和 to之间的距离是多少。N,M,Q<=100000

 

Output

 

  输出Q行,每行一个整数,第 i行是第 i个询问的答案。

 

Sample Input

 

5 2 3
1 4
1 3
4 2
4 5
4 3
3 2
6 9
1 8
5 3

 

Sample Output

 

6
3
3

 

HINT

 

 

经过两次操作后,大树变成了下图所示的形状:



结点6到9之间经过了6条边,所以距离为6;类似地,结点1到8之间经过了3条边;结点5到3之间也经过了3条边。

 

 

Source

 

思维难度低,代码难度高。

直接上主席树即可。

代码用时:0.5h。抄一次代码,错误率还是比较低的,只有一个地方接口参数写反了。

 

#include<cstdio>
#include<algorithm>
#include<iostream>
#define rep(i,l,r) for (int i=l; i<=r; i++)
typedef long long ll;
using namespace std;

const int N=100100,M=2000100;
int n,m,tot,dfn,sm[M],ls[M],rs[M],rt
,lf
,rg
,id
;
struct P{ int id,rt,fa; ll l,r; }a
;

template<typename T>inline void rd(T &x){
int t; char ch;
for (t=0; !isdigit(ch=getchar()); t=(ch=='-'));
for (x=ch-'0'; isdigit(ch=getchar()); x=x*10+ch-'0');
if (t) x=-x;
}

void ins(int x,int &y,int L,int R,int k){
y=++tot; sm[y]=sm[x]+1;
if (L==R) return; int mid=(L+R)>>1;
if (k<=mid) rs[y]=rs[x],ins(ls[x],ls[y],L,mid,k);
else ls[y]=ls[x],ins(rs[x],rs[y],mid+1,R,k);
}

int que(int k,int z){
int L=1,R=n,mid,t,x=rt[lf[k]-1],y=rt[rg[k]];
while (L<R){
mid=(L+R)>>1; t=sm[ls[y]]-sm[ls[x]];
if (z<=t) R=mid,x=ls[x],y=ls[y];
else L=mid+1,z-=t,x=rs[x],y=rs[y];
}
return L;
}

int getid(ll x,int ed){
int L=1,R=ed+1,mid;
while (L+1<R){
mid=(L+R)>>1;
if (a[mid].l<=x) L=mid; else R=mid;
}
return L;
}

struct T{
int tot,fst
,pnt[N<<1],len[N<<1],nxt[N<<1];
int fa
,sz
,son
,anc
;
ll d
;
void add(int x,int y,int z)
{ pnt[++tot]=y; len[tot]=z; nxt[tot]=fst[x]; fst[x]=tot; }

void dfs(int x){
int p; sz[x]=1;
for (p=fst[x]; p; p=nxt

){ int y=pnt[p]; if (y!=fa[x]){ fa[y]=x; d[y]=d[x]+len[p]; dfs(y); sz[x]+=sz[y]; if (sz[y]>sz[son[x]]) son[x]=y; } } } void nbr(int x,int tp){ lf[x]=rg[x]=++dfn; id[dfn]=x; anc[x]=tp; int p; if (son[x]) nbr(son[x],tp),rg[x]=rg[son[x]]; for (p=fst[x]; p; p=nxt[p]){ int y=pnt[p]; if (y!=fa[x] && y!=son[x]) nbr(y,y),rg[x]=rg[y]; } } void div(int x,int tp){ anc[x]=tp; int p; if (son[x]) div(son[x],tp); for (p=fst[x]; p; p=nxt[p]){ int y=pnt[p]; if (y!=fa[x] && y!=son[x]) div(y,y); } } int lca(int x,int y){ for (; anc[x]!=anc[y]; x=fa[anc[x]]) if (d[anc[x]]<d[anc[y]]) swap(x,y); return (d[x]<d[y]) ? x : y; } int gettp(int x,int y){ int z; for (; anc[x]!=anc[y]; x=fa[anc[x]]) z=anc[x]; return (x==y) ? z : son[y]; } void build(){ rep(i,1,n) ins(1,n,rt[i-1],rt[i],id[i]); } ll dist(int x,int y){ return d[x]+d[y]-(d[lca(x,y)]<<1); } }t1,t2; int main(){ freopen("bzoj4539.in","r",stdin); freopen("bzoj4539.out","w",stdout); rd(n); rd(m); int cas,z; ll x,y; rd(cas); rep(i,1,n-1) rd(x),rd(y),t1.add(x,y,1),t1.add(y,x,1); t1.dfs(1); t1.nbr(1,1); rep(i,1,n) ins(rt[i-1],rt[i],1,n,id[i]); a[1].id=1; a[1].rt=1; a[1].l=1; a[1].r=n; rep(i,1,m){ rd(x); rd(y); a[i+1].rt=x; a[i+1].id=i+1; a[i+1].l=a[i].r+1; a[i+1].r=a[i].r+t1.sz[x]; z=getid(y,i); a[i+1].fa=y=que(a[z].rt,y-a[z].l+1); t2.add(z,i+1,t1.d[y]-t1.d[a[z].rt]+1); } t2.dfs(1); t2.div(1,1); int u,v,w; ll ans; while (cas--){ rd(x); rd(y); u=getid(x,m+1); v=getid(y,m+1); w=t2.lca(u,v); x=que(a[u].rt,x-a[u].l+1); y=que(a[v].rt,y-a[v].l+1); if (u==v) printf("%lld\n",t1.dist(x,y)); else{ if (u==w) swap(u,v),swap(x,y); if (v==w){ v=t2.gettp(u,w); ans=t1.d[x]-t1.d[a[u].rt]+t2.d[u]-t2.d[v]; x=a[v].fa; ans+=t1.dist(x,y)+1; }else{ ans=t1.d[x]-t1.d[a[u].rt]+t1.d[y]-t1.d[a[v].rt]+t2.dist(u,v); u=t2.gettp(u,w); v=t2.gettp(v,w); x=a[u].fa; y=a[v].fa; ans-=(t1.d[t1.lca(x,y)]-t1.d[a[w].rt])<<1; } printf("%lld\n",ans); } } return 0; }

[p] 

 

 

 

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