Heavy Buckets

AtCoder
IDabc438_e
Time3000ms
Memory256MB
Difficulty
There are $N$ people and $N$ buckets. Both the people and buckets are numbered $1, 2, \ldots, N$. Initially, person $i$ holds only bucket $i$, and bucket $i$ is empty. From now on, the following operation will be performed $10^9$ times: * For $i = 1, 2, \ldots, N$ **simultaneously**, person $i$ adds $i$ units of water to each of the buckets they are holding and passes those buckets to person $A_i$. Here, there is no limit on the amount of water that can be put in a bucket. For $i = 1, 2, \ldots, Q$, answer the following queries: * Find the amount of water in bucket $B_i$ immediately after the $T_i$\-th operation. ## Constraints * $2 \leq N \leq 2 \times 10^5$ * $1 \leq Q \leq 2 \times 10^5$ * $1 \leq A_i \leq N$ * $1 \leq T_i \leq 10^9$ * $1 \leq B_i \leq N$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $Q$ $A_1$ $A_2$ $\ldots$ $A_N$ $T_1$ $B_1$ $T_2$ $B_2$ $\vdots$ $T_Q$ $B_Q$ [samples]
Samples
Input #1
5 6
3 4 2 2 5
4 3
6 5
1 4
10 1
10 2
1000000000 1
Output #1
11
30
4
28
30
2999999998

We will explain the first query.
Initially, bucket $3$ is held by person $3$.
Immediately after the $1$\-st operation, bucket $3$ is held by person $2$ and contains $3$ units of water.
Immediately after the $2$\-nd operation, bucket $3$ is held by person $4$ and contains $5$ units of water.
Immediately after the $3$\-rd operation, bucket $3$ is held by person $2$ and contains $9$ units of water.
Immediately after the $4$\-th operation, bucket $3$ is held by person $4$ and contains $11$ units of water.
Thus, the answer to the first query is $11$.
API Response (JSON)
{
  "problem": {
    "name": "Heavy Buckets",
    "description": {
      "content": "There are $N$ people and $N$ buckets. Both the people and buckets are numbered $1, 2, \\ldots, N$. Initially, person $i$ holds only bucket $i$, and bucket $i$ is empty. From now on, the following opera",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc438_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There are $N$ people and $N$ buckets. Both the people and buckets are numbered $1, 2, \\ldots, N$.\nInitially, person $i$ holds only bucket $i$, and bucket $i$ is empty.\nFrom now on, the following opera...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments