wa 30 可能的错误

P2839 [国家集训队] middle

Thusloop @ 2022-08-08 10:25:25

会有值相同的点,离散化不要去重 可以这样处理

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define yesno if(fg)yes;else no;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=8e18;
const int maxn=2e5+100;
vector<int>v;
int n,q;
int a[maxn],b[5];
vector<int>pos[maxn];
int id[maxn];
int find(int x)
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
struct node
{
    int l,r;
    int sum;
    int pre;
    int suf;
} t[maxn*32];
int root[maxn],cnt;
void push_up(node &u,node l,node r)
{
    u.sum=l.sum+r.sum;
    u.pre=max(l.pre,l.sum+r.pre);
    u.suf=max(r.suf,r.sum+l.suf);
}
void build(int l,int r,int &x)
{
    x=++cnt;
    if(l==r)
    {
        t[x].sum=1;
        t[x].pre=1;
        t[x].suf=1;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,t[x].l);
    build(mid+1,r,t[x].r);
    push_up(t[x],t[t[x].l],t[t[x].r]);
}
void update(int l,int r,int &x,int y,int num)
{
    x=++cnt;
    t[x]=t[y];
    if(l==r)
    {
        t[x].sum=-1;
        t[x].pre=-1;
        t[x].suf=-1;
        return ;
    }
    int mid=(l+r)>>1;
    if(num<=mid) update(l,mid,t[x].l,t[y].l,num);
    else update(mid+1,r,t[x].r,t[y].r,num);
    push_up(t[x],t[t[x].l],t[t[x].r]);

}
int query(int l,int r,int x,int L,int R)
{
    if(l>R||r<L)return 0;
    if(L<=l&&r<=R)
    {
        return t[x].sum;
    }
    int mid=(l+r)>>1;
    int ans=0;
    ans+=query(l,mid,t[x].l,L,R);
    ans+=query(mid+1,r,t[x].r,L,R);
    return ans;
}
node query2(int l,int r,int x,int L,int R)
{
    if(l>R||r<L)return {0,0,0,-inf,-inf};
    if(L<=l&&r<=R)
    {
        return t[x];
    }
    int mid=(l+r)>>1;
    node sl=query2(l,mid,t[x].l,L,R);
    node sr=query2(mid+1,r,t[x].r,L,R);
    node now;
    push_up(now,sl,sr);
    return now;
}

bool ck(int mid)
{
    int sum=0;
    if(b[2]+1<=b[3]-1)sum=query(1,n,root[mid],b[2]+1,b[3]-1);
    int mxpre=query2(1,n,root[mid],b[3],b[4]).pre;
    int mxsuf=query2(1,n,root[mid],b[1],b[2]).suf;
    if(sum+mxpre+mxsuf>=0)return 1;
    return 0;
}
bool cmp(int x,int y)
{
    return a[x]<a[y];
}
signed main()
{
    IOS
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        id[i]=i;
    }
    sort(id+1,id+n+1,cmp);
    build(1,n,root[1]);
    for(int i=2; i<=n; i++)
    {
        update(1,n,root[i],root[i-1],id[i-1]);  
    }
    int ans=0;
    cin>>q;
    while(q--)
    {
        for(int i=1; i<=4; i++)
        {
            cin>>b[i];
            b[i]=(b[i]+ans)%n+1;
        }
        sort(b+1,b+4+1);
        int l=1,r=n;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(ck(mid))l=mid+1;
            else r=mid-1;
        }
        ans=a[id[r]];
        cout<<ans<<"\n";
    }
}

by 出言不逊王子 @ 2022-12-08 23:08:24

@Thusloop 所以为啥这玩意是对的而第一篇题解也照样离散化了都过的好好的,感觉本质原因不在这个吧?

(我离散化结果WA30然后改成您的做法过了)


|