求助RE

P4168 [Violet] 蒲公英

Prean @ 2020-10-13 20:58:17

莫名其妙RE了。。。

求dalao康康/kel

#include<algorithm>
#include<cstdio>
const int N=205,M=40005;
int n,m,p,ans,num,len,a[M],g[M],cnt[M],lsh[M],vis[M],ANS[N][N],sum[N][M];
int l[N],r[N],L[M],R[M],pos[M];
inline double abs(const double&a){
    return a>0?a:-a;
}
inline int min(const int&a,const int&b){
    return a>b?b:a;
}
inline double sqrt(const int&x){
    double x0=x*0.5;
    while(abs(x0*x0-x)>1e-3)x0-=(x0*x0-x)/(2*x0);
    return x0;
}
inline void Update(const int&id,const int&val){
    cnt[id]+=val;
    if(cnt[id]>cnt[ans]||(cnt[id]==cnt[ans]&&id<ans))ans=id;
}
inline int Query(int l,int r){
    int i,q=pos[l],p=pos[r];
    if(q+1>=p){
        for(i=l;i<=r;++i){
            Update(a[i],1);
        }
        for(i=l;i<=r;++i)--cnt[a[i]];
        return ans;
    }
    for(i=l;i<=R[l];++i){
        if(!vis[a[i]])g[++len]=a[i];
        vis[a[i]]=1;
        Update(a[i],1);
    }
    for(i=L[r];i<=r;++i){
        if(!vis[a[i]])g[++len]=a[i];
        vis[a[i]]=1;
        Update(a[i],1);
    }
    for(i=1;i<=len;++i){
        Update(g[i],sum[p-1][g[i]]-sum[q][g[i]]);
    }
    if(!vis[ANS[q+1][p-1]]){
        Update(ANS[q+1][p-1],sum[p-1][ANS[q+1][p-1]]-sum[q][ANS[q+1][p-1]]);
        cnt[ANS[q+1][p-1]]=0;
    }
    for(i=l;i<=R[l];++i){
        cnt[a[i]]=vis[a[i]]=0;
    }
    for(i=L[r];i<=r;++i){
        cnt[a[i]]=vis[a[i]]=0;
    }
}
signed main(){
    register int i,j,k,x=0,ql,qr;
    scanf("%d%d",&n,&m);p=n/sqrt(m);
    for(i=1;i<=n;++i){
        scanf("%d",a+i);lsh[i]=a[i];
        pos[i]=(i-1)/p+1;
        L[i]=(pos[i]-1)*p+1;
        R[i]=min(pos[i]*p,n);
    }
    num=pos[n];
    for(i=1;i<=num;++i)l[i]=r[i-1]+1,r[i]=i*p;
    r[num]=n;
    std::sort(lsh+1,lsh+n+1);
    len=std::unique(lsh+1,lsh+n+1)-lsh-1;
    for(i=1;i<=n;++i)++sum[pos[i]][a[i]=std::lower_bound(lsh+1,lsh+len+1,a[i])-lsh];
    len=0;
    for(i=1;i<=num;++i){
        for(j=1;j<=n;++j){
            sum[i][j]+=sum[i-1][j];
        }
    }
    for(i=1;i<=num;++i){
        for(j=i;j<=num;++j){
            for(k=l[j];k<=r[j];++k){
                Update(a[k],1);
            }
            ANS[i][j]=ans;
        }
        for(j=l[i];j<=n;++j)--cnt[a[j]];
    }
    for(i=1;i<=m;++i){
        scanf("%d%d",&ql,&qr);
        ql=(ql+x-1)%n+1;qr=(qr+x-1)%n+1;
        if(ql>qr)ql^=qr^=ql^=qr;
        printf("%d\n",lsh[x=Query(ql,qr)]);
        ans=len=0;
    }
}

by 素质玩家孙1超 @ 2020-10-13 21:02:55

ANS[N][N],sum[N][M];

这个是对的吗?


by Remake_ @ 2020-10-13 21:17:08

手写牛迭开根好评


by Prean @ 2020-10-14 21:37:12

@素质玩家孙1超 是对的,ANS是从q到p的众数,sum是前n个块中m的出现次数


|