SPOJ QTREE Query on a tree

来源:互联网 发布:js变量为空 编辑:程序博客网 时间:2024/04/29 08:42

Description

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1.

We will ask you to perfrom some instructions of the following form:

  • CHANGE i ti : change the cost of the i-th edge to ti
    or
  • QUERY a b : ask for the maximum edge cost on the path from node a to node b

Input

The first line of input contains an integer t, the number of test cases (t <= 20). t test cases follow.

For each test case:

  • In the first line there is an integer N (N <= 10000),
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between ab of cost c (c <= 1000000),
  • The next lines contain instructions "CHANGE i ti" or "QUERY a b",
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "QUERY" operation, write one integer representing its result.

Example

Input:131 2 12 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONEOutput:1

3

树链剖分板子题

#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)#define loop(i,j,k) for (int i = j;i != -1; i = k[i])#define lson x << 1, l, mid#define rson x << 1 | 1, mid + 1, r#define ff first#define ss second#define mp(i,j) make_pair(i,j)#define pb push_back#define pii pair<int,int>#define inone(x) scanf("%d", &x)#define intwo(x,y) scanf("%d%d", &x, &y)using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-4;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 2e5 + 10;int T, n, x, y, z;int ft[N], nt[N], u[N], v[N], sz, va[N];int ct[N], mx[N], fa[N], o[N];int top[N], bel[N], dep[N], cnt;int f[N], g[N];char s[N];void dfs(int x, int f){dep[x] = dep[f] + 1;fa[x] = f; ct[x] = 1; mx[x] = 0;loop(i, ft[x], nt){if (u[i] == f) continue;o[u[i]] = v[i];dfs(u[i], x);ct[x] += ct[u[i]];mx[x] = ct[u[i]] > ct[mx[x]] ? u[i] : mx[x];}}void Dfs(int x, int t){bel[o[x]] = ++cnt;g[cnt] = x;top[x] = t ? top[fa[x]] : x;if (mx[x]) Dfs(mx[x], 1);loop(i, ft[x], nt){if (u[i] == fa[x]) continue;if (u[i] == mx[x]) continue;Dfs(u[i], 0);}}void build(int x, int l, int r){if (l == r) { f[x] = va[o[g[l]]]; return; }int mid = l + r >> 1;build(lson); build(rson);f[x] = max(f[x << 1], f[x << 1 | 1]);}void change(int x, int l, int r, int u, int v){if (l == r) { f[x] = v; return; }int mid = l + r >> 1;if (u <= mid) change(lson, u, v);else change(rson, u, v);f[x] = max(f[x << 1], f[x << 1 | 1]);}int get(int x, int l, int r, int ll, int rr){if (ll <= l&&r <= rr) return f[x];int mid = l + r >> 1, ans = 0;if (ll <= mid) ans = max(ans, get(lson, ll, rr));if (rr > mid) ans = max(ans, get(rson, ll, rr));return ans;}void query(int x, int y){int ans = 0;while (1){if (top[x] == top[y]){if (x == y) break;if (dep[x] > dep[y]) swap(x, y);ans = max(ans, get(1, 1, n, bel[o[x]] + 1, bel[o[y]]));break;}else{if (dep[top[x]] <= dep[top[y]]) swap(x, y);ans = max(ans, get(1, 1, n, bel[o[top[x]]], bel[o[x]]));x = fa[top[x]];}}printf("%d\n", ans);}int main(){inone(T);while (T--){o[1] = va[0] = dep[0] = ct[0] = 0;cnt = sz = 0;inone(n);rep(i, 1, n) ft[i] = -1;rep(i, 1, n - 1){intwo(x, y),inone(va[i]);u[sz] = y; v[sz] = i; nt[sz] = ft[x]; ft[x] = sz++;u[sz] = x; v[sz] = i; nt[sz] = ft[y]; ft[y] = sz++;}dfs(1, 0);Dfs(1, 0);build(1, 1, n);while (scanf("%s", s), s[0] != 'D'){intwo(x, y);if (s[0] == 'Q') query(x, y); else change(1, 1, n, bel[x], y);}}return 0;}


0 0
原创粉丝点击