This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define pii pair<int,int>
#define vi vector<int>
#define fi first
#define se second
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define ALL(x) x.begin(),x.end()
#define sz(x) int(x.size())
#define ll long long
using namespace std;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
const int N=4e6+5,P = 1e9 + 7;
int n,q,c[N],tot,ans[N],now[N],lst[N];
vector <int> G[N];
int main(){
n = read(),q = read();
rep(i,1,n){
c[i] = read();
rep(j,1,c[i]){
int x = read();
G[i].pb(x);
}
}
int u = 1;
rep(i,1,4e6){
ans[++tot] = u;
now[u] = (now[u] + 1) % c[u];
u = G[u][now[u]];
}
int T = n,S = 1;
memset(lst,0x3f,sizeof(lst));
rep(i,1,tot){
if(T < i - lst[ans[i]]){
S = lst[ans[i]];
T = i - lst[ans[i]];
}
lst[ans[i]] = i;
}
while(q--){
ll k = read() + 1;
if(k <= tot)cout << ans[k] << '\n';
else{
k = (k - S) % T + S;
cout << ans[k] << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |