Problem:


Score : 500 points

Problem Statement

We have N bulbs arranged on a number line, numbered 1 to N from left to right.
Bulb i is at coordinate i.

Each bulb has a non-negative integer parameter called intensity.
When there is a bulb of intensity d at coordinate x, the bulb illuminates the segment from coordinate x-d-0.5 to x+d+0.5.
Initially, the intensity of Bulb i is A_i. We will now do the following operation K times in a row:

For each integer i between 1 and N (inclusive), let B_i be the number of bulbs illuminating coordinate i. Then, change the intensity of each bulb i to B_i.

Find the intensity of each bulb after the K operations.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq K \leq 2 \times 10^5

0 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the intensity A{'}_i of each bulb i after the K operations to Standard Output in the following format:

A{'}_1 A{'}_2 \ldots A{'}_N

Sample Input 1

5 1
1 0 0 1 0

Sample Output 1

1 2 2 1 2 

Initially, only Bulb 1 illuminates coordinate 1, so the intensity of Bulb 1 becomes 1 after the operation.
Similarly, the bulbs initially illuminating coordinate 2 are Bulb 1 and 2, so the intensity of Bulb 2 becomes 2.

Sample Input 2

5 2
1 0 0 1 0

Sample Output 2

3 3 4 4 3 



Sample input:
5 1
1 0 0 1 0


Sample output:
1 2 2 1 2

Submission status: Accepted

Solution code:
import numpy as np
from numba import njit
import sys
# import copy
# def input(): return sys.stdin.readline().rstrip()
# input = sys.stdin.readline
input = sys.stdin.buffer.readline
# sys.setrecursionlimit(10 ** 7)

N, K = map(int, input().split())
# A = np.array(list(map(int, (input().split()))))
A = np.array(input().split(), dtype=np.int64)
# A = list(map(int, (input().split())))
# nd = np.zeros((N,N), dtype=bool)
# print(*A)

# @njit('(i8[::1],)', cache=True)
@njit(cache=True)
def loop(A):
    for i in range(K):
        # cnt = [0]*N
        cnt = np.zeros_like(A)
        for j, x in enumerate(A):
            # nd[j, max(j-A[j],0):min(j+A[j]+1,N)] = True
            l = max(j-x,0)
            r = min(j+x+1,N)
            cnt[l] += 1
            if r<N:
                cnt[r] -= 1
            # for k in range(max(j-A[j],0), min(j+A[j]+1,N)):
            #     cnt[k] += 1
        # print(nd)
        # A = np.count_nonzero(nd, axis=0)
        # print(cnt)
        A = np.cumsum(cnt)
        if A[0] == A[N-1] == N:
            break
        # print(*A)
    return A

A = loop(A)
print(*A)

Submission status: Accepted

Solution code:
import numpy as np
from numba import njit
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())), dtype=np.int64)


@njit
def solve(N, K, A):
    # 一度照らされると二度目以降は確実に照らされるはず。
    for i in range(K):
        # j番目の電球を操作
        B = np.zeros(N + 1, dtype=np.int64)
        for j in range(N):
            # 左辺を確認
            B[max(j - A[j], 0)] += 1
            # 右辺を確認(なんで-1?)
            B[min(j + A[j] + 1, N)] -= 1

        A = np.cumsum(B)[:-1]

        if np.all(A == N):
            return A

    else:
        return A


A = solve(N, K, A)
for i in range(N):
    if i != (N - 1):
        print(A[i], end=" ")
    else:
        print(A[i])

Submission status: Accepted

Solution code:
n,k,*a=map(int,open(0).read().split())
r=range(n)
while(min(a)<n)*k:
 k-=1;b=[0]*n*3
 for i in r:b[max(0,i-a[i])]+=1;b[i-~a[i]]-=1
 for i in r:b[i+1]+=b[i]
 a=b[:n]
print(*a)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
ls=list(map(int,input().split()))
for _ in range(K):
  if ls==[N]*N:
    break
  else:   
    R=[0]*(N)
    for i in range(N):
      R[max([0,i-ls[i]])]+=1
      if i+ls[i]+1<N:
        R[i+ls[i]+1]-=1  
    ls[0]=R[0]
    for i in range(N-1):
      ls[i+1]=ls[i]+R[i+1]   
