QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#402236#8546. Min or Max 2GraphcityWA 296ms32992kbC++205.0kb2024-04-30 09:40:032024-04-30 09:40:03

Judging History

你现在查看的是最新测评结果

  • [2024-04-30 09:40:03]
  • 评测
  • 测评结果:WA
  • 用时:296ms
  • 内存:32992kb
  • [2024-04-30 09:40:03]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=5e5;

int n,a[Maxn+5],b[Maxn+5],id[Maxn+5],ans[Maxn+5];
vector<pair<int,int>> all;
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)

struct Node
{
    int mn,mx;
    inline void GetMx(int x) {if(x>=mn) mn=mx=x; else mx=max(mx,x);}
    inline void GetMn(int x) {if(x<=mx) mn=mx=x; else mn=min(mn,x);}
    inline int Get(int x) {return min(mn,max(mx,x));}
};
inline Node operator+(Node a,Node b) {a.GetMx(b.mx),a.GetMn(b.mn); return a;}
struct BIT
{
    Node t[Maxn*4+5];
    inline void push_up(int p) {t[p]=t[ls(p)]+t[rs(p)];}
    inline void Build(int l,int r,int p)
    {
        if(l==r) {t[p].mx=b[l],t[p].mn=(l==1?b[l]:n+1); return;}
        int mid=(l+r)>>1; Build(l,mid,ls(p)),Build(mid+1,r,rs(p)),push_up(p);
    }
    inline void Modify(int l,int r,int p,int k)
    {
        if(l==r) {t[p].mn=b[l],t[p].mx=(l==1?b[l]:0); return;} int mid=(l+r)>>1;
        if(k<=mid) Modify(l,mid,ls(p),k); else Modify(mid+1,r,rs(p),k); push_up(p);
    }
    inline Node Count(int nl,int nr,int l,int r,int p)
    {
        if(l>r) return Node{n+1,0}; if(l<=nl && nr<=r) return t[p]; int mid=(nl+nr)>>1;
        if(l<=mid && r>mid) return Count(nl,mid,l,r,ls(p))+Count(mid+1,nr,l,r,rs(p));
        if(l<=mid) return Count(nl,mid,l,r,ls(p)); else return Count(mid+1,nr,l,r,rs(p));
    }
} BT;

inline pair<int,int> operator+(pair<int,int> a,pair<int,int> b)
{a.first=min(a.first,b.first),a.second=max(a.second,b.second); return a;}
struct SegTree
{
    pair<int,int> t[Maxn*4+5]; Node tag[Maxn*4+5];
    inline void push_up(int p) {t[p]=t[ls(p)]+t[rs(p)];}
    inline void mk(int p,Node k)
    {tag[p]=tag[p]+k,t[p].first=k.Get(t[p].first),t[p].second=k.Get(t[p].second);}
    inline void push_down(int p)
    {
        if(tag[p].mn==n+1 && tag[p].mx==0) return;
        mk(ls(p),tag[p]),mk(rs(p),tag[p]),tag[p]=Node{n+1,0};
    }
    inline void Build(int l,int r,int p)
    {
        t[p]=make_pair(n+1,0),tag[p]=Node{n+1,0}; if(l==r) return;
        int mid=(l+r)>>1; Build(l,mid,ls(p)),Build(mid+1,r,rs(p));
    }
    inline void Insert(int l,int r,int p,int pos,pair<int,int> k)
    {
        if(l==r) {t[p]=k; return;} int mid=(l+r)>>1; push_down(p);
        if(pos<=mid) Insert(l,mid,ls(p),pos,k); else Insert(mid+1,r,rs(p),pos,k); push_up(p);
    }
    inline void Modify(int nl,int nr,int l,int r,int p,Node k)
    {
        if(l>r) return;
        if(l<=nl && nr<=r) {mk(p,k); return;}
        int mid=(nl+nr)>>1; push_down(p);
        if(l<=mid) Modify(nl,mid,l,r,ls(p),k);
        if(r>mid) Modify(mid+1,nr,l,r,rs(p),k);
        push_up(p);
    }
    inline pair<int,int> Count(int nl,int nr,int l,int r,int p)
    {
        if(l>r) return make_pair(n+1,0); if(l<=nl && nr<=r) return t[p];
        int mid=(nl+nr)>>1; push_down(p);
        if(l<=mid && r>mid) return Count(nl,mid,l,r,ls(p))+Count(mid+1,nr,l,r,rs(p));
        if(l<=mid) return Count(nl,mid,l,r,ls(p)); else return Count(mid+1,nr,l,r,rs(p));
    }
} T;

