QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#391745 | #5116. Contests | Nahidameow | WA | 2ms | 6040kb | C++20 | 2.1kb | 2024-04-16 19:00:04 | 2024-04-16 19:00:05 |
Judging History
answer
#include<bits/stdc++.h>
#define pd push_back
#define all(A) A.begin(),A.end()
#define lower_bound lb
#define ve std::vector
typedef long long ll;
typedef long long ll;
typedef __int128 Int;
typedef unsigned long long ul;
typedef long double LD;
bool FileIfstream(std::string name){
std::ifstream f(name.c_str());
return f.good();
}
namespace Math{
ll QP(ll x,ll y,ll mod){ll ans=1;for(;y;y>>=1,x=x*x%mod)if(y&1)ans=ans*x%mod;return ans;}
ll inv(ll x,ll mod){return QP(x,mod-2,mod);}
}
const int N=1e5+10;
const int mod=998244353;
int f[N][23][6];
void solve(){
//don't forget to open long long
int n,m;std::cin>>n>>m;
ve<ve<int>>v(m+1,ve<int>(n+1));
ve<ve<int>>pos(m+1,ve<int>(n+1));
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
std::cin>>v[i][j],pos[i][v[i][j]]=j;
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++)
f[j][0][i]=pos[i][j];
for(int j=1;j<=m;j++){
int S=n+1;
for(int k=n;k>=1;k--)
S=std::min(S,pos[i][v[j][k]]),
f[v[j][k]][0][i]=std::min(f[v[j][k]][0][i],S);
}
}
for(int k=1;k<=20;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
f[i][k][j]=f[i][k-1][j];
for(int S=1;S<=m;S++)
f[i][k][j]=std::min(f[i][k][j],f[v[S][f[i][k-1][S]]][k-1][j]);
}
int q;std::cin>>q;
while(q--){
int S,T;std::cin>>S>>T;
std::array<int,6>to;
for(int i=1;i<=m;i++)
if((to[i]=pos[i][S])<pos[i][T]){
std::cout<<1<<'\n';
goto flg;
}
{
int ans=1;
for(int k=2;~k;k--){
std::array<int,6>P=to;
for(int i=1;i<=m;i++){
for(int j=1;j<=m;j++)
P[i]=std::min(P[i],f[v[j][to[j]]][k][i]);
if(P[i]<=pos[i][T])goto tflg;
}
ans+=(1<<k);
to=P;
tflg:;
}
std::cout<<(ans>=5e5?-1:ans+1)<<'\n';
}
flg:;
}
}
int main(){
#ifndef ONLINE_JUDGE
if(!FileIfstream("IO.in")){
freopen("IO.in","w",stdout);
return 0;
}
freopen("IO.in","r",stdin);
freopen("IO.out","w",stdout);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int T=1;
//std::cin>>T;
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3768kb
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: 2ms
memory: 6040kb
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:
9 9 1 1 1 9 1 9 9 1 9 9 9 1 1 1 9 1 1 1 1 9 1 9 9 1 1 1 9 9 1 1 9 9 9 9 1 1 1 1 1 1 1 1 9 9 9 1 1 9 9 1 1 9 1 1 9 1 9 1 1 1 4 9 1 1 9 9 9 1 1 9 9 1 1 1 9 9 9 1 9 9 9 9 9 1 9 9 1 1 1 9 9 9 9 9 9 1 9 1 1 2 1 1 1 9 9 1 9 1 9 9 9 9 1 1 6 1 9 9 9 1 1 9 1 9 9 9 9 1 1 5 1 9 1 9 1 9 9 9 1 1 1 9 9 9 9 1 1 9 ...
result:
wrong answer 1st numbers differ - expected: '94', found: '9'