for i in range(N):
  print(ls[i],end=" ")

Submission status: Accepted

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

for i in range(k):
    s = [0] * (n + 1)
    for j in range(n):
        l = max(0, j - a[j])
        r = min(n, j + a[j]+1)
        s[l] += 1
        s[r] += -1
    m = s[0]
    for j in range(1,n+1):
        s[j] += s[j-1]
        if j < n:
            m = min(m, s[j])
    a = s[:-1]
    if m == n:
        print(' '.join(map(str, a)))
        exit()
print(' '.join(map(str, a)))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
def main():
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    zeros=[0]*n

    if min(a)==n:
        print(*a)
        exit()

    for i in range(k):
        x=zeros.copy()
        for j in range(n):

        
            l=max(0,j-a[j])
            r=min(j+a[j]+1,n)
            #print(l,r)
            x[l]+=1
            if r!=n:
                #print(r)
                x[r]+=-1
            #print(x)
        ruiseki=[0]*n
        ruiseki[0]=x[0]
        a[0]=ruiseki[0]
        for i in range(1,n):
            ruiseki[i]=ruiseki[i-1]+x[i]
            a[i]=ruiseki[i]
        if min(a)==n:
            print(*a)
            exit()
 
        #print(*ruiseki)
    print(*a)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.buffer.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from copy import copy
    n, k = map(int, readline().split())
    a = list(map(int, readline().split()))
    cur = copy(a)

    for cnt in range(k):
        cum = [0] * (n + 1)
        for i, x in enumerate(cur):
            left = max(0, i - x)
            cum[left] += 1
            right = min(i + x + 1, n)
            cum[right] -= 1

        cur[0] = cum[0]
        for i in range(n-1):
            cur[i + 1] = cur[i] + cum[i + 1]

        if all([j == n for j in cur]):
            break

    print(*cur[:n])

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = lr() + [0]
# ある回数から先は全ての電球にNを足す
for _ in range(min(K, 41)):
    next = [0] * (N+1)
    for i in range(N):
        x = A[i]
        next[max(0, i-x)] += 1
        next[min(N-1, i+x)+1] -= 1
    for i in range(N):
        next[i+1] += next[i]
    A = next.copy()

print(*A[:-1])

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time
from decimal import *


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False
# show_flg = True

def pl(x, d):
        return x - d - 0.5


def pr(x, d):
    return x + d + 0.5


def b(A, K):
    for k in range(K):
        ans = []
        for i in range(len(A)):
            c = 0
            for j in range(len(A)):
                l = pl(j, A[j])
                r = pr(j, A[j])

                if i >= l and i <= r:
                    c += 1
            ans.append(c)
        A = ans
        if ans[-1] == len(A):
            print(k, ans)
            return ans
    return A

def main():
    N, K = MI()
    A = LI()

    for i in range(min(100, K)):
        B = [0] * N
        for i in range(N):
            l = max(0, i - A[i])
            r = min(N - 1, i + A[i])
            B[l] += 1
            if r+1 < N:
                B[r+1] -= 1

        for i in range(1, N):
            B[i] += B[i-1]
        A = B

    print(*A)
    # N = 100000
    # A = [0] * N
    # l_arr = []
    # r_arr = []

    # print(b(A, K))

    # for i in range(N):
    #     l = pl(i, A[i])
    #     r = pr(i, A[i])
    #     l_arr.append((l, r))
    #     r_arr.append((r, l))
    #     # c = r - l
    #     # v.append(math.ceil(c))
    #     # print(i+1, 'left', pl(i+1, A[i]), 'right', pr(i+1, A[i]))

    # l_arr.sort()
    # r_arr.sort()
    # # print('l_arr', l_arr)
    # # print('r_arr', r_arr)

    # ans = [0] * N

    # for k in range(min(41, K)):
    #     nl_arr = []
    #     nr_arr = []
    #     for i in range(N):
    #         l_idx = bisect.bisect_right(l_arr, (i, None))
    #         r_idx = bisect.bisect_right(r_arr, (i, None))
    #         ans[i] = l_idx - r_idx
    #         l = pl(i, ans[i])
    #         r = pr(i, ans[i])
    #         nl_arr.append((l, r))
    #         nr_arr.append((r, l))

    #     nl_arr.sort()
    #     nr_arr.sort()
    #     l_arr = nl_arr
    #     r_arr = nr_arr

        # print(k, ans[-1])
        # print(idx, l_arr[idx])
        # print(*ans)
    # print(*A)
    # print(k)
    # print(*ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for n_iter in range(k):
    b = [0] * n
    for i in range(n):
        left = max(0, i - a[i])
        right = min(n-1, i + a[i])
        b[left] += 1
        if right + 1 < n: 
            b[right + 1] -= 1

    a[0] = b[0]
    for i in range(1, n):
        b[i] += b[i-1]
        a[i] = b[i]
    
    if n_iter >= 50:
        break

