Title Link: Poj 2763 housewife Wind
Topic: Given a tree, then 2 operations:
- 0 u: Output path s to U's weights and S becomes u
- 1 I w: node I add w
Problem-solving ideas: Tree chain, and then use line tree maintenance, single-point modification of the interval query.
#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intMAXN =100005;intN, Q, S, NE;intFIRST[MAXN], JUMP[MAXN *2];intID, FAR[MAXN], SON[MAXN], CNT[MAXN], DEP[MAXN], TOP[MAXN], IDX[MAXN];structEdge {intU, V, W;void Set(intUintVintW) { This->u = u; This->v = v; This->w = W; }}ED[MAXN *2];voidDfs_fir (intUintPreintd) {Far[u] = pre; Dep[u] = D; Cnt[u] =1; Son[u] =0; for(inti = First[u]; i +1; i = Jump[i]) {intv = ed[i].v;if(v = = Pre)Continue; Dfs_fir (V, u, d+1); Cnt[u] + = Cnt[v];if(Cnt[son[u]] < CNT[V]) son[u] = v; }}voidDfs_sec (intUintRot) {Top[u] = rot; Idx[u] = id++;if(Son[u]) dfs_sec (Son[u], rot); for(inti = First[u]; i +1; i = Jump[i]) {intv = ed[i].v;if(v = = Far[u] | | v = = Son[u])Continue; Dfs_sec (V, v); }}inline voidAdd_edge (intUintVintW) {Ed[ne].Set(U, V, W); Jump[ne] = First[u]; First[u] = ne++;}#Define Lson (x) ((x) <<1)#Define Rson (x) (((x) <<1) |typedef Long LongllintLC[MAXN <<2], RC[MAXN <<2];ll S[MAXN <<2];inline voidPushup (intu) {S[u] = S[lson (u)] + S[rson (u)];}voidBuild (intUintLintR) {Lc[u] = l; Rc[u] = r; S[u] =0;if(L = = r)return;intMid = (L + r)/2; Build (Lson (U), L, mid); Build (Rson (U), Mid +1, R); Pushup (u);}voidModify (intUintXintW) {if(Lc[u] = = x && Rc[u] = = x) {S[u] = W;return; }intMID = (Lc[u] + rc[u])/2;if(x <= mid) Modify (Lson (U), X, W);ElseModify (Rson (U), X, W); Pushup (u);} ll query (intUintLintR) {if(l <= Lc[u] && Rc[u] <= R)returnS[u]; LL ret =0;intMID = (Lc[u] + rc[u])/2;if(L <= mid) ret + = query (Lson (U), L, R);if(R > Mid) ret + = query (Rson (U), L, R);returnRET;}voidInit () {ne =0; ID =1;intU, V, W;memset(First,-1,sizeof(first)); for(inti =0; I < N-1; i++) {scanf("%d%d%d", &u, &v, &w); Add_edge (U, V, W); Add_edge (V, U, W); } Dfs_fir (1,0,0); Dfs_sec (1,1); Build1,1, N); for(inti =0; I < N-1; i++) {if(dep[ed[i*2].U] < dep[ed[i*2].V]) Swap (ed[i*2].U, ed[i*2].V); Modify1, idx[ed[i*2].U], ed[i*2].W); }}ll Solve (intUintV) {LL ret =0;intp = top[u], q = top[v]; while(P! = q) {if(Dep[p] < dep[q]) {Swap (P, q); Swap (U, v); } ret + = query (1, Idx[p], idx[u]); U = far[p]; p = Top[u]; }if(U = = v)returnRetif(Dep[u] > Dep[v]) Swap (U, v); RET + = query (1, Idx[son[u]], idx[v]);returnRET;}intMain () { while(scanf("%d%d%d", &n, &q, &s) = =3) {init ();intK, u, W; while(q--) {scanf("%d%d", &k, &u);if(k) {scanf("%d", &w); Modify1, idx[ed[u*2-2].U], W); }Else{printf("%lld\n", Solve (S, u)); S = u; } } }return 0;}
POJ 2763 Housewife Wind (tree-chain split)