POJ2763 Housewife Wind

 
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 9701   Accepted: 2661

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

Source

 
 
树链剖分。
 
↑然而并不会写树剖。
 
 
LCA+树状数组。
用树状数组维护差分数组的前缀和,差分数组里存边的权值(按DFS序存储,每个结点的进入时间戳加权值,退出时间戳减权值),这样修改边权会很方便。
求从一个点到另一个点的路程用LCA,差分数组前缀和可以得到“从根节点到当前节点”的距离dis,那么从x到y需要dis(x)+dis(y)-2*dis(LCA(x,y))
 
看嘛,挺简单的。
但是神TM我半夜肝这道题忘了加LCA的初始化,WA了一个多小时查不出原因??!!
深夜不能肝难题!
深夜不能肝难题!
 
  1 /*by SilverN*/
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<cmath>
  7 using namespace std;
  8 const int mxn=400010;
  9 //read
 10 int read(){
 11     int x=0,f=1;char ch=getchar();
 12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
 14     return x*f;
 15 }
 16 //bas
 17 int n,q,s;
 18 int in[mxn],out[mxn];
 19 int cnt=0;
 20 
 21 //edge
 22 struct edge{
 23     int v,next;
 24     int dis;
 25     int id;
 26 }e[mxn];
 27 int hd[mxn],mcnt=0;   // hd[u]
 28 void add_edge(int u,int v,int dis,int id){
 29     e[++mcnt].v=v;e[mcnt].next=hd[u];e[mcnt].dis=dis;e[mcnt].id=id;hd[u]=mcnt;
 30 }
 31 int tto[mxn];//第i条边的去向 
 32 //tree
 33 int t[mxn];
 34 int tree_lmn;
 35 int lowbit(int x){return x&-x;}
 36 void add(int p,int v){
 37     while(p<=tree_lmn){t[p]+=v;p+=lowbit(p);}
 38     return;
 39 }
 40 int smm(int x){
 41     int res=0;
 42     while(x){res+=t[x];x-=lowbit(x);}
 43     return res;
 44 }
 45 //DFS
 46 int dep[mxn];//int dis[mxn];
 47 int disto[mxn];
 48 int fa[mxn][19];
 49 void DFS(int u,int father){
 50     int i,j;
 51     in[u]=++cnt;
 52     for(i=hd[u];i;i=e[i].next){
 53         int v=e[i].v;
 54         if(v==father)continue;
 55         tto[e[i].id]=v;
 56         disto[v]=e[i].dis;
 57         dep[v]=dep[u]+1;//深度 
 58         fa[v][0]=u;
 59         DFS(v,u);
 60     }
 61     out[u]=++cnt;
 62 }
 63 //LCA
 64 void initLCA(){
 65     int i,j;
 66     for(i=1;i<=17;i++){
 67         for(j=1;j<=n;j++){
 68             fa[j][i]=fa[fa[j][i-1]][i-1];
 69         }
 70     }
 71     return;
 72 }
 73 int LCA(int x,int y){
 74     if(dep[x]<dep[y])swap(x,y);
 75     int i;
 76     for(i=17;i>=0;i--){
 77         if(dep[fa[x][i]]>=dep[y])
 78             x=fa[x][i];
 79     }
 80     if(x==y) return y;
 81     for(i=17;i>=0;i--)
 82         if(fa[x][i]!=fa[y][i]){
 83             x=fa[x][i];
 84             y=fa[y][i];
 85         }
 86     return fa[x][0];
 87 }
 88 //calc
 89 int tmp;
 90 int dist(int x,int y){//算书上路径 
 91     tmp=LCA(x,y);
 92     return smm(in[x])+smm(in[y])-2*smm(in[tmp]);
 93 }
 94 //main
 95 int main(){
 96     n=read();q=read();s=read();
 97     tree_lmn=mxn-10;
 98     int i,j;
 99     int u,v,d;
100     for(i=1;i<n;i++){
101         u=read();v=read();d=read();
102         add_edge(u,v,d,i);        
103         add_edge(v,u,d,i);
104     }
105     //
106     dep[1]=1;
107     DFS(1,0);
108     initLCA();//调了一晚上没有加这个初始化!初始化!初始化!初始化!初始化! 
109     for(i=1;i<=n;i++){//预处理dfs序数组 
110         add(in[i],disto[i]);
111         add(out[i],-disto[i]);
112     }
113     int opr;
114     while(q--){
115         opr=read();
116         if(opr==0){//从s去v点 
117             v=read();
118             printf("%d\n",dist(s,v));
119             s=v;
120         }
121         else{//改权值 
122             v=read();d=read();
123             v=tto[v];
124             add(in[v],d-disto[v]);
125             add(out[v],-d+disto[v]);
126             disto[v]+=d-disto[v];
127         }
128     }
129     return 0;
130 }

 

posted @ 2016-09-20 16:04  SilverNebula  阅读(376)  评论(0编辑  收藏  举报
AmazingCounters.com