QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376348#5116. ContestsTx_LcyTL 12ms169792kbC++141.7kb2024-04-04 08:21:452024-04-04 08:21:45

Judging History

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

  • [2024-04-04 08:21:45]
  • 评测
  • 测评结果:TL
  • 用时:12ms
  • 内存:169792kb
  • [2024-04-04 08:21:45]
  • 提交

answer

//A tree without skin will surely die.
//A man without face will be alive.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define all(x) (x).begin(),(x).end()
#define rep(i,j,k) for (int i=j;i<=k;++i)
#define per(i,j,k) for (int i=j;i>=k;--i)
int const N=1e5+10;
int n,m,q,a[10][N],dp1[20],dp2[20],nxt[N][10][20],rk[10][N];
inline void solve(){
	cin>>n>>m;
	memset(nxt,0x3f,sizeof(nxt));
	rep(i,1,m) rep(j,1,n)
		cin>>a[i][j],rk[i][a[i][j]]=j;
	rep(i,1,n) rep(p,1,m) rep(j,1,m){
		int k=1e18;
		rep(s,rk[p][i],n) k=min(k,rk[j][a[p][s]]);
		nxt[i][j][0]=min(nxt[i][j][0],k);
	}
	//nxt[i][j][k] i 开始,跳到序列 j,用了 2^k 步的最小排名
	rep(j,1,19) rep(p,1,m) rep(i,1,n) rep(la,1,m)
		nxt[i][p][j]=min(nxt[i][p][j],nxt[a[la][nxt[i][la][j-1]]][p][j-1]);
	// rep(i,1,n) rep(j,1,m) rep(k,0,19)
		// if (nxt[i][j][k]<(1e17))
			// cerr<<i<<' '<<j<<' '<<k<<' '<<nxt[i][j][k]<<" wenhao?\n";
	cin>>q;
	while (q--){
		int x,y,ans=0;cin>>x>>y;
		rep(i,1,m) dp1[i]=rk[i][x];
		rep(i,1,m)
			if (rk[i][x]<=rk[i][y]){ans=1;break;}
		if (ans==1){cout<<"1\n";continue;}
		per(p,19,0){
			rep(i,1,m) dp2[i]=dp1[i];
			rep(i,1,m) rep(j,1,m)
				dp2[j]=min(dp2[j],nxt[a[i][dp1[i]]][j][p]);
			int tag=0;
			rep(i,1,m)
				if (dp2[i]<=rk[i][y]){tag=1;break;}
			if (!tag){
				ans+=1<<p;
				rep(i,1,m) dp1[i]=dp2[i];
			}
		}
		if (ans==(1<<20)-1) cout<<"-1\n";
		else cout<<ans+2<<'\n';
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t=1;
	// cin>>t;
	while (t--) solve();
	cerr<<"Time: "<<(double)clock()/CLOCKS_PER_SEC<<" s\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 163720kb

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: 0
Accepted
time: 12ms
memory: 169792kb

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:

94
-1
1
1
1
21
1
153
-1
1
19
-1
25
1
1
1
80
1
1
1
1
-1
1
36
102
1
1
1
30
92
1
1
-1
37
36
60
1
1
1
1
1
1
1
1
16
-1
-1
1
1
-1
-1
1
1
140
1
1
77
1
60
1
1
1
4
90
1
1
-1
36
93
1
1
17
148
1
1
1
136
-1
-1
1
28
-1
-1
32
180
1
-1
-1
1
1
1
56
-1
75
25
115
122
1
-1
1
1
2
1
1
1
11
-1
1
-1
1
76
-1
127
-1
1
1
6
1...

result:

ok 1000 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

99997 5
13846 76247 14430 82900 22678 13000 88476 33335 50407 77862 3545 90269 94531 98082 56502 14027 27673 54173 42855 59901 40095 36046 32844 88104 38879 84883 45979 3117 80875 18985 43692 81119 8682 6390 99780 18979 73738 26817 37612 38509 34976 81565 9953 33844 61610 17678 38492 57218 96547 157...

output:


result: