1984-month-old BZOJ "Mao Jing tree" Tree chain division

Source: Internet
Author: User

Given a tree with edge weight, a bunch of messy operations (0.0) are provided, and the maximum edge weight between two points is repeatedly asked.

Place the edge weight of each edge on the point below the edge, and then process it according to the point weight. Note that the point weights of the two points in the LCA cannot be included in the path.

Nima UBUNTU wonderful system ...... I did not write the return value, but actually returned the re to me. Then I tried it all ...... Hand it over to WA ...... I kneel down

Don't dare to stop writing-Wall anymore ......

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define M 100100 using namespace std; struct Segtree {Segtree * ls, * rs; int change_mark, add_mark, max_num; void Add (int x); void Change (int x); void * operator new (size_t size);} * tree, mempool [M <1], * C = mempool; struct abcd {int to, f, next;} table [M <1]; int head [M], tot = 1; int n; int fa [M], son [M], dpt [M], size [M]; int pos [M], top [M], f [M], posf [M], cnt; vo Id Segtree: Add (int x) {add_mark + = x; max_num + = x;} void Segtree: Change (int x) {add_mark = 0; change_mark = x; max_num = x;} void * Segtree: operator new (size_t size) {C-> change_mark =-1; return C ++;} void Build_Tree (Segtree * & p, int x, int y) {int mid = x + y> 1; p = new Segtree; if (x = y) {p-> max_num = posf [mid]; return;} Build_Tree (p-> ls, x, mid); Build_Tree (p-> rs, mid + 1, y ); p-> max_num = max (p-> ls-> max_num, p-> rs-> max_num);} void Ch Ange (Segtree * p, int x, int y, int z, int v) {int mid = x + y> 1; if (x = y) {p-> max_num = v; return;} if (~ P-> change_mark) {p-> ls-> Change (p-> change_mark); p-> rs-> Change (p-> change_mark ); p-> change_mark =-1;} if (p-> add_mark) {p-> ls-> Add (p-> add_mark ); p-> rs-> Add (p-> add_mark); p-> add_mark = 0;} if (z <= mid) Change (p-> ls, x, mid, z, v); else Change (p-> rs, mid + 1, y, z, v); p-> max_num = max (p-> ls-> max_num, p-> rs-> max_num);} void Change (Segtree * p, int x, int y, int l, int r, int v) {int mid = x + y> 1; if (x = l & y = r) {p-> Change (v); return;} if (~ P-> change_mark) {p-> ls-> Change (p-> change_mark); p-> rs-> Change (p-> change_mark ); p-> change_mark =-1;} if (p-> add_mark) {p-> ls-> Add (p-> add_mark ); p-> rs-> Add (p-> add_mark); p-> add_mark = 0;} if (r <= mid) Change (p-> ls, x, mid, l, r, v); else if (l> mid) Change (p-> rs, mid + 1, y, l, r, v ); else Change (p-> ls, x, mid, l, mid, v), Change (p-> rs, mid + 1, y, mid + 1, r, v); p-> max_num = max (p-> ls-> max_num, p-> rs-> max_num);} void Add (Segtree * p, int x, int y, int L, int r, int v) {int mid = x + y> 1; if (x = l & y = r) {p-> Add (v); return;} if (~ P-> change_mark) {p-> ls-> Change (p-> change_mark); p-> rs-> Change (p-> change_mark ); p-> change_mark =-1;} if (p-> add_mark) {p-> ls-> Add (p-> add_mark ); p-> rs-> Add (p-> add_mark); p-> add_mark = 0;} if (r <= mid) Add (p-> ls, x, mid, l, r, v); else if (l> mid) Add (p-> rs, mid + 1, y, l, r, v ); else Add (p-> ls, x, mid, l, mid, v), Add (p-> rs, mid + 1, y, mid + 1, r, v); p-> max_num = max (p-> ls-> max_num, p-> rs-> max_num);} int Get_Ans (Segtree * p, int x, int y, int l, int r ){ Int mid = x + y> 1; if (x = l & y = r) return p-> max_num; if (~ P-> change_mark) {p-> ls-> Change (p-> change_mark); p-> rs-> Change (p-> change_mark ); p-> change_mark =-1;} if (p-> add_mark) {p-> ls-> Add (p-> add_mark ); p-> rs-> Add (p-> add_mark); p-> add_mark = 0;} if (r <= mid) return Get_Ans (p-> ls, x, mid, l, r); if (l> mid) return Get_Ans (p-> rs, mid + 1, y, l, r ); return max (Get_Ans (p-> ls, x, mid, l, mid), Get_Ans (p-> rs, mid + 1, y, mid + 1, r);} void Add (int x, int y, int z) {table [++ tot]. to = y; table [tot]. f = z; tabl E [tot]. next = head [x]; head [x] = tot;} void DFS1 (int x) {int I; dpt [x] = dpt [fa [x] + 1; size [x] = 1; for (I = head [x]; I = table [I]. next) {if (table [I]. to = fa [x]) continue; fa [table [I]. to] = x; f [table [I]. to] = table [I]. f; DFS1 (table [I]. to); size [x] + = size [table [I]. to]; if (size [table [I]. to]> size [son [x]) son [x] = table [I]. to ;}} void DFS2 (int x) {int I; if (son [fa [x] = x) top [x] = top [fa [x]; elsetop [x] = x; posf [pos [x] = ++ cnt] = f [x]; if (son [x]) DFS2 (son [x]); for (I = head [x]; I = table [I]. next) {if (table [I]. to = fa [x] | table [I]. to = son [x]) continue; DFS2 (table [I]. to) ;}} int Query (int x, int y) {int re =-2147483647, fx = top [x], fy = top [y]; while (fx! = Fy) {if (dpt [fx] <dpt [fy]) swap (x, y), swap (fx, fy); re = max (re, Get_Ans (tree, 1, n, pos [fx], pos [x]); fx = top [x = fa [fx];} if (x = y) return re; if (dpt [x] <dpt [y]) swap (x, y); re = max (re, Get_Ans (tree, 1, n, pos [y] + 1, pos [x]); return re;} void Change (int x, int y, int z) {int fx = top [x], fy = top [y]; while (fx! = Fy) {if (dpt [fx] <dpt [fy]) swap (x, y), swap (fx, fy); Change (tree, 1, n, pos [fx], pos [x], z); fx = top [x = fa [fx];} if (x = y) return; if (dpt [x] <dpt [y]) swap (x, y); Change (tree, 1, n, pos [y] + 1, pos [x], z);} void _ Add (int x, int y, int z) {int fx = top [x], fy = top [y]; while (fx! = Fy) {if (dpt [fx] <dpt [fy]) swap (x, y), swap (fx, fy); Add (tree, 1, n, pos [fx], pos [x], z); fx = top [x = fa [fx];} if (x = y) return; if (dpt [x] <dpt [y]) swap (x, y); Add (tree, 1, n, pos [y] + 1, pos [x], z);} int main () {# ifdef PoPoQQQfreopen ("1984.in"," r ", stdin); freopen (" 1984.out", "w", stdout); # endifint I, x, y, z; char p [20]; cin> n; for (I = 1; I <n; I ++) {scanf ("% d", & x, & y, & z); Add (x, y, z); Add (y, x, z);} DFS1 (1); DFS2 (1); Build_Tree (tree, 1, n); while (1) {scanf ("% s", p ); switch (p [1]) {case 'a': scanf ("% d", & x, & y); printf ("% d \ n ", query (x, y); break; case 'H': int temp; scanf ("% d", & temp, & z ); x = table [temp <1]. to; y = table [temp <1 | 1]. to; if (dpt [x] <dpt [y]) swap (x, y); Change (tree, 1, n, pos [x], z); break; case 'O': scanf ("% d", & x, & y, & z); Change (x, y, z); break; case 'D': scanf ("% d", & x, & y, & z); _ Add (x, y, z); break; case 'T': return 0 ;}}}


1984-month-old BZOJ "Mao Jing tree" Tree chain division

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.