科学与社会研讨课部分代码保存——修改操作拓展

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define _for(i,a,b) for(register int (i)=(a);(i)<=(b);(i)++)
#define For(i,a,b) for(register int (i)=(a);(i)>=(b);(i)--)
#define INF 0x7fffffff
#define il inline
#define rg register
inline long long read(){
    long long num=0;int z=1;char c=getchar();
    if(c=='-') z=-1;
    while((c<'0'||c>'9')&&c!='-') c=getchar();
    if(c=='-') z=-1,c=getchar();
    while(c>='0'&&c<='9') num=(num<<1)+(num<<3)+(c^48),c=getchar();
    return z*num;
}
const int N=2e6+5;
int n,m;
int qwq,awa,len;
class Node{
    public:
        int l,r,t,id;
}p[N];
class node{
    public:
        int pos,val;
}q[N];
int cnt[N],ans[N],sum,a[N],pos[N];
class cmp{
    public:
        il bool operator () (const Node &a,const Node &b){
            if(pos[a.l]!=pos[b.l]) return a.l<b.l;
            if(pos[a.r]!=pos[b.r]) return a.r<b.r;
            return a.t<b.t;
        }
};
il void add(int o){ if(++cnt[o]==1) ++sum; }
il void reduce(int o){ if(--cnt[o]==0) --sum; }
il void init(int x,int y){
    if(q[x].pos>=p[y].l and q[x].pos<=p[y].r){
        if(--cnt[a[q[x].pos]]==0) --sum;
        if(++cnt[q[x].val]==1) ++sum;
    }
    swap(a[q[x].pos],q[x].val);
}
il void solve(){
    int l(1),r(0),owo(0);
    _for(i,1,qwq){
        while(l<p[i].l) reduce(a[l++]);
        while(l>p[i].l) add(a[--l]);
        while(r>p[i].r) reduce(a[r--]);
        while(r<p[i].r) add(a[++r]);
        while(owo<p[i].t) init(++owo,i);
        while(owo>p[i].t) init(owo--,i);
        ans[p[i].id]=sum;
    }
}
int main(){
    n=read(),m=read();
    len=pow(n,0.666);
    _for(i,1,n) a[i]=read(),pos[i]=(i-1)/len+1;
    _for(i,1,m){
        char op[5];
        scanf("%s",op+1);
        if(op[1]=='Q') p[++qwq].l=read(),p[qwq].r=read(),p[qwq].t=awa,p[qwq].id=qwq;
        if(op[1]=='R') q[++awa].pos=read(),q[awa].val=read();
    }
    sort(p+1,p+qwq+1,cmp());
    solve();
    _for(i,1,qwq) printf("%d\n",ans[i]);
    return 0;
}
posted @ 2024-05-30 17:52  空与灵之白  阅读(32)  评论(0)    收藏  举报
Title