【树链剖分】[ZJOI2008][HYSBZ/BZOJ1036]树的统计Count
题目链接
树链剖分模板题(有树链剖分求LCA)
代码
#include<cstdio>
#include<algorithm>
#define INF 0x7fffffff
#define MAXN 30000
using namespace std;
int n,m,size[MAXN+10],bl[MAXN+10],length[MAXN+10],fa[MAXN+10],wt[MAXN+10],Q,dep[MAXN+10],pos[MAXN+10];
void Read(int &x){
char c;
bool f=0;
while(c=getchar(),c!=EOF){
if(c=='-')
f=1;
else if(c>='0'&&c<='9'){
x=c-'0';
while(c=getchar(),c>='0'&&c<='9')
x=x*10+c-'0';
ungetc(c,stdin);
if(f)
x=-x;
return;
}
}
}
struct node{
int v;
node *next;
}*adj[MAXN+10],edge[MAXN*2+10],*ecnt=edge;
struct Seg_tree{
int mx,sum;
}tree[MAXN*4+10],*tcnt=tree,*root[MAXN+10];
Seg_tree *New_tree(int len){
Seg_tree *ret=tcnt;
tcnt+=4*len;
return ret;
}
void addedge(int u,int v){
node *p=++ecnt;
p->v=v;
p->next=adj[u];
adj[u]=p;
}
void dfs1(int u){
dep[u]=dep[fa[u]]+1;
size[u]=1;
for(node *p=adj[u];p;p=p->next){
if(p->v!=fa[u]){
fa[p->v]=u;
dfs1(p->v);
size[u]+=size[p->v];
}
}
}
void insert(Seg_tree *tree,int i,int l,int r,int pos,int wt){
if(l==r){
tree[i].mx=tree[i].sum=wt;
return;
}
int mid=(l+r)>>1;
if(pos<=mid)
insert(tree,i<<1,l,mid,pos,wt);
else
insert(tree,(i<<1)|1,mid+1,r,pos,wt);
tree[i].mx=max(tree[i<<1].mx,tree[(i<<1)|1].mx);
tree[i].sum=tree[i<<1].sum+tree[(i<<1)|1].sum;
}
void dfs2(int u,int len){
int heavy=0;
for(node *p=adj[u];p;p=p->next)
if(p->v!=fa[u]&&size[p->v]>size[heavy])
heavy=p->v;
if(!heavy){
int tp=u,i;
for(i=1;i<len;i++)
tp=fa[tp];
root[tp]=New_tree(len);
length[tp]=len;
for(i=len;i;i--){
pos[u]=i;
bl[u]=tp;
insert(root[tp],1,1,len,i,wt[u]);
u=fa[u];
}
return;
}
dfs2(heavy,len+1);
for(node *p=adj[u];p;p=p->next)
if(p->v!=fa[u]&&p->v!=heavy)
dfs2(p->v,1);
}
int LCA(int x,int y){
while(bl[x]!=bl[y]){
if(dep[bl[x]]>=dep[bl[y]])
x=fa[bl[x]];
else
y=fa[bl[y]];
}
return dep[x]<dep[y]?x:y;
}
void init(){
Read(n);
int i,u,v;
for(i=1;i<n;i++){
Read(u),Read(v);
addedge(u,v);
addedge(v,u);
}
for(i=1;i<=n;i++)
Read(wt[i]);
dfs1(1);
dfs2(1,1);
}
int Get_Max(Seg_tree *tree,int i,int l,int r,int ll,int rr){
if(l>=ll&&r<=rr)
return tree[i].mx;
if(r<ll||l>rr)
return -INF;
int mid=(l+r)>>1;
return max(Get_Max(tree,i<<1,l,mid,ll,rr),Get_Max(tree,(i<<1)|1,mid+1,r,ll,rr));
}
int Get_sum(Seg_tree *tree,int i,int l,int r,int ll,int rr){
if(l>=ll&&r<=rr)
return tree[i].sum;
if(r<ll||l>rr)
return 0;
int mid=(l+r)>>1;
return Get_sum(tree,i<<1,l,mid,ll,rr)+Get_sum(tree,(i<<1)|1,mid+1,r,ll,rr);
}
int Query_Max(int u,int lca){
int ret=-INF;
while(bl[u]!=bl[lca]){
ret=max(Get_Max(root[bl[u]],1,1,length[bl[u]],1,pos[u]),ret);
u=fa[bl[u]];
}
ret=max(Get_Max(root[bl[u]],1,1,length[bl[u]],pos[lca],pos[u]),ret);
return ret;
}
int Query_sum(int u,int lca){
int ret=0;
while(bl[u]!=bl[lca]){
ret+=Get_sum(root[bl[u]],1,1,length[bl[u]],1,pos[u]);
u=fa[bl[u]];
}
ret+=Get_sum(root[bl[u]],1,1,length[bl[u]],pos[lca],pos[u]);
return ret;
}
void solve(){
char s[20];
int u,v,lca;
Read(Q);
while(Q--){
scanf("%s",s);
Read(u),Read(v);
if(s[1]=='S'){
lca=LCA(u,v);
printf("%d\n",Query_sum(u,lca)+Query_sum(v,lca)-wt[lca]);
}
else if(s[1]=='M'){
lca=LCA(u,v);
printf("%d\n",max(Query_Max(u,lca),Query_Max(v,lca)));
}
else
insert(root[bl[u]],1,1,length[bl[u]],pos[u],wt[u]=v);
}
}
int main()
{
init();
solve();
}