带修莫队求条,有数据
查看原帖
带修莫队求条,有数据
1041060
_chicken_楼主2024/10/2 21:59
#include<bits/stdc++.h>
#define int long long
#define pc putchar
using namespace std;
void read(int &p){
    int res=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
    p=res*fh;
}
void prt(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>=10) prt(x/10);
    putchar(x%10+'0');
}
const int N=1.5e6+10;
const int inf=0x7fffffffffffffff;
const int MOD=1e9+7;
int n,m,block,a[N],b[N];
struct node{
    int l,r,t,id,ans;
    bool operator<(const node &xx)const{
        int a1=(l-1)/block+1;
        int a2=(xx.l-1)/block+1;
        int b1=(r-1)/block+1;
        int b2=(xx.r-1)/block+1;
        if(a1!=a2) return a1<a2;
        if(b1!=b2) return b1<b2;
        return t<xx.t;
    }
}x[N];
struct cg{
    int x,w;
}y[N];
int l=1,r=0;
bool cmp(node xx,node yy){
    return xx.id<yy.id;
}
int cnt[N],res;
void add(int xx){
    // cout<<"add "<<xx<<"\n";
    if(cnt[a[xx]]==0) res++;
    cnt[a[xx]]++;
}
void del(int xx){
    // cout<<"del "<<xx<<"\n";
    cnt[a[xx]]--;
    if(cnt[a[xx]]==0) res--;
    // if(cnt[a[xx]]<0) printf("wrong %lld\n",xx);
}
void push(int xx){
    if(y[xx].x>=l&&y[xx].x<=r){
        del(y[xx].x);
        a[y[xx].x]=y[xx].w;
        add(y[xx].x);
    }
    else{
        a[y[xx].x]=y[xx].w;
    }
}
void pp(int xx){
    if(y[xx].x>=l&&y[xx].x<=r){
        del(y[xx].x);
        a[y[xx].x]=b[y[xx].x];
        add(y[xx].x);
    }
    else a[y[xx].x]=b[y[xx].x];
}
signed main(){
    read(n);read(m);
    block=((int)pow((double)n,2.0/3));
    for(int i=1;i<=n;i++) read(a[i]),b[i]=a[i];
    int nw=0,cntx=0,cnty=0;
    for(int i=1;i<=m;i++){
        char opt;
        scanf("%c",&opt);
        while(opt!='Q'&&opt!='R') scanf("%c",&opt);
        int aa,bb;
        read(aa);read(bb);
//        cout<<opt<<" "<<aa<<" "<<bb<<"\n";
        if(opt=='R') y[++cnty].x=aa,y[cnty].w=bb,nw++;
        else x[++cntx].l=aa,x[cntx].r=bb,x[cntx].id=cntx,x[cntx].t=nw;
    }
    nw=0;
    sort(x+1,x+cntx+1);
    for(int i=1;i<=cntx;i++){
        // printf("%d %d %d %d %d %d\n",l,r,x[i].l,x[i].r,x[i].t,x[i].id);
        while(nw<x[i].t) push(++nw);
        while(nw>x[i].t) pp(nw),nw--;
        // for(int j=1;j<=n;j++){
        //     prt(a[j]);
        //     pc(' ');
        // }pc('\n');
        // for(int j=1;j<=20;j++) prt(cnt[j]),pc(' ');pc('\n');
        while(r<x[i].r) add(r+1),r++;
        while(r>x[i].r) del(r),r--;
        while(l<x[i].l) del(l),l++;
        while(l>x[i].l) add(l-1),l--;
        // for(int j=1;j<=20;j++) prt(cnt[j]),pc(' ');pc('\n');
        x[i].ans=res;
    }
    sort(x+1,x+cntx+1,cmp);
    for(int i=1;i<=cntx;i++) prt(x[i].ans),pc('\n');
    return 0;
}

data:

input:
45 23
16 11 9 9 1 11 11 6 12 20 10 11 17 1 7 18 5 13 13 13 5 20 16 10 2 11 14 7 14 14 12 17 3 18 10 16 16 4 14 2 13 8 14 20 19 
R 4 13
Q 23 45
R 9 4
Q 18 29
R 15 16
R 39 9
Q 11 40
R 13 9
Q 11 12
R 22 6
Q 1 43
Q 15 16
Q 12 21
R 12 17
R 42 18
R 42 7
Q 20 31
Q 11 25
Q 18 23
R 8 12
Q 2 15
R 38 4
Q 6 17
except:
15
9
16
2
18
2
7
10
10
4
10
10
print:
15
9
16
2
18
2
7
10
10
4
10
11
2024/10/2 21:59
加载中...