Title: Give a string of colors, there are two operations, 1. Ask how many blocks of color. 2. Change one color to another.
Thought: It seems to be compared with what is dyed, but after looking at the solution, I find it is not so at all.
For each color maintained a linked list, and then when modifying the color, the violence changes one color to become another color, with the heuristic combination can ensure that the complexity is not more than O (Nlogn). But because it is a heuristic merge, it may cause you to change the color, this time to record a map, and then put the wrong record. A variety of information to discuss carefully ...
CODE:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 1000010using namespace Std; struct list{List *next; int POS; List (int _ = 0) {next = NULL; pos = _; }}*head[max]; int cnt,asks;int src[max];int Now,num[max];int C[max]; void Pretreatment () {for (int i = 1; i < MAX; ++i) c[i] = i;} inline void Add (int col,int pos) {List *temp = new List (POS); Temp->next = Head[col]; Head[col] = temp;} inline void Change (int. from,int aim) {if (from = = aim) return; if (Num[c[from]] > Num[c[aim]]) swap (C[from],c[aim]); from = C[from],aim = C[aim]; if (head[from] = = NULL) return; for (List *x = Head[from]; x = NULL; x = x->next) {if (src[x->pos-1] = = aim)--now; if (Src[x->pos + 1] = = aim)--now; ADD (Aim,x->pos); } for (List *x = Head[from]; x = NULL; x = x->next) Src[x->pos] = aim; Static List *stack[max]; int top = 0; for (List *x = Head[from]; x = NULL; x = x->next) Stack[++top] = x; while (top) delete stack[top--]; Head[from] = NULL; Num[aim] + = Num[from]; Num[from] = 0;} int main () {pretreatment (); CIN >> CNT >> asks; int last = 0; for (int i = 1; I <= cnt; ++i) {scanf ("%d", &src[i]); ADD (Src[i],i); ++num[src[i]]; if (src[i]! = last) {last = Src[i]; ++now; }} for (int flag,x,y,i = 1; I <= asks; ++i) {scanf ("%d", &flag); if (flag = = 1) {scanf ("%d%d", &x,&y); Change (x, y); } else printf ("%d\n", now); } return 0;}
Bzoj 1483 Hnoi 2009 Fantasy Pudding list + heuristic merge