码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2763

时间:2014-05-06 11:04:00      阅读:402      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

Housewife Wind
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 5471   Accepted: 1371

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 加树状数组
bubuko.com,布布扣
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 
  6 using namespace std;
  7 
  8 const int MAX_N = 100005;
  9 const int edge = MAX_N * 2;
 10 int N,Q,S;
 11 int first[MAX_N],Next[edge],w[edge],V[edge];
 12 int id[MAX_N],vs[MAX_N * 2],dep[MAX_N * 2];
 13 int d[2 * MAX_N][30],qid[2 * MAX_N][30];
 14 int E[edge],c[MAX_N * 2];
 15 int vis[MAX_N * 2];
 16 int n;
 17 
 18 int lowbit(int x) {
 19         return x & (-x);
 20 }
 21 
 22 int sum(int x) {
 23         int ret = 0;
 24         while(x > 0) {
 25                 ret += c[x];
 26                 x -= lowbit(x);
 27         }
 28         return ret;
 29 }
 30 
 31 void add(int x,int d) {
 32     //printf("x = %d\n",x);
 33         while(x <= n) {
 34                 c[x] += d;
 35                 x += lowbit(x);
 36         }
 37 }
 38 
 39 void add_edge(int id,int u) {
 40         int e = first[u];
 41         Next[id] = e;
 42         first[u] = id;
 43 }
 44 
 45 int no(int x) {
 46         if(x > N - 1) return x - N + 1;
 47         else return x + N - 1;
 48 }
 49 
 50 void dfs(int u,int fa,int d,int &k,int m) {
 51         id[u] = k;
 52         vs[k] = u;
 53         add(k,w[m]);
 54         vis[m] = 1;
 55         E[m] = k;
 56         dep[k++] = d;
 57         for(int e = first[u]; e != -1; e = Next[e]) {
 58                 if(V[e] != fa) {
 59                         dfs(V[e],u,d + 1,k,e);
 60                         vs[k] = u;
 61                         add(k,-w[e]);
 62                         vis[no(e)] = -1;
 63                         E[no(e)] = k;
 64                         dep[k++] = d;
 65                 }
 66         }
 67 }
 68 
 69 
 70 void RMQ() {
 71         for(int i = 1; i <= n; ++i) {
 72                 d[i][0] = dep[i];
 73                 qid[i][0] = i;
 74         }
 75         for(int j = 1; (1 << j) <= n; ++j) {
 76                 for(int i = 1; i + (1 << j) - 1 <= n; ++i) {
 77                         if(d[i][j - 1] > d[i + (1 << (j - 1))][j - 1]) {
 78                                 d[i][j] = d[i + (1 << (j - 1))][j - 1];
 79                                 qid[i][j] = qid[i + (1 << (j - 1))][j - 1];
 80                         } else {
 81                                 d[i][j] = d[i][j - 1];
 82                                 qid[i][j] = qid[i][j - 1];
 83                         }
 84                 }
 85         }
 86 }
 87 
 88 int query(int L,int R) {
 89         int k = 0;
 90         //printf("L = %d R = %d\n",L,R);
 91         while( (1 << (k + 1)) < (R - L + 1) ) ++k;
 92         //printf("k = %d\n",k);
 93         //printf("d= %d %d\n",d[L][1 << k] , d[R - (1 << k) + 1][1 << k]);
 94         return d[L][k] < d[R - (1 << k) + 1][k] ?
 95                qid[L][k] : qid[R - (1 << k) + 1][k];
 96 
 97 }
 98 
 99 int main()
100 {
101    // freopen("sw.in","r",stdin);
102     scanf("%d%d%d",&N,&Q,&S);
103     n = 2 * N - 1;
104     for(int i = 1; i <= N; ++i) first[i] = -1;
105     for(int i = 1; i <= N - 1; ++i) {
106             int u;
107             scanf("%d%d%d",&u,&V[i],&w[i]);
108             V[i + N - 1] = u;
109             w[i + N - 1] = w[i];
110             add_edge(i,u);
111             add_edge(i + N - 1,V[i]);
112     }
113 
114     int k = 1;
115     dfs(S,-1,0,k,0);
116     RMQ();
117     //printf("k = %d\n",k);
118 
119     int now = S;
120     for(int i = 1; i <= Q; ++i) {
121             int ch,v,id1;
122             scanf("%d",&ch);
123             if(ch == 0) {
124                     scanf("%d",&v);
125                    // printf("now = %d\n",now);
126                     int p = vs[ query(min(id[now],id[v]),max(id[now],id[v])) ];
127                     printf("%d\n",sum(id[now] ) + sum( id[v] ) - 2 * sum( id[p] ));
128                     now = v;
129             } else {
130                     scanf("%d%d",&id1,&v);
131                     int d = v - w[id1];
132                     w[id1] = v;
133                     add(E[id1],vis[id1] * d);
134                     add(E[id1 + N - 1],vis[id1 + N - 1] * d);
135             }
136     }
137 
138 
139     return 0;
140 }
View Code

 

poj 2763,布布扣,bubuko.com

poj 2763

标签:des   style   blog   class   code   tar   

原文地址:http://www.cnblogs.com/hyxsolitude/p/3710642.html

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