E
倍增即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| void solve() { int N; ll K; cin>>N>>K;
vi X(N); rep(N) cin>>X[i],--X[i];
vi A(N); rep(N) cin>>A[i];
vi Y(N); rep(N) Y[i]=i;
while(K){ if(K%2==1){ vi YY(N); rep(N) YY[i]=Y[X[i]]; Y.swap(YY); } vi XX(N); rep(N) XX[i]=X[X[i]]; X.swap(XX); K/=2; }
rep(N) cout<<A[Y[i]]<<" \n"[i+1==N]; }
|
F
哈希
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| void solve() { int n,q; cin>>n>>q;
vc<ull> A(n+1); vc<ull> B(n+1);
rep(n){ int x; cin>>x; A[i+1]=A[i]+hsh[x]; }
rep(n){ int x; cin>>x; B[i+1]=B[i]+hsh[x]; }
rep(q){ int l1,r1,l2,r2; cin>>l1>>r1>>l2>>r2; --l1,--l2; ull t1=A[r1]-A[l1]; ull t2=B[r2]-B[l2]; if(t1==t2){ cout<<"Yes\n"; }else{ cout<<"No\n"; } } }
|
G
https://www.luogu.com.cn/problem/P5577