样例过全WA求调,悬赏1~3关注

P4168 [Violet] 蒲公英

qhzx_FeS2_Butterfly @ 2023-12-16 18:21:41


#include<bits/stdc++.h>
#define blk(i) ((i)-1)/b+1
#define lft(i) ((i)-1)*b+1
#define rgt(i) (i)*b
using namespace std;
const int N=40005,B=800;
int n,m,b,a[N],md[B][B],w[N],blt[N];
vector<int> v[N];
map<int,int> dy;
void init(int bl)
{
    memset(blt,0,sizeof(blt));
    int mx=0,ans=0;
    for(int i=lft(bl);i<=n;i++)
    {
        blt[a[i]]++;
        if(blk(a[i])>mx||(blk(a[i])==mx&&w[a[i]]<w[ans]))
        {
            ans=a[i];
            mx=blt[ans];
        }
        md[bl][blk(i)]=ans;
    }
}
int fd(int l,int r,int w)
{
    return upper_bound(v[w].begin(),v[w].end(),r)-lower_bound(v[w].begin(),v[w].end(),l);
}
int query(int l,int r)
{
    int ans=md[blk(l)+1][blk(r)-1],mx=fd(l,r,md[blk(l)+1][blk(r)-1]);
    for(int i=l;i<=min(rgt(blk(l)),r);i++)
    {
        int t=fd(l,r,a[i]);
        if(t>mx||(t==mx&&w[a[i]]<w[ans]))
        {
            ans=a[i];
            mx=t;
        }
    }
    if(blk(l)==blk(r))return ans;
    for(int i=lft(blk(r));i<=r;i++)
    {
        int t=fd(l,r,a[i]);
        if(t>mx||(t==mx&&w[a[i]]<w[ans]))
        {
            ans=a[i];
            mx=t;
        }
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int la=0,cg=0;
    cin>>n>>m;
    b=sqrt(n/log2(n));
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(!dy[a[i]])
        {
            dy[a[i]]=++cg;
            w[cg]=a[i];
        }
        a[i]=dy[a[i]];
        v[a[i]].push_back(i);
    }
    for(int i=1;i<=blk(n);i++)init(i);
    while(m--)
    {
        int l,r;
        cin>>l>>r;
        l=(l+la-1)%n+1;
        r=(r+la-1)%n+1;
        if(l>r)swap(l,r);
        la=w[query(l,r)];
        cout<<la<<'\n';
    }
}

by lyc1001 @ 2023-12-16 18:58:53

不会调,但是求关谢谢喵


by qw1234321 @ 2023-12-28 21:52:18

@FeS2_qwq

if(blk(a[i])>mx||(blk(a[i])==mx&&w[a[i]]<w[ans]))

应改为

if(blt[a[i]]>mx||(blt[a[i]]==mx&&w[a[i]]<w[ans]))

by qhzx_FeS2_Butterfly @ 2023-12-29 10:53:57

@Flying_hq 抱歉,已经过了


|