跳到正文

剪贴板

剪贴板 nf22nitx

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
using namespace std;

const int maxn = 150009;

int qsize;

struct query
{
    int id,t,l,r;
    bool operator<(const query &b)const
    {
        if(l / qsize != b.l / qsize)
        {
            return l / qsize > b.l / qsize;
        }
        else if(r / qsize != b.r / qsize)
        {
            return r / qsize > b.r / qsize;
        }
        else
        {
            return t > b.t;
        }
    }
}q[maxn];

struct op
{
    int p,x;
}ope[maxn];

char opera;
int n,m,x,y,cur;
int qcnt,rcnt;
int mp[1500009],a[maxn],ans[maxn];

void add(int x)
{
    if(!mp[x])
    {
        cur += 1;
    }
    mp[x] += 1;
}

void del(int x)
{
    mp[x] -= 1;
    if(!mp[x])
    {
        cur -= 1;
    }
}

void solve()
{
    sort(q + 1,q + qcnt + 1);int l = 1,r = 0,last = 0;
    for(int i = 1;i <= qcnt;i++)
    {
        while(r < q[i].r)
        {
            add(a[++r]);
        }
        while(l > q[i].l)
        {
            add(a[--l]);
        }
        while(r > q[i].r)
        {
            del(a[r--]);
        }
        while(l < q[i].l)
        {
            del(a[l++]);
        }
        while(last < q[i].t)
        {
            last += 1;
            if(ope[last].p >= l && ope[last].p <= r)
            {
                add(ope[last].x);
                del(a[ope[last].p]);
            }
            swap(a[ope[last].p],ope[last].x);
        }
        while(last > q[i].t)
        {
            if(ope[last].p >= l && ope[last].p <= r)
            {
                add(ope[last].x);
                del(a[ope[last].p]);
            }
            swap(a[ope[last].p],ope[last].x);
            last--;
        }
        ans[q[i].id] = cur;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m;
    qsize = pow(n,2.0 / 3.0);
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
    }
    for(int i = 1;i <= m;i++)
    {
        char op;
        int x,y;
        cin >> op >> x >> y;
        if(op == 'Q')
        {
            q[++qcnt] = {qcnt,rcnt,x,y};
        }
        else
        {
            ope[++rcnt] = {x,y};
        }
    }
    solve();
    for(int i = 1;i <= qcnt;i++)
    {
        cout << ans[i] << '\n';
    }
    return 0;
}