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?
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.
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
POJ Monthly--2006.02.26,zgl & twb
就是一个可变权值的LCA问题
可用树状数组或者线段树维护权值的变化
用RMQ去算LCA
不知道为什么超时
我的TLE版本:
- #include<cstdio>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- struct node
- {
- int x,y,t;
- }origin[100010],tosort[100010];
- int fa[100010],s[100010],e[100010],dis[100010],l[100010],r[100010],in[100010];
- int n,total;
- int early[100010];
- int num;
- int a[200020];
- int m[200020][30];
- int village[100010];
- int ee[30];
- void process(int N)
- {
- ee[0] = 1;
- int i, j;
- for (i = 1 ; i <= N ; i++) ee[i] = ee[i-1] * 2;
- for (i = 0; i < N; i++) m[i][0] = i;
- for (j = 1; 1 << j <= N; j++)
- for (i = 0; i + (1 << j) - 1 < N; i++)
- if (a[m[i][j - 1]] < a[m[i + (1 << (j - 1))][j - 1]])
- m[i][j] = m[i][j - 1];
- else
- m[i][j] = m[i + (1 << (j - 1))][j - 1];
- }
- int RMQ(int i,int j)
- {
- float kk = log((float)(j - i + 1))/log(2.0);
- int k = (int) kk;
- if (a[m[i][k]] <= a[m[j-ee[k]+1][k]]) return m[i][k];
- else return m[j-ee[k]+1][k];
- }
- bool cmp(struct node a,struct node b)
- {
- return a.x < b.x;
- }
- void dfs(int k)
- {
- total++;
- village[total] = k;
- l[k] = total;
- a[num] = l[k];
- early[k] = num;
- num++;
- for (int i = s[k] ; i <= e[k] ; i++)
- {
- int next = tosort[i].y;
- if (fa[next] == -1)
- {
- fa[next] = k;
- dis[next] = dis[k] + tosort[i].t;
- dfs(next);
- a[num] = l[k];
- num++;
- }
- }
- r[k] = total;
- }
- int lowbit(int t)
- {
- return (t & (-t));
- }
- int sum(int t)
- {
- int ans = 0;
- while (t > 0)
- {
- ans += in[t];
- t -= lowbit(t);
- }
- return ans;
- }
- void update(int t,int v)
- {
- while (t <= n)
- {
- in[t] += v;
- t += lowbit(t);
- }
- }
- int dist(int st,int ed)
- {
- int mid;
- if (l[st] < l[ed]) mid = village[a[RMQ(early[l[st]],early[l[ed]])]];
- else mid = village[a[RMQ(early[l[ed]],early[l[st]])]];
- int dis1 = dis[st] + sum(l[st]);
- int dis2 = dis[mid] + sum(l[mid]);
- int dis3 = dis[ed] + sum(l[ed]);
- return dis1 - dis2 + dis3 - dis2;
- }
- int main()
- {
- int q,st;
- scanf("%d%d%d",&n,&q,&st);
- for (int i = 1 ; i < n ; i++)
- {
- scanf("%d%d%d",&origin[i].x,&origin[i].y,&origin[i].t);
- tosort[i-1].x = origin[i].x;
- tosort[i-1].y = origin[i].y;
- tosort[i-1].t = origin[i].t;
- }
- sort(tosort,tosort+n-1,cmp);
- for (int i = 1 ; i <= n ; i++)
- {
- fa[i] = -1;
- s[i] = n;
- e[i] = -1;
- }
- for (int i = 0 ; i < n-1 ; i++)
- {
- if (i < s[tosort[i].x]) s[tosort[i].x] = i;
- if (i > e[tosort[i].x]) e[tosort[i].x] = i;
- }
- fa[1] = 1;
- dis[1] = 0;
- total = 0;
- num = 0;
- for (int i = 1 ; i <= n ; i++)
- {
- in[i] = 0;
- early[i] = 3*n;
- }
- dfs(1);
- process(2*n-1);
- while (q--)
- {
- int op;
- scanf("%d",&op);
- if (op == 0)
- {
- int ed;
- scanf("%d",&ed);
- printf("%d\n",dist(st,ed));
- st = ed;
- }
- if (op == 1)
- {
- int num2,newt;
- scanf("%d%d",&num2,&newt);
- int ox = origin[num2].x;
- int oy = origin[num2].y;
- int dif = newt - origin[num2].t;
- origin[num2].t = newt;
- if (fa[oy] == ox)
- {
- update(l[oy],dif);
- update(r[oy]+1,-dif);
- }
- else
- {
- update(l[ox],dif);
- update(r[ox]+1,-dif);
- }
- }
- }
- return 0;
- }
转载于:https://blog.51cto.com/nemomojie/1185752