print(*a)

Submission status: Accepted

Solution code:
def Csum(a):
    b,c=[0],0
    for i in range(len(a)):
        c+=a[i]
        b.append(c)
    return b

n,k=map(int,input().split())
a=list(map(int,input().split()))
if k>100:
    a=[n]*n
    print(*a)
    exit()
for i in range(k):
    b=[0]*(n+1)
    for j in range(n):
        l,r=max(0,j-a[j]),min(n,j+a[j]+1)
        b[l]+=1
        b[r]-=1
    a=Csum(b)[1:-1]
print(*a)

Submission status: Accepted

Solution code:
import math

N, K = map(int, input().split())
K = min(41, K)
A = list(map(int, input().split()))
A.insert(0, 0)

def operation():
  global A, N
  B = [0] * (N+1)
  for i in range(1, N+1):
    left = max(1, math.ceil(i - A[i] - 0.5))
    right = min(N, math.floor(i + A[i] + 0.5))
    B[left] += 1
    if right+1 <= N:
      B[right+1] -= 1
  for i in range(1, N+1):
    B[i] += B[i-1]
  return B


operation()

#print(A)
#print(B)

for _ in range(K):
  A = operation()

print(*A[1:])

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    for _ in range(K):
        B = [0] * (N + 1)
        for i in range(N):
            B[max(i - A[i], 0)] += 1
            B[min(i + A[i] + 1, N)] -= 1
        for i in range(1, N):
            B[i] += B[i-1]
        A = B
        if sum(A[:-1]) == N * N:
            break
    print(' '.join(str(i) for i in A[:-1]))
main()

Submission status: Accepted

