Time Limit:10 Sec Memory limit:64 MB
submit:2655 solved:1063 Description
N pudding is placed in a row for M operations. Each time a color's pudding is all turned into another color, then ask the current total number of colors. For example, four pudding with a color of 1,2,2,1, a total of 3 colors.
Input
The first line gives N,m the number of pudding and the number of times a friend is operating. Second row n number a1,a2 ... An indicates that the color of the first I pudding has M line from the third line, and for each operation, if the first number is 1 to change the color, then the two integer, x, y, or X, indicates that turning all colors into y,x may be equal to Y. If the first number is 2 to ask how many colors are currently in question, you should output an integer. 0
Output
Ask for the second type of action, and then output the current number of colors.
Sample Input4 3
1 2 2 1
2
1 2 1
2
Sample Output3
1
HINT
1<=n,m<=100,000; 0<ai,x,y<1,000,000
Source
See the data range comparison counseling, but see the solution is a very violent method just past. Think of it as if there is some truth.
Linked list + heuristic merging
Use the list to store the position of all the puddings for each color, merge the linked list and color markers each time it is modified, record the link list length, and the list of violent modified lengths.
However the writing hung up. After bowing to the puzzle, it was found that the original color of the pudding was not recorded, causing the stain to deviate.
↓ Note the statement differences between lines 37 and 38. To determine whether to dye according to the primary color.
1#include <algorithm>2#include <cstring>3#include <cmath>4#include <cstdio>5 using namespacestd;6 Const intmxn=2000100;7 intRead () {8 intx=0, f=1;CharCh=GetChar ();9 while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}Ten while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} One returnx*F; A } - intn,m; - intHD[MXN],NXT[MXN],CNT[MXN]; the intNOW[MXN]; - intCO[MXN]; - intMain () - { + inti,j; -N=read (); m=read (); + intans=0; A for(i=1; i<=n;i++){ atco[i]=read (); -cnt[co[i]]++; -nxt[i]=Hd[co[i]]; -now[co[i]]=Co[i]; -hd[co[i]]=i; - if(co[i]!=co[i-1]) ans++; in } - intx, y; to for(i=1; i<=m;i++){ +x=read (); - if(x==2) {printf ("%d\n", ans);} the Else{ *X=read (); y=read (); $ if(x==y)Continue;Panax Notoginseng //if (Cnt[x]>cnt[y]) swap (x, y); - if(cnt[now[x]]>Cnt[now[y]) swap (now[x],now[y]); theX=now[x];y=now[y];//Primary Colors + if(!cnt[x])Continue; A intlast=0; the for(j=hd[x];j;j=Nxt[j]) { + if(co[j-1]==y) ans--; - if(co[j+1]==y) ans--; $last=J; $ } - for(j=hd[x];j;j=Nxt[j]) { -co[j]=y; the } -nxt[last]=Hd[y];Wuyihd[y]=Hd[x]; thehd[x]=0; -cnt[y]+=Cnt[x]; Wucnt[x]=0; - } About } $ return 0; -}
Bzoj1483 [HNOI2009] Dream pudding