Holes (Codeforces 13E)
Tags: Others, dsu

Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for one person with following rules:There are N holes located in a single row and numbered from left to right with numbers from 1 to N. Each hole has it's own power (hole number i has the power ai). If you throw a ball into hole i it will immediately jump to hole i + ai, then it will jump out of it and so on. If there is no hole with such number, the ball will just jump out of the row. On each of the M moves the player can perform one of two actions:   Set the power of the hole a to value b.  Throw a ball into the hole a and count the number of jumps of a ball before it jump out of the row and also write down the number of the hole from which it jumped out just before leaving the row. Petya is not good at math, so, as you have already guessed, you are to perform all computations.

Input: The first line contains two integers N and M (1 ≤ N ≤ 105, 1 ≤ M ≤ 105) — the number of holes in a row and the number of moves. The second line contains N positive integers not exceeding N — initial values of holes power. The following M lines describe moves made by Petya. Each of these line can be one of the two types:   0 a b  1 a  Type 0 means that it is required to set the power of hole a to b, and type 1 means that it is required to throw a ball into the a-th hole. Numbers a and b are positive integers do not exceeding N.

Output: For each move of the type 1 output two space-separated numbers on a separate line — the number of the last hole the ball visited before leaving the row and the number of jumps it made.


Sample Inputs:
8 5
1 1 1 1 1 2 8 2
1 1
0 1 3
1 1
0 3 4
1 2

Sample Outputs:
8 7
8 5
7 3

Solution (Python):
import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
p = list(map(int, input().split()))
LENGTH = 320
block = [i // LENGTH for i in range(n)]
lompat = [0] * n
akhir = [0] * n
for i in range(n - 1, -1, -1):
    next = i + p[i]
    if next >= n:
        akhir[i] = i + n
        lompat[i] = 1
    elif block[i] != block[next]:
        lompat[i] = 1
        akhir[i] = next
    else:
        lompat[i] = lompat[next] + 1
        akhir[i] = akhir[next]
ans = []
for _ in range(m):
    s = input().strip()
    if s[0] == '0':
        (a, b, c) = s.split()
        b = int(b) - 1
        c = int(c)
        p[b] = c
        i = b
        while i >= 0 and block[i] == block[b]:
            next = i + p[i]
            if next >= n:
                akhir[i] = i + n
                lompat[i] = 1
            elif block[i] != block[next]:
                lompat[i] = 1
                akhir[i] = next
            else:
                lompat[i] = lompat[next] + 1
                akhir[i] = akhir[next]
            i -= 1
    else:
        (a, b) = s.split()
        sekarang = int(b) - 1
        total = 0
        while sekarang < n:
            total += lompat[sekarang]
            sekarang = akhir[sekarang]
        ans.append(str(sekarang - n + 1) + ' ' + str(total))
print('\n'.join(ans))

Solution (Python):
import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
p = list(map(int, input().split()))
LENGTH = 320
block = [i // LENGTH for i in range(n)]
lompat = [0] * n
akhir = [0] * n
for i in range(n - 1, -1, -1):
    next = i + p[i]
    if next >= n:
        akhir[i] = i + n
        lompat[i] = 1
    elif block[i] != block[next]:
        lompat[i] = 1
        akhir[i] = next
    else:
        lompat[i] = lompat[next] + 1
        akhir[i] = akhir[next]
ans = []
for _ in range(m):
    s = input().strip()
    if s[0] == '0':
        (a, b, c) = s.split()
        b = int(b) - 1
        c = int(c)
        p[b] = c
        i = b
        while i >= 0 and block[i] == block[b]:
            next = i + p[i]
            if next >= n:
                akhir[i] = i + n
                lompat[i] = 1
            elif block[i] != block[next]:
                lompat[i] = 1
                akhir[i] = next
            else:
                lompat[i] = lompat[next] + 1
                akhir[i] = akhir[next]
            i -= 1
    else:
        (a, b) = s.split()
        sekarang = int(b) - 1
        total = 0
        while sekarang < n:
            total += lompat[sekarang]
            sekarang = akhir[sekarang]
        ans.append(str(sekarang - n + 1) + ' ' + str(total))
print('\n'.join(ans))

Solution (Python):
import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
p = list(map(int, input().split()))
size = 320
block = [i // size for i in range(n)]
(par, cnt) = ([0] * n, [0] * n)
for i in range(n - 1, -1, -1):
    setelah = i + p[i]
    if setelah >= n:
        par[i] = i + n
        cnt[i] = 1
    elif block[setelah] == block[i]:
        par[i] = par[setelah]
        cnt[i] = cnt[setelah] + 1
    else:
        par[i] = setelah
        cnt[i] = 1
ans = []
for _ in range(m):
    s = input().strip()
    if s[0] == '0':
        (Z, a, b) = s.split()
        a = int(a) - 1
        b = int(b)
        p[a] = b
        i = a
        while i >= 0 and block[i] == block[a]:
            setelah = i + p[i]
            if setelah >= n:
                par[i] = i + n
                cnt[i] = 1
            elif block[i] == block[setelah]:
                par[i] = par[setelah]
                cnt[i] = cnt[setelah] + 1
            else:
                par[i] = setelah
                cnt[i] = 1
            i -= 1
    else:
        (Z, a) = s.split()
        a = int(a) - 1
        now = 0
        while a < n:
            now += cnt[a]
            a = par[a]
        ans.append(str(a - n + 1) + ' ' + str(now))
print('\n'.join(ans))