Solution code:
import sys
import copy
from itertools import accumulate
input=sys.stdin.readline
def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    ans = copy.deepcopy(A)
    for _ in range(K):
        cumsum = [0]*N
        for i,a in enumerate(ans):
            l = max(0,i-a)
            r = min(N-1,i+a)
            cumsum[l] += 1
            if r+1 < N:
                cumsum[r+1] -= 1
        ans = list(accumulate(cumsum))
        if ans[0] == N:
            break
    print(*ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N,K = LI()
A = LI()

for r in range(K):
    b = [0]*(N+1)
    for i in range(N):
        b[max(0,i-A[i])] += 1
        b[min(N,i+A[i]+1)] -= 1
    cb = [0]*N
    flag = True
    for i in range(N):
        cb[i] = min(cb[i-1]+b[i],N)
        if cb[i] != N:
            flag = False
    A = cb[:]
    if flag:
        break

print(*A)

Submission status: Accepted

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

# 以下カンニング
for _ in range( min( k, 41 ) ): # 41 ~ log( 2 * 10 ** 5 )
    b = [ 0 ] * n

    for i in range( n ):
        l = max( 0, i - a[ i ] )
        r = min( n - 1, i + a[ i ] )
        b[ l ] += 1
        if r + 1 < n:
            b[ r + 1 ] -= 1

    for i in range( 1, n ):
        b[ i ] += b[ i - 1 ]

    a = b

print( " ".join( list( map( str, a ) ) ) )

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N,K=map(int,input().split())
A=list(map(int,input().split()))

for k in range(min(50,K)):
    B=[0]*N
    for i in range(N):
        l=max(0,i-A[i])
        r=min(N,i+A[i]+1)
        B[l]+=1
        if r<N:
            B[r]-=1
    for i in range(N-1):
        B[i+1]+=B[i]
    A=B[:]
print(*A)

Submission status: Accepted

Solution code:
import copy
n,k = map(int,input().split())
a = list(map(int,input().split()))

def sim(a):
    b = [0]*n
    for i in range(n):
        l = max(0,i-a[i])
        r = min(n-1,i+a[i])
        b[l] +=1
        if r+1<n:
            b[r+1] -= 1
    for i in range(1,n):
        b[i] += b[i-1]
    return b

for i in range(min(k,41)):
    a = copy.copy(sim(a))
print(*a)

Submission status: Accepted

Solution code:
import itertools
def main():
    n,k = tuple([int(t)for t in input().split()])

    a = tuple([int(t)for t in input().split()])

    for _ in range(min(41,k)):
        b = [0]*(n+1)
        for i in range(n):
            l = max(0,i-a[i])
            r = min(n,i+a[i]+1)
            b[l] += 1
            b[r] -= 1
        a = list(itertools.accumulate(b))[:n]

    print(*a)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
n,k,*a=map(int,open(0).read().split())
for _ in range(k):
    b=[0]*n
    for i in range(n):
        b[max(0,i-a[i])]+=1
        if i+a[i]+1<n:b[i+a[i]+1]-=1
    a=list(accumulate(b))
    if a==[n]*n:break
print(" ".join(map(str,a)))

Submission status: Accepted

Solution code:
from math import log2

def main():
    N,K = map(int,input().split())
    A = list(map(int,input().split()))
    if K > log2(N)+ 50:
        return print(*[N]*N,sep = " ")
    for _ in range(K):
        imos = [0]*(N + 1)
        for i in range(N):
            imos[max(0,i  - A[i])] += 1
            imos[min(N,i + 1 + A[i])] -= 1
        for j in range(N):
            imos[j+1] += imos[j]
        A = imos[:N].copy()
    print(*A,sep=" ")
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


n, k = inpl()
a = inpl()
dp = [(0, 0)] * n
changed = set(range(n))
for j in range(k):
    for i in changed:
        l = max(0, i - a[i])
        r = min(n - 1, i + a[i])
        dp[i] = (l, r)
    imos = [0] * n
    for i in range(n):
        l, r = dp[i]
        imos[l] += 1
        if r < n - 1:
            imos[r + 1] -= 1
    tmp = 0
    for i in range(n):
        tmp += imos[i]
        a[i] = tmp
    if imos[0]==n:
        break
print(*a)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))

for i in range(1,K+1):
  B = [0]*N
  for j in range(N):
    l = max(0, j-A[j])
    r = min(N-1, j+A[j])
    B[l] += 1
    if r+1<N:
      B[r+1] -= 1
    
  if B[0]==N:
    x = 1
  else:
    x = 0
  for i in range(1,N):
    B[i] += B[i-1]
    if B[i]==N:
      x += 1
  A = B.copy()
  if (x==N)or(i==K):
    break
    
print(' '.join(list(map(str,A))))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

A = A + [0]
for k in range(K):
    t = [0]*(N+1)
    for i in range(N):
        l = max(i-A[i], 0)
        r = min(i+A[i], N-1)
        t[l] += 1
        t[r+1] -= 1

    S = [0]*N
    S[0] = t[0]
    for i in range(1, N):
        S[i] = S[i-1] + t[i]

    A = S.copy()
    if all(a == N for a in A):
        print(*A)
        exit()
print(*S)

Submission status: Accepted

Solution code:
n,k,*a=map(int,open(0).read().split())

