PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000012. Truy vấn nhảy trên mảng (Array Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn nhảy trên mảng (Array Queries)

Array Queries

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array aa of nn positive integers, with every ai≤na_i\le n, each query specifies two integers p,kp,k. Repeatedly perform

p←p+ap+kp\leftarrow p+a_p+k

while p≤np\le n. Each application counts as one operation, including the operation that makes p>np>n. Stop once p>np>n.

For each query, output the number of operations. Queries are independent: the array is never modified and the starting value of pp is specific to that query. All visited indices strictly increase because apa_p and kk are positive.

Input

The first line contains nn; the second contains nn integers aia_i. The third line contains qq. Each of the following qq lines contains p k.

Output

For each query, print the number of transformations required for pp to exceed nn, one result per line.

Subtasks

  • Subtask 1 (10 points): 1≤n≤701\le n\le 70; 1≤q≤801\le q\le 80; 1≤ai≤n1\le a_i\le n; 1≤p,k≤n1\le p,k\le n.
  • Subtask 2 (20 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; 1≤ai≤n1\le a_i\le n; 1≤p,k≤n1\le p,k\le n.
  • Subtask 3 (30 points): 1≤n≤200001\le n\le 20000; 1≤q≤200001\le q\le 20000; 1≤ai≤n1\le a_i\le n; 1≤p,k≤n1\le p,k\le n.
  • Subtask 4 (40 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000; 1≤ai≤n1\le a_i\le n; 1≤p,k≤n1\le p,k\le n.

Examples

Input

3
1 1 1
3
1 1
2 1
3 1

Output

2
1
1

Explanation

Query (1,1) goes from p=1p=1 to 33, then to 5>35>3, for 22 operations. Query (2,1) reaches 4>34>3 immediately; query (3,1) reaches 5>35>3 immediately.