HDU 5997 Rausen loves cakes (number of segments merged + heuristic modified)

Source: Internet
Author: User

Title Link: hdu 5997 Rausen loves cakes

Test instructions

Give you n points, each point has a color, now has two operations, the first action, the color x is changed to the color Y, the second action, ask [X, Y] interval how many color segments (color segments defined as the same color from left to right for a period, encountered a different for the next paragraph, ie:144112 for the 4-segment color)

Exercises

For the second operation we can write a segment tree merge to fix, for the first operation, we need to use heuristic modification to do, how heuristic?

We open an array to record each color corresponding to the color, the first is to correspond to their own, and then open a vector to record the position of each color, and then encounter the change a color to B color, the number of violent changes to the smallest, and then record the corresponding color on the line, This reduces the total complexity of the modification operation to the log level.

1#include <bits/stdc++.h>2 #definePB Push_back3 #defineLS l,m,rt<<14 #defineRS M+1,r,rt<<1|15 #defineF (I,A,B) for (int i=a;i<=b;++i)6 using namespacestd;7 8 Const intn=1e5+7;9 intcol[n*Ten];Tenvector<int>cnt[n*Ten]; One intt,n,q; A structnode{intL,r,val;} tr[n*4]; -  - voidUpintRT) the { -tr[rt].l=tr[rt<<1].L; -tr[rt].r=tr[rt<<1|1].R; -tr[rt].val=tr[rt<<1].val+tr[rt<<1|1].val; +     if(tr[rt<<1].r==tr[rt<<1|1].L) tr[rt].val--; - } +  A voidBuildintL=1,intR=n,intrt=1) at { -     if(l==R) -     { -scanf"%d",&tr[rt].l); -Tr[rt].r=tr[rt].l,tr[rt].val=1; - CNT[TR[RT].L].PB (l); in         return; -     } to     intM=l+r>>1; + build (LS), build (RS); - Up (RT); the } *  $ voidUpdateintPosintBintL=1,intR=n,intrt=1)Panax Notoginseng { -     if(l==R) the     { +Tr[rt].r=tr[rt].l=b,tr[rt].val=1; A         return; the     } +     intM=l+r>>1; -     if(pos<=m) update (POS,B,LS); $     ElseUpdate (POS,B,RS); $ Up (RT); - } -  theNode Query (intXintYintL=1,intR=n,intrt=1) - {Wuyi     if(L==R)returnTr[rt]; the     if(x<=l&&r<=y)returnTr[rt]; -     intM=l+r>>1; Wu node Ll,rr,ans; -     if(y<=m) ans=query (X,Y,LS); About     Else if(x>m) ans=query (X,Y,RS); $     Else -     { -Ll=query (X,Y,LS); -Rr=query (X,Y,RS); AAns.l=ll.l,ans.r=RR.R; +         if(LL.R!=RR.L) ans.val=ll.val+Rr.val; the         Elseans.val=ll.val+rr.val-1; -     } $     returnans; the } the  the intMain () { thescanf"%d",&t); -      while(t--) in     { theF (I,1,1000000) Cnt[i].clear (), col[i]=i; thescanf"%d%d",&n,&q); About build (); theF (I,1, Q) the         { the             inta,b,c; +scanf"%d%d%d",&a,&b,&c); -             if(a==1) the             {Bayi                 intx=col[b],y=Col[c]; the                 if(x==y)Continue; the                 if(Cnt[x].size () <cnt[y].size ()) -                 { -                     intEn=cnt[x].size ()-1; theF (J,0, en) the                     { the Update (cnt[x][j],y); the CNT[Y].PB (Cnt[x][j]); -                     } the cnt[x].clear (); the}Else the                 {94col[b]=y,col[c]=x; the                     intEn=cnt[y].size ()-1; theF (J,0, en) the                     {98 Update (CNT[Y][J],X); About CNT[X].PB (Cnt[y][j]); -                     }101 cnt[y].clear ();102                 }103}Elseprintf"%d\n", Query (B,C). val);104         } the     }106     return 0;107}
View Code

HDU 5997 Rausen loves cakes (number of segments merged + heuristic modified)

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.