Housewife Wind
Time Limit: 4000MS |
|
Memory Limit: 65536K |
Total Submissions: 11419 |
|
Accepted: 3140 |
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 is some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village are so special, we can reach any and huts starting from an arbitrary hut. If Each road cannot was walked along twice, then the route between every pair is unique.
Since Jiajia earned enough, 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 could be different. For example, Wind takes 5 minutes on a road normally, but could take the 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 his children, so she would like to tell her children the exact time she'll spend on the roads. Can you help her?
Input
The first line contains three integers n, q, S. There was n huts in XX Village, q messages to process, and wind was currently in Hut S. N < 100001, q < 100001.
The following n-1 lines each contains three integers a, B and W. That means there was a road directly connecting hut A and B, time required is W. 1<=w<= 10000.
The following q lines is one of the following and the types:
Message a:0 u
A kid in Hut U calls wind. She should go to hut u from hers current position.
Message B:1 i W
The time required for I-th Road was changed to W. Note that the time change would not be happen when wind was on her road. The changed can only happen if wind was 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 11 2 12 3 20 21 2 30 3
Sample Output
13
Title Link: POJ 2763
Given a tree, two operations, one is to inquire the shortest distance from the current point to the V point, and after the output answer the current point becomes V Point, and the second is to change the length of an edge to W.
Consider how to do this only in the case of an inquiry, obviously the LCA is a bare question, set dis[u] to represent the distance from the U point to the root node, there are: $dis (u, v) = Dis[u]+dis[v]-2*dis[lca (u,v)]$, and then consider the second operation, It can be found that when you change the length of an edge (weight), it will affect the whole sub-tree under this edge, such as a side of u--v, and U is closer to the root node than V, then the effect is V is the entire subtree of the effect, that is, the sub-tree is a deeper point connected. Because it is the whole subtree, it is obvious that the DFS sequence can be converted by using a tree array or a segment tree to maintain the DFS series, the DIS array, the operation is the interval update, single-point query-change the length of the edge is obviously the whole sub-tree sequence interval length has changed $w '-w$. As if the general wording of the question is the tree chain, Konjac konjac I do not ... Just use a tree-like array, how do the tree arrays update? The left point is +val, and the right endpoint + 1-val can be located.
ZZ WA took the afternoon to discover that the D array was used as a depth array ...
Code:
#include <stdio.h> #include <iostream> #include <algorithm> #include <cstdlib> #include < sstream> #include <numeric> #include <cstring> #include <bitset> #include <string> #include <deque> #include <stack> #include <cmath> #include <queue> #include <set> #include <map >using namespace std, #define INF 0x3f3f3f3f#define LC (x) (x<<1) #define RC (x) ((x<<1) +1) #define MID (x, y) ( (x+y) >>1) #define FIN (name) freopen (name, "R", stdin) #define FOUT (name) freopen (name, "W", stdout) #define CLR (arr, SUM) memset (arr,sum,sizeof (arr)) #define FAST_IO Ios::sync_with_stdio (false); Cin.tie (0); typedef pair<int, Int> Pii;typedef Long Long ll;const double PI = ACOs ( -1.0); const int N = 100010;struct edge{int to, NXT, D;edge () {}edge (int _t o, int _nxt, int _d): to (_to), NXT (_NXT), D (_d) {}};edge e[n << 1];int head[n], tot;int ver[n << 1], d[n <& Lt 1], F[n], sz, dp[n << 1][19];int w[n], a[n], B[n];intL[n], R[n], t[n], ts;bitset<n>vis;void init () {CLR (head,-1); tot = 0;sz = 0;ts = 0; CLR (T, 0); Vis.reset ();} void Update (int k, int v) {while (K < N) {T[k] + = v;k + = (k & k);}} int query (int k) {int ret = 0;while (k) {ret + = t[k];k-= (k & k);} return ret;} inline void Add (int s, int t, int d) {E[tot] = Edge (t, Head[s], D); head[s] = tot++;} void Dfs (int u, int d, int sumdis) {Ver[++sz] = U;d[sz] = D; F[u] = sz; L[u] = ++ts;update (L[u], Sumdis); Update (L[u] + 1,-sumdis); Vis[u] = 1;for (int i = head[u]; ~i; i = e[i].nxt) {int v = e[i] . To;if (!vis[v]) {DFS (V, D + 1, Sumdis + E[I].D); Ver[++sz] = U;d[sz] = D;}} R[u] = ts;} void Rmqinit (int l, int r) {int I, j;for (i = l; I <= R; ++i) dp[i][0] = i;for (j = 1; l + (1 << J)-1 <= R; + + j) {for (i = l; i + (1 << J)-1 <= R; ++i) {int a = Dp[i][j-1], B = dp[i + (1 << (j-1))][j-1];DP [I][j] = D[a] < D[b]? a:b;}}} int LCA (int u, int v) {int L = F[u], r = f[v];if (L > R) Swap (L, r); int len = r-l + 1;int k = 0;whiLe (1 << (k + 1) <= len) ++k;int a = Dp[l][k], B = dp[r-(1 << k) + 1][k];return D[a] < D[b]? Ver[a]: ver[b];} int main (void) {int n, q, S, I, A, B, W;while (~SCANF ("%d%d%d", &n, &q, &s)) {init (); for (i = 1; I <= n-1; ++i) {scanf ("%d%d%d", &a, &b, &w); A[i] = A; B[i] = b; W[i] = W;add (A, B, w); Add (b, A, w);} DFS (s, 1, 0); Rmqinit (1, SZ), while (q--) {int ops;scanf ("%d", &ops), if (Ops = = 0) {int v;scanf ("%d", &v);p rintf ("%d\n", Query (l[ S]) + query (L[v])-2 * Query (L[LCA (S, v)])); s = V;} Else{int K, neww;scanf ("%d%d", &k, &NEWW); int u = a[k], v = b[k];if (L[u] > L[v]) Swap (U, v); Update (L[v], NEWW- W[K]); Update (R[v] + 1,-(neww-w[k])); W[k] = NEWW;}}} return 0;}
POJ 2763 housewife Wind (Dfs sequence +lca+ tree array)