QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#200729#5116. ContestsA_zjzjWA 71ms36608kbC++142.3kb2023-10-04 19:09:412023-10-04 19:09:43

Judging History

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

  • [2023-10-04 19:09:43]
  • 评测
  • 测评结果:WA
  • 用时:71ms
  • 内存:36608kb
  • [2023-10-04 19:09:41]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=1e5+10,M=5,K=__lg(N)+2;
int n,m,q,a[M][N],b[M][N];
int c[M][N][M],f[K][N][M];
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		for(int j=1;j<=n;j++){
			scanf("%d",&a[i][j]),b[i][a[i][j]]=j;
		}
		debug("a",ary(a[i],1,n));
		debug("b",ary(b[i],1,n));
	}
	for(int i=0;i<m;i++){
		fill(c[i][n+1],c[i][n+1]+m,n+1);
		for(int j=n;j>=1;j--){
			for(int k=0;k<m;k++){
				c[i][j][k]=min(c[i][j+1][k],b[k][a[i][j]]);
			}
			debug("c",i,j,ary(c[i][j],0,m-1));
		}
	}
	for(int u=1;u<=n;u++){
		for(int j=0;j<m;j++){
			f[0][u][j]=n+1;
			for(int k=0;k<m;k++){
				f[0][u][j]=min(f[0][u][j],c[k][b[k][u]][j]);
			}
		}
	}
	for(int i=1;(1<<i)<=n;i++){
		for(int u=1;u<=n;u++){
			for(int j=0;j<m;j++){
				f[i][u][j]=n+1;
				for(int k=0;k<m;k++){
					f[i][u][j]=min(f[i][u][j],f[i-1][b[k][f[i-1][u][k]]][j]);
				}
			}
		}
	}
	for(int i=0;(1<<i)<=n;i++){
		for(int u=1;u<=n;u++){
			debug("f",i,u,ary(f[i][u],0,m-1));
		}
	}
	scanf("%d",&q);
	for(int s,t;q--;){
		scanf("%d%d",&s,&t);
		auto chk1=[&](){
			for(int i=0;i<m;i++){
				if(b[i][s]<b[i][t])return 1;
			}
			return 0;
		};
		if(chk1()){
			puts("1");continue;
		}
		int now[M]={0,0,0,0,0};
		for(int i=0;i<m;i++)now[i]=b[i][s];
		int ans=0;
		for(int i=__lg(n);~i;i--){
			int nex[M]={0,0,0,0,0};
			fill(nex,nex+m,n+1);
			for(int j=0;j<m;j++){
				for(int k=0;k<m;k++){
					nex[k]=min(nex[k],f[i][a[j][now[j]]][k]);
				}
			}
			auto chk2=[&](){
				for(int j=0;j<m;j++){
					if(nex[j]<=b[j][t])return 1;
				}
				return 0;
			};
			if(!chk2()){
				ans+=1<<i;
				copy(nex,nex+m,now);
			}
		}
		auto chk3=[&](){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					if(f[0][a[i][now[i]]][j]<=b[j][t])return 1;
				}
			}
			return 0;
		};
		printf("%d\n",chk3()?ans+2:-1);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

1
2
5
3

result:

ok 4 number(s): "1 2 5 3"

Test #2:

score: -100
Wrong Answer
time: 71ms
memory: 36608kb

input:

998 5
1 2 6 3 4 5 7 11 8 9 14 12 13 10 15 16 18 19 20 17 23 22 21 26 24 25 27 29 30 28 32 31 34 33 37 38 39 35 40 36 41 43 42 46 44 48 45 47 51 49 50 56 52 57 53 54 55 62 58 59 63 64 60 65 66 61 67 69 70 68 71 73 74 75 72 78 77 79 82 76 81 85 84 80 86 87 83 88 90 91 89 92 96 98 94 95 97 101 93 103 1...

output:

54
-1
1
1
1
14
1
-1
-1
1
13
-1
-1
1
1
1
43
1
1
1
1
-1
1
22
-1
1
1
1
-1
54
1
1
-1
23
19
34
1
1
1
1
1
1
1
1
-1
-1
-1
1
1
106
-1
1
1
80
1
1
-1
1
-1
1
1
1
-1
48
1
1
-1
22
-1
1
1
12
-1
1
1
1
-1
-1
-1
1
18
-1
-1
-1
99
1
-1
-1
1
1
1
31
-1
-1
17
64
-1
1
-1
1
1
2
1
1
1
9
-1
1
-1
1
40
-1
71
-1
1
1
5
1
-1
85
9...

result:

wrong answer 1st numbers differ - expected: '94', found: '54'