There are times you recall a good old friend and everything you've come through together. Luckily there are social networks — they store all your message history making it easy to know what you argued over 10 years ago.

More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to n where n is the total number of messages in the chat.

Each message might contain a link to an earlier message which it is a reply to. When opening a message x or getting a link to it, the dialogue is shown in such a way that k previous messages, message x and k next messages are visible (with respect to message x). In case there are less than k messages somewhere, they are yet all shown.

Digging deep into your message history, you always read all visible messages and then go by the link in the current message x (if there is one) and continue reading in the same manner.

Determine the number of messages you'll read if your start from message number t for all t from 1 to n. Calculate these numbers independently. If you start with message x, the initial configuration is x itself, k previous and k next messages. Messages read multiple times are considered as one.


-----Input-----

The first line contains two integers n and k (1 ≤ n ≤ 10^5, 0 ≤ k ≤ n) — the total amount of messages and the number of previous and next messages visible.

The second line features a sequence of integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} < i), where a_{i} denotes the i-th message link destination or zero, if there's no link from i. All messages are listed in chronological order. It's guaranteed that the link from message x goes to message with number strictly less than x.


-----Output-----

Print n integers with i-th denoting the number of distinct messages you can read starting from message i and traversing the links while possible.


-----Examples-----
Input
6 0
0 1 1 2 3 2

Output
1 2 2 3 3 3 

Input
10 1
0 1 0 3 4 5 2 3 7 0

Output
2 3 3 4 5 6 6 6 8 2 

Input
2 2
0 1

Output
2 2 



-----Note-----

Consider i = 6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.

In the second sample case i = 6 gives you messages 5, 6, 7 since k = 1, then 4, 5, 6, then 2, 3, 4 and then the link sequence breaks. The number of distinct messages here is equal to 6.

Difficulty: MEDIUM

