HDU5274 Dylans loves trees (tree chain split + xor)

Source: Internet
Author: User

Portal: http://acm.hdu.edu.cn/showproblem.php?pid=5274
Test instructions is very simple, input later, when the query
0 x Y, change the x point to Y.
1 x y, is the query [x, y], whether the number of occurrences is even, an odd number of <=1.
is an even number, output-1, with odd words, output odd weights.
Train of thought: This problem is also a naked problem, but this problem of line tree maintenance is more ingenious, because a number of different or own even several times is 0, so ask for an interval XOR and on the line. But there is a place to pay attention, that is, the weight of 0, the difference or self, odd number of times is also 0. So you can put all the weight value of +1, the output time again-1 is good. If you are + 1, do not forget to change the weight of the value of +1.

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <cctype>#include <string>#include <iostream>#include <vector>#include <map>#include <queue>#include <ctime>using namespace STD;#pragma COMMENT (linker, "/stack:102400000,102400000")typedef Long LongLL;typedefpair<int,int> PII;#define PB push_back#define Lson l,m,rt<<1#define Rson m+1,r,rt<<1|1#define CALM (l+r) >>1Const intInf= (int)1e9+7;Const intmaxn=100010;structee{intTo,next; EE () {} EE (intTo,intNext): To, Next (next) {}}edge[maxn*2];intN,Q,ECNT,HEAD[MAXN],TOT,VAL[MAXN];intTOP[MAXN],FA[MAXN],NUM[MAXN],SON[MAXN],ID[MAXN],REV[MAXN],DEEP[MAXN];inline voidAddedge (intAintb) {Edge[ecnt]=ee (b,head[a]); head[a]=ecnt++;}voidDFS1 (intSintPreintd) {fa[s]=pre;deep[s]=d;num[s]=1; son[s]=-1; for(intI=head[s];~i;i=edge[i].next) {intt=edge[i].to;if(T==pre)Continue; DFS1 (t,s,d+1); num[s]+=num[t];if(son[s]==-1||        Num[t]>num[son[s]]) {son[s]=t; }    }}voidDFS2 (intSintRT) {Top[s]=rt;id[s]=++tot;rev[tot]=s;if(son[s]==-1)return; DFS2 (SON[S],RT); for(intI=head[s];~i;i=edge[i].next) {intt=edge[i].to;if(t==fa[s]| | T==son[s])Continue;    DFS2 (t,t); }}//segmenttreeintsum[maxn<<2];inline voidPushup (intRT) {sum[rt]=sum[rt<<1]^sum[rt<<1|1];}voidBuildintLintRintRT) {if(L==R) {Sum[rt]=val[rev[l]];return; }intM=calm;    Build (Lson); build (Rson); Pushup (RT);}intQueryintLintRintLintRintRT) {if(L&LT;=L&AMP;&AMP;R&LT;=R) {returnSUM[RT]; }intm=calm,ans=0;if(l<=m) Ans^=query (L,r,lson);if(r>m) Ans^=query (L,r,rson);returnAns;}voidUpdateintXintVintLintRintRT) {if(L==R) {sum[rt]=v;return; }intM=calm;if(x<=m) update (X,v,lson);ElseUpdate (X,v,rson); Pushup (RT);}intFindsum (intXintY) {intf1=top[x],f2=top[y],ans=0; while(F1!=F2) {if(Deep[f1]<deep[f2])        {swap (F1,F2); swap (x, y); } ans^=query (Id[f1],id[x],1N1);    X=FA[F1];F1=TOP[X]; }if(Deep[x]>deep[y]) swap (x, y); Ans^=query (Id[x],id[y],1N1);returnAns;}intMain () {//freopen ("/home/xt/code/acm/input.txt", "R", stdin);    intTscanf("%d", &t); while(t--) {scanf("%d%d", &n,&q);memset(head,-1,sizeofHead); Ecnt=0; tot=0; for(intI=1; i<n;i++) {intb;scanf("%d%d", &a,&b);        Addedge (A, b); Addedge (b,a); } for(intI=1; i<=n;i++) {scanf("%d", &val[i]);        val[i]++; } DFS1 (1,0,1);d FS2 (1,1); Build1N1); while(q--) {intOpscanf("%d", &op);intb;scanf("%d%d", &a,&b);if(op==0) {Update (id[a],b+1,1N1);//update also to +1}Else{printf("%d\n", Findsum (A, B)-1);//output-1}        }    }//printf ("[Run in%.1fs]\n", (double) clock ()/clocks_per_sec);    return 0;}

HDU5274 Dylans loves trees (tree chain split + xor)

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.