# BZOJ3626LCA

3626: [LNOI2014]LCA
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1374 Solved: 503
Description

（即，求在[l,r]區間內的每個節點i與z的最近公共祖先的深度之和）
Input

Output

Sample Input
5 2
0
0
1
1
1 4 3
1 4 2
Sample Output
8
5
HINT

Source

P.S.如果你仔細看struct後面的東西會發現一些喜聞樂見的事。。

``````#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define mod 201314
#define MAXN 50001
int n,m,cnt,sz,h[MAXN],father[MAXN][16],pos[MAXN],belong[MAXN],deep[MAXN],size[MAXN];
bool vis[MAXN];
struct will
{
int to,next;
}edge[MAXN];
struct with
{
int z,ans1,ans2;
}q[MAXN];
struct kx
{
int num,p;
bool flag;
}a[MAXN<<1];
struct forever
{
int l,r,sum,delta,size;
}node[MAXN<<2];
bool operator < (kx a,kx b)
{
return a.p<b.p;
}

{
int w=0,c=1; char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-') c=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
w=w*10+ch-'0',ch=getchar();
return w*c;
}

{
cnt++,edge[cnt].next=h[u],h[u]=cnt,edge[cnt].to=v;
}

void dfs1(int x)
{
int i;
size[x]=1,vis[x]=true;
for (i=1;i<=15;i++)
{
if (deep[x]<(1<<i)) break;
father[x][i]=father[father[x][i-1]][i-1];
}
for (i=h[x];i;i=edge[i].next)
{
if (vis[edge[i].to]) continue;
deep[edge[i].to]=deep[x]+1,father[edge[i].to][0]=x;
dfs1(edge[i].to),size[x]+=size[edge[i].to];
}
}

void dfs2(int x,int chain)
{
int k=n,i;
pos[x]=++sz,belong[x]=chain;
for (i=h[x];i;i=edge[i].next)
if (deep[edge[i].to]>deep[x] && size[edge[i].to]>size[k])
k=edge[i].to;
if (k!=n) dfs2(k,chain);
for (i=h[x];i;i=edge[i].next)
if (deep[edge[i].to]>deep[x] && k!=edge[i].to)
dfs2(edge[i].to,edge[i].to);
}

void pushdown(int s)
{
if (node[s].l==node[s].r || !node[s].delta) return;
int delta=node[s].delta;node[s].delta=0;
node[s*2].sum=node[s*2].sum+node[s*2].size*delta;
node[s*2+1].sum=node[s*2+1].sum+node[s*2+1].size*delta;
node[s*2].delta=node[s*2].delta+delta;
node[s*2+1].delta=node[s*2+1].delta+delta;
}

void build(int s,int l,int r)
{
node[s].l=l,node[s].r=r,node[s].size=r-l+1;
if (l==r) return;
build(s*2,l,(l+r)/2),build(s*2+1,(l+r)/2+1,r);
}

void update(int s,int x,int y)
{
pushdown(s);
int l=node[s].l,r=node[s].r;
if (l==x && y==r)
{
node[s].delta++,node[s].sum+=node[s].size;
return;
}
int mid=(l+r)/2;
if (y<=mid) update(s*2,x,y);
else if (x>mid) update(s*2+1,x,y);
else update(s*2,x,mid),update(s*2+1,mid+1,y);
node[s].sum=node[s*2].sum+node[s*2+1].sum;
}

void solveupdate(int x,int f)
{
while (belong[x]!=belong[f])
{
update(1,pos[belong[x]],pos[x]);
x=father[belong[x]][0];
}
update(1,pos[f],pos[x]);
}

int query(int s,int x,int y)
{
pushdown(s);
int l=node[s].l,r=node[s].r;
if (l==x && y==r) return node[s].sum;
int mid=(l+r)/2;
if (y<=mid) return query(s*2,x,y);
else if (x>mid) return query(s*2+1,x,y);
else return query(s*2,x,mid)+query(s*2+1,mid+1,y);
}

int solvequery(int x,int f)
{
int sum=0;
while (belong[x]!=belong[f])
{
sum+=query(1,pos[belong[x]],pos[x]);
sum%=mod;
x=father[belong[x]][0];
}
sum+=query(1,pos[f],pos[x]),sum%=mod;
return sum;
}

int main()
{
int i,x,tot=0,l,r,t,now;
memset(node,0,sizeof(node));
for (i=1;i<=m;i++)
{
a[++tot].p=l-1,a[tot].num=i,a[tot].flag=false;
a[++tot].p=r,a[tot].num=i,a[tot].flag=true;
}
build(1,1,n);
sort(a+1,a+tot+1);
dfs1(0),dfs2(0,0);
now=-1;
for (i=1;i<=tot;i++)
{
while (now<a[i].p)
now++,solveupdate(now,0);
t=a[i].num;
if (!a[i].flag) q[t].ans1=solvequery(q[t].z,0);
else q[t].ans2=solvequery(q[t].z,0);
}
for (i=1;i<=m;i++)
printf("%d\n",(q[i].ans2-q[i].ans1+mod)%mod);
return 0;
}``````