Main topic
Given a tree, each node in the tree has two weights, each of which is the religious rating of the node and the religion of this node's religion. Modify these two weights several times, each asking the tree on the path to the point on the same religious maximum rating and rating.
Ideas
Do not think too much, each religion establishes a line of tree, space not to think about the dynamic node segment tree. Then, maintain the tree-chain splits on each segment tree.
Do you want to know the value range of C?
[0,10^5]
CODE
#define _crt_secure_no_warnings#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 100010using namespace STD;structSegtree *nil;structsegtree{Segtree *son[2];int_max,sum; Segtree () {_max = SUM =0; son[0] = son[1] = nil; }voidModify (intLintRintXintc) {if(L = = r) {_max = sum = C;return; }intMid = (L + r) >>1;if(x <= mid) {if(son[0] = = nil) son[0] =NewSegtree (); son[0]->modify (L,MID,X,C); }Else{if(son[1] = = nil) son[1] =NewSegtree (); son[1]->modify (Mid +1, r,x,c); } _max = max (son[0]->_max,son[1]->_max); sum = son[0]->sum + son[1]->sum; }intGetsum (intLintRintXintY) {if( This= nil)return 0;if(L = = x && y = = r)returnSumintMid = (L + r) >>1;if(Y <= mid)returnson[0]->getsum (L,mid,x,y);if(X > Mid)returnson[1]->getsum (Mid +1, r,x,y);intleft = son[0]->getsum (L,mid,x,mid);intright = son[1]->getsum (Mid +1, R,mid +1, y);returnLeft + right; }intGetmax (intLintRintXintY) {if( This= nil)return 0;if(L = = x && y = = r)return_max;intMid = (L + r) >>1;if(Y <= mid)returnson[0]->getmax (L,mid,x,y);if(X > Mid)returnson[1]->getmax (Mid +1, r,x,y);intleft = son[0]->getmax (L,mid,x,mid);intright = son[1]->getmax (Mid +1, R,mid +1, y);returnMax (left,right); }}none,*root[max];intPoints,asks;intHead[max],total;int_next[max <<1],aim[max <<1];inline voidADD (intXintY) {_next[++total] = head[x]; Aim[total] = y; HEAD[X] = total;}intP[max],belief[max];intSize[max],son[max],father[max],deep[max];inttop[max],pos[max],cnt;voidPredfs (intXintLast) {deep[x] = Deep[last] +1; Father[x] = last; SIZE[X] =1;intMax_size =0; for(inti = head[x]; I i = _next[i]) {if(Aim[i] = = last)Continue; Predfs (AIM[I],X); SIZE[X] + = size[aim[i]];if(Size[aim[i]] > max_size) max_size = size[aim[i]],son[x] = Aim[i]; }}voidDasointXintLastint_top) {pos[x] = ++cnt; TOP[X] = _top;if(Son[x]) DFS (Son[x],x,_top); for(inti = head[x]; I i = _next[i]) {if(Aim[i] = = Last | | aim[i] = = Son[x])Continue; DFS (Aim[i],x,aim[i]); }}inline intGetsum (intXintYintP) {intRe =0; while(top[x]! = Top[y]) {if(Deep[top[x]] < Deep[top[y]]) swap (x, y); Re + = Root[p]->getsum (1, Points,pos[top[x]],pos[x]); x = father[top[x]]; }if(Deep[x] < deep[y]) swap (x, y); Re + = Root[p]->getsum (1, Points,pos[y],pos[x]);returnRe;}inline intGetmax (intXintYintP) {intRe =0; while(top[x]! = Top[y]) {if(Deep[top[x]] < Deep[top[y]]) swap (x, y); Re = max (Re,root[p]->getmax (1, points,pos[top[x]],pos[x])); x = father[top[x]]; }if(Deep[x] < deep[y]) swap (x, y); Re = max (Re,root[p]->getmax (1, points,pos[y],pos[x]));returnRe;}Chars[Ten];intMain () {nil = &none;Cin>> points >> asks; for(inti =0; i < MAX; ++i) Root[i] =NewSegtree (); for(inti =1; I <= points; ++i)scanf("%d%d", &p[i],&belief[i]); for(intX,y,i =1; I < points; ++i) {scanf("%d%d", &x,&y); Add (x, y), add (y,x); } Predfs (1,0); DFS (1,0,1); for(inti =1; I <= points; ++i) Root[belief[i]]->modify (1, Points,pos[i],p[i]); for(intX,y,i =1; I <= asks; ++i) {scanf("%s%d%d", s,&x,&y);if(s[0] ==' C '&& s[1] ==' C ') {root[belief[x]]->modify (1, Points,pos[x],0); ROOT[BELIEF[X] = y]->modify (1, Points,pos[x],p[x]); }Else if(s[0] ==' C '&& s[1] ==' W ') Root[belief[x]]->modify (1, points,pos[x],p[x] = y);Else if(s[0] ==' Q '&& s[1] ==' S ')printf("%d\n", Getsum (x,y,belief[x]));Else printf("%d\n", Getmax (x,y,belief[x])); }return 0;}
Bzoj 3531 sdoi 2014 travel