QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#707#431663#8546. Min or Max 2HuTaograss8cowFailed.2024-06-27 17:57:182024-06-27 17:57:19

Details

Extra Test:

Accepted
time: 3637ms
memory: 28692kb

input:

1
500000
178103 32256 411431 250260 379853 66932 66972 113673 297731 123431 293092 152796 142030 381097 220711 59185 394645 17247 80936 40343 225409 203209 333023 320367 323789 372642 50091 106381 238898 278766 91514 243034 59285 208673 399160 236139 144627 334725 382014 384714 14184 419400 412290 1...

output:

10 17 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 17 19 20 20 20 20 18 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 15 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 ...

result:

ok 500000 numbers

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431663#8546. Min or Max 2grass8cowAC ✓4028ms28416kbC++172.9kb2024-06-05 21:23:432024-06-05 21:23:44

answer

#include<bits/stdc++.h>
using namespace std;
int n;
struct qq{bool a[3][3];}e[2001000];
#define xp(a,b) ((b==1)?a:b)
qq operator + (const qq &a,const qq &b){
    qq c;
    memset(c.a,0,sizeof(c.a));
    for(int i=0;i<3;i++)for(int j=0;j<3;j++)if(a.a[i][j])
    for(int i_=0;i_<3;i_++)for(int j_=0;j_<3;j_++)if(b.a[i_][j_])c.a[xp(i,i_)][xp(j,j_)]=1;
    return c;
}
int A[501000][2];
void build(int p,int l,int r){
    if(l==r){memset(e[p].a,0,sizeof(e[p].a));e[p].a[2][2]=e[p].a[1][1]=1,A[l][0]=A[l][1]=2;return;}
    int mi=(l+r)>>1;
    build(p<<1,l,mi),build(p<<1|1,mi+1,r),e[p]=e[p<<1]+e[p<<1|1];
}
void up(int p,int l,int r,int x,int z1,int z2){
    if(x==1)return;
    if(l==r){
        A[x][z1]=z2;
        memset(e[p].a,0,sizeof(e[p].a));
        e[p].a[max(1,A[x][0])][max(1,A[x][1])]=1;
        e[p].a[min(1,A[x][0])][min(1,A[x][1])]=1;
        return;
    }
    int mi=(l+r)>>1;
    if(x<=mi)up(p<<1,l,mi,x,z1,z2);
    else up(p<<1|1,mi+1,r,x,z1,z2);
    e[p]=e[p<<1]+e[p<<1|1];
}
bool t[3][3],t_[3][3];
void ask(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y){
        memset(t_,0,sizeof(t_));
        for(int i=0;i<3;i++)for(int j=0;j<3;j++)if(t[i][j])
        for(int i_=0;i_<3;i_++)for(int j_=0;j_<3;j_++)if(e[p].a[i_][j_])
            t_[(i_==1)?i:i_][(j_==1)?j:j_]=1;
        for(int i=0;i<3;i++)for(int j=0;j<3;j++)t[i][j]=t_[i][j];
        return;
    }
    int mi=(l+r)>>1;
    if(x<=mi)ask(p<<1,l,mi,x,y);
    if(y>mi)ask(p<<1|1,mi+1,r,x,y);
}
void hm(int x,int y){
    memset(t_,0,sizeof(t_));
    for(int i=0;i<3;i++)for(int j=0;j<3;j++)if(t[i][j])
    t_[max(i,x)][max(j,y)]=1,
    t_[min(i,x)][min(j,y)]=1;
    for(int i=0;i<3;i++)for(int j=0;j<3;j++)t[i][j]=t_[i][j];
}
#define sg(a,b) ((a<b)?2:(((a==b)?1:0)))
int p[501000],q[501000],p_[500100],q_[501000],x,y;
bool chk(int nx,int ny){
    if(nx<=0||nx>n)return 0;if(ny<=0||ny>n)return 0;
    while(x<nx)up(1,2,n,p_[++x],0,0);
    while(x>nx)up(1,2,n,p_[x--],0,2);
    while(y<ny)up(1,2,n,q_[++y],1,0);
    while(y>ny)up(1,2,n,q_[y--],1,2);
    memset(t,0,sizeof(t)),t[sg(nx,p[1])][sg(ny,q[1])]=1;
    int a=p_[x],b=q_[y];if(a>b)swap(a,b);
    if(2<a)ask(1,2,n,2,a-1);
    if(a>=2)hm(sg(nx,p[a]),sg(ny,q[a]));
    if(a+1<b)ask(1,2,n,a+1,b-1);
    if(a<b)hm(sg(nx,p[b]),sg(ny,q[b]));
    if(b<n)ask(1,2,n,b+1,n);
    return t[1][1];
}
int ans[501000];
void sol(){
    scanf("%d",&n);
    for(int i=0;i<n;i++)ans[i]=0;
    for(int i=1;i<=n;i++)scanf("%d",&p[i]),p_[p[i]]=i;
    for(int i=1;i<=n;i++)scanf("%d",&q[i]),q_[q[i]]=i;
    build(1,2,n);
    x=0,y=0;
    int ox=0,oy=0;
    while(1){
        if(ox&&oy)ans[abs(ox-oy)]++;
        if(chk(ox+1,oy))ox++;
        else if(chk(ox,oy+1))oy++;
        else if(chk(ox+1,oy+1))ox++,oy++;
        else break;
    }
    for(int i=0;i<n;i++)printf("%d ",ans[i]);puts("");
}
int main(){
    int T;scanf("%d",&T);while(T--)sol();
    return 0;
}