1036: [ZJOI2008] Tree statistics count time limit:10 Sec Memory limit:162 MB
submit:8095 solved:3300
[Submit] [Status] [Discuss] Description
There are n nodes on a tree, numbered 1 to N, each with a weight of W. We will ask you to do something about this tree in the following form: I. Change u t: Changing the weight of the node U to t ii. QMAX U V: asks for the maximum weight of the node on the path from point u to v. Qsum u V: ask for the weights of nodes on the path from point U to V and note that nodes on the path from point u to v include U and V itself
Input
The first behavior of the input is an integer n, which represents the number of nodes. The next n–1 line, with 2 integers a and b per line, indicates that there is an edge connected between Node A and Node B. Next n rows, one integer per line, and the integer of line I, WI represents the weight of node I. The next 1 lines, an integer q, represent the total number of operations. The next Q line, one operation per line, is given in the form "Change U T" or "QMAX u V" or "Qsum u V". For 100% data, ensure that the weight of each node in the 1<=n<=30000,0<=q<=200000; midway operation is between 30000 and 30000.
Output
For each "QMAX" or "qsum" operation, each line outputs an integer representing the result of the required output.
Sample Input4
1 2
2 3
4 1
4 2 1 3
12
QMAX 3 4
QMAX 3 3
QMAX 3 2
QMAX 2 3
Qsum 3 4
Qsum 2 1
Change 1 5
QMAX 3 4
Change 3 6
QMAX 3 4
QMAX 2 4
Qsum 3 4Sample Output4
1
2
2
10
6
5
6
5
-HINT Source
The partition of the tree
The puzzle: With my constant LCT will hang, don't be lazy ... I rub ... The tree also wrote the wrong pitch ... Hand residual error ....
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 #defineCH for (int d=0;d<2;d++) if (Ch[d])Ten #defineLson x->ch[0],l,m One #defineRson X->ch[1],m+1,r A using namespacestd; - Const intmaxn=30000+Ten, maxnode=60000+Ten, inf=-1u>>1; - structnode{ thenode*ch[2];intMx,sm,siz;node () {mx=-inf;sm=0;} - voidInitinta) {mx=sm=a;return;} - voidSett (inta) {mx=a;sm=siz*a;return;} - voidUpdate () {sm=0; mx=-inf; Ch{sm+=ch[d]->sm;mx=max (MX,CH[D]->MX);}return;} +}seg[maxnode],*nodecnt=seg,*Root; - intA[MAXN],N,Q,POS,CV,_MX,_SM,QL,QR; + voidBuild (Node*&x=root,intL=1,intR=N) { Ax=nodecnt++;intM=l+r>>1;if(l==r) x->init (a[m]); at ElseBuild (Lson), Build (Rson), x->update (); x->siz=r-l+1;return; - } - voidUpdate (Node*&x=root,intL=1,intR=N) { - if(L==R) {X->sett (CV);return;} - intM=l+r>>1;if(pos<=m) update (Lson);ElseUpdate (Rson); X->update ();return; - } in voidQuery (Node*x=root,intL=1,intR=N) { - if(ql<=l&&r<=qr) { to_mx=max (_mx,x->MX); +_sm+=x->SM; -}Else{intM=l+r>>1; the if(ql<=m) query (Lson);if(qr>M) query (Rson); *}return; $ }Panax Notoginseng structted{intX,Y;TED*NXT;} adj[maxn<<1],*fch[maxn],*ms=adj; - voidAddintXinty) { the*ms= (Ted) {x,y,fch[x]};fch[x]=ms++;*ms= (Ted) {y,x,fch[y]};fch[y]=ms++;return; + } A inttop[maxn],p[maxn],sz=0, SIZ[MAXN],SON[MAXN],FA[MAXN],DEP[MAXN]; the voidDfsintx) { +siz[x]=1;d ep[x]=dep[fa[x]]+1; - for(ted*e=fch[x];e;e=e->NXT) { $ intv=e->y;if(v!=Fa[x]) { $Fa[v]=x;dfs (v); siz[x]+=Siz[v]; - if(Siz[son[x]]<siz[v]) son[x]=v; - } the}return; - }Wuyi voidBuildintXintTP) { thep[x]=++sz;top[x]=TP; - if(Son[x]) build (SON[X],TP); Wu for(ted*e=fch[x];e;e=e->NXT) { - intv=e->y;if(v!=fa[x]&&v!=Son[x]) build (v,v); About}return; $ } - voidQueryintXinty) { - intF1=top[x],f2=Top[y]; - while(f1!=F2) { A if(dep[f1]<Dep[f2]) swap (F1,F2), swap (x, y); +Ql=p[f1];qr=p[x];query (); x=fa[f1];f1=Top[x]; the}if(Dep[x]>dep[y]) swap (x, y); Ql=p[x];qr=p[y];query ();return; - } $InlineintRead () { the intx=0, sig=1;CharCh=GetChar (); the for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=0; the for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0'; the returnsig?x:-x; - } inInlinevoidWriteintx) { the if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; the intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; About for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; the } the voidinit () { then=read (); + for(intI=1; i<n;i++) Add (read (), read ()); -Dfs1); Build (1,1); the for(intI=1; i<=n;i++) a[p[i]]=read ();Bayi build (); the return; the } - voidWork () { -Q=read ();intx, y;Chars[Ten]; the while(q--){ thescanf"%s", s); the if(s[0]=='C') Pos=p[read ()],cv=read (), update (); the Else{_mx=-inf;_sm=0; query (read (), read ()); - if(s[1]=='M') write (_MX), ENT;ElseWrite (_SM), ENT; the } the } the return;94 } the voidprint () { the return; the }98 intMain () {init (); work ();p rint ();return 0;}
Bzoj 1036 [ZJOI2008] Tree statistics count