• 数据结构(启发式合并):HNOI 2009 梦幻布丁


    Description

    N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色.

    Input

    第 一行给出N,M表示布丁的个数和好友的操作次数. 第二行N个数A1,A2...An表示第i个布丁的颜色从第三行起有M行,对于每个操作,若第一个数字是1表示要对颜色进行改变,其后的两个整数X,Y表 示将所有颜色为X的变为Y,X可能等于Y. 若第一个数字为2表示要进行询问当前有多少段颜色,这时你应该输出一个整数. 0

    Output

    针对第二类操作即询问,依次输出当前有多少段颜色.

    Sample Input

    4 3
    1 2 2 1
    2
    1 2 1
    2

    Sample Output

    3

    1

      本地AC,BZOJ上RE,唉。

      水题,注意不要把set定义在结构体里,大数据会错,有个陷阱就是x==y。

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <set>
     6 using namespace std;
     7 const int N=200010;
     8 set<int>::iterator it1,it2;
     9 int c[N],id[N*10],s[N];
    10 int n,Q,ans,tot;set<int>v[N];
    11 void Insert(int p,int i){
    12     it2=v[p].lower_bound(i);
    13     if(it2!=v[p].end()&&i+1==*it2)s[p]-=1;
    14     if(it2!=v[p].begin()&&i-1==*(--it2))s[p]-=1;
    15     v[p].insert(i);s[p]+=1;
    16 }
    17 
    18 int main(){
    19     freopen("pudding.in","r",stdin);
    20     freopen("pudding.out","w",stdout);
    21     ios::sync_with_stdio(false);
    22     cin.tie(NULL),cout.tie(NULL);
    23     cin>>n>>Q;
    24     for(int i=1;i<=n;i++)cin>>c[i];
    25     for(int i=1;i<=n;i++){
    26         int &p=id[c[i]];
    27         if(!p)p=++tot;
    28         Insert(p,i);
    29     }
    30         
    31     for(int i=1;i<=tot;i++)ans+=s[i];
    32     int tp,x,y,px,py;
    33     while(Q--){
    34         cin>>tp;
    35         if(tp==1){
    36             cin>>x>>y;if(x==y)continue;
    37             if(v[id[x]].size()>v[id[y]].size())swap(id[x],id[y]);
    38             int px=id[x],py=id[y];ans-=s[px]+s[py];
    39             for(it1=v[px].begin();it1!=v[px].end();it1++)
    40                 Insert(py,*it1);
    41             ans+=s[py];s[px]=0;v[px].clear(); 
    42         }
    43         if(tp==2)cout<<ans<<"
    ";
    44     }    
    45     return 0;
    46 }
  • 相关阅读:
    SSM框架整合步骤
    Spring-data-jpa
    allure定制报告
    pytest常用选项
    staticmethod&classmethod&property
    __slot__
    python的参数传递
    闭包和装饰器
    内置高阶函数
    str
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5869526.html
Copyright © 2020-2023  润新知