POJ 3237 tree (chain split)

Source: Internet
Author: User

Title Address: POJ 3237
The question took an afternoon. It was supposed to be that Max and min two arrays are not changed, only need to change the lazy array, and then use the lazy tag at query time to return to Max or-min, and later found that the error is very serious.
The problem is to modify the max and Min arrays in the pushdown to achieve the maximum value inversion.
The code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#define LL Long Long#define PI ACOs ( -1.0)#pragma COMMENT (linker, "/stack:1024000000,1024000000")Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-3;Const intmaxn=10000+Ten;#define ROOT 1, tot, 1#define Lson L, Mid, rt<<1#define Rson mid+1, R, Rt<<1|1intHEAD[MAXN], CNT, tot;intSIZ[MAXN], W[MAXN], TOP[MAXN], SON[MAXN], DEP[MAXN], FA[MAXN];intmax[maxn<<2], min[maxn<<2], lazy[maxn<<2];structnode{intU, V, W, next;} edge[maxn<<1];voidAddintUintVintW) {edge[cnt].u=u;        Edge[cnt].v=v;        Edge[cnt].w=w;        Edge[cnt].next=head[u]; head[u]=cnt++;}voidInit () {memset(head,-1,sizeof(head)); cnt=tot=0;memset(Son,0,sizeof(son));memset(DEP,0,sizeof(DEP));memset(Lazy,0,sizeof(lazy));memset(Max,-inf,sizeof(Max));memset(Min,inf,sizeof(Min));}voidDFS1 (intUintP) {siz[u]=1; for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(v==p)Continue; dep[v]=dep[u]+1;                Fa[v]=u; DFS1 (V,u);if(Siz[son[u]]<siz[v]) son[u]=v; }}voidDFS2 (intUintTP) {W[u]=++tot; TOP[U]=TP;if(Son[u]) DFS2 (SON[U],TP); for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(V!=fa[u]&&v!=son[u]) DFS2 (V,V); }}structline_tree{voidUpdatenode (intRT) {swap (max[rt],min[rt]); max[rt]*=-1; min[rt]*=-1; }voidPushup (intRT) {Max[rt]=max (max[rt<<1],max[rt<<1|1]); Min[rt]=min (min[rt<<1],min[rt<<1|1]); }voidPushdown (intRT) {if(Lazy[rt]) {lazy[rt<<1]=1-lazy[rt<<1]; lazy[rt<<1|1]=1-lazy[rt<<1|1]; Updatenode (rt<<1); Updatenode (rt<<1|1); lazy[rt]=0; }        }voidUpdate (intPintXintLintRintRT) {if(L==R) {max[rt]=min[rt]=x;return; }intMid=l+r>>1; Pushdown (RT);if(P<=mid) Update (P,x,lson);ElseUpdate (P,x,rson);        Pushup (RT); }voidNegate (intllintRrintLintRintRT) {if(LL&LT;=L&AMP;&AMP;RR&GT;=R) {lazy[rt]=1-LAZY[RT]; Updatenode (RT);return; }intMid=l+r>>1; Pushdown (RT);if(Ll<=mid) Negate (Ll,rr,lson);if(Rr>mid)                Negate (Ll,rr,rson);        Pushup (RT); }intQuery (intllintRrintLintRintRT) {if(LL&LT;=L&AMP;&AMP;RR&GT;=R) {returnMAX[RT]; }intMid=l+r>>1, Ans=-inf; Pushdown (RT);if(Ll<=mid) Ans=max (Ans,query (Ll,rr,lson));if(Rr>mid) Ans=max (Ans,query (Ll,rr,rson));returnAns }}lt;voidChangeintUintV) {intF1=top[u], f2=top[v]; while(F1!=F2) {if(Dep[f1]<dep[f2])                        {swap (U,V);                Swap (F1,F2); } Lt.                Negate (W[f1],w[u],root);        U=fa[f1];f1=top[u]; }if(U==V)return;if(Dep[u]<dep[v]) swap (U,V); Lt. Negate (w[son[v]],w[u],root);}intSolveintUintV) {intF1=top[u], f2=top[v], Ans=-inf; while(F1!=F2) {if(Dep[f1]<dep[f2])                        {swap (U,V);                Swap (F1,F2); } Ans=max (Ans,lt.                Query (W[f1],w[u],root));        U=fa[f1];f1=top[u]; }if(U==V)returnAnsif(Dep[u]<dep[v]) swap (U,V);returnMax (ans,lt. Query (W[son[v]],w[u],root));}intMain () {intT, N, I, u, V, c, id;Chars[Ten];scanf("%d", &t); while(t--) {scanf("%d", &n); Init (); for(i=1; i<n;i++) {scanf("%d%d%d", &u,&v,&c);                        Add (U,V,C);                Add (V,U,C); } DFS1 (1,-1); DFS2 (1,1); for(i=0; i<cnt;i+=2) {u=edge[i].u; V=EDGE[I].V;if(Dep[u]<dep[v]) swap (U,V); Lt.                Update (W[u],edge[i].w,root); } while(scanf('%s ', s)!=eof&&s[0]!=' D '){if(s[0]==' C '){scanf("%d%d", &id,&c); u=edge[id-1<<1].u; v=edge[id-1<<1].V;if(Dep[u]<dep[v]) swap (U,V); Lt.                        Update (W[u],c,root); }Else if(s[0]==' N '){scanf("%d%d", &u,&v);                        Change (U,V); }Else{scanf("%d%d", &u,&v);printf("%d\n", Solve (u,v)); }                }        }return 0;}

POJ 3237 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.