for _ in range(k):
    tab=[0]*n
    for i in range(n):
        q=a[i]
        l=max(0,i-q)
        tab[l]+=1
        r=i+q+1
        if r<n:
            tab[r]-=1
    b=[tab[0]]
    for i in range(n-1):
        b.append(b[-1]+tab[i+1])
        
    if a==b:
        break
    else:
        a=b[:]
print(*a)

Submission status: Accepted

Solution code:
import copy
import sys
input = sys.stdin.readline

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

new_a = [0 for i in range(n)]
for i in range(k):
    for j in range(n):
        new_a[max(j-a[j], 0)] += 1
        if j+a[j]+1 < n:
            new_a[j+a[j]+1] += -1
    for j in range(n):
        if j > 0:
            new_a[j] += new_a[j-1]
    if new_a == [n for i in range(n)]:
        a = copy.copy(new_a)
        break
    a = copy.copy(new_a)
    new_a = [0 for i in range(n)]

print(" ".join(list(map(str, a))))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
a=list(map(int,input().split()))

for i in range(min(k,500)):
    ans=[0]*(n)
    for j in range(n):
        l=max(0,j-a[j])
        r=j+1+a[j]
        ans[l]+=1
        if r<n:
            ans[r]-=1
    for z in range(n-1):
        ans[z+1]+=ans[z]
    a=ans
print(" ".join(list(map(str,ans))))

Submission status: Accepted

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [-int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()
    DD = ((1,0),(0,1),(-1,0),(0,-1))

    N,K = LI()
    a = LI()

    for _ in range(K):
        x = [0] * N
        for i in range(N):
            s = i - a[i]
            if s < 0: s = 0
            x[s] += 1
            t = i + a[i] + 1
            if t < N:
                x[t] -= 1
        a = []
        cnt = 0
        finish = True
        for i in range(N):
            cnt += x[i]
            if cnt < N: finish = False
            a.append(cnt)
        if finish: break
    print(*a,sep=' ')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    k = min(50,k)

    dp = [[0]*n for _ in range(k+1)]
    for i in range(n):
        dp[0][i] = A[i]

    flag = False
    for i in range(1,k+1):
        pos = [0]*n
        neg = [0]*n

        for j in range(n):
            pwr = dp[i-1][j]
            pos[max(0,j-pwr)]+=1
            neg[min(n-1,j+pwr)]+=1

        ac_pos = 0
        ac_neg = 0
        for j in range(n):
            ac_pos += pos[j]
            ac_neg += neg[j]
            pos[j] = ac_pos
            neg[j] = ac_neg

        if pos[0]==n and neg[n-2]==0:
            flag = True
            break

        dp[i][0] = pos[0]
        for j in range(1,n):
            dp[i][j] = pos[j] - neg[j-1]

    
    if flag:
        for j in range(n-1):
            print(n, end=" ")  
        print(n)

    else:
        for j in range(n-1):
            print(dp[i][j], end=" ")
        print(dp[i][n-1])
main()

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))

for i in range(min(49,K)):
    B = [0 for _ in range(N)]
    for j in range(N):
        l=max(0,j-A[j])
        r=min(N-1,j+A[j])
        B[l]=B[l]+1
        if r+1<N:
            B[r+1]=B[r+1]-1
    for j in range(N-1):
        B[j+1]=B[j+1]+B[j]
    #print(B)
    A=[]
    A=B
    #input()
print(*B)

Submission status: Accepted

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

for _ in range(k):
    tmp = [0] * (n + 1)
    for i in range(n):
        tmp[max(0, i - a[i])] += 1
        tmp[min(n, i + a[i] + 1)] -= 1
    for i in range(n):
        a[i] = tmp[i]
        tmp[i + 1] += tmp[i]
    if min(a) == n:
        break

print('\n'.join(map(str, a)))

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


n, k = inpl()
a = inpl()
dp = [(0, 0)] * n
changed=set(range(n))
for j in range(k):
    for i in changed:
        l = max(0, i - a[i])
        r = min(n - 1, i + a[i])
        dp[i] = (l, r)
    imos = [0] * n
    for i in range(n):
        l, r = dp[i]
        imos[l] += 1
        if r < n - 1:
            imos[r + 1] -= 1
    tmp = 0
    for i in range(n):
        tmp += imos[i]
        a[i] = tmp
    if j>50:
        break
