Little Devil I

Source: Internet
Author: User
Tags define local

Face: Once there is a country, the city inside the country is a tree-shaped structure. The king of the country was a Lori, and fell in love with a little demon.

The Little Devil likes to create chaos and often allows the king to modify the road system between cities. Each road has two colors, black and white.

There are two kinds of changes, one is to turn the road between the city A and b all the color, the second is to the city A and B road between the adjacent road to turn color.

The King's Lori Daughter, WJMZBMR, is very concerned about the state of the road, and she will ask how much of the road between A and B is black.

All the roads were white at first.

Analysis:

The first operation is the simplest tree-chain split.

The second operation needs to be considered carefully if it is to be split with a tree chain.

First of all, still can only update that LOGN chain, can think of marking on the chain to indicate the update of the surrounding points. Is it feasible to do so?

On the heavy chain of the path, the middle point is constant and is marked two times, which is legal.

Consider asking. When asked, there are two questions, 1. Heavy chain points, 2. A point has a lot of degrees. This causes the complexity to become O (n).

The meaning of the tag needs to be changed, and for 2, only one point on one side is required, and the most appropriate point is the parent node of the edge.

For 1, the middle point on the heavy chain is not necessarily marked.

On top of that, playing markers in a parent node indicates that the child has modified its light side . Left O (logn) endpoints special treatment, complex will not degenerate.

Mark interval update point, flip interval update edge, this operation can be completed with the line segment tree. (The ID of the edge is numbered with its sub-node, which is very error-prone when it is written.)

Complexity O (QLOGNLOGN)

/**********************************************************            ------------------                          ** Author Abyssfish ***********************************************************/#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<stack>#include<map>#include<Set>#include<algorithm>#include<cmath>#include<numeric>#include<climits>using namespacestd;Const intMAXN = 1e5+5;intHD[MAXN], nx[maxn<<1], to[maxn<<1], EC;voidAdd_edge (intUintv) {Nx[ec]=Hd[u]; TO[EC]=v; Hd[u]= ec++;}voidInit_g (intN) {memset (hd+1,0xFFNsizeof(int)); EC =0; }#defineEachedge int i = hd[u]; ~i; i = Nx[i]#defineifvalid int v = to[i]; if (v = = Fa[u]) continue;intN;/*----------------------------------------*/intFA[MAXN];intTR_SZ[MAXN];intTOP[MAXN];intSON[MAXN];intDEP[MAXN];intID[MAXN];intid_cnt;//id[0] = tr_sz[0] = 0voidDfsintUintf =0,intD =0) {Dep[u]=D; Son[u]=0; Fa[u]=F; Tr_sz[u]=1;  for(Eachedge) {ifvalid dfs (v,u,d+1); Tr_sz[u]+=Tr_sz[v]; if(Tr_sz[v] >Tr_sz[son[u]]) {Son[u]=v; }    }}voidSUB_DV (intUintTP) {Top[u]=TP; Id[u]= ++id_cnt; if(Son[u]) SUB_DV (SON[U],TP);  for(Eachedge) {ifvalidif(V! =Son[u]) SUB_DV (V,V); }}/*----------------------------------------*/#definepara int o = 1,int L = 1,int r = N#defineLo (o<<1)#defineRO (o<<1|1)#defineTvar int md = (l+r) >>1;#defineLSN LO,L,MD#defineRSN Ro,md+1,r#defineINSD QL&LT;=L&AMP;&AMP;R&LT;=QR#defineQpara int Ql,int Qr,para#defineQlsn QL, QR,LSN#defineQRSN QL, QR,RSNConst intSt_size =1<< -;intFlp[st_size], sum[st_size];//EdgeintFlg[st_size];//Vertexvoidbuild (para) {sum[o]= Flp[o] = Flg[o] =0; if(L <R)        {Tvar build (LSN);    Build (RSN); }}inlinevoidSinkintOinttot) {Flp[o]^=1; Sum[o]= Tot-Sum[o];} InlinevoidDWN_FLP (intOintLintR) {    if(Flp[o]) {Tvar sink (LO,MD+1-l); Sink (Ro,r-MD); Flp[o]=0; }}voidUPDATE_FLP (Qpara) {if(INSD) {sink (O,r+1-M); }    Else{Tvar DWN_FLP (o,l,r); if(QL <=MD) UPDATE_FLP (QLSN); if(QR >MD) UPDATE_FLP (QRSN); Sum[o]= Sum[lo] +Sum[ro]; }}intQ_flp_sum (Qpara) {if(INSD)returnSum[o]; Else{Tvar DWN_FLP (o,l,r); intRe =0; if(QL <= md) Re + =q_flp_sum (QLSN); if(QR > MD) RE + =q_flp_sum (QRSN); returnre; }}/*----------------------------------------*/intQ_FLG (intQPOs, para) {    if(L = = r)returnFlg[o]; Else{Tvarreturnflg[o]^ (QPOs <= MD?)Q_FLG (QPOS,LSN): Q_flg (QPOS,RSN)); }}voidUPDATE_FLG (Qpara) {if(INSD) Flg[o] ^=1; Else{Tvarif(QL <=MD) UPDATE_FLG (QLSN); if(QR >MD) UPDATE_FLG (QRSN); }}/*----------------------------------------*/voidRvpath (intAintb) {    intp = top[a], q =Top[b];  while(P! =q) {        if(Dep[p] <Dep[q]) swap (A, b), swap (P,Q);        UPDATE_FLP (Id[p],id[a]); P= Top[a =Fa[p]]; }    if(A! =BB) {        if(Dep[a] >dep[b]) swap (A, b);    UPDATE_FLP (Id[son[a]],id[b]); }}voidRvadj (intAintb) {    intp = top[a], q =Top[b];  while(P! =q) {        if(Dep[p] <Dep[q]) swap (A, b), swap (P,Q);        UPDATE_FLG (Id[p],id[a]);        UPDATE_FLP (Id[p],id[p]); if(Son[a]) {UPDATE_FLP (id[son[a]],id[son[a]); } P= Top[a =Fa[p]]; }    if(Dep[a] >dep[b]) swap (A, b);    UPDATE_FLG (Id[a],id[b]);    UPDATE_FLP (Id[a],id[a]); if(Son[b]) {UPDATE_FLP (id[son[b]],id[son[b]); }}intQueryintAintb) {    intRe =0; intp = top[a], q =Top[b];  while(P! =q) {        if(Dep[p] <Dep[q]) swap (A, b), swap (P,Q); if(A! =p) {Re+=q_flp_sum (Id[son[p]],id[a]); } Re+ = Q_FLG (Id[fa[p]) ^q_flp_sum (id[p],id[p]); P= Top[a =Fa[p]]; }    if(A! =b) {        if(Dep[a] >dep[b]) swap (A, b); Re+=q_flp_sum (id[son[a]],id[b]); }    returnre;}/*----------------------------------------*/voidinit () {scanf ("%d",&N);    Init_g (n); intb;  for(inti =1; I < n; i++) {scanf ("%d%d",&a,&b); Add_edge (A, b);    Add_edge (B,a); }}voidsolve () {id_cnt=0; DFS (1); SUB_DV (1,1);    Build (); intQ; scanf ("%d",&Q); intt,a,b;  while(q--) {scanf ("%d%d%d",&t,&a,&b); if(T = =1) Rvpath (A, b); Else if(T = =2) Rvadj (A, b); Else if(T = =3) printf ("%d\n", query (A, b)); }}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif    //cout<<log2 (MAXN);    intT; scanf ("%d",&T);  while(t--) {init ();    Solve (); }    return 0;}

Little Devil I

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.