0pts,过样例,求条,必关
查看原帖
0pts,过样例,求条,必关
1382038
linruicong_gegeji楼主2025/7/30 21:34

某个难倒蒟蒻的题目
某只蒟蒻的记录

#include<bits/stdc++.h>
using namespace std;
int n,m,a[533335],qtot,ctot,len,ll,rr,tmp,vis[533335],ans[533335];
struct str{
    int l,r,pre,id;
}mo[533335];
struct strt{
    int pp,cc;
}chan[533335];
bool cmp(str x,str y)
{
    if((x.l-1)/len!=(y.l-1)/len) return x.l/len<y.l/len;
    else if((x.r-1)/len!=(y.r-1)/len) return x.r/len<y.r/len;
    return x.pre<y.pre;
}
inline int faster_read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+(ch-48);
        ch=getchar();
    }
    return x*f;
}
inline void faster_write(int x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9) faster_write(x/10);
    putchar(x%10+48);
}
void add(int x)
{
    vis[x]++;
    if(vis[x]==1) tmp++;
}
void del(int x)
{
    vis[x]--;
    if(vis[x]==0) tmp--;
}
void work(int x,int y)
{
    if(chan[x].pp>=mo[y].l&&chan[x].pp<=mo[y].r)
    {
        if(--vis[a[chan[x].pp]]==0) tmp--;
        if(++vis[chan[x].cc]==1) tmp++;
    }
    swap(chan[x].cc,a[chan[x].pp]);
}
int main()
{
    n=faster_read();
    m=faster_read();
    len=sqrt(n);
    for(int i=1;i<=n;i++) a[i]=faster_read();
    for(int i=1;i<=m;i++)
    {
        char op[5];
        scanf("%s",op);
        if(op[0]=='Q')
        {   
            qtot++;
            mo[qtot].l=faster_read();
            mo[qtot].r=faster_read();
            mo[qtot].id=qtot;
            mo[qtot].pre=ctot;
        }
        else 
        {
            ctot++;
            chan[ctot].pp=faster_read();
            chan[ctot].cc=faster_read();
        }
    }
    sort(mo+1,mo+1+qtot,cmp);
    ll=1;
    rr=0;
    int now=0;
    for(int i=1;i<=m;i++)
    {
        while(ll<mo[i].l) del(a[ll++]);
        while(ll>mo[i].l) add(a[--ll]);
        while(rr<mo[i].r) add(a[++rr]);
        while(rr>mo[i].r) del(a[rr--]);
        while(now<mo[i].pre) work(++now,i);
        while(now>mo[i].pre) work(now--,i);
        ans[mo[i].id]=tmp;
    }
    for(int i=1;i<=qtot;i++)
    {
        faster_write(ans[i]);
        if(i!=qtot) printf("\n");
    }
    return 0;
}
2025/7/30 21:34
加载中...