print(*a)

Submission status: Accepted

Solution code:
import sys
import numpy as np
from numba import njit
 
readline = sys.stdin.readline
 
@njit('i8,i8,i8[::1]',cache=True)
def loop(n,k,a):
    for j in range(k):
        b = np.zeros(n+1, dtype=np.int64)
        for i in range(n):
            b[max(i-a[i], 0)] += 1
            b[min(i+a[i]+1, n)] -= 1
        a = np.cumsum(b)[:-1]
        if np.all(a==n):
            return(a)
            break
    else:
        return(a)
 
 
n, k = map(int, readline().split())
a = np.array(readline().split(), dtype=np.int64)
 
ans = loop(n,k,a)
print(*ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, K: int, A: "List[int]"):
    for idx in range(min(50,K)):
        B = [0] * (N+1)
        for i in range(N):
            B[max(i-A[i],0)] += 1
            B[min(i+A[i]+1,N)] -= 1
        
        add = 0
        for i in range(N):
            add += B[i]
            A[i] = add
    
    print(*A)

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
#sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    n, k = geta(int)
    a = tuple(geta(int))

    def update(b):
        r = [0] * n
        for i, bi in enumerate(b):
            i1 = max(0, i - bi)
            i2 = min(n - 1, i + bi)
            r[i1] += 1

            if i2 < n - 1:
                r[i2 + 1] -= 1

        updated = (not r[0] == b[0])
        for i in range(1, n):
            r[i] += r[i - 1]
            if r[i] != b[i]:
                updated = True

        return r, updated

    for i in range(k):
        a, updated = update(a)
        if not updated:
            break

    print(*a, sep=" ")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

lenA = len(A)
for i in range(K):
    B = [0 for _ in range(lenA)]
#    print(B)
    for j in range(lenA):
        num = A[j]
#        print(j, num)
        if j - num >= 0:
            B[max(0, j - num)] += 1
        else:
            B[0] += 1
        if j + num + 1 < lenA:
            B[min(lenA, j + num + 1)] -= 1
#        print(j, B)
    for i in range(1, lenA):
        B[i] = B[i-1] + B[i]
    if all([a == b for a, b in zip(A,B)]):
#        print('same')
        break
    else:
        A = B
#print(i+1)
print(' '.join(map(str, A)))

Submission status: Accepted

Solution code:
import math
from sys import stdin,stdout
#% 998244353
from heapq import heapify,heappop,heappush
import collections

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


for i in range(min(k,42)):
    #nw=[0]*n
    pr=[0]*n
    for x,d in enumerate(a):
        if d==0:
            pr[x]+=1
            if x!=n-1:
                pr[x+1]-=1
            continue
        l=math.floor(x-d)
        r=math.floor(x+d)
        l=max(0,l)
        r=min(n-1,r)
        pr[l]+=1
        if r!=n-1:
            pr[r+1]-=1
    
    for m in range(1,n):
        pr[m]+=pr[m-1]
    
    a[:]=pr


        

for i in a:
    print(i,end=" ")

Submission status: Accepted

Solution code:
N,K = map(int,(input().split()))
A = list(map(int,(input().split())))
for _ in range(min(K,100)):
    s = [0]*(N+1)
    for i in range(N):
        l = max(i - A[i],0)
        r = min(i + A[i] + 1,N)
        s[l] += 1
        s[r] -= 1
    for i in range(1,N):
        s[i] += s[i-1]
    for i in range(N):
        A[i] = s[i]
print(*A)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
*A, = map(int, input().split())
for _ in range(min(K, 50)):
    counter = [0] * (N+2)
    for i, a in enumerate(A, 1):
        counter[max(0, i - a)] += 1
        counter[min(N + 1, i + a + 1)] += -1
    for i in range(1, len(counter) - 1):
        counter[i] += counter[i-1]
    A = counter[1:N+1]
print(*A)

Submission status: Accepted

