Spoj-qtree 375 query on a tree Chain Division + line segment tree

Source: Internet
Author: User

Operation 1: Modify the K edge weight.

Operation 2: query the maximum edge right between two points.

Tree link segmentation, and the line segment tree maintains the maximum value

# Include <cstdio> # include <cstring> # include <cmath> # include <iostream> # include <algorithm> # include <set> # include <map> # include <queue> # include <vector> # include <string> # define EPS 1e-12 # define INF 0x7fffffff # define maxn 11111 using namespace STD; char STR [maxn]; struct node {int to, next;} e [maxn <1]; int head [maxn], en, N; int Fa [maxn], siz [maxn], top [maxn], son [maxn], W [maxn], DEP [maxn], ID; int Maxs [maxn <2]; int d [maxn] [3]; void Init () {Fa [1] = 0; Dep [1] = 0; id = en = 0; memset (Head,-1, sizeof (head); memset (siz, 0, sizeof (siz); memset (maxs, 0, sizeof (Maxs);} void add (int A, int B) {e [En]. to = B; E [En]. next = head [a]; head [a] = EN ++;} void DFS (INT now) {siz [now] = 1; son [now] = 0; for (INT I = head [now]; ~ I; I = E [I]. Next) {int to = E [I]. To; If (! = Fa [now]) {Fa [to] = now; Dep [to] = Dep [now] + 1; DFS (); if (siz [to]> siz [son [now]) Son [now] = to; siz [now] + = siz [to] ;}} void GETID (INT now, int root) {W [now] = ++ ID; top [now] = root; If (son [now]) GETID (son [now], top [now]); For (INT I = head [now]; ~ I; I = E [I]. Next) {If (E [I].! = Son [now] & E [I].! = Fa [now]) {GETID (E [I]. to, E [I]. to) ;}} void Update (int rt, int L, int R, int K, int add) {If (L = r) {Maxs [RT] = add; return;} int mid = (L + r)/2; int lson = (RT <1); int rson = (RT <1 | 1 ); if (k <= mid) Update (lson, L, mid, K, add); else Update (rson, Mid + 1, R, K, add ); maxs [RT] = max (Maxs [lson], Maxs [rson]);} int query (int rt, int L, int R, int Ql, int QR) {If (QL <= L & R <= QR) return Maxs [RT]; int mid = (L + r)/2; int lson = (RT <1); In T rson = (RT <1 | 1); int TMP = 0; If (QL <= mid) TMP = max (TMP, query (lson, L, mid, Ql, QR); If (QR> mid) TMP = max (TMP, query (rson, Mid + 1, R, Ql, QR); Return TMP ;} int find (int A, int B) {int F1 = top [a]; int F2 = top [B]; int ans = 0; while (F1! = F2) {If (DEP [F1]> Dep [F2]) {ans = max (ANS, query (1, 1, n, W [F1], W [a]); A = Fa [F1]; F1 = top [a];} else {ans = max (ANS, query (1, 1, n, W [F2], W [B]); B = Fa [F2]; F2 = top [B] ;}} if (a = B) Return ans; if (DEP [a]> Dep [B]) ans = max (ANS, query (1, 1, n, W [son [B], W [a]); else ans = max (ANS, query (, N, W [son [a], W [B]); Return ans;} int main () {int cas, a, B, C; scanf ("% d", & CAS); While (CAS --) {Init (); scanf ("% d", & N ); for (INT I = 1; I <n; I ++) {scanf ("% d", & A, & B, & C ); d [I] [0] = A; d [I] [1] = B; d [I] [2] = C; add (A, B ); add (B, A);} DFS (1); GETID (1, 1); For (INT I = 1; I <n; I ++) {If (DEP [d [I] [0]> Dep [d [I] [1]) Swap (d [I] [0], d [I] [1]); // place the edge weight on the son node and change it to vertex weight Update (, N, W [d [I] [1], d [I] [2]);} while (1) {scanf ("% s", STR); If (STR [0] = 'D') break; scanf ("% d", & A, & B); If (STR [0] = 'q') {printf ("% d \ n ", find (A, B);} else {Update (, N, W [d [a] [1], B) ;}} return 0 ;}


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.