QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#521364 | #5047. Permutation | lycegg | WA | 41ms | 20320kb | C++20 | 6.1kb | 2024-08-16 09:16:59 | 2024-08-16 09:16:59 |
Judging History
answer
#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]]++;
add(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: 20176kb
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: 0
Accepted
time: 41ms
memory: 20320kb
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 1 24 4 6 6 6 4 1 12 1 6 6 24 18 2 18 4 6 6 18 6 4 1 6 18 1 6 24 18 6 1 12 18 6 4 2 24 12 4 24 4 4 24 6 1 1 1 1 6 1 4 1 18 1 18 4 4 6 24 6 4 6 1 12 1 4 4 6 24 18 6 2 6 1 12 6 24 1 4 6 1 1 6 1 1 24 12 18 1 4 18 1 4 24 6 4 24 6 24 1 1 6 1 18 24 1 4 1 1 2 6 1 6 4 18 1 24 6 6 4 24 ...
result:
ok 100000 numbers
Test #3:
score: -100
Wrong Answer
time: 11ms
memory: 20236kb
input:
10000 10 2 7 4 2 9 1 3 10 5 8 6 10 7 10 6 1 5 2 4 7 9 8 3 10 4 10 7 6 2 8 1 5 4 3 9 10 4 4 9 6 2 10 7 8 5 1 3 10 5 9 8 7 4 5 3 2 10 6 1 10 3 5 7 8 1 9 4 6 10 3 2 10 4 6 2 10 8 4 7 9 5 3 1 10 5 10 1 3 7 5 9 4 2 8 6 10 7 10 3 4 5 6 1 9 2 8 7 10 4 1 8 9 7 5 6 3 10 4 2 10 3 6 7 3 1 9 4 10 8 5 2 10 3 5 1...
output:
576 5040 2304 24 201600 720 5040 120 1 20160 720 120 1 360 120 120 2160 161280 192 1 480 1 108 24 1 432 25200 1 30240 35280 35280 720 2160 768 1296 120 35280 20160 432 432 8 201600 192 432 161280 201600 2304 720 5760 576 1 120 201600 576 360 241920 40320 24 1 1 1 35280 1 1 1 3600 720 108 720 1 1296 ...
result:
wrong answer 1st numbers differ - expected: '432', found: '576'