QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#598014#4422. Two Permutationshuaxiamengjin#AC ✓1765ms59856kbC++141.1kb2024-09-28 20:01:002024-09-28 20:01:01

Judging History

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

  • [2024-09-28 20:01:01]
  • 评测
  • 测评结果:AC
  • 用时:1765ms
  • 内存:59856kb
  • [2024-09-28 20:01:00]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int NN=998244353;
int a[300300],b[300300],c[600600];
int aa[300300],bb[300300];
int f[600300][2],g[600300][2];
vector<int>d[600300];
int n;
void solve(){
	cin>>n;
	for (int i=1;i<=2*n+1;i++)d[i].clear();
	for (int i=1;i<=n;i++)
	cin>>a[i],aa[a[i]]=i;aa[n+1]=n+1;a[n+1]=n+1;
	for (int i=1;i<=n;i++)
	cin>>b[i],bb[b[i]]=i;bb[n+1]=n+1;b[n+1]=n+1;
	for (int i=1;i<=2*n;i++)
	cin>>c[i],d[c[i]].push_back(i),f[i][0]=f[i][1]=g[i][0]=g[i][1]=0;
	d[n+1].push_back(2*n+1);
	f[2*n+1][0]=1,f[2*n+1][1]=1;g[2*n+1][0]=n+1,g[2*n+1][1]=n+1;
	for (int i=2*n;i;i--){
		for (auto j:d[a[aa[c[i]]+1]])
		if(j>i){
			int t2=g[j][0],fl=1;
			for (int k=j-1;k>i;k--)
			if(b[bb[t2]-1]==c[k])t2=c[k];
			else {fl=0;break;}
			if(fl==1)f[i][0]=(f[i][0]+f[j][0])%NN,g[i][0]=t2;
		}
		for (auto j:d[b[bb[c[i]]+1]])
		if(j>i){
			int t2=g[j][1],fl=1;
			for (int k=j-1;k>i;k--)
			if(a[aa[t2]-1]==c[k])t2=c[k];
			else {fl=0;break;}
			if(fl==1)f[i][1]=(f[i][1]+f[j][1])%NN,g[i][1]=t2;
		}
	}
	cout<<(f[1][0]+f[1][1])%NN<<"\n";
}
signed main(){
	int T;cin>>T;
	while(T--)solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1765ms
memory: 59856kb

input:

282
1000
434 697 735 906 614 835 227 800 116 98 776 601 110 371 262 452 608 368 719 717 241 572 203 410 440 749 604 457 516 637 488 691 841 493 418 307 745 499 833 789 819 179 357 288 129 954 29 391 80 389 771 613 653 747 928 570 518 1000 547 587 727 778 669 554 426 899 256 681 515 532 409 677 533 3...

output:

4
2
16
2
2
2
1
1
8
8
4
4
2
2
2
1
2
2
2
2
2
1
1
2
8
1
2
2
4
2
2
1
2
2
8
2
2
2
4
4
1
4
1
4
4
1
1
8
4
0
4
4
1
2
4
2
2
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0...

result:

ok 282 lines