带修莫队模板求调 RE,40pts
查看原帖
带修莫队模板求调 RE,40pts
965685
yyrwlj楼主2024/3/29 16:41

Code

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 140005;
struct Que{
    int id, l, r, t;
}q[N];
struct Upd{
    int pos, c;
}c[N];
int a[N], cnt[1000005], ans[N], sq, res;
inline int get(int i)
{
    return i / sq;
}
bool cmp(Que A,Que B)
{
    int la = get(A.l), lb = get(B.l);
    int ra = get(A.r), rb = get(B.r);
    if (la != lb)
        return la < lb;
    if (ra != rb)
        return ra < rb;
    return A.t < B.t;
}
inline void add(int i)
{
    if (cnt[a[i]] ++ == 0)
        res ++;
}
inline void del(int i)
{
    if (-- cnt[a[i]] == 0)
        res --;
}
int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i=1;i<=n;i++)
        scanf("%d", &a[i]);
    sq = ceil(pow(n, 2.0 / 3.0));
    int cq = 0, cc = 0;
    for (int i=1;i<=m;i++)
    {
        char op[2];
        int a, b;
        scanf("%s%d%d", op, &a, &b);
        if (op[0] == 'Q')
        {
            cq ++;
            q[cq] = {cq, a, b, cc};
        }
        else
            c[++cc] = {a, b};
    }
    sort(q + 1, q + cq + 1, cmp);
    for (int i=1,l=1,r=0,last=0;i<=cq;i++)
    {
        int L = q[i].l, R = q[i].r;
        while (r < R)
            add(++ r);
        while (R < r)
            del(r --);
        while (l < L)
            del(l ++);
        while (L < l)
            add(-- l);
        while (last < q[i].t)
        {
            last ++;
            if (L <= c[last].pos && c[last].pos <= R)
            {
                del(a[c[last].pos]);
                add(c[last].c);
            }
            swap(c[last].c, a[c[last].pos]);
        }
        while (last > q[i].t)
        {
            if (L <= c[last].pos && c[last].pos <= R)
            {
                del(a[c[last].pos]);
                add(c[last].c);
            }
            swap(c[last].c, a[c[last].pos]);
            last --;
        }
        ans[q[i].id] = res;
    }
    for (int i=1;i<=cq;i++)
        printf("%d\n", ans[i]);
    return 0;
}
2024/3/29 16:41
加载中...