Solution code:
from itertools import accumulate
from math import log2

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

for j in range(k):
    a01 = [0] * (n + 1)
    for i, ai in enumerate(a):
        l, r = max(0, i - ai), min(i + ai + 1, n)
        a01[l] += 1
        a01[r] -= 1
    a = list(accumulate(a01))
    if all(ai == n for ai in a[:-1]):
        break
print(*a[:-1])

Submission status: Accepted

Solution code:
from copy import deepcopy

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

for _ in range(k):
    if sum(As) == n * n:
        break
    Bs = [0] * n
    for i in range(n):
        l = max(0, i - As[i])
        r = min(n - 1, i + As[i])
        Bs[l] += 1
        if r + 1 < n:
            Bs[r + 1] -= 1
    for i in range(1, n):
        Bs[i] += Bs[i - 1]
    As = Bs

print(" ".join(map(str, As)))

Submission status: Accepted

Solution code:
from itertools import accumulate

n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [0]*(n+1)

if k > 41:
    k = 41

for _ in range(k):
    b = [0]*(n+1)
    for i,j in enumerate(a):
        b[max(0,i-j)] += 1
        b[min(n,i+j+1)] -= 1
    a = list(accumulate(b))
    for i in range(n+1):
        if a[i] >= n:
            a[i] = n

print(*a[:-1])

Submission status: Accepted

Solution code:
from itertools import accumulate
import sys

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

for cnt in range(k):
    tmp = [0] * (n + 2)
    for i in range(n):
        power = arr[i]
        left = max(0, i - power + 1)
        right = min(n + 1, i + power + 2)
        tmp[left] += 1
        tmp[right] -= 1
    arr = list(accumulate(tmp))[1:-1]
    if cnt > 40:
        arr = [n] * n
        print(*arr)
        sys.exit()

print(*arr)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(min(K,300)):
    imos = [0]*(N+1)
    for i, ai in enumerate(A):
        imos[max(0, i-ai)] += 1
        imos[min(N, i+ai+1)] -= 1
    A = [imos[0]]
    for i in range(1, N):
        A += [imos[i]+A[i-1]]
print(' '.join(map(str, A)))

Submission status: Accepted

Solution code:
import numpy as np
from numba import njit


@njit
def solve(n, k, a):
    for _ in range(min(k, 41)):
        diff = np.zeros(3 * n + 1, dtype=np.int64)
        for pos in range(n):
            diff[n + pos - a[pos]] += 1
            diff[n + pos + a[pos] + 1] -= 1
        diff = diff.cumsum()
        a = diff[n:2 * n]
    return a


n, k = map(int, input().split())
a = np.array(list(map(int, input().split())), dtype=np.int64)
a = solve(n, k, a)
print(" ".join(map(str, a)))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
def main():
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    zeros=[0]*n

    if min(a)==n:
        print(*a)
        exit()

    for i in range(k):
        x=zeros.copy()
        for j in range(n):
            if a[j]==0:
                l=j
                r=min(j+1,n)
                #print(l,r)
                x[l]+=1
                if r!=n:
                #print(r)
                    x[r]+=-1
                #print(x)
            else:
                l=max(0,j-a[j])
                r=min(j+a[j]+1,n)
                #print(l,r)
                x[l]+=1
                if r!=n:
                    #print(r)
                    x[r]+=-1
                #print(x)
        ruiseki=[0]*n
        ruiseki[0]=x[0]
        a[0]=ruiseki[0]
        for i in range(1,n):
            ruiseki[i]=ruiseki[i-1]+x[i]
            a[i]=ruiseki[i]
        if min(a)==n:
            print(*a)
            exit()
 
        #print(*ruiseki)
    print(*a)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate

N, K = map(int, input().split())
A = list(map(int, input().split()))

for _ in range(K):
    if A == [N] * N:
        break
    B = [0] * (N+1)
    for i, a in enumerate(A):
        start = max(0, i - a)
        end = min(N-1, i + a) + 1
        B[start] += 1
        B[end] -= 1
    A = list(accumulate(B))[:-1]
print(' '.join(map(str, A)))