BZOJ 1483 [HNOI2009]梦幻布丁(启发式合并)

题意中文。

做法:由于是同种颜色的改变颜色,所以只会有合并,不会有拆分,我们可以利用启发式合并,要合并2堆,就把小的那堆暴力合并给大的那堆,可以发觉,一个布丁最多只会被合并logn次,因为每次暴力合并后大小都会至少乘2。

我写法比较暴力,用了vector保存不同颜色的布丁,用map保存某种颜色对应哪个vector。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define ll __int64
#define ull unsigned __int64
#define eps 1e-8
#define NMAX 1000000010
#define MOD 1000003
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
const int maxn = 200000+10;
vectorv[maxn];
mapmp;
int bel[maxn],nct,data[maxn];

int vec(int x)
{
    if(mp[x] == 0)
    {
        v[nct].clear();
        mp[x] = nct++;
    }
    return mp[x];
}

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        mp.clear();
        nct = 1;
        int ans = 0;
        data[0] = -NMAX;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&data[i]);
            if(data[i] != data[i-1]) ans++;
            int pos = vec(data[i]);
            v[pos].push_back(i);
            bel[i] = pos;
        }
        while(m--)
        {
            int op;
            scanf("%d",&op);
            if(op == 1)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                if(x == y) continue;
                int p1 = vec(x), p2 = vec(y);
                int sz1 = v[p1].size(), sz2 = v[p2].size();
                if(sz1 < sz2)
                {
                    for(int i = 0; i < sz1; i++)
                    {
                        int tmp = v[p1][i];
                        if(tmp != 1 && bel[tmp-1] == p2) ans--;
                        if(tmp != n && bel[tmp+1] == p2) ans--;
                    }
                    for(int i = 0; i < sz1; i++)
                    {
                        int tmp = v[p1][i];
                        v[p2].push_back(tmp);
                        bel[tmp] = p2;
                    }
                    v[p1].clear();
                }
                else
                {
                    for(int i = 0; i < sz2; i++)
                    {
                        int tmp = v[p2][i];
                        if(tmp != 1 && bel[tmp-1] == p1) ans--;
                        if(tmp != n && bel[tmp+1] == p1) ans--;
                    }
                    for(int i = 0; i < sz2; i++)
                    {
                        int tmp = v[p2][i];
                        v[p1].push_back(tmp);
                        bel[tmp] = p1;
                    }
                    v[p2].clear();
                    mp[y] = p1;
                    mp[x] = 0;
                }
            }
            else printf("%d\n",ans);
        }
    }
    return 0;
}


你可能感兴趣的:(数据结构)