bzoj3578: GTY的人类基因组计划2

STL即玄学。。

一看过去就觉得要hash一下吧,顺便瞄了一眼hint那我就想mp记录hash值咯

然而修改不好搞啊。。。暴力重算肯定挂啊。。

%了一下发现原来可以用异或来离散化(感觉这个很看脸的样子)

把没有重复插入set,这样就不用穷举区间啦~

边查找边删除不好弄啊。。这个RE了很久,s.end()!=it这个判法还是很迷。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
set<int>s;
set<int>::iterator it;
map<LL,bool>mp;

int n,m;
LL h[110000],num[110000];
int tot[110000],bel[110000];
void init()
{
    srand(2647);
    for(int i=1;i<=n;i++)num[i]=(LL(rand()%1000000000))*(LL(rand()%1000000000));
    
    tot[1]=n;
    memset(h,0,sizeof(h));
    for(int i=1;i<=n;i++) h[1]^=num[i], bel[i]=1;
    s.insert(1);
}

char ss[10];
int main()
{
    int Q;
    scanf("%d%d%d",&n,&m,&Q);init();
    while(Q--)
    {
        int x,y,k;
        scanf("%s%d%d",ss+1,&k,&y);
        if(ss[1]=='C')
        {
            x=bel[k];
            if(x==y)continue;
            
            s.erase(x);
            s.erase(y);
            
            h[x]^=num[k];tot[x]--;
            h[y]^=num[k];tot[y]++;
            if(mp[h[x]]==false)s.insert(x);
            if(mp[h[y]]==false)s.insert(y);
            
            bel[k]=y;
        }
        else
        {
            x=k;
            int ans=0;
            for(it=s.lower_bound(x);*it<=y&&s.end()!=it;it=s.lower_bound(x))
            {
                mp[h[*it]]=true;
                ans+=tot[*it];
                s.erase(it);
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/AKCqhzdy/p/8888692.html
今日推荐