求调RE

P4168 [Violet] 蒲公英

银河AI @ 2021-05-02 21:27:14

源代码如下,仅有25分,其余都RE

#include<bits/stdc++.h>
#define int long long
#include<vector>
using namespace std;
const int N=4e4+1,blocks=4e3+1;
int n,m,lastans,len; 
int a[N],b[N],st[blocks],ed[blocks],bel[N],ori[N];
int p[N],f[blocks][blocks],sum[N],g[blocks][blocks];
vector<int> pos[N];
inline int read(){
    int s=0;char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+ch-'0',ch=getchar();
    return s;
}
inline void init(){
    len=ceil((double)n/blocks);
    for(register int i=1;i<=len;i++) st[i]=ed[i-1]+1,ed[i]=i*blocks;
    ed[len]=n;
    for(int i=1;i<=len;i++) for(int j=st[i];j<=ed[i];j++) bel[j]=i;
    for(int i=1;i<=len;i++){
        for(int j=i;j<=len;j++){
            f[i][j]=f[i][j-1];
            g[i][j]=g[i][j-1];
            for(int k=st[j];k<=ed[j];k++){
                sum[a[k]]++;
                if(f[i][j]<sum[a[k]]||(f[i][j]==sum[a[k]]&&g[i][j]>a[k])) f[i][j]=sum[a[k]],g[i][j]=a[k];
            }
        }
        for(int j=st[i];j<=n;j++) if(sum[a[j]]) sum[a[j]]=0;
    }
}
inline int ask(int l,int r){
    int qa=bel[l],qb=bel[r],maxn=0,ans=99999999;
    if(qa==qb){
        for(int i=l;i<=r;i++){
            sum[a[i]]++;
            if(sum[a[i]]>maxn||(sum[a[i]]==maxn&&ans>a[i])) maxn=sum[a[i]],ans=a[i];
        }
        for(int i=l;i<=r;i++) if(sum[a[i]]) sum[a[i]]=0;
        return ans;
    }
    maxn=f[qa+1][qb-1],ans;
    for(int i=l;i<=ed[qa];i++){
        if(maxn+p[i]<pos[a[i]].size()&&pos[a[i]][maxn+p[i]]<=r) maxn++,ans=min(ans,a[i]);
        while(maxn+p[i]<pos[a[i]].size()&&pos[a[i]][maxn+p[i]]<=r) maxn++,ans=a[i];
    }
    for(int i=st[qb];i<=r;i++){
        if(p[i]-maxn>=0&&pos[a[i]][p[i]-maxn]>=l) maxn++,ans=min(ans,a[i]);
        while(p[i]-maxn>=0&&pos[a[i]][p[i]-maxn]>=l) maxn++,ans=a[i];
    }
    return ans; 
}
int ttt;
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++) a[i]=read(),b[i]=a[i];
    sort(b+1,b+n+1);
    int num=unique(b+1,b+n+1)-b-1;
    for(int i=1;i<=n;i++) ttt=lower_bound(b+1,b+num+1,a[i])-b,ori[ttt]=a[i],a[i]=ttt,pos[a[i]].push_back(i),p[i]=pos[a[i]].size()-1;
    init();
    while(m--){
        int l,r;
        l=read(),r=read();
        l=(l+lastans-1)%n+1,r=(r+lastans-1)%n+1;
        if(l>r) swap(l,r);
        lastans=ori[ask(l,r)];
        printf("%lld\n",lastans);
    }
}

by FourteenObsidian @ 2021-05-02 21:52:30

@银河AI RE 应该是因为您

pos[a[i]][maxn+p[i]]

这样吧


by FourteenObsidian @ 2021-05-02 21:54:42

特判一下越界试试


by 银河AI @ 2021-05-02 21:56:26

@ObsidianY 我前面判了

maxn+p[i]<pos[a[i]].size()

所以应该不会越界?


by FourteenObsidian @ 2021-05-02 21:57:37

哦是的,我才发现qwq

我再看一下


by 银河AI @ 2021-05-02 21:58:30

@ObsidianY 谢谢您


by FourteenObsidian @ 2021-05-02 22:08:53

@银河AI ask 里如果 ans 没有更新就可能 RE


by FourteenObsidian @ 2021-05-02 22:10:32

两端点不在同一个块里时 ans 初值要设为 g


by 银河AI @ 2021-05-03 08:06:01

@ObsidianY 哦对哦,谢谢您


by FourteenObsidian @ 2021-05-03 17:30:39

@银河AI 不用谢~


|