Hdu 5044 tree (chain split + tag array)

Source: Internet
Author: User

Links: http://acm.hdu.edu.cn/showproblem.php?pid=5044

This problem is really poisonous, before using tree chain split + line tree write, Tle 10,000 rounds, crazy optimization, finally gave up, looked at the other people's problem

The person stuck the hand segment tree, can not be written with a line tree .... Then change the tag array to write (it feels simpler to write than the line tree). ),

The input is then optimized. And the result is TLE!!!!!

Debugging a day more really can't stand, hand over the other to the code ... Discover all tle ...

Just think of yourself as a problem.

Implementation code:

#pragmaComment (linker, "/stack:1024000000,1024000000")#include<bits/stdc++.h>using namespacestd;#definell Long Long#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineMid int m = (L + r) >> 1Const intM = 2e5+Ten;structnode{intTo,next;} E[M];intSon[m],fa[m],head[m],siz[m],top[m],dep[m],tid[m],rk[m],a[m];ll sum1[m],sum[m];intCnt1,cnt,n;voidAddintUintv) {e[++cnt1].to = V;e[cnt1].next = Head[u];head[u] =Cnt1; e[++cnt1].to = U;e[cnt1].next = Head[v];head[v] =cnt1;}voidDFS1 (intUintFazintDeep ) {Dep[u]=Deep ; Fa[u]=FAZ; Siz[u]=1;  for(inti = head[u];i;i=E[i].next) {        intv =e[i].to; if(V! =Fa[u]) {DFS1 (V,u,deep+1); Siz[u]+=Siz[v]; if(Son[u] = =-1|| SIZ[V] >Siz[son[u]]) Son[u]=v; }     }}voidDFS2 (intUintt) {Top[u]=T; Tid[u]=CNT; RK[CNT]=u; //rk1[cnt] = Wt[u];cnt++; if(Son[u] = =-1)return;    DFS2 (son[u],t);  for(inti = Head[u];i;i =E[i].next) {        intv =e[i].to; if(V! = son[u]&&v! =Fa[u]) DFS2 (V,V); }}voidScan_d (int& X,Charc =0,intFlag =0 ) {     while((c = GetChar ())! ='-'&& (C <'0'|| C >'9' ) ) ; if(c = ='-') flag =1, x =0 ; Elsex = C-'0' ;  while((c = GetChar ()) >='0'&& C <='9') x = x *Ten+ C-'0' ; if(flag) x =-x;}voidChangeintXintYintc) {    intFX = Top[x],fy =Top[y];  while(FX! =FY) {        //cout<<1<<endl;        if(Dep[fx] <Dep[fy]) swap (FX,FY), swap (x, y); SUM[TID[FX]]+=c; sum[tid[x]+1] -=C; X= Fa[fx]; FX =Top[x]; }    if(Dep[x] >Dep[y]) swap (x, y); SUM[TID[X]]+ = c;sum[tid[y]+1]-=C; return ;}voidChange1 (intXintYintc) {    intFX = Top[x],fy =Top[y];  while(FX! =FY) {        if(Dep[fx] <Dep[fy]) swap (FX,FY), swap (x, y); SUM1[TID[FX]]+=c,sum1[tid[x]+1]-=C; X= Fa[fx]; FX =Top[x]; }    if(Dep[x] >Dep[y]) swap (x, y); SUM1[TID[FX]+1]+=c;sum1[tid[x]+1]-=C; return ;}voidinit () {memset (son,-1,sizeof(son)); memset (SUM,0,sizeof(sum)); memset (SUM1,0,sizeof(SUM1)); memset (Siz,0,sizeof(siz));  for(inti =0; I <= N;i + +) {e[i].to=0; e[i].next=0; head[i]=0; }}intMain () {intT,x,m,y,c,u[m],v[m];    Scan_d (t); intT1 =T;  while(t--) {init ();        Scan_d (n); Scan_d (m); Cnt1=1; cnt =1;  for(inti =0; i < n-1; i + +) {Scan_d (u[i]); Scan_d (V[i]);        Add (U[i],v[i]); } DFS1 (1,0,1);d FS2 (1,1); Chars[Ten];  while(m--) {scanf ("%s", s);            Scan_d (x); Scan_d (y); Scan_d (c); if(s[3]=='1') Change (X,Y,C); ElseChange1 (X,Y,C); } printf ("Case #%d:\n", t1-t);  for(inti =2; I <= N;i + +) {Sum[i]+ = sum[i-1]; Sum1[i]+ = sum1[i-1]; }             for(inti =1; I <= N;i + +){                if(i = = N) printf ("%lld\n", Sum[tid[i]]); Elseprintf"%lld", Sum[tid[i]]); }             for(inti =0; i < n-1; i + +){                if(Dep[u[i]) >Dep[v[i]) swap (u[i],v[i]); if(I! = N2) printf ("%lld", Sum1[tid[u[i]]); Elseprintf"%lld\n", Sum1[tid[u[i]]); }    }    return 0;}

Hdu 5044 tree (chain split + tag array)

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.