inline void Work(int TP)
{
    For(i,1,n) id[a[i]]=i;
    BT.Build(1,n,1),T.Build(1,n,1);
    static int mn[Maxn+5],mx[Maxn+5];
    For(i,1,n)
    {
        mn[i]=n+1,mx[i]=0;
        if(i==1) mn[1]=mx[1]=b[1];
        else
        {
            auto k=T.Count(1,n,1,a[i]-1,1);
            if(k.first<=k.second)
            {
                k.first=max(k.first,b[i]),k.second=max(k.second,b[i]);
                mn[i]=min(mn[i],k.first),mx[i]=max(mx[i],k.second);
            } k=T.Count(1,n,a[i]+1,n,1);
            if(k.first<=k.second)
            {
                k.first=min(k.first,b[i]),k.second=min(k.second,b[i]);
                mn[i]=min(mn[i],k.first),mx[i]=max(mx[i],k.second);
            }
        }
        T.Modify(1,n,1,a[i]-1,1,Node{b[i],0});
        T.Modify(1,n,a[i]+1,n,1,Node{n+1,b[i]});
        if(mn[i]<=mx[i]) T.Insert(1,n,1,a[i],make_pair(mn[i],mx[i]));
    }
    Rof(_,n,1)
    {
        int i=id[_]; auto k=BT.Count(1,n,i+1,n,1);
        if(mn[i]<=mx[i])
        {
            int l=k.Get(mn[i]),r=k.Get(mx[i]);
            if(l>=1 && l<=n)
            {
                if(TP==0) all.emplace_back(a[i],l);
                else all.emplace_back(l,a[i]);
            }
            if(r>=1 && r<=n)
            {
                if(TP==0) all.emplace_back(a[i],r);
                else all.emplace_back(r,a[i]);
            }
        } BT.Modify(1,n,1,i);
    }
}
inline void Solve()
{
    cin>>n; all.clear();
    For(i,1,n) cin>>a[i];
    For(i,1,n) cin>>b[i];
    For(i,0,n) ans[i]=0;
    Work(0); For(i,1,n) swap(a[i],b[i]); Work(1);
    sort(all.begin(),all.end()),all.erase(unique(all.begin(),all.end()),all.end());
    for(auto [x,y]:all) ans[abs(x-y)]++;
    For(i,0,n-1) cout<<ans[i]<<' '; cout<<endl;
}

int main()
{
    // freopen("1.in","r",stdin);

    ios::sync_with_stdio(false);
    int T; cin>>T;
    while(T--) Solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 32444kb

input:

4
2
1 2
2 1
5
2 4 1 5 3
2 4 1 5 3
5
1 2 3 4 5
5 4 3 2 1
8
5 8 3 4 2 7 1 6
4 6 3 8 5 1 2 7

output:

2 0 
5 0 0 0 0 
2 2 2 2 0 
5 5 2 2 1 0 0 0 

result:

ok 20 numbers

Test #2:

score: -100
Wrong Answer
time: 296ms
memory: 32992kb

input:

66664
7
4 2 6 5 7 1 3
6 5 3 1 4 7 2
10
6 8 10 7 5 1 4 3 9 2
5 10 3 8 6 7 2 9 1 4
9
3 2 4 8 7 6 9 1 5
8 1 2 9 6 7 4 3 5
10
4 3 9 6 7 2 10 1 8 5
3 5 4 1 2 7 10 9 6 8
5
3 4 1 2 5
5 1 3 2 4
5
2 4 3 5 1
2 3 1 4 5
6
2 6 1 3 4 5
6 4 5 1 3 2
10
10 1 2 7 5 8 4 3 9 6
9 4 2 3 6 1 7 8 5 10
5
1 2 4 5 3
4 1 2 5 3...

output:

4 4 2 2 1 0 0 
5 6 3 2 2 1 0 0 0 0 
5 6 3 2 1 0 0 0 0 
4 4 4 3 2 1 0 0 0 0 
5 4 0 0 0 
2 2 2 2 0 
3 3 3 1 0 0 
5 7 4 2 1 0 0 0 0 0 
5 2 0 0 0 
6 5 0 0 0 0 
3 3 2 0 0 
5 5 2 1 0 0 0 
3 2 3 1 0 0 
4 6 3 0 0 0 0 
3 4 3 2 1 0 0 
3 2 2 2 2 2 2 1 0 
4 5 3 1 0 0 0 
3 4 3 2 3 3 1 0 0 0 
8 5 0 0 0 0 0 0 
7 8...

result:

wrong answer 38th numbers differ - expected: '3', found: '4'