Bzoj 1483 Hnoi 2009 Fantasy Pudding list + heuristic merge

Source: Internet
Author: User

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

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.