URAL 1553. Caves and tunnels tree chain split

Source: Internet
Author: User

A tree each starting point right-click value is 0

2 operating mode 1. First I right-click value Add x 2. Begging u to V on this road upper right value

Tree-based interlocking division title

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn = 100010; struct Edge{int V, next;} E[maxn*2];int FIRST[MAXN], cnt;void addedge (int u, int v) {e[cnt].v = V;e[cnt].next = First[u];first[u] = CNT++;E[CNT].V = U;e[cnt].next = first[v];first[v] = cnt++;} int TOP[MAXN], SZ[MAXN], SON[MAXN], F[MAXN], Dep[maxn];int N, Id[maxn], RANK[MAXN], tid;void init () {memset (First,-1, size of (first)); cnt = 0;memset (son,-1, sizeof (son)); tid = 0;} void dfs1 (int u, int fa, int d) {Sz[u] = 1;f[u] = Fa;dep[u] = d;for (int i = first[u]; i = 1; i = e[i].next) {int v = e[i]. V;if (v = = FA) continue;dfs1 (V, U, d+1); Sz[u] + = sz[v];if (Son[u] = = 1 | | sz[son[u]] < SZ[V]) son[u] = V;}} void dfs2 (int u, int tp) {Top[u] = Tp;id[u] = ++tid;rank[tid] = u;if (Son[u] = =-1) return;dfs2 (Son[u], TP); for (int i = First [u]; I! =-1; i = e[i].next) {int v = e[i].v;if (v = f[u] && son[u]! = v) dfs2 (V, v);}} int a[maxn<<2];void pushup (int rt) {A[rt] = max (a[rt<<1], a[rt<<1|1]);}  void build (int l, int r, int rt) {A[rt] = 0;if (L = = r) Return;int m = (L + R) >> 1;build (L, M, rt<<1); Build (M+1, R, rt<<1|1);} int query (int x, int y, int l, int r, int rt) {if (x = = L && y = = r) {return A[RT];} int m = (L + R) >> 1;if (y <= m) return query (x, Y, L, M, rt<<1), else if (x > M) return query (x, Y, m+1, R, rt<<1|1); Else{return max (query (x, M, L, M, rt<<1), query (m+1, y, m+1, R, Rt<<1|1));}} void update (int x, int l, int r, int rt, int d) {if (L = = r) {A[rt] + = D;return;} int m = (L + R) >> 1;if (x <= m) update (x, L, M, rt<<1, D); Elseupdate (x, m+1, R, Rt<<1|1, D);p Ushup (RT) ;} int change (int u, int v) {int ans = 0;while (top[u]! = Top[v]) {if (Dep[top[u]] < DEP[TOP[V]) swap (U, v); ans = max (ans, que Ry (Id[top[u]], id[u], 1, N, 1)); u = F[top[u]];} if (Dep[u] > Dep[v]) Swap (U, v); ans = max (ans, query (Id[u], id[v], 1, N, 1)); return ans; int main () {while (scanf ("%d", &n)! = EOF) {init (); for (int i = 1; I < n; i++) {int u, v;scanf ("%d%d", &u, &v); Addedge (U, v);} DFS1 (1, 0, 0);d FS2 (1, 1); Build (1, N, 1), int q;scanf ("%d", &q), while (q--) {char s[10];int u, v;scanf ("%s%d%d", S, &amp ; u, &v), if (s[0] = = ' G ') {printf ("%d\n", Change (U, v));} Else{update (Id[u], 1, N, 1, v);}} return 0;}

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

URAL 1553. Caves and tunnels tree chain split

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.