POJ 2763

 Housewife Wind

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 4682   Accepted: 1186

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問題
可用樹狀數組或者線段樹維護權值的變化
用RMQ去算LCA
不知道爲什麼超時
 
我的TLE版本:
  1. #include<cstdio> 
  2. #include<algorithm> 
  3. #include<cmath> 
  4. using namespace std; 
  5.  
  6. struct node 
  7.     int x,y,t; 
  8. }origin[100010],tosort[100010]; 
  9.  
  10. int fa[100010],s[100010],e[100010],dis[100010],l[100010],r[100010],in[100010]; 
  11. int n,total; 
  12. int early[100010]; 
  13. int num; 
  14. int a[200020]; 
  15. int m[200020][30]; 
  16. int village[100010]; 
  17. int ee[30]; 
  18.  
  19. void process(int N) 
  20.     ee[0] = 1; 
  21.     int i, j; 
  22.     for (i = 1 ; i <= N ; i++) ee[i] = ee[i-1] * 2; 
  23.     for (i = 0; i < N; i++) m[i][0] = i; 
  24.     for (j = 1; 1 << j <= N; j++) 
  25.         for (i = 0; i + (1 << j) - 1 < N; i++) 
  26.             if (a[m[i][j - 1]] < a[m[i + (1 << (j - 1))][j - 1]]) 
  27.                 m[i][j] = m[i][j - 1]; 
  28.             else 
  29.                 m[i][j] = m[i + (1 << (j - 1))][j - 1]; 
  30.  
  31. int RMQ(int i,int j) 
  32.     float kk = log((float)(j - i + 1))/log(2.0); 
  33.     int k = (int) kk; 
  34.     if (a[m[i][k]] <= a[m[j-ee[k]+1][k]]) return m[i][k]; 
  35.     else return m[j-ee[k]+1][k]; 
  36.  
  37. bool cmp(struct node a,struct node b) 
  38.     return a.x < b.x; 
  39.  
  40. void dfs(int k) 
  41.     total++; 
  42.     village[total] = k; 
  43.     l[k] = total; 
  44.     a[num] = l[k]; 
  45.     early[k] = num; 
  46.     num++; 
  47.     for (int i = s[k] ; i <= e[k] ; i++) 
  48.     { 
  49.         int next = tosort[i].y; 
  50.         if (fa[next] == -1) 
  51.         { 
  52.             fa[next] = k; 
  53.             dis[next] = dis[k] + tosort[i].t; 
  54.             dfs(next); 
  55.             a[num] = l[k]; 
  56.             num++; 
  57.         } 
  58.     } 
  59.     r[k] = total; 
  60.  
  61. int lowbit(int t) 
  62.     return (t & (-t)); 
  63.  
  64. int sum(int t) 
  65.     int ans = 0; 
  66.     while (t > 0) 
  67.     { 
  68.         ans += in[t]; 
  69.         t -= lowbit(t); 
  70.     } 
  71.     return ans; 
  72.  
  73. void update(int t,int v) 
  74.     while (t <= n) 
  75.     { 
  76.         in[t] += v; 
  77.         t += lowbit(t); 
  78.     } 
  79.  
  80. int dist(int st,int ed) 
  81.     int mid; 
  82.     if (l[st] < l[ed]) mid = village[a[RMQ(early[l[st]],early[l[ed]])]]; 
  83.     else mid = village[a[RMQ(early[l[ed]],early[l[st]])]]; 
  84.     int dis1 = dis[st] + sum(l[st]); 
  85.     int dis2 = dis[mid] + sum(l[mid]); 
  86.     int dis3 = dis[ed] + sum(l[ed]); 
  87.     return dis1 - dis2 + dis3 - dis2; 
  88.  
  89. int main() 
  90.     int q,st; 
  91.     scanf("%d%d%d",&n,&q,&st); 
  92.     for (int i = 1 ; i < n ; i++) 
  93.     { 
  94.         scanf("%d%d%d",&origin[i].x,&origin[i].y,&origin[i].t); 
  95.         tosort[i-1].x = origin[i].x; 
  96.         tosort[i-1].y = origin[i].y; 
  97.         tosort[i-1].t = origin[i].t; 
  98.     } 
  99.     sort(tosort,tosort+n-1,cmp); 
  100.     for (int i = 1 ; i <= n ; i++) 
  101.     { 
  102.         fa[i] = -1; 
  103.         s[i] = n; 
  104.         e[i] = -1; 
  105.     } 
  106.     for (int i = 0 ; i < n-1 ; i++) 
  107.     { 
  108.         if (i < s[tosort[i].x]) s[tosort[i].x] = i; 
  109.         if (i > e[tosort[i].x]) e[tosort[i].x] = i; 
  110.     } 
  111.     fa[1] = 1; 
  112.     dis[1] = 0; 
  113.     total = 0; 
  114.     num = 0; 
  115.     for (int i = 1 ; i <= n ; i++) 
  116.     { 
  117.         in[i] = 0; 
  118.         early[i] = 3*n; 
  119.     } 
  120.     dfs(1); 
  121.     process(2*n-1); 
  122.     while (q--) 
  123.     { 
  124.         int op; 
  125.         scanf("%d",&op); 
  126.         if (op == 0) 
  127.         { 
  128.             int ed; 
  129.             scanf("%d",&ed); 
  130.             printf("%d\n",dist(st,ed)); 
  131.             st = ed; 
  132.         } 
  133.         if (op == 1) 
  134.         { 
  135.             int num2,newt; 
  136.             scanf("%d%d",&num2,&newt); 
  137.             int ox = origin[num2].x; 
  138.             int oy = origin[num2].y; 
  139.             int dif = newt - origin[num2].t; 
  140.             origin[num2].t = newt; 
  141.             if (fa[oy] == ox) 
  142.             { 
  143.                 update(l[oy],dif); 
  144.                 update(r[oy]+1,-dif); 
  145.             } 
  146.             else 
  147.             { 
  148.                 update(l[ox],dif); 
  149.                 update(r[ox]+1,-dif); 
  150.             } 
  151.         } 
  152.     } 
  153.     return 0; 

  154.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章