样例过全WA求调

P4168 [Violet] 蒲公英

ADNAP @ 2024-02-17 08:54:58

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define PLL pair<ll,ll>
#define x first 
#define y second
const ll N=1e5+1e4,M=1e3+1e2;
const ll Max=1e12;
const ll C=1;
ll n,m;
struct node
{
    ll num;
    ll id;
    ll sot;
}a[N];
ll l,r;
PLL p[M][M];
ll s[220][42000];
//p[i][j].x表示块i到块j中出现次数最多的种类编号
//p[i][j].y表示块i到块j中出现次数最多的离散化后编号
//s[i][j]是块i前j出现的次数
ll res[N];
ll xb[N];
bool cmp1(node a,node b)
{
    return a.num<b.num;
}
bool cmp2(node a,node b)
{
    return a.id<b.id;
}
ll len;
ll q[N];
ll qp[42000];
ll vist[42000];
ll ask(ll l,ll r)
{
    if(xb[r]-xb[l]<=1)
    {
        memset(res,0,sizeof res);
        ll mx=0,ans=Max;
        for(ll i=l;i<=r;i++)
        {
            res[a[i].sot]++;
            if(mx<res[a[i].sot])mx=res[a[i].sot],ans=a[i].num;
            if(mx==res[a[i].sot]&&ans>a[i].num)ans=a[i].num;
        }
        return ans;
    }//
    else
    {
        ll zz=0;
        ll i=l,j=r;
        while(xb[i]==xb[l])
        {
            if(!vist[a[i].sot])
            {
                vist[a[i].sot]=true;
                q[++zz]=a[i].sot;
                qp[zz]=a[i].num;
            }
            res[a[i].sot]++,i++;
        }
        while(xb[j]==xb[r])
        {
            if(!vist[a[j].sot])
            {
                vist[a[j].sot]=true;
                q[++zz]=a[j].sot;
                qp[zz]=a[j].num;
            }
            res[a[j].sot]++,j--;
        }//
        ll mx=0,ans=Max;

        // q[++zz]=p[xb[i]][xb[j]].y;
        // qp[zz]=p[xb[i]][xb[j]].x;
        for(ll k=1;k<=zz;k++)
        {
            if(mx<res[q[k]]+s[xb[j]][q[k]]-s[xb[i]-1][q[k]])mx=res[q[k]]+s[xb[j]][q[k]]-s[xb[i]-1][q[k]],ans=qp[k];
            else if(mx==res[q[k]]+s[xb[j]][q[k]]-s[xb[i]-1][q[k]]&&ans>qp[k])ans=qp[k];
        }
        if(mx<res[p[xb[i]][xb[j]].y]+s[xb[j]][p[xb[i]][xb[j]].y]-s[xb[i]-1][p[xb[i]][xb[j]].y])mx=res[p[xb[i]][xb[j]].y]+s[xb[j]][p[xb[i]][xb[j]].y]-s[xb[i]-1][p[xb[i]][xb[j]].y],ans=p[xb[i]][xb[j]].x;
        else if(mx==res[p[xb[i]][xb[j]].y]+s[xb[j]][p[xb[i]][xb[j]].y]-s[xb[i]-1][p[xb[i]][xb[j]].y]&&ans>p[xb[i]][xb[j]].x)ans=p[xb[i]][xb[j]].x;

        // memset(vist,0,sizeof vist);
        // memset(res,0,sizeof res);
        for(ll k=l;k<=i;k++)vist[k]=false,res[a[k].sot]=0;
        for(ll k=j;k<=r;k++)vist[k]=false,res[a[k].sot]=0;
        return ans;
    }
}
signed main()
{
    cin>>n>>m;
    len=sqrt(n);
    for(ll i=1;i<=n;i++)xb[i]=i/len;
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i].num;
        a[i].id=i;
    }
    sort(a+1,a+n+1,cmp1);
    ll cnt=0;
    for(ll i=1;i<=n;i++)
    {
        if(a[i].num!=a[i-1].num)
            ++cnt;
        a[i].sot=cnt;
    }
    sort(a+1,a+n+1,cmp2);
    for(ll i=xb[1];i<=xb[n];i++)
    {
        ll mx=0,ans=Max;
        ll op;
        for(ll j=i;j<=xb[n];j++)
        {
            for(ll k=max(C,j*len);k<=min(n,j*len+len-1);k++)
            {
                res[a[k].sot]++;
                if(mx<res[a[k].sot])mx=res[a[k].sot],ans=a[k].num,op=a[k].sot;
                if(mx==res[a[k].sot]&&ans>a[k].num)ans=a[k].num,op=a[k].sot;
            }
            p[i][j].x=ans;
            p[i][j].y=op;
        }
        memset(res,0,sizeof res);
    }
    for(ll i=xb[1];i<=xb[n];i++)
    {
        for(ll j=1;j<=cnt;j++)
            s[i][j]=s[i-1][j];
        for(ll j=max(C,i*len);j<=min(n,i*len+len-1);j++)
            s[i][a[j].sot]++;
    }
    ll x=0;
    while(m--)
    {
        cin>>l>>r;
        l=((l+x-1)%n)+1;
        r=((r+x-1)%n)+1;
        if(l>r)swap(l,r);
        x=ask(l,r);
        // cout<<l<<" "<<r<<"\n";
        cout<<x<<"\n";
    }
}

|