//by yjz
#include<bits/stdc++.h>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
typedef long long ll;
using namespace std;
const int maxn = 100111;
int n, a[maxn], L;
ll K;
int go[60][maxn];
ll ans[maxn];
vector<int> route;
int rid[maxn], T;
bool on_route[maxn];
ll lz[60][maxn];
void add(int x, ll m, ll v)
{
for (int i=59; i>=0; i--)
{
if (m>=(1ll<<i))
{
lz[i][x] += v;
m -= 1ll<<i;
x = go[i][x];
}
}
assert(m==0);
}
void add_lazy(int x, ll l, ll r, ll v)
{
// cerr<<"add_lazy:"<<x<<" "<<l<<","<<r<<" "<<v<<endl;
add(x, r, v);
add(x, l-1, -v);
}
void dfs(int x, ll l)
{
if (!l||on_route[x]) return;
on_route[x] = true;
rid[x] = route.size();
route.PB(x);
dfs(a[x], l-1);
}
void after_work()
{
for (int i=59; i>=0; i--)
{
for (int x=1; x<=n; x++)
{
if (i>0)
{
lz[i-1][x] += lz[i][x];
lz[i-1][go[i-1][x]] += lz[i][x];
}
else
{
ans[x] += lz[0][x];
}
}
}
}
int main()
{
scanf("%d%lld", &n, &K);
for (int i=1; i<=n; i++) scanf("%d", &a[i]), go[0][i] = a[i];
dfs(1, K);
for (int i=1; i<60; i++) for (int j=1; j<=n; j++) go[i][j] = go[i-1][go[i-1][j]];
T = 1;
for (int i=0; i<60; i++) if ((K>>i)&1) T = go[i][T];
L = route.size();
//a is not in route
{
int cnt = 0;
for (int i=1; i<=n; i++) cnt += !on_route[i];
ans[T] += 1ll*cnt*n;
}
// for (int i=1; i<=n; i++) cerr<<ans[i]<<" "; cerr<<endl;
//a is in route, b is not in route
{
for (int i=1; i<=n; i++)
{
if (!on_route[i])
{
add_lazy(i, K-L, K-1, 1);
}
}
}
//a is in route, b is in route
{
for (int i=1; i<=L; i++)
{
int c = K%i;
int lst = i-2;
for (int k=0; k*i+c<L; k++)
{
int r = min(L-1, k*i+i-1+c);
ans[route[k*i+c]] += r-lst;
lst = r;
}
/*
for (int j=i-1; j<L; j++)
{
ans[route[((j-c)/i)*i+c]]++;
}
*/
}
for (int i=0; i<L; i++)
{
int c = K%(L-i);
ans[route[c]] += max(0, L-(c+i+1));
ans[route[c+i]] += L-i-1-max(0, L-(c+i+1));
}
}
// for (int i=1; i<=n; i++) cerr<<ans[i]<<" "; cerr<<endl;
after_work();
// for (int i=1; i<=n; i++) cerr<<ans[i]<<" "; cerr<<endl;
for (int i=1; i<=n; i++) printf("%lld\n", ans[i]);
return 0;
}
/*
5 7
5 1 4 3 2
7 10
2 3 4 1 6 7 5
*/
Compilation message
space_pirate.cpp: In function 'int main()':
space_pirate.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%lld", &n, &K);
~~~~~^~~~~~~~~~~~~~~~~~
space_pirate.cpp:66:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i=1; i<=n; i++) scanf("%d", &a[i]), go[0][i] = a[i];
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
516 ms |
72156 KB |
Output is correct |
2 |
Correct |
146 ms |
75908 KB |
Output is correct |
3 |
Correct |
352 ms |
74284 KB |
Output is correct |
4 |
Correct |
448 ms |
72912 KB |
Output is correct |
5 |
Correct |
142 ms |
75888 KB |
Output is correct |
6 |
Correct |
326 ms |
74100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |