CF643D.Bearish Fanpages

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a social website with nn fanpages, numbered 11 through nn . There are also nn companies, and the ii -th company owns the ii -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 ii follows jj and at the same time jj follows ii . Also, a fanpage can't follow itself.

Let’s say that fanpage ii follows some other fanpage j0j_{0} . Also, let’s say that ii is followed by kk other fanpages j1,j2,...,jkj_{1},j_{2},...,j_{k} . Then, when people visit fanpage ii they see ads from k+2k+2 distinct companies: i,j0,j1,...,jki,j_{0},j_{1},...,j_{k} . Exactly tit_{i} people subscribe (like) the ii -th fanpage, and each of them will click exactly one add. For each of k+1k+1 companies j0,j1,...,jkj_{0},j_{1},...,j_{k} , exactly people will click their ad. Remaining people will click an ad from company ii (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 ii follows fanpage fif_{i} . Your task is to handle qq queries of three types:

  • 1 i j — fanpage ii follows fanpage jj from now. It's guaranteed that ii didn't follow jj 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 ii -th company.
  • 3 — print two integers: the smallest income of one company and the biggest income of one company.

输入格式

The first line of the input contains two integers nn and qq ( 3<=n<=1000003<=n<=100000 , 1<=q<=1000001<=q<=100000 ) — the number of fanpages and the number of queries, respectively.

The second line contains nn integers t1,t2,...,tnt_{1},t_{2},...,t_{n} ( 1<=ti<=10121<=t_{i}<=10^{12} ) where tit_{i} denotes the number of people subscribing the ii -th fanpage.

The third line contains nn integers f1,f2,...,fnf_{1},f_{2},...,f_{n} ( 1<=fi<=n1<=f_{i}<=n ). Initially, fanpage ii follows fanpage fif_{i} .

Then, qq lines follow. The ii -th of them describes the ii -th query. The first number in the line is an integer typeitype_{i} ( 1<=typei<=31<=type_{i}<=3 ) — the type of the query.

There will be at most 5000050000 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.

输出格式

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.

输入输出样例

  • 输入#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
    

    输出#1

    10
    36
    28
    40
    36
    9
    57
    27
    28
    29
    9 57
    

说明/提示

In the sample test, there are 55 fanpages. The ii -th of them has i10i·10 subscribers.

On drawings, numbers of subscribers are written in circles. An arrow from AA to BB means that AA follows BB .

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

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

首页