Codeforces Round 351 (VK Cup 2016 Round 3, Div. 2 Edition)
F
Bearish Fanpages
Codeforces
未评级
原题
← E→
翻译中…

There is a social website with $*n*$ fanpages, numbered $1$ through $*n*$. There are also $*n*$ companies, and the $*i*$-th company owns the $*i*$-th fanpage.

翻译中…

Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow.

翻译中…

The website doesn’t allow a situation where $*i*$ follows $*j*$ and at the same time $*j*$ follows $*i*$. Also, a fanpage can't follow itself.

翻译中…

Let’s say that fanpage $*i*$ follows some other fanpage $*j*_{0}$. Also, let’s say that $*i*$ is followed by $*k*$ other fanpages $*j*_{1}, *j*_{2}, ..., *j*_{*k*}$. Then, when people visit fanpage $*i*$ they see ads from $*k* + 2$ distinct companies: $*i*, *j*_{0}, *j*_{1}, ..., *j*_{*k*}$. Exactly $*t*_{*i*}$ people subscribe (like) the $*i*$-th fanpage, and each of them will click exactly one add. For each of $*k* + 1$ companies $*j*_{0}, *j*_{1}, ..., *j*_{*k*}$, exactly image people will click their ad. Remaining image people will click an ad from company $*i*$ (the owner of the fanpage).

翻译中…

The total income of the company is equal to the number of people who click ads from this copmany.

翻译中…

Limak and Radewoosh ask you for help. Initially, fanpage $*i*$ follows fanpage $*f*_{*i*}$. Your task is to handle $*q*$ queries of three types:

翻译中…
  • 1 i j — fanpage $*i*$ follows fanpage $*j*$ from now. It's guaranteed that $*i*$ didn't follow $*j*$ just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section).
  • 2 i — print the total income of the $*i*$-th company.
  • 3 — print two integers: the smallest income of one company and the biggest income of one company.
翻译中…

Input

翻译中…

The first line of the input contains two integers $*n*$ and $*q*$ ($3 ≤ *n* ≤ 100 000$, $1 ≤ *q* ≤ 100 000$) — the number of fanpages and the number of queries, respectively.

翻译中…

The second line contains $*n*$ integers $*t*_{1}, *t*_{2}, ..., *t*_{*n*}$ ($1 ≤ *t*_{*i*} ≤ 10^{12}$) where $*t*_{*i*}$ denotes the number of people subscribing the $*i*$-th fanpage.

翻译中…

The third line contains $*n*$ integers $*f*_{1}, *f*_{2}, ..., *f*_{*n*}$ ($1 ≤ *f*_{*i*} ≤ *n*$). Initially, fanpage $*i*$ follows fanpage $*f*_{*i*}$.

翻译中…

Then, $*q*$ lines follow. The $*i*$-th of them describes the $*i*$-th query. The first number in the line is an integer $*type*_{*i*}$ ($1 ≤ *type*_{*i*} ≤ 3$) — the type of the query.

翻译中…

There will be at most $50 000$ queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty).

翻译中…

It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other.

翻译中…

Output

翻译中…

For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively.

翻译中…

Note

翻译中…

image

翻译中…

In the sample test, there are $5$ fanpages. The $*i*$-th of them has $*i*·10$ subscribers.

翻译中…

On drawings, numbers of subscribers are written in circles. An arrow from $*A*$ to $*B*$ means that $*A*$ follows $*B*$.

翻译中…

The left drawing shows the initial situation. The first company gets income image from its own fanpage, and gets income image from the $2$-nd fanpage. So, the total income is $5 + 5 = 10$. After the first query ("2 1") you should print $10$.

翻译中…

The right drawing shows the situation after a query "1 4 2" (after which fanpage $4$ follows fanpage $2$). Then, the first company still gets income $5$ from its own fanpage, but now it gets only image from the $2$-nd fanpage. So, the total income is $5 + 4 = 9$ now.

翻译中…

样例

Input #1
5 12
10 20 30 40 50
2 3 4 5 2
2 1
2 2
2 3
2 4
2 5
1 4 2
2 1
2 2
2 3
2 4
2 5
3
Output #1
10
36
28
40
36
9
57
27
28
29
9 57
InputCopy OutputCopy
题目信息
题号
673F
时间限制
5 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。