显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int re()
{
int f=1,num=0;
char c=getchar();
while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') num=num*10+c-'0',c=getchar();
return num*f;
}
const int N=100010;
int f[N][40],s[N][40],minn[N][40];
int n,k,last,anss,ansm;
void getans(int x)
{
last=k;anss=0;ansm=LONG_LONG_MAX;
for(int i=38;i>=0;i--) if((1ll<<i)<=last)
{
anss+=s[x][i];
ansm=min(ansm,minn[x][i]);
x=f[x][i];last-=(1ll<<i);
}
printf("%lld %lld\n",anss,ansm);
return;
}
signed main()
{
freopen("pathsjump.in","r",stdin);
freopen("pathsjump.out","w",stdout);
n=re();k=re();
for(int i=1;i<=n;i++) f[i][0]=re();
for(int i=1;i<=n;i++) s[i][0]=minn[i][0]=re();
for(int i=1;i<=39;i++) for(int j=1;j<=n;j++)
{
f[j][i]=f[f[j][i-1]][i-1];
s[j][i]=s[j][i-1]+s[f[j][i-1]][i-1];
minn[j][i]=min(minn[j][i-1],minn[f[j][i-1]][i-1]);
}
for(int i=1;i<=n;i++) getans(i);
return 0;
}