QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#706#431663#8546. Min or Max 2HuTaograss8cowFailed.2024-06-27 17:56:072024-06-27 17:56:09

Details

Extra Test:

Accepted
time: 3803ms
memory: 26980kb

input:

1
500000
467864 470102 8692 22216 300527 106521 232213 285779 196226 233864 21124 352861 154739 68228 216509 288670 457752 450638 6838 414925 240592 113796 101773 489249 153941 467759 175032 226463 392299 416100 300308 209126 453148 489973 426786 122972 460350 479693 340271 51876 128645 232682 41912...

output:

24 46 46 46 45 44 43 42 42 41 40 40 39 38 38 38 38 38 38 38 38 37 35 34 34 34 34 33 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 ...

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;
}