QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#521361#5047. PermutationlyceggWA 43ms20296kbC++206.1kb2024-08-16 09:13:252024-08-16 09:13:26

Judging History

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

  • [2024-08-16 09:13:26]
  • 评测
  • 测评结果:WA
  • 用时:43ms
  • 内存:20296kb
  • [2024-08-16 09:13:25]
  • 提交

answer


/*
1
5 4
4 2 1 3 5
*/#include<bits/stdc++.h>
using namespace std;
#define M 998244353
#define int long long
#define left 1
#define right 2
#define close 3
int t,c,n;
int p[500500];
int pos[500500];
int fa[500500];
int l[500500];
int r[500500];
int dir[500500];
int ljmp[500500],rjmp[500500];
int bt[500500];
void add(int x){
    for(;x<=n;x+=(x&-x))
        bt[x]++;
}
int qry(int x){
    int ret=0;
    for(;x;x-=(x&-x))ret+=bt[x];
    return ret;
}
int getfa(int x){
    if(x==fa[x])return x;
    fa[x]=getfa(fa[x]);
    return fa[x];
}
signed main(){
    scanf("%lld",&t);
    while(t--){
        scanf("%lld%lld",&n,&c);
        for(int i=1;i<=n;i++){
            scanf("%lld",p+i);
            pos[p[i]]=i;
            fa[i]=i;
            bt[i]=0;
            ljmp[i]=0;
            rjmp[i]=0;
            dir[i]=0;
        }
        for(int i=1;i<=n;i++){
            l[i]=pos[i];
            r[i]=pos[i];
        }
        for(int i=1;i<=n;i++){
            int curint=getfa(i);
            if(l[curint]==r[curint]){
                if(pos[curint]>c){
                    int lmost=p[pos[curint]-c];
                    lmost=getfa(lmost);
                    if(l[lmost]==r[lmost]){
                        if(qry(pos[curint]-c-1)==qry(pos[curint]-1)){
                            for(int i=pos[curint]-c;i<pos[curint]-1;i++)
                                fa[p[i]]=p[pos[curint]-1];
                            l[p[pos[curint]-1]]=pos[curint]-c;
                            r[p[pos[curint]-1]]=pos[curint]-1;
                            dir[p[pos[curint]-1]]=left;
                        }
                    }
                    else{
                        if(dir[lmost]==right){
                            for(int i=r[lmost]+1;i<pos[curint];i++)
                                fa[p[i]]=lmost;
                            r[lmost]=pos[curint]-1;
                            dir[lmost]=close;
                        }
                    }
                }
                if(pos[curint]+c<=n){
                    int rmost=p[pos[curint]+c];
                    rmost=getfa(rmost);
                    if(l[rmost]==r[rmost]){
                        if(qry(pos[curint]+c)==qry(pos[curint])){
                            for(int i=pos[curint]+2;i<=pos[curint]+c;i++)
                                fa[p[i]]=p[pos[curint]+1];
                            l[p[pos[curint]+1]]=pos[curint]+1;
                            r[p[pos[curint]+1]]=pos[curint]+c;
                            dir[p[pos[curint]+1]]=right;
                        }
                    }
                    else{
                        if(dir[rmost]==left){
                            for(int i=pos[curint]+1;i<l[rmost];i++)
                                fa[p[i]]=rmost;
                            l[rmost]=pos[curint]+1;
                            dir[rmost]=close;
                        }
                    }
                }
            }
            else{
                if(dir[curint]==left){
                    if(l[curint]<=c+1){
                        if(l[curint]!=1){
                            ljmp[curint]++;
                            for(int i=1;i<l[curint];i++)
                                fa[p[i]]=curint;
                            l[curint]=1;
                        }
                    }
                    else{//gengxin l,r
                        int lmost=p[l[curint]-c];//aaaaa
                        ljmp[curint]++;
                        lmost=getfa(lmost);
                        if(l[lmost]==r[lmost]){
                            for(int i=l[curint]-c;i<=l[curint]-1;i++)
                                fa[p[i]]=curint;
                            l[curint]=l[curint]-c;
                        }
                        else{
                            for(int i=r[lmost]+1;i<l[curint];i++)
                                fa[p[i]]=lmost;
                            fa[curint]=lmost;
                            r[lmost]=r[curint];
                            ljmp[lmost]=ljmp[curint];
                            dir[lmost]=close;
                        }
                    }
                }
                if(dir[curint]==right){
                    if(r[curint]+c>=n){
                        if(r[curint]!=n){
                            rjmp[curint]++;
                            for(int i=r[curint]+1;i<=n;i++)
                                fa[p[i]]=curint;
                            r[curint]=n;
                        }
                    }
                    else{//gengxin l,r
                        int rmost=p[r[curint]+c];//aaaa
                        rjmp[curint]++;
                        rmost=getfa(rmost);
                        if(l[rmost]==r[rmost]){
                            for(int i=r[curint]+1;i<=r[curint]+c;i++)
                                fa[p[i]]=curint;
                            r[curint]+=c;
                        }
                        else{
                            for(int i=r[curint]+1;i<l[rmost];i++)
                                fa[p[i]]=rmost;
                            fa[curint]=rmost;
                            l[rmost]=l[curint];
                            rjmp[rmost]=rjmp[curint];
                            dir[rmost]=close;
                        }
                    }

                }
            }
            bt[pos[i]]++;
        }
        int ans=1;
        for(int i=1;i<=n;i++){
            int x=getfa(i);
            if(x==i){
                int len=r[x]-l[x]+1;
                //printf("%lld %lld %lld\n",x,l[x],r[x]);
                while(ljmp[x]){
                    ans=ans*ljmp[x]%M*c%M;
                    ljmp[x]--;
                    len--;
                }
                while(rjmp[x]){
                    ans=ans*rjmp[x]%M*c%M;
                    rjmp[x]--;
                    len--;
                }
                while(len){
                    ans=ans*len%M;
                    len--;
                }
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 20296kb

input:

5
5 3
3 4 2 1 5
5 4
4 2 1 3 5
5 2
4 5 3 1 2
5 3
4 3 2 1 5
5 2
2 3 1 5 4

output:

6
1
4
6
4

result:

ok 5 number(s): "6 1 4 6 4"

Test #2:

score: -100
Wrong Answer
time: 43ms
memory: 20296kb

input:

100000
5 4
1 3 2 5 4
5 3
5 1 4 2 3
5 2
1 4 5 3 2
5 4
5 2 4 3 1
5 4
2 5 4 1 3
5 4
1 2 3 5 4
5 4
4 3 2 5 1
5 3
1 5 4 3 2
5 3
3 2 5 4 1
5 4
4 3 1 5 2
5 4
4 3 5 2 1
5 2
3 2 1 4 5
5 3
2 4 5 1 3
5 3
2 1 4 3 5
5 3
2 1 5 4 3
5 2
2 1 3 4 5
5 4
2 3 1 4 5
5 2
1 2 4 5 3
5 3
2 4 1 5 3
5 3
2 4 3 5 1
5 3
4 1 3 5 2...

output:

24
6
6
24
1
24
24
6
18
24
24
4
6
6
6
4
24
12
6
6
6
24
18
2
18
4
6
6
18
6
4
6
6
18
1
6
24
18
6
24
12
18
6
4
2
24
12
4
24
4
4
24
6
1
1
1
1
6
18
4
1
18
24
18
4
4
6
24
6
4
6
18
12
24
4
4
6
24
18
6
2
6
24
12
6
24
1
4
6
18
1
6
1
6
24
12
18
1
4
18
1
4
24
6
4
24
6
24
1
1
6
6
18
24
18
4
18
6
2
6
1
6
4
18
6
2...

result:

wrong answer 10th numbers differ - expected: '1', found: '24'