codeforces940F. Machine Learning(带修莫队)

带修莫队有点裸 因为感觉再暴力求mex的时候感觉不会太大 最多就是个常数级
带修莫队还写的不够熟 TLE了好久 注意下分块的大小 以及带关键字的排序
很奇怪这个题明明都是1e5的数据范围 开1e5的数组大小会re 所以都开的2e5 (⊙﹏⊙)

#include 
#include 
#include 
#include 
#include 
using namespace std;

int n, m, blo, ans, cn1, cn2, tt, cnt, l, r;
int q[200005];
int w[200005];
int sum[200005];
int mx[200005];
int aa[200005];
map mp;

struct node1
{
    int l, r, t, ans, id;
    bool operator < (const struct node1 A) const
    {
        if((l - 1) / blo == (A.l - 1) / blo)
        {
            if((r - 1) / blo == (A.r - 1) / blo)
                return t < A.t;
            else
                return r < A.r;
        }
        else return l < A.l;
    }
}E[200005];

struct node2
{
    int pos, pre, aft;
}C[200005];

void update(int x, int y)
{
    if(x >= l && x <= r)
    {
        mx[sum[q[x]]]--;
        mx[--sum[q[x]]]++;
        mx[sum[y]]--;
        mx[++sum[y]]++;
    }
    q[x] = y;
}

void rever(int x, int ad)
{
    mx[sum[q[x]]]--;
    mx[sum[q[x]] += ad]++;
}

int main()
{
    scanf("%d%d", &n, &m); blo = pow(n, 0.666666); cn1 = cn2 = cnt = 0; tt = 0;
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &q[i]);
        if(!mp[q[i]]) mp[q[i]] = ++cnt;
        w[i] = q[i] = mp[q[i]];
    }

    for(int i = 1; i <= m; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        if(a == 1)
        {
            cn1++;
            E[cn1].id = i;
            E[cn1].l = b; E[cn1].r = c;
            E[cn1].t = cn2;
        }
        else if(a == 2)
        {
            if(!mp[c]) mp[c] = ++cnt;
            cn2++;
            C[cn2].pos = b;
            C[cn2].pre = w[b]; C[cn2].aft = w[b] = mp[c];
        }
    }
    sort(E + 1, E + 1 + cn1);

    l = 1, r = 0;
    for(int i = 1; i <= cn1; i++)
    {
        while(tt < E[i].t) update(C[tt + 1].pos, C[tt + 1].aft), tt++;
        while(tt > E[i].t) update(C[tt].pos, C[tt].pre), tt--;

        while(r < E[i].r) rever(r + 1, 1), r++;
        while(l < E[i].l) rever(l, -1), l++;
        while(l > E[i].l) rever(l - 1, 1), l--;
        while(r > E[i].r) rever(r, -1), r--;

        for(int j = 1; j <= 100000; j++)
            if(!mx[j])
            {
                aa[E[i].id] = j;
                break;
            }
    }

    for(int i = 1; i <= m; i++) if(aa[i]) printf("%d\n", aa[i]);
    return 0;
}

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