码迷,mamicode.com
首页 > Windows程序 > 详细

POJ 2763 Housewife Wind

时间:2016-02-21 22:34:10      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:

Housewife Wind
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 8618   Accepted: 2291

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

不多说了……这题就是裸的树链剖分(给个超链接切到树链剖分)……直接上代码……

AC代码:

技术分享
  1 #include<iostream>
  2 #include<cctype>
  3 using namespace std;
  4 const int MAXN=200000+10;
  5 //------------------------
  6 void read(int &x){
  7     x=0;int f=1;char ch=getchar();
  8     for(;!isdigit(ch);ch=getchar())if(ch==-)f=-1;
  9     for(;isdigit(ch);ch=getchar())x=x*10+ch-0;
 10     x*=f;
 11 }
 12 //-----------------------
 13 int first[MAXN],next[MAXN],to[MAXN],e;
 14 void AddEdge(int a,int b,int c){
 15     to[++e]=b;
 16     next[e]=first[a];
 17     first[a]=e;
 18 }
 19 //------------------------
 20 int n,q,s;
 21 int _sum,sum[2*MAXN];
 22 int d[MAXN][3];
 23 int siz[MAXN],dep[MAXN],son[MAXN],fa[MAXN],top[MAXN],last[MAXN];
 24 void dfs1(int x){
 25     son[x]=0;siz[x]=1;
 26     for(int i=first[x];i;i=next[i]){
 27         if(to[i]!=fa[x]){
 28             dep[to[i]]=dep[x]+1;
 29             fa[to[i]]=x;
 30             dfs1(to[i]);
 31             if(siz[son[x]]<siz[to[i]])son[x]=to[i];
 32             siz[x]+=siz[to[i]];
 33         }
 34     }
 35 }
 36 //-------------------------
 37 int z;
 38 void dfs2(int x,int tp){
 39     top[x]=tp;last[x]=++z;
 40     if(son[x]!=0)dfs2(son[x],tp);
 41     for(int i=first[x];i;i=next[i]){
 42         if(to[i]!=son[x] && to[i]!=fa[x])
 43             dfs2(to[i],to[i]);
 44     }
 45 }
 46 //-------------------------
 47 void update(int o,int l,int r,int v,int x){
 48     if(v<l || v>r)return;
 49     if(l==r){sum[o]=x;return;}
 50     int mid=(l+r)>>1,lo=o<<1,ro=lo|1;
 51     update(lo,l,mid,v,x);
 52     update(ro,mid+1,r,v,x);
 53     sum[o]=sum[lo]+sum[ro];
 54 }
 55 //---------------------------
 56 void query(int o,int l,int r,int ql,int qr){
 57     if(ql>qr)swap(ql,qr);
 58     if(ql<=l && qr>=r)_sum+=sum[o];
 59     else{
 60         int mid=(l+r)>>1,lo=o<<1,ro=lo|1;
 61         if(ql<=mid)query(lo,l,mid,ql,qr);
 62         if(qr>mid)query(ro,mid+1,r,ql,qr);
 63     }
 64 }
 65 //--------------------------
 66 long long find(int va,int vb){
 67     long long dist=0;
 68     int f1=top[va],f2=top[vb];
 69     while(f1!=f2){
 70         if(dep[f1]<dep[f2]){
 71             swap(f1,f2);
 72             swap(va,vb);
 73         }
 74         _sum=0;
 75         query(1,1,z,last[f1],last[va]);
 76         dist+=_sum;
 77         va=fa[f1];f1=top[va]; 
 78     }
 79     if(va==vb)return dist;
 80     _sum=0;
 81     if(dep[va]<dep[vb])swap(va,vb);
 82     query(1,1,z,last[son[vb]],last[va]);
 83     dist+=_sum;
 84     return dist;
 85 }
 86 //----------------------------
 87 int main(){
 88     read(n);read(q);read(s);
 89     for(int i=1;i<n;i++){
 90         int a,b,c;
 91         read(a);read(b);read(c);
 92         d[i][0]=a;
 93         d[i][1]=b;
 94         d[i][2]=c;
 95         AddEdge(a,b,c);
 96         AddEdge(b,a,c);
 97     }
 98     dfs1(1);
 99     dfs2(1,1);
100     for(int i=1;i<n;i++){
101         if(dep[d[i][0]]<dep[d[i][1]])swap(d[i][0],d[i][1]);
102         update(1,1,z,last[d[i][0]],d[i][2]);
103     }
104     int ord,u,i,w;
105     for(int t=1;t<=q;t++){
106         read(ord);
107         if(!ord){
108             read(u);
109             printf("%lld\n",find(s,u));
110             s=u;
111         }
112         else{
113             read(i);read(w);
114             if(dep[d[i][0]]<dep[d[i][1]])swap(d[i][0],d[i][1]);
115             update(1,1,z,last[d[i][0]],w);
116         }
117     }
118 }
View Code

 

POJ 2763 Housewife Wind

标签:

原文地址:http://www.cnblogs.com/543Studio/p/5205674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!