solution-p3988

· · 题解

随机跳题,看了一圈大家都是平衡树和线段树。

感觉分块应该可以啊?

把数列分块,每次先跳大块,再跳零散点。记录下每个块的大小以及第一个点,每次跳的时候可以先回退到当前块的第一个点(细节好写一点)。

复杂度 O(n\sqrt n),吸氧过了。

#include <bits/stdc++.h>
const int N=700005;
int n,nxt[N],pre[N],fir[N],siz[N],x;
int read(){
    int t=0;
    char ch=getchar();
    while (ch<'0' || ch>'9') ch=getchar();
    while (ch>='0' && ch<='9') t=t*10+ch-'0',ch=getchar();
    return t; 
}
int main(){
    n=read();
    int block=(int)sqrt(2*n);
    for (int i=1;i<n;i++) nxt[i]=i+1,pre[i+1]=i;
    nxt[n]=1,pre[1]=n;
    int tot=0;
    for (int l=1,r=block;l<=n;l+=block,r+=block,tot++){
        if (r>n) r=n;
        siz[tot]=r-l+1;
        fir[tot]=l;
    }
    int p=1,now=0;
    for (int i=1,res=n;i<=n;i++,res--){
        x=read();
        x=x%res;
        int t=(p-1)/block;
        x+=now;
        while (x>=siz[t]){
            x-=siz[t];
            t=(t+1)%tot;
        }
        now=0;
        p=fir[t];
        while (x){
            p=nxt[p];
            now++,x--; 
        }
        printf("%d\n",p);
        nxt[pre[p]]=nxt[p];
        pre[nxt[p]]=pre[p];
        siz[t]--;
        if (p==fir[t]) fir[t]=nxt[fir[t]];
    }
}