Topic Portal
Test instructions: Two operations, ask U to v distance, and u go to V; change the distance of the I-Bar to W
Analysis: According to the DFS access order, the tree is processed into a chain, then the back edge processing as a negative weight, then the LCA plus bit to know the distance from the U to V, bit store information on each side, so that the second operation can be quickly resolved with a bit
Using RMQ's writing, I do not know where to write a setback, instead of multiplication method
/************************************************* author:running_time* Created TIME:2015/10/6 Tuesday 11:45:03* Fil E Name:P oj_2763.cpp ************************************************/#include <cstdio> #include <algorithm > #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include < string> #include <vector> #include <queue> #include <deque> #include <stack> #include < list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime >using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 1e5 + 10;const int D = 20;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const Dou ble EPS = 1e-8;struct Edge {int V, W, id, NEX;} edge[n<<1];struct BIT {int c[n<<1], NN; void init (int n) {NN = n * 2; memset (c, 0, sizeof (c)); } void UpdatA (int i, int x) {while (I <= NN) {C[i] + = x; i + = i & (-i); }} int query (int i) {int ret = 0; while (i) {ret + = C[i]; I-= i & (-i); } return ret; }}bit;int Head[n];int Dep[n], rt[d][n], id[n], in[n], Out[n];int cost[n];int E, tim;void init (void) {memset (head,-1, sizeof (head)); e = 0;} void Add_edge (int u, int v, int w, int id) {Edge[e] = (edge) {V, W, id, head[u]}; Head[u] = e++;} void DFS (int u, int fa, int d) {dep[u] = D; rt[0][u] = FA; for (int i=head[u]; ~i; i=edge[i].nex) {Edge &e = Edge[i]; if (e.v = = FA) continue; In[e.id] = id[e.v] = ++tim; DFS (E.V, u, D + 1); Out[e.id] = ++tim; }}int LCA (int u, int v) {if (Dep[u] < dep[v]) {swap (U, v); } for (int i=0; i<d; ++i) {if ((Dep[u]-dep[v]) >> I & 1) {u = rt[i][u]; }} if (U = = v) return u; For (int i=d-1; i>=0;-I.) {if (Rt[i][u]! = Rt[i][v]) {u = rt[i][u]; v = rt[i][v]; }} return rt[0][u];} int main (void) {int n, q, S; while (scanf ("%d%d%d", &n, &q, &s) = = 3) {init (); for (int u, V, W, I=1; i<n; ++i) {scanf ("%d%d%d", &u, &v, &cost[i]); Add_edge (U, V, cost[i], i); Add_edge (V, u, Cost[i], i); } Tim = 0; DFS (1,-1, 0); for (int i=1, i<d; ++i) {for (int j=1; j<=n; ++j) {rt[i][j] = rt[i-1][j] = =-1?-1: RT[I-1][RT[I-1][J]]; }} bit.init (n); for (int i=1; i<n; ++i) {bit.updata (In[i], cost[i]); Entry number Bit.updata (Out[i],-cost[i]); Back edge ordinal} int u = s; for (int op, i=1; i<=q; ++i) {scanf ("%d", &op); if (op = = 0) {int v; ScanF ("%d", &v); In point ordinal and back point ordinal number printf ("%d\n", Bit.query (Id[u]) + bit.query (Id[v])-Bit.query (Id[lca (U, v)]) * 2); U = V; } else {int p, W; scanf ("%d%d", &p, &w); Bit.updata (In[p], w-cost[p]); Bit.updata (Out[p], Cost[p]-W); Cost[p] = W; }}} return 0;}
lca+ tree-like array POJ 2763 housewife Wind