Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 14271 | Accepted: 3950 |
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.
Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!'
At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.
Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?
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.
The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.
The following q lines each is one of the following two types:
Message A: 0 u
A kid in hut u calls Wind. She should go to hut u from her current position.
Message B: 1 i w
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.
Output
For each message A, print an integer X, the time required to take the next child.
Sample Input
3 3 1 1 2 1 2 3 2 0 2 1 2 3 0 3
Sample Output
1 3
思路: lca+ 求到根的距离+树状数组区间更新,单点查询。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N =1e5+5; /// 节点的个数
int rmq[N*2]; /// 就是欧拉序列对应的深度序列
struct ST
{
int mm[N*2];
int anc[2*N][20];
void init(int n)
{
mm[0]=-1;
for(int i=1;i<=n;i++){
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
anc[i][0]=i;
}
for(int j=1;j<=mm[n];j++){
for(int i=1;i+(1<<j)-1<=n;i++){
anc[i][j]=rmq[anc[i][j-1]] < rmq[anc[i+(1<<(j-1))][j-1]]?anc[i][j-1]:anc[i+(1<<(j-1))][j-1];
}
}
}
int query(int a,int b)
{
if(a>b) swap(a,b);
int k=mm[b-a+1];
return rmq[anc[a][k]] <= rmq[anc[b-(1<<k)+1][k]]?anc[a][k]:anc[b-(1<<k)+1][k];
}
};
struct node
{
int v,next;
int id;
ll w;
}edge[N*2];
int tot,head[N];
int dfns[N*2]; /// 欧拉序列,也就是dfs序, 长度为2*n-1, 下标从1 开始.
int pp[N]; /// pp[i] 表示在dfns 中第一次出现的位置
int L[N],R[N]; /// 表示当前节点dfs序中管辖的区间看情况使用
int cnt;
int clo; /// 时钟标记 用于L,R
ST st;
int n,m,ss;
ll ww[N];
ll c[N];
int to[N];
void init()
{
tot=0;
memset(head,-1,sizeof(head));
memset(c,0,sizeof(c));
clo=0; cnt=0;
}
void add(int u,int v,ll w,int id)
{
edge[++tot].v=v; edge[tot].id=id; edge[tot].w=w;
edge[tot].next=head[u]; head[u]=tot;
}
void dfs(int u,int fa,int deep)
{
//cout<<"u "<<u<<" fa "<<fa<<" deep "<<deep<<endl;
dfns[++cnt]=u; rmq[cnt]=deep; pp[u]=cnt;
L[u]=++clo;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(v==fa) continue;
to[edge[i].id]=v;
dfs(v,u,deep+1);
dfns[++cnt]=u;
rmq[cnt]=deep;
}
R[u]=clo;
}
void LCA_init(int rt,int node_num)
{
cnt=0; clo=0;
dfs(rt,rt,0);
st.init(2*node_num-1);
}
int LCA(int u,int v)
{
return dfns[st.query(pp[u],pp[v])];
}
/// init 初始化边, LCA_init() 初始化lca
int lowbit(int x)
{
return x&(-x);
}
void update(int x,ll w)
{
for(;x<=n;x+=lowbit(x)) c[x]+=w;
}
ll getsum(int x)
{
ll s=0;
for(;x>0;x-=lowbit(x)) s+=c[x];
return s;
}
ll getans(int u,int v)
{
ll sum1,sum2,sum3;
int lca=LCA(u,v);
sum1=getsum(L[u]);
sum2=getsum(L[v]);
sum3=getsum(L[lca]);
return sum1+sum2-2*sum3;
}
int main()
{
int u,v;
ll w;
while(scanf("%d %d %d",&n,&m,&ss)!=EOF)
{
init();
for(int i=1;i<n;i++){
scanf("%d %d %lld",&u,&v,&w);
add(u,v,w,i);
add(v,u,w,i);
ww[i]=w;
}
LCA_init(1,n);
for(int i=1;i<n;i++){
int tmp=to[i];
update(L[tmp],ww[i]);
update(R[tmp]+1,-ww[i]);
}
int op;
while(m--)
{
scanf("%d",&op);
if(op==0){
scanf("%d",&v);
ll ans=getans(ss,v);
printf("%lld\n",ans);
ss=v;
}
else{
scanf("%d %lld",&u,&w);
int tmp=to[u];
update(L[tmp],w-ww[u]);
update(R[tmp]+1,ww[u]-w);
ww[u]=w;
}
}
}
return 0;
}