Solutions:
(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = [min(i - 1, k) + 1 + min(n - i, k) for i in range(1, n + 1)]
for i in range(1, n + 1):
	a_ = a[i - 1]
	if a_ != 0:
		(l, r) = sorted([i, a_])
		d1 = ans[a_ - 1]
		d2 = max(0, l + 1 + min(n - l, k) - (r - min(r - 1, k)))
		delta = d1 - d2
		ans[i - 1] += delta
print(' '.join(map(str, ans)))


(n, k) = map(int, input().split())
li = [0]
li += list(map(int, input().split()))
ans = [0] * (n + 1)
for i in range(1, n + 1):
	if li[i] == 0:
		ans[i] = min(n, i + k) - max(1, i - k) + 1
	elif li[i] + k >= n:
		ans[i] = ans[li[i]]
	else:
		ans[i] = ans[li[i]] + min(min(n, i + k) - min(n, li[i] + k + 1) + 1, min(n, i + k) - max(1, i - k) + 1)
print(*ans[1:], sep=' ')


(n, k) = map(int, input().split())
s = list(map(int, input().split()))
ans = [[0] for i in range(n)]
for i in range(len(s)):
	s[i] -= 1
for i in range(n):
	l = max(0, i - k)
	r = min(n - 1, i + k)
	if s[i] == -1:
		ans[i] = [r - l + 1, r]
	else:
		ans[i] = [ans[s[i]][0] + (r - l + 1) - max(0, ans[s[i]][1] - l + 1), r]
for j in range(n):
	print(ans[j][0], end=' ')


from math import *
from random import *
from copy import *
import os, sys
(n, k) = list(map(int, input().split()))
A = list(map(int, input().split()))
res = [0] * (n + 1)
for i in range(1, n + 1):
	prev = A[i - 1]
	res[i] = res[prev]
	prev = prev - k if prev == 0 else prev
	res[i] += min(max(0, i - prev - k - 1), k) + max(0, min(k + 1, n - i + 1) - max(0, min(n - prev + 1, prev + k - i + 1)))
print(' '.join(list(map(str, res[1:]))))


def read_line():
	return input()

def add_new_messages(current, link, k, n):
	minimal = link + k if link != 0 else 0
	start = current - k - 1 if current - k > 0 else 0
	end = current + k if current + k < n else n
	new_messages = end - (start if start > minimal else minimal)
	return max(new_messages, 0)
[n, k] = map(int, read_line().split())
result = []
index = 0
for message in read_line().split():
	message = int(message)
	answer = add_new_messages(index + 1, message, k, n)
	if message != 0:
		answer += result[message - 1]
	result.append(answer)
	index += 1
	print(answer, end=' ')


(n, k) = map(int, input().split())
a = list(map(int, input().split()))

def len_line(q, b, x):
	if q > x:
		return 0
	return min(b + 1, x) - max(1, q + 1) + 1
dp = [0] * n
for i in range(n):
	if a[i] == 0:
		dp[i] = len_line(i - k, i + k, n)
	else:
		dp[i] = dp[a[i] - 1] + len_line(max(a[i] + k, i - k), i + k, n)
print(*dp)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a[0] = (1 + min(k, n - 1), min(k, n - 1))
for i in range(1, n):
	p = a[i] - 1
	if p == -1:
		a[i] = (1 + min(k, i) + min(k, n - i - 1), min(k + i, n - 1))
		continue
	(ans, l) = a[p]
	ans += 1 + min(k, i) + min(k, n - i - 1)
	if l >= max(i - k, 0):
		ans -= l - max(i - k, 0) + 1
	a[i] = (ans, min(k + i, n - 1))
for i in range(n):
	print(a[i][0], end=' ')


(n, k) = map(int, input().split())
ans = [0] * n
a = list(map(int, input().split()))
for i in range(n):
	if a[i] == 0:
		ans[i] = min(i, k) + min(n - i - 1, k) + 1
	else:
		prev = a[i] - 1
		right = min(n - prev - 1, k)
		ans[i] = 1 + min(n - i - 1, k) + ans[prev]
		if i - prev > 2 * k:
			ans[i] += k
		elif i - prev > k:
			totspace = i - prev - 1
			ans[i] += totspace - k
		else:
			ans[i] -= right - (i - prev - 1)
print(*ans)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [min(1 + k, n)] * n
for i in range(1, n):
	x = a[i] - 1
	if x == -1:
		b[i] = min(i, k) + 1 + min(n - i - 1, k)
	else:
		b[i] = b[x] + min(k + 1, i - (x + min(k, n - x - 1))) + min(n - i - 1, k)
print(*b)


(n, k) = map(int, input().split())
lst = []
for x in input().split():
	lst.append(int(x) - 1)
array = []
for x in range(len(lst)):
	if lst[x] == -1:
		t = (min(n, x + k + 1) - max(0, x - k), min(n, x + k + 1))
	else:
		t = (array[lst[x]][0] + min(min(n, x + k + 1) - array[lst[x]][1], 1 + min(n - x - 1, k) + min(x + 1, k)), min(n, x + k + 1))
	array.append(t)
for (c, r) in array:
	print(c, end=' ')


(n, k) = (int(s) for s in input().split())
a = [0] + [int(s) for s in input().split()]
ans = [0]
for i in range(1, n + 1):
	if not a[i]:
		ans.append(min(i - 1, k) + 1 + min(n - i, k))
	elif a[i] + k < i - k:
		ans.append(min(i - 1, k) + 1 + min(n - i, k) + ans[a[i]])
	else:
		ans.append(ans[a[i]] + min(n, i + k) - a[i] - min(k, n - a[i]))
print(*ans[1:])


(n, k) = map(int, input().split())
s = list(map(int, input().split()))

def len_otr(s, f):
	global n
	(s, f) = (s + 1, f + 1)
	if s > n:
		return 0
	s = max(1, s)
	f = min(f, n)
	return f - s + 1
dp = [0] * n
for i in range(n):
	if s[i] == 0:
		dp[i] = len_otr(i - k, i + k)
	else:
		ind = s[i] - 1
		dp[i] = dp[ind] + len_otr(max(ind + k + 1, i - k), i + k)
print(*dp)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
a = [i - 1 for i in a]
ans = []
for i in range(n):
	if a[i] == -1:
		ans.append(1 + min(n - i - 1, k) + min(i, k))
	else:
		ref = a[i]
		r1 = min(ref + k, n - 1)
		r2 = min(i + k, n - 1)
		l2 = max(i - k, 0)
		if l2 > r1:
			ans.append(ans[ref] + (r2 - l2 + 1))
		else:
			ans.append(ans[ref] + (r2 - r1))
print(' '.join([str(i) for i in ans]))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = [0] * n
for i in range(n):
	if a[i] == 0:
		b[i] = min(k, i)
for i in range(n):
	if a[i] != 0:
		b[i] = b[a[i] - 1] + min(2 * k, i - a[i]) + 1
ans = [0] * n
for i in range(n):
	ans[i] = b[i] + min(k, n - i - 1) + 1
print(*ans)


(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
a.insert(0, 0)
dp = [0 for _ in range(n + 1)]
for i in range(1, n + 1):
	p = a[i]
	if p == 0:
		start = max(1, i - k)
		end = min(n, i + k)
		dp[i] = end - start + 1
	else:
		dp[i] = dp[p]
		start = max(p + k + 1, i - k)
		end = min(n, i + k)
		dp[i] += max(0, end - start + 1)
for i in range(1, n + 1):
	print(dp[i], end=' ')
print()


(n, k) = list(map(int, input().split()))
answer = [None] * n
for (i, ref) in enumerate(map(int, input().split())):
	ref -= 1
	max_cur_page = min(i + k, n - 1)
	min_cur_page = max(i - k, 0)
	answer[i] = max_cur_page - min_cur_page + 1
	if ref >= 0:
		max_ref_page = min(ref + k, n - 1)
		answer[i] += answer[ref] - max(0, max_ref_page - min_cur_page + 1)
print(' '.join(map(str, answer)))


from os import path
import sys, time
mod = int(1000000000.0 + 7)
from math import ceil, floor, gcd, log, log2, factorial, sqrt
from collections import defaultdict, Counter, OrderedDict, deque
from itertools import combinations, permutations
from string import ascii_lowercase, ascii_uppercase
from bisect import *
from functools import reduce
from operator import mul
maxx = float('inf')
I = lambda : int(sys.stdin.buffer.readline())
lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda : sys.stdin.readline().strip('\n')
grid = lambda r: [lint() for i in range(r)]
localsys = 0
start_time = time.time()
nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r)

def ceill(n, x):
	return (n + x - 1) // x
T = 0

def solve():
	(n, k) = lint()
	(a, ans) = (lint(), [0] * n)
	for (i, e) in enumerate(a):
		e -= 1
		(l, r) = (max(0, i - k), min(n - 1, i + k))
		if e >= 0:
			ans[i] += ans[e]
			l = max(l, min(n - 1, e + k) + 1)
		ans[i] += r - l + 1
	print(*ans)

def run():
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
run()
T = I() if T else 1
for _ in range(T):
	solve()
if localsys:
	print('\n\nTime Elased :', time.time() - start_time, 'seconds')


(n, k) = map(int, input().split(' '))
l = list(map(int, input().split(' ')))
dp = []
for (i, x) in enumerate(l):
	if x == 0:
		z = min(i + k + 1, n) - max(0, i - k)
	else:
		z = dp[x - 1] + max(0, min(i + k + 1, n) - max(i - k, x + k))
	dp.append(z)
print(*dp)


def solve(n, k, links):
	ans = [0] * n
	for t in range(n):
		prevLink = -1
		currLink = t
		while currLink != -1:
			prevMin = max(0, prevLink - k)
			currMin = max(0, currLink - k)
			currMax = min(n - 1, currLink + k)
			if ans[currLink] == 0:
				ans[t] += currMax - currMin + 1
				if prevLink != -1 and prevMin <= currMax:
					ans[t] -= currMax - prevMin + 1
				prevLink = currLink
				currLink = links[currLink]
			else:
				ans[t] += ans[currLink]
				if prevLink != -1 and prevMin <= currMax:
					ans[t] -= currMax - prevMin + 1
				break
	return ' '.join([str(x) for x in ans])

def main():
	(n, k) = map(int, input().strip().split())
	links = [int(x) - 1 for x in input().strip().split()]
	print(solve(n, k, links))
main()


(n, k) = list(map(int, input().split()))
a = list(map(int, ('0 ' + input()).split()))
clv = {}

def getcnt(x):
	global c, a, clv
	if x in clv:
		return clv[x]
	if a[x] == 0:
		return min(k, x - 1) + 1 + min(k, n - x)
	else:
		per = min(a[x] + k, n) - max(1, x - k) + 1
		if per < 0:
			per = 0
		retclv = min(k, x - 1) + 1 + min(k, n - x) + getcnt(a[x]) - per
		clv[x] = retclv
		return retclv
for i in range(1, n + 1):
	print(getcnt(i), end=' ')


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
dp = [0 for i in range(n)]
for i in range(n):
	if l[i] == 0:
		dp[i] = min(i, k) + min(n - i - 1, k) + 1
	else:
		dp[i] = min(i, k) + 1 + min(n - i - 1, k) + dp[l[i] - 1]
		if i - l[i] < 2 * k + 1:
			t1 = max(i - k + 1, 1)
			t2 = min(i + k + 1, n)
			t11 = max(l[i] - k, 1)
			t12 = min(l[i] + k, n)
			dp[i] = dp[i] - (t12 - t1 + 1)
print(*dp)


intln = lambda : [int(s) for s in input().split()]
floatln = lambda : [float(s) for s in input().split()]

def printarr(a):
	s = ''
	for i in a:
		s += str(i) + ' '
	print(s)
(n, k) = intln()
a = intln()
b = [0] * n
for i in range(n):
	a[i] -= 1
for i in range(n):
	b[i] += 1
	if i < k:
		b[i] += i
	else:
		b[i] += k
	if n - i - 1 < k:
		b[i] += n - i - 1
	else:
		b[i] += k
	if a[i] != -1:
		b[i] += b[a[i]]
		if i - a[i] < 2 * k + 1:
			b[i] -= 2 * k + 1 - (i - a[i])
		b[i] += abs(min(0, i - k))
		b[i] += max(a[i] + k - (n - 1), 0)
printarr(b)


(n, k) = map(int, input().split())
a = map(int, input().split())
b = [0] * n
for (i, p) in enumerate(a):
	cur_start = max(0, i - k)
	cur_end = min(n - 1, i + k)
	if p == 0:
		b[i] = cur_end - cur_start + 1
	else:
		p -= 1
		prev_end = min(n - 1, p + k)
		if cur_start > prev_end:
			b[i] = b[p] + cur_end - cur_start + 1
		else:
			b[i] = b[p] + cur_end - prev_end
print(' '.join(map(str, b)))


(q, w) = map(int, input().split())
a = [0] + list(map(int, input().split()))
s = [0 for i in range(0, q + 1)]

def f(q, w, pos):
	d = 1
	if w >= pos:
		d += pos - 1
	else:
		d += w
	if pos >= q - w:
		d += q - pos
	else:
		d += w
	return d

def g(q, w, i, j):
	ans = w * 2 - j + i + 1
	if j - w <= 0:
		ans -= w - j + 1
	if i + w > q:
		ans -= i + w - q
	return ans
for i in range(1, q + 1):
	if a[i] == 0:
		s[i] = f(q, w, i)
	else:
		t = i - a[i]
		if t > 2 * w:
			s[i] = f(q, w, i) + s[a[i]]
		else:
			s[i] = s[a[i]] + f(q, w, i) - g(q, w, a[i], i)
print(*s[1:])


def inter(s11, s12, s21, s22):
	return max(0, min(s12, s22) - max(s11, s21) + 1)
(n, k) = map(int, input().split())
ai = list(map(int, input().split()))
anss = [0] * n
for i in range(n):
	if ai[i] == 0:
		s11 = 0
		s12 = n - 1
		s21 = i - k
		s22 = i + k
		anss[i] = inter(s11, s12, s21, s22)
	else:
		num = ai[i] - 1
		s11 = num - k
		s12 = num + k
		s21 = i - k
		s22 = i + k
		temp = inter(s11, s12, s21, s22)
		s11 = n
		s12 = 2 * n
		s21 = i - k
		s22 = i + k
		temp2 = inter(s11, s12, s21, s22)
		s11 = num - k
		s12 = num + k
		s21 = n
		s22 = 2 * n
		temp3 = inter(s11, s12, s21, s22)
		s11 = -n
		s12 = 0
		s21 = i - k
		s22 = i + k
		temp4 = inter(s11, s12, s21, s22)
		anss[i] = anss[num] + 2 * k + 1 - temp - temp2 + temp3
print(' '.join(map(str, anss)))


def s(x, y):
	global k, n
	if x - y - 1 >= 2 * k:
		return k + 1 + min(k, n - x - 1)
	elif x - y - 1 >= k:
		return min(n - x - 1, k) + x - y - k
	else:
		return min(n - 1, x + k) - min(n - 1, y + k)

def search(i):
	global a, n, k, ans
	if a[i] == -1:
		ans[i] = min(k, i) + min(k, n - i - 1) + 1
		return min(k, i) + min(k, n - i - 1) + 1
	elif ans[i] == 0:
		ans[i] = search(a[i]) + s(i, a[i])
		return search(a[i]) + s(i, a[i])
	else:
		return ans[i]
(n, k) = input().split()
n = int(n)
k = int(k)
ans = []
a = input().split()
for i in range(n):
	a[i] = int(a[i]) - 1
	ans.append(0)
for i in range(n):
	if ans[i] == 0:
		ans[i] = search(i)
for i in range(len(ans)):
	print(ans[i], end=' ')


def solve():
	(n, k) = (int(x) for x in input().split())
	idx___parent = [int(x) - 1 for x in input().split()]
	idx___l_cl = [max(0, idx - k) for idx in range(n)]
	idx___r_op = [min(n, idx + k + 1) for idx in range(n)]
	idx___message_count = []
	for idx in range(n):
		curr_l_cl = idx___l_cl[idx]
		curr_r_op = idx___r_op[idx]
		if idx___parent[idx] == -1:
			idx___message_count.append(curr_r_op - curr_l_cl)
			continue
		parent = idx___parent[idx]
		prev_r_op = idx___r_op[parent]
		curr_message_count = idx___message_count[parent]
		if prev_r_op > curr_l_cl:
			curr_l_cl = prev_r_op
		curr_message_count += curr_r_op - curr_l_cl
		idx___message_count.append(curr_message_count)
	print(*idx___message_count)
t = 1
for _ in range(t):
	solve()


def count_unic_mess(crt, display, array, n, result_arr):
	low_bound = 0 if crt - display < 0 else crt - display
	high_bound = n if crt + display + 1 > n else crt + display + 1
	result = high_bound - low_bound
	if array[crt] == 0:
		result_arr.append(result)
		return result
	ref = array[crt] - 1
	high_bound_ref = n if ref + display + 1 > n else ref + display + 1
	if high_bound_ref >= low_bound:
		result += result_arr[ref] - high_bound_ref + low_bound
	else:
		result += result_arr[ref]
	result_arr.append(result)
	return result
(n, k) = map(int, input().split(' '))
links = [int(x) for x in input().split(' ')]
count_arr = []
for i in range(n):
	print(count_unic_mess(i, k, links, n, count_arr), end=' ')


(n, k) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
res = []
for (i, el) in enumerate(a, 1):
	r = min(n, i + k) - max(1, i - k) + 1
	if el != 0:
		r += res[el - 1]
		rb = max(1, i - k)
		lb = min(n, el + k)
		if rb <= lb:
			r -= lb - rb + 1
	res.append(r)
for el in res:
	print(el, end=' ')


(n, k) = map(int, input().split())
k = min(k, n - 1)
a = list(map(lambda x: int(x) - 1, input().split()))
kek = [0 for i in range(n)]
ans = [0 for i in range(n)]
for i in range(n):
	if a[i] == -1:
		cur_l = max(0, i - k)
		cur_r = min(i + k, n - 1)
		ans[i] = i - cur_l + (cur_r - i) + 1
	else:
		x = ans[a[i]]
		d = i - a[i] - 1
		if d >= 2 * k:
			x += min(k, i) + min(k, n - i - 1) + 1
		else:
			o = 0
			cur_l = max(0, i - k)
			cur_r = min(i + k, n - 1)
			his_l = min(0, a[i] - k)
			his_r = min(a[i] + k, n - 1)
			if 2 * k >= d >= k:
				o = d - 2 * k
			elif d == k - 1:
				o = -d - 2
			else:
				o = -(his_r - a[i] + 1) - (a[i] - cur_l)
			x += i - cur_l + (cur_r - i) + 1 + o
		ans[i] = x
print(' '.join(map(str, ans)))


(n, k) = map(int, input().split())
a = list(map(lambda x: int(x) - 1, input().split()))
dp = [0] * n
for i in range(n):
	if a[i] == -1:
		dp[i] = min(i, k) + min(n - i - 1, k) + 1
	else:
		dp[i] = dp[a[i]] - min(n - a[i] - 1, k) + min(n - i - 1, k) + min(2 * k, i - a[i] - 1) + 1
print(' '.join(list(map(str, dp))))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
dp = [0] * n
a = [c - 1 for c in a]
for i in range(0, n):
	if a[i] == -1:
		dp[i] = 1 + min(k, n - i - 1) + min(k, i)
	else:
		dp[i] = 1 + min(k, n - i - 1) + min(k, i) + dp[a[i]]
		l = max(i - k, 0)
		r = min(a[i] + k, n - 1)
		if l <= r:
			dp[i] -= r - l + 1
print(*dp)


def get_border(i, n, k):
	left = i - k
	if left < 0:
		left = 0
	right = i + k
	if n <= right:
		right = n - 1
	return [left, right]

def add_border(borders, new_border):
	if not borders:
		borders.append(new_border)
		return
	last_border = borders[-1]
	left = last_border[0]
	new_right = new_border[1]
	new_left = new_border[0]
	if left <= new_right:
		last_border[0] = new_left
	else:
		borders.append(new_border)

def count(borders):
	result = 0
	for border in borders:
		result += border[1] - border[0] + 1
	return result

def read(idx):
	borders = []
	i = idx
	border = get_border(idx, n, k)
	while True:
		if i < 0:
			break
		if results[i][0] != -1:
			intersection = results[i][1] - borders[-1][0] + 1
			if intersection < 0:
				intersection = 0
			result = results[i][0] + count(borders) - intersection
			results[idx] = (result, border[1])
			return result
		add_border(borders, get_border(i, n, k))
		i = refs[i] - 1
	result = count(borders)
	results[idx] = (result, border[1])
	return result
n = int()
k = int()
refs = []
results = []

def main():
	global n, k, refs, results
	(n, k) = map(lambda s: int(s), input().split())
	refs = list(map(lambda s: int(s), input().split()))
	results = [(-1, -1) for i in range(n)]
	for idx in range(n):
		print(read(idx), end=' ')
	print()
main()


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
dp = [0] * n
dp[0] = min(m + 1, n)
print(dp[0], end=' ')
for i in range(1, n):
	if l[i] == 0:
		v2 = min(n - 1, i + m)
		v1 = max(0, i - m)
		dp[i] = v2 - v1 + 1
		print(dp[i], end=' ')
		continue
	v2 = min(n - 1, i + m)
	v1 = max(0, i - m)
	v3 = max(0, l[i] - 1 - m)
	v4 = min(n - 1, l[i] - 1 + m)
	dp[i] = dp[l[i] - 1] + (v2 - v1 + 1) - max(0, min(v2, v4) - max(v1, v3) + 1)
	print(dp[i], end=' ')


def readline():
	return tuple(map(int, input().split()))

def main():
	(n, k) = readline()
	a = readline()
	answer = list()
	for (i, link) in enumerate(a):
		bottom = max(0, i - k)
		top = min(n, i + k + 1)
		if link == 0:
			answer.append(top - bottom)
		else:
			bottom = max(bottom, link + k)
			answer.append(max(0, top - bottom) + answer[link - 1])
	print(' '.join(map(str, answer)))
main()


(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = [0 for i in range(n)]
s[0] = 1 + min(n - 1, k)
for i in range(n):
	if a[i] == 0:
		s[i] = 1 + min(i, k) + min(n - 1 - i, k)
	else:
		j = a[i] - 1
		s[i] = s[j] + min(i + k, n - 1) - min(j + k, n - 1)
		if i - k > j + k:
			s[i] -= i - k - (j + k) - 1
print(' '.join([str(i) for i in s]))


(n, k) = map(int, input().split())
L = list(map(int, input().split()))
ans = []
b = []
for i in range(n):
	a = max(0, i - k)
	b.append(min(n, i + k + 1))
	ans.append(b[i] - a)
	if L[i] != 0:
		ans[i] += ans[L[i] - 1]
		if a < b[L[i] - 1]:
			ans[i] -= b[L[i] - 1] - a
	print(ans[i], end=' ')


def main():
	(n, k) = map(int, input().split())
	b = list(map(int, input().split()))
	a = [0] * n
	for i in range(n):
		(l, r) = (0, n - 1)
		a[i] = 0
		if b[i]:
			l = b[i] + k
			a[i] += a[b[i] - 1]
		l = max(l, i - k)
		r = min(r, i + k)
		if r >= l:
			a[i] += r - l + 1
	print(' '.join(map(str, a)))
main()


(n, k) = map(int, input().split())
dp = [0]
bound = [0]
a = list(map(int, input().split()))
for i in range(1, n + 1):
	dp.append(dp[a[i - 1]])
	bound.append(i + k)
	t1 = max(bound[a[i - 1]] + 1, i - k)
	t2 = min(i + k, n)
	t3 = t2 - t1 + 1
	if t3 > 0:
		dp[i] += t3
for i in range(1, n + 1):
	print(dp[i], end=' ')


def read_ints():
	return list(map(int, input().split()))
(n_messages, n_visible) = read_ints()
refs = read_ints()
NO_REF = -1
refs = [i - 1 for i in refs]
read = [0] * n_messages

def messages_seen_at(index):
	return 1 + min(index, n_visible) + min(n_messages - index - 1, n_visible)

def intersection_seen(index, reference):
	if index <= reference:
		print('NOoooooooooo')

	def segment(index):
		return (max(0, index - n_visible), min(reference + n_visible, n_messages - 1))
	(a, b) = segment(reference)
	(c, d) = segment(index)
	return max(b - c + 1, 0)

def get_read(index):
	if refs[index] == NO_REF:
		read[index] = messages_seen_at(index)
	else:
		reads_by_ref = read[refs[index]]
		read[index] = reads_by_ref + messages_seen_at(index) - intersection_seen(index, refs[index])
	return read[index]
for i in range(n_messages):
	get_read(i)
print(' '.join(map(str, read)))


def read():
	return list(map(int, input().split()))
(n, k) = read()
a = read()
b = [min(k, n - 1) + 1]
for i in range(1, n):
	if a[i] == 0:
		b.append(min(i, k) + 1 + min(n - i - 1, k))
	else:
		b.append(b[a[i] - 1] - min(k, n - a[i]) + min(i - a[i], 2 * k) + min(n - i - 1, k) + 1)
print(*b)


(n, k) = map(int, input().split())
m = [0] + list(map(int, input().split()))
dp = [0] * (n + 1)
dp[1] = min(n, k + 1)
print(dp[1], end=' ')
for i in range(2, n + 1):
	if not m[i]:
		dp[i] = min(i + k, n) - max(i - k - 1, 0)
	else:
		dp[i] = dp[m[i]] + (min(n, i + k) - max(i - k - 1, m[i] + k) if i > m[i] + k else min(n, i + k) - min(m[i] + k, n))
	print(dp[i], end=' ')


(n, k) = map(int, input().split())
p = list(map(int, input().split()))
arr_d = [0 for _ in range(n)]
for i in range(len(p)):
	l_space = k if i - k >= 0 else i
	r_space = k if i + k < len(p) else abs(len(p) - i - 1)
	arr_d[i] += l_space + r_space + 1
	if p[i] != 0:
		arr_d[i] += arr_d[p[i] - 1]
		link_r_pos = p[i] - 1 + k if p[i] - 1 + k < len(p) else len(p) - 1
		now_l_pos = i - l_space
		if link_r_pos >= now_l_pos:
			arr_d[i] -= abs(link_r_pos - now_l_pos) + 1
print(*arr_d)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
dp = [0 for _ in range(n)]
for i in range(0, n):
	if a[i] == 0:
		dp[i] = 1 + min(i, k) + min(n - 1 - i, k)
	elif not a[i] - 1 + k < i - k:
		dp[i] = 1 + min(i, k) + min(n - i - 1, k) + dp[a[i] - 1] - (min(min(i + k, n - 1), min(a[i] - 1 + k, n - 1)) - max(max(i - k, 0), max(a[i] - 1 - k, 0)) + 1)
	else:
		dp[i] = 1 + min(i, k) + min(n - i - 1, k) + dp[a[i] - 1]
print(*dp)


(n, k) = map(int, input().split())
A = list(map(int, input().split()))
B = []
for q in range(n):
	B.append(0)
y = n - k - 1
if y > 0:
	y = 0
B[0] = k + 1 + y
for q in range(1, n):
	z = k - q
	y = n - q - k - 1
	y1 = n - A[q] + 1 - k - 1
	if y1 > 0:
		y1 = 0
	if y > 0:
		y = 0
	if z < 0:
		z = 0
	r = q - A[q] + 1
	x1 = r - 2 * k - 1
	x = r - 2 * k - 1 + z - y1
	if x > 0:
		x = 0
	if A[q] != 0:
		B[q] = B[A[q] - 1] + 2 * k + 1 + x + y - z
	else:
		B[q] = 2 * k + 1 + y - z
print(*B)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
r = [0] * n
for i in range(min(k + 1, n)):
	r[i] = 1 + i + (min(i + k, n - 1) - i)
for i in range(k + 1, n):
	if a[i] == 0:
		r[i] = 1 + k + (min(i + k, n - 1) - i)
	else:
		parent = a[i] - 1
		r[i] = 1 + k + (min(i + k, n - 1) - i) + r[parent] - max(0, min(parent + k, n - 1) - (i - k) + 1)
print(*r)


(n, k) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [0] * n
for i in range(len(a)):
	num = a[i] - 1
	right = min(n - 1, i + k)
	if num == -1:
		left = max(0, i - k)
		b[i] = right - left + 1
	else:
		right_p = min(n - 1, num + k)
		left = max(right_p + 1, i - k)
		b[i] = b[a[i] - 1] + right - left + 1
print(*b)


(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
answ = []
for i in range(n):
	if arr[i] == 0:
		temp = 1 + min(i + 1 + k, n) - max(i + 1 - k, 1)
		answ.append(temp)
	else:
		if i + 1 - arr[i] >= 2 * k + 1:
			temp = 1 + min(i + 1 + k, n) - max(i + 1 - k, 1) + answ[arr[i] - 1]
		if 2 * k + 1 > i + 1 - arr[i] > k:
			temp = min(i + 1 + k, n) - max(i + 1 - (i + 1 - arr[i] - k), 1) + answ[arr[i] - 1]
		if i + 1 - arr[i] <= k:
			temp = min(i + 1 + k, n) - min(arr[i] + k, n) + answ[arr[i] - 1]
		answ.append(temp)
print(*answ)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
d = [0] * n
a = [c - 1 for c in a]
for i in range(0, n):
	if a[i] == -1:
		d[i] = 1 + min(k, n - i - 1) + min(k, i)
	else:
		d[i] = 1 + min(k, n - i - 1) + min(k, i) + d[a[i]]
		lt = max(i - k, 0)
		rt = min(a[i] + k, n - 1)
		if lt <= rt:
			d[i] -= rt - lt + 1
print(*d)


(n, k) = input().split(' ')
n = int(n)
k = int(k)
arr = input().split(' ')
arr = list(map(lambda x: int(x), arr))
arr_d = [0] * 100005
arr_d_right = [0] * 100005

def count(i, k, n):
	if i - k > 0:
		left_ind = i - k
	else:
		left_ind = 0
	if i + k < n - 1:
		right_ind = i + k
	else:
		right_ind = n - 1
	next_i = arr[i] - 1
	if next_i != -1:
		prev_val = arr_d[next_i]
		if left_ind <= arr_d_right[next_i]:
			left_ind = arr_d_right[next_i] + 1
	else:
		prev_val = 0
	arr_d[i] = prev_val + right_ind - left_ind + 1
	arr_d_right[i] = right_ind
	return arr_d[i]
res_arr = []
for ind in range(0, len(arr)):
	res_arr.append(str(count(ind, k, n)))
print(' '.join(res_arr))


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = [0] * n
for (i, item) in enumerate(a):
	own_l = max(i - k, 0)
	own_r = min(i + k, n - 1)
	anc_l = max(item - 1 - k, 0)
	anc_r = min(item - 1 + k, n - 1)
	if item == 0:
		b[i] = own_r - own_l + 1
		continue
	if own_l > anc_r:
		b[i] = b[item - 1] + own_r - own_l + 1
	else:
		b[i] = b[item - 1] + own_r - anc_r
print(*b)


def f(a, k):
	a = list(map(lambda s: s - 1, a))
	n = len(a)
	dp = [0] * len(a)
	for i in range(n):
		if a[i] == -1:
			dp[i] = 1 + min(n - i - 1, k) + min(i, k)
		else:
			dp[i] = min(min(n - 1, i + k) - min(n - 1, a[i] + k), 1 + min(i, k) + min(n - i - 1, k)) + dp[a[i]]
	return dp
(a, b) = map(int, input().strip().split())
lst = list(map(int, input().strip().split()))
print(*f(lst, b))


(n, k) = map(int, input().split())
mes = list(map(int, input().split()))
ss = [0] * n

def calc(start):
	last = n
	cur = start
	s = 0
	while cur != 0 and last > 0:
		if cur <= last:
			s += 1
			s += min(k, cur - 1)
			s += min(k, last - cur)
		else:
			s += max(last - (cur - min(k, cur - 1) - 1), 0)
		last = cur - min(k, cur - 1) - 1
		cur = mes[cur - 1]
		if ss[cur - 1] != 0:
			s += max(ss[cur - 1] - max(min(cur + k, n) - last, 0), 0)
			break
	ss[start - 1] = s
	return s
for i in range(1, len(mes)):
	print(calc(i), end=' ')
print(calc(len(mes)))


(n, k) = (int(x) for x in input().split())
a = [int(x) for x in input().split()]

def min_(a, b):
	return a if a < b else b
cache = []
bounds = []
for i in range(n):
	cache.append(1 + min_(k, i) + min_(k, n - i - 1))
	bounds.append(max(i - k, 0))
	if a[i] > 0:
		x = a[i] - 1
		l = max(x - k, 0)
		r = min_(x + k, n - 1)
		cache[i] += cache[x] - (r - bounds[i] + 1 if r >= bounds[i] else 0)
		bounds[i] = min_(bounds[i], l) if r > bounds[i] else bounds[i]
print(' '.join([str(x) for x in cache]))


(n, k) = [int(_) for _ in input().split()]
links = [int(_) - 1 for _ in input().split()]
ans = [0] * n
for i in range(n):
	ans[i] = 1 + min(k, i - 0) + min(k, n - i - 1)
	if links[i] != -1:
		j = links[i]
		ans[i] += ans[j] - max(0, 1 + min(k, i - 0) + min(k, n - j - 1) - (i - j))
print(' '.join((str(_) for _ in ans)))


(n, k) = map(int, input().split())
a = [int(i) for i in input().split()]
d = [0 for i in range(n)]
d[0] = 1 + min(n - 1, k)
for i in range(1, n):
	if a[i] == 0:
		d[i] = 1 + min(i, k) + min(n - 1 - i, k)
	else:
		j = a[i] - 1
		d[i] = d[j] + min(n - 1, i + k) - min(n - 1, j + k)
		if i - k > j + k:
			d[i] -= i - k - (j + k) - 1
for i in range(len(d)):
	print(d[i], end=' ')


(n, k) = map(int, input().split())
a = [int(x) for x in input().split()]
ans = []
for i in range(n):
	c = 0
	imin = max(0, i - k)
	imax = min(n - 1, i + k)
	if a[i] != 0:
		c += ans[a[i] - 1]
		amin = max(0, a[i] - 1 - k)
		amax = min(n - 1, a[i] - 1 + k)
		if imin > amax:
			c += imax - imin + 1
		else:
			c += imax - amax
	else:
		c += imax - imin + 1
	ans.append(c)
print(' '.join([str(x) for x in ans]))


(n, k) = map(int, input().split())
mx = list(map(int, input().split()))

def mail(mx, mn):
	global n
	(mx, mn) = (mx + 1, mn + 1)
	if mx > n:
		return 0
	mn = min(mn, n)
	mx = max(1, mx)
	return mn - mx + 1
masiv = [0] * n
for i in range(n):
	if mx[i] == 0:
		masiv[i] = mail(i - k, i + k)
	else:
		masiv[i] = masiv[mx[i] - 1] + mail(max(mx[i] - 1 + k + 1, i - k), i + k)
print(*masiv)


from sys import setrecursionlimit
setrecursionlimit(10 ** 6)
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
save = [[] for i in range(0, len(a) + 1)]

def fun(pos):
	if save[pos] == []:
		left = max(pos - 1 - k, 0)
		right = min(pos + k, len(a))
		if a[pos - 1] != 0:
			new_set = fun(a[pos - 1])
			if new_set[-1][1] >= left:
				new_set[-1] = (new_set[-1][0], right)
			else:
				new_set.append(tuple([left, right]))
		else:
			new_set = [tuple([left, right])]
		save[pos] = new_set
		return new_set.copy()
	else:
		return save[pos].copy()

def enc(a):
	z = 0
	for i in a:
		z = z + i[1] - i[0]
	return z
for i in range(1, len(a) + 1):
	print(enc(fun(i)), end=' ')


(n, k) = map(int, input().split())
A = map(int, input().split())
ans = [0] * n
for (i, a) in enumerate(A):
	a -= 1
	(l, r) = (max(0, i - k), min(n - 1, i + k))
	if a >= 0:
		ans[i] += ans[a]
		l = max(l, min(n - 1, a + k) + 1)
	ans[i] += r - l + 1
print(' '.join(map(str, ans)))


def intersection(a, b, k, l):
	max_limit = min(a + k, l - 1)
	min_limit = max(b - k, 0)
	return max(max_limit - min_limit + 1, 0)

def left_k(i, k):
	return k - max(k - i, 0)

def right_k(i, k, l):
	return k - max(i + 1 + k - l, 0)

def window(messages, k):
	amounts = []
	for (message, link) in enumerate(messages):
		link = link - 1
		if link > -1:
			inter = intersection(link, message, k, len(messages))
			amount = amounts[link] + left_k(message, k) + right_k(message, k, len(messages)) + 1 - inter
		else:
			amount = left_k(message, k) + right_k(message, k, len(messages)) + 1
		amounts.append(amount)
	return amounts
(l, k) = input().split(' ')
messages = input().split(' ')
answer = window([int(m) for m in messages], int(k))
print(' '.join([str(a) for a in answer]))