Housewife Wind
Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique. Input The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001. Output For each message A, print an integer X, the time required to take the next child. Sample Input Sample Output Source POJ Monthly--2006.02.26,zgl & twb |
[Submit] [Go Back] [Status] [Discuss]
【题意】
给出树,有边权。操作0:查询上一次所在的点s到u之间的距离。操作1:修改第i条边的权值为w
【分析】
以1为根做一次树链剖分,把边权转移给对应的儿子结点。
比较好的模板题。
线段树维护(有点耗时长)
树状数组维护好用些
【代码】
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long ll;
const int MAX=1e5+5;
const int INF=0x3f3f3f3f;
/*********** 图 ***********/
struct node{
int t,next;
ll w;
int id;
}edge[MAX*2];
int head[MAX],cnt;
void init(int n)
{
cnt=0; memset(head,-1,sizeof(head[0])*(n+1));
}
void addedge(int u,int v,int w,int id)
{
edge[cnt]=node{v,head[u],w,id};
head[u]=cnt++;
}
/************ 树链剖分 **************/
int dep[MAX],dad[MAX],siz[MAX],son[MAX],bev[MAX]; //bev[i]表示第i边对应的点
int top[MAX],rk[MAX],id[MAX];
void dfs1(int u,int pre)
{
siz[u]=1; //u本身
son[u]=-1;
int maxsiz=0; //最大的儿子子树大小
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].t;
if(v==pre)continue;
dep[v]=dep[u]+1;
dad[v]=u;
bev[edge[i].id]=v; //边对应点
dfs1(v,u); //注意dfs的位置
siz[u]+=siz[v];
if(maxsiz<siz[v])
{
maxsiz=siz[v];
son[u]=v;
}
}
}
void dfs2(int u,int fat,int &tag) //结点u,重链顶端
{
top[u]=fat;
rk[u]=++tag; //时间戳
id[tag]=u; //tag对应在树上的结点号
if(son[u]==-1)return; //没有孩子
dfs2(son[u],fat,tag); //优先重链dfs
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].t;
if(v==dad[u]||v==son[u])continue;
dfs2(v,v,tag);
}
}
void cuttree(int root) //将root为根的树链剖分
{
int tag=0;
dep[root]=0; //根的深度和父结点做一个初始化
dad[root]=root; //这是一个假设,根的父结点还是根
dfs1(root,root);
dfs2(root,root,tag);
}
/************* 线段树 ****************/
ll sum[MAX<<2],mark[MAX<<2];
void build(int root,int l,int r) //初始线段树
{
mark[root]=0;
if(l==r)return;//叶子节点
int mid=(l+r)/2;
build(root*2,l,mid); //左子树的创建
build(root*2+1,mid+1,r); //右子树
}
void nodeupdate(int root,int l,int r,ll num) //因题而异
{
mark[root]=num;
sum[root]=num*(r-l+1);
}
void pushdown(int root,int l,int r)//传递给两孩子
{
if(mark[root]==0)return;
int mid=(l+r)/2;
nodeupdate(root*2,l,mid,mark[root]);
nodeupdate(root*2+1,mid+1,r,mark[root]);
mark[root]=0;
}
void pushup(int root,int l,int r)
{
sum[root]=sum[root<<1]+sum[root<<1|1];
}
void update(int left,int right,ll num, int root,int l,int r)//修改
{
if(left<=l&&r<=right){
nodeupdate(root,l,r,num);
return;
}
pushdown(root,l,r);
int mid=(l+r)/2;
if(left<=mid)
update(left,right,num,root*2,l,mid);
if(mid<right)
update(left,right,num,root*2+1,mid+1,r);
pushup(root,l,r);
}
ll Qsum(int left,int right, int root,int l,int r)//区间最大值
{
if(left<=l&&r<=right)
return sum[root];
pushdown(root,l,r);
int mid=(l+r)/2;
ll res=0; ///赋初值!!!
if(left<=mid)
res+=Qsum(left,right,root*2,l,mid);//区间在左子树
if(right>mid)
res+=Qsum(left,right,root*2+1,mid+1,r);//在右子树
return res;
}
/************** 树链->线段树 *********/
void modify(int u,int v,ll num,int limit) //更新,negative
{
int fu=top[u],fv=top[v];
while(fu!=fv) //uv不在同一条链上
{
if(dep[fu]>=dep[fv]) //u的深度大
{
update(rk[fu],rk[u],num,1,1,limit);
u=dad[fu]; fu=top[u];
}
else
{
update(rk[fv],rk[v],num,1,1,limit);
v=dad[fv]; fv=top[v];
}
} //循环结束时,fu-fv在同一链
if(dep[u]>dep[v])swap(u,v);
if(u!=v)update(rk[u]+1,rk[v],num,1,1,limit); //注意!边权题,lca不取
}
ll query(int u,int v, int limit) //查询u-v路径和
{
ll res=0;
int fu=top[u],fv=top[v];
while(fu!=fv) //uv不在同一条链上
{
if(dep[fu]>=dep[fv]) //u的深度大
{
res+=Qsum(rk[fu],rk[u],1,1,limit);
u=dad[fu]; fu=top[u];
}
else
{
res+=Qsum(rk[fv],rk[v],1,1,limit);
v=dad[fv]; fv=top[v];
}
} //循环结束时,fu-fv在同一链
if(dep[u]>dep[v])swap(u,v);
if(u!=v)
res+=Qsum(rk[u]+1,rk[v],1,1,limit); ///注意+1,lca不取
return res;
}
/************** solve **************/
int a[MAX];
bool solve()
{
int n,q,s,u,v,w,op;
scanf("%d%d%d",&n,&q,&s);
init(n); //初始图
for(int i=1;i<n;i++) //边权
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,i);
addedge(v,u,w,i);
}
cuttree(1); //树链剖分
build(1,1,n); //建立线段树
for(int i=1;i<n;i++)a[bev[i]]=edge[(i-1)*2].w; //边权转化为点权
//以下变为点权做法,但是lca结点不可取!!!这是与纯点权图的区别
for(int i=2;i<=n;i++)update(rk[i],rk[i],a[i],1,1,n);//初态
while(q--)
{
scanf("%d",&op);
if(op==0) //查询
{
scanf("%d",&u);
ll ans=query(u,s,n); //注意这里是查询点之间
s=u;
printf("%lld\n",ans);
}
else //边u权值修改为w
{
scanf("%d%d",&u,&w);
u=bev[u];
modify(u,dad[u],w,n); //这里是点之间
}
}
}
int main()
{
solve();
}