#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define L 60 /* L = ceil(log2(10^18)) */
int *ej[N], eo[N], *ed[N * 6 + 1], eo_[N * 6 + 1], pp[N], pp_[L + 1][N], ii[N + 1], tt[N], tt_[N], n;
long long cc[N], bb[N], cc_[L + 1][N], k, time;
void init(int n, long long l, long long r) {
int d;
long long a;
for (d = 1; d <= n; d++) {
a = l / d * d;
while (a < l)
a += d;
while (a >= l + d)
a -= d;
while (a <= r)
eo_[a - l]++, a += d;
}
for (a = l; a <= r; a++)
ed[a - l] = (int *) malloc(eo_[a - l] * sizeof *ed[a - l]), eo_[a - l] = 0;
for (d = 1; d <= n; d++) {
a = l / d * d;
while (a < l)
a += d;
while (a >= l + d)
a -= d;
while (a <= r)
ed[a - l][eo_[a - l]++] = d, a += d;
}
}
void upd(int i, int b) {
cc[i] -= b * time, bb[i] += b;
}
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
void dfs1(int i, int t) {
int o;
if (tt_[i] != -1)
return;
tt_[i] = t;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs1(j, t);
}
}
void update(int i, long long k, int x) {
int l;
for (l = L; l >= 0; l--)
if (k >= 1LL << l)
cc_[l][i] += x, k -= 1LL << l, i = pp_[l][i];
}
int qu[N * 2 + 1], h1, t1, t2;
void dfs2(int p, int i, int cnt, int h_, int h0) {
int o, o_;
time++;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
int h0_ = tt[j] != -1 ? cnt : h0, d, a;
qu[cnt] = j;
d = cnt - h1;
upd(qu[cnt - 1 - (k + cnt - t1 - 1) % d], -1), upd(qu[cnt - (k + cnt - t2) % d], 1);
a = (k + cnt - t1) - (k - n * 3);
for (o_ = eo_[a]; o_--; ) {
d = ed[a][o_];
if (d >= cnt - h0 && d <= cnt - h_ && d > cnt - h1)
upd(qu[cnt - d], -1), upd(qu[cnt], 1);
}
a = (k + cnt - t2) - (k - n * 3);
for (o_ = eo_[a]; o_--; ) {
d = ed[a][o_];
if (d >= cnt - h0 && d <= cnt - h_ && d < cnt - h1)
upd(qu[cnt - d], -1), upd(qu[cnt], 1);
}
if (tt[j] == -1) {
d = cnt - h0;
upd(qu[cnt - (k + cnt - (cnt - d + 1 <= h1 ? t1 : t2)) % d], -1);
}
d = cnt - h_ + 1;
upd(qu[cnt - (k + cnt - (cnt - d + 1 <= h1 ? t1 : t2)) % d], 1);
dfs2(i, j, cnt + 1, h_, h0_);
d = cnt - h_ + 1;
upd(qu[cnt - (k + cnt - (cnt - d + 1 <= h1 ? t1 : t2)) % d], -1);
if (tt[j] == -1) {
d = cnt - h0;
upd(qu[cnt - (k + cnt - (cnt - d + 1 <= h1 ? t1 : t2)) % d], 1);
}
d = cnt - h1;
upd(qu[cnt - (k + cnt - t2) % d], -1), upd(qu[cnt - 1 - (k + cnt - t1 - 1) % d], 1);
a = (k + cnt - t1) - (k - n * 3);
for (o_ = eo_[a]; o_--; ) {
d = ed[a][o_];
if (d >= cnt - h0 && d <= cnt - h_ && d > cnt - h1)
upd(qu[cnt], -1), upd(qu[cnt - d], 1);
}
a = (k + cnt - t2) - (k - n * 3);
for (o_ = eo_[a]; o_--; ) {
d = ed[a][o_];
if (d >= cnt - h0 && d <= cnt - h_ && d < cnt - h1)
upd(qu[cnt], -1), upd(qu[cnt - d], 1);
}
}
}
}
int main() {
int g, h, h_, i, i_, j, t, l, cnt, d, o;
long long k_, a;
o = a = 0;
scanf("%d%lld", &n, &k), init(n, k - n * 3, k + n * 3);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (i = 0; i < n; i++) {
scanf("%d", &pp[i]), pp[i]--;
append(pp[i], i);
}
for (i = 0; i < n; i++)
pp_[0][i] = pp[i];
for (l = 1; l <= L; l++)
for (i = 0; i < n; i++)
pp_[l][i] = pp_[l - 1][pp_[l - 1][i]];
memset(tt, -1, n * sizeof *tt), memset(tt_, -1, n * sizeof *tt_);
for (i = 0, t = 0; tt[i] == -1; i = pp[i], t++)
ii[tt[i] = t] = i, dfs1(i, t);
ii[t] = i_ = i;
for (i = 0; i < n; i++)
if (tt_[i] == -1)
tt_[i] = t;
j = i_, k_ = (k - t) % (t - tt[i_]);
while (k_--)
j = pp[j];
for (i = 0; i < n; i++)
if (tt[i] == -1)
cc[j] += n;
for (i = 0; i < n; i++)
update(i, k, 1), update(i, k - tt_[i], -1);
for (l = L; l > 0; l--)
for (i = 0; i < n; i++)
cc_[l - 1][i] += cc_[l][i], cc_[l - 1][pp_[l - 1][i]] += cc_[l][i];
for (i = 0; i < n; i++)
cc[i] += cc_[0][i];
cnt = 0;
for (h = t - 1; h > tt[i_]; h--)
qu[cnt++] = ii[h];
qu[cnt] = i_;
h1 = t - tt[i_] - 1, t1 = tt[qu[h1]] + h1, t2 = tt[qu[cnt]] + cnt, h_ = cnt - (t - tt[i_]) + 1;
for (h = t; h > tt[i_]; h--) {
qu[cnt] = ii[h];
if (h == t)
for (g = cnt; g >= 0; g--)
upd(qu[cnt - (k + cnt - (g <= h1 ? t1 : t2)) % (cnt - g + 1)], 1);
else if (h == t - 1) {
for (d = 1; d <= cnt - h_; d++)
upd(qu[cnt - 1 - (k + cnt - (cnt - d <= h1 ? t1 : t2) - 1) % d], -1);
t2 = tt[qu[cnt]] + cnt, h_++;
for (d = 1, g = cnt; d <= cnt - h_ + 1; d++, g--)
upd(qu[cnt - (k + cnt - (cnt - d + 1 <= h1 ? t1 : t2)) % d], 1);
} else {
h_++;
d = cnt - h1;
upd(qu[cnt - 1 - (k + cnt - t1 - 1) % d], -1), upd(qu[cnt - (k + cnt - t2) % d], 1);
a = (k + cnt - t1) - (k - n * 3);
for (o = eo_[a]; o--; ) {
d = ed[a][o];
if (d <= cnt - h_ + 1 && d > cnt - h1)
upd(qu[cnt - d], -1), upd(qu[cnt], 1);
}
a = (k + cnt - t2) - (k - n * 3);
for (o = eo_[a]; o--; ) {
d = ed[a][o];
if (d <= cnt - h_ + 1 && d < cnt - h1)
upd(qu[cnt - d], -1), upd(qu[cnt], 1);
}
}
dfs2(ii[h - 1], ii[h], cnt + 1, cnt - (t - tt[i_]) + 1, cnt);
cnt++;
}
for (i = 0; i < n; i++)
upd(i, -bb[i]);
for (i = 0; i < n; i++)
printf("%lld\n", cc[i]);
return 0;
}
Compilation message
space_pirate.c: In function 'append':
space_pirate.c:44:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
44 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
space_pirate.c: In function 'main':
space_pirate.c:133:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | scanf("%d%lld", &n, &k), init(n, k - n * 3, k + n * 3);
| ^~~~~~~~~~~~~~~~~~~~~~~
space_pirate.c:137:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | scanf("%d", &pp[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1088 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1088 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
6 ms |
4436 KB |
Output is correct |
16 |
Correct |
7 ms |
4440 KB |
Output is correct |
17 |
Correct |
7 ms |
4308 KB |
Output is correct |
18 |
Correct |
9 ms |
4556 KB |
Output is correct |
19 |
Correct |
8 ms |
4436 KB |
Output is correct |
20 |
Correct |
9 ms |
4580 KB |
Output is correct |
21 |
Correct |
8 ms |
4820 KB |
Output is correct |
22 |
Correct |
7 ms |
4700 KB |
Output is correct |
23 |
Correct |
6 ms |
4936 KB |
Output is correct |
24 |
Correct |
7 ms |
4692 KB |
Output is correct |
25 |
Correct |
6 ms |
4436 KB |
Output is correct |
26 |
Correct |
7 ms |
4640 KB |
Output is correct |
27 |
Correct |
12 ms |
4564 KB |
Output is correct |
28 |
Correct |
10 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
689 ms |
123668 KB |
Output is correct |
2 |
Correct |
566 ms |
131148 KB |
Output is correct |
3 |
Correct |
687 ms |
127324 KB |
Output is correct |
4 |
Correct |
614 ms |
123756 KB |
Output is correct |
5 |
Correct |
634 ms |
131192 KB |
Output is correct |
6 |
Correct |
635 ms |
127144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
1088 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
6 ms |
4436 KB |
Output is correct |
16 |
Correct |
7 ms |
4440 KB |
Output is correct |
17 |
Correct |
7 ms |
4308 KB |
Output is correct |
18 |
Correct |
9 ms |
4556 KB |
Output is correct |
19 |
Correct |
8 ms |
4436 KB |
Output is correct |
20 |
Correct |
9 ms |
4580 KB |
Output is correct |
21 |
Correct |
8 ms |
4820 KB |
Output is correct |
22 |
Correct |
7 ms |
4700 KB |
Output is correct |
23 |
Correct |
6 ms |
4936 KB |
Output is correct |
24 |
Correct |
7 ms |
4692 KB |
Output is correct |
25 |
Correct |
6 ms |
4436 KB |
Output is correct |
26 |
Correct |
7 ms |
4640 KB |
Output is correct |
27 |
Correct |
12 ms |
4564 KB |
Output is correct |
28 |
Correct |
10 ms |
4436 KB |
Output is correct |
29 |
Correct |
689 ms |
123668 KB |
Output is correct |
30 |
Correct |
566 ms |
131148 KB |
Output is correct |
31 |
Correct |
687 ms |
127324 KB |
Output is correct |
32 |
Correct |
614 ms |
123756 KB |
Output is correct |
33 |
Correct |
634 ms |
131192 KB |
Output is correct |
34 |
Correct |
635 ms |
127144 KB |
Output is correct |
35 |
Correct |
461 ms |
123536 KB |
Output is correct |
36 |
Correct |
391 ms |
123492 KB |
Output is correct |
37 |
Correct |
347 ms |
123956 KB |
Output is correct |
38 |
Correct |
728 ms |
130184 KB |
Output is correct |
39 |
Correct |
673 ms |
127840 KB |
Output is correct |
40 |
Correct |
731 ms |
133592 KB |
Output is correct |
41 |
Correct |
701 ms |
131268 KB |
Output is correct |
42 |
Correct |
749 ms |
132908 KB |
Output is correct |
43 |
Correct |
352 ms |
140584 KB |
Output is correct |
44 |
Correct |
356 ms |
133152 KB |
Output is correct |
45 |
Correct |
362 ms |
124016 KB |
Output is correct |
46 |
Correct |
604 ms |
131088 KB |
Output is correct |
47 |
Correct |
1459 ms |
129980 KB |
Output is correct |
48 |
Correct |
957 ms |
126932 KB |
Output is correct |