Jumping Through the Array
Contest: Codeforces Round 755 (Div. 1, based on Technocup 2022 Elimination Round 2)
Difficulty rating: 3500
Tags: binary search, data structures, graphs, two pointers

You are given an array of integers $$$a$$$ of size $$$n$$$ and a permutation $$$p$$$ of size $$$n$$$. There are $$$q$$$ queries of three types coming to you:

1. For given numbers $$$l$$$ and $$$r$$$, calculate the sum in array $$$a$$$ on the segment from $$$l$$$ to $$$r$$$: $$$\sum\limits_{i=l}^{r} a_i$$$.
2. You are given two numbers $$$v$$$ and $$$x$$$. Let's build a directed graph from the permutation $$$p$$$: it has $$$n$$$ vertices and $$$n$$$ edges $$$i \to p_i$$$. Let $$$C$$$ be the set of vertices that are reachable from $$$v$$$ in this graph. You should add $$$x$$$ to all $$$a_u$$$ such that $$$u$$$ is in $$$C$$$.
3. You are given indices $$$i$$$ and $$$j$$$. You should swap $$$p_i$$$ and $$$p_j$$$.

The graph corresponding to the permutation $$$[2, 3, 1, 5, 4]$$$.

Please, process all queries and print answers to queries of type $$$1$$$.

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array and permutation.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^8 \le a_i \le 10^8$$$).

The third line contains $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$).

The fourth line contains a single integer $$$q$$$ — the number of queries ($$$1 \le q \le 2 \cdot 10^5$$$).

Next $$$q$$$ lines contain description of queries. The $$$i$$$-th of them starts with an integer $$$t_i$$$ ($$$1 \le t_i \le 3$$$) — the query type.

- If $$$t_i = 1$$$, then the $$$i$$$-th line also contains two integers $$$l$$$, $$$r$$$ ($$$1 \le l \le r \le n$$$).
- If $$$t_i = 2$$$, then the $$$i$$$-th line also contains two integers $$$v$$$, $$$x$$$ ($$$1 \le v \le n$$$, $$$-10^8 \le x \le 10^8$$$).
- If $$$t_i = 3$$$, then the $$$i$$$-th line also contains also two integers $$$i$$$, $$$j$$$ ($$$1 \le i, j \le n$$$).

Output format:
For every first type query, print a single integer — the answer to this query.

Constraints:
Time limit: 8.0 s
Memory limit: 512.0 MB

Note:
In the first example:

The graph corresponding to the initial permutation.

There are $$$6$$$ queries.

1. The sum on the segment from $$$1$$$ to $$$5$$$ is $$$a_1 + a_2 + a_3 + a_4 + a_5 = 6 + 9 + (-5) + 3 + 0 = 13$$$.
2. If we start from $$$1$$$, we can reach $$$\{1, 2, 3\}$$$. After this query $$$a$$$ is: $$$[7, 10, -4, 3, 0]$$$.
3. The sum on the segment from $$$1$$$ to $$$5$$$ is $$$a_1 + a_2 + a_3 + a_4 + a_5 = 6 + 9 + (-5) + 3 + 0 = 16$$$.
4. After this query $$$p = [4, 3, 1, 5, 2]$$$.   The graph corresponding to the new permutation.
5. If we start from $$$2$$$, we can reach $$$\{1, 2, 3, 4, 5\}$$$. After this query $$$a$$$ is: $$$[6, 9, -5, 2, -1]$$$.
6. The sum on the segment from $$$1$$$ to $$$5$$$ is $$$a_1 + a_2 + a_3 + a_4 + a_5 = 6 + 9 + (-5) + 2 + (-1) = 11$$$.

Example input:
5
6 9 -5 3 0
2 3 1 5 4
6
1 1 5
2 1 1
1 1 5
3 1 5
2 1 -1
1 1 5

Example output:
13
16
11

Example input:
8
-15 52 -4 3 5 9 0 5
2 4 6 8 1 3 5 7
10
2 2 2
2 5 -1
1 1 8
1 1 5
1 5 8
3 1 6
2 1 50
1 1 8
2 6 -20
1 1 8

Example output:
61
45
22
461
301

Editorial:
Let's call $$$B = \lfloor \sqrt{n} \rfloor$$$.
Let's divide an array $$$a$$$ into consecutive blocks of size $$$B$$$.
To answer the query we will have to sum $$$O(B)$$$ $$$a_i$$$ near the segment's bounds and $$$O(\frac{n}{B})$$$ sums on blocks. Let's try to calculate them fast.
There are two types of cycles:
• small: with length $$$< B$$$
• big: with length $$$\geq B$$$
If there is a second type query for the small cycle it is easy to make it in $$$O(B)$$$ time: let's iterate over cycle's elements and add $$$x$$$ into its $$$a_i$$$ and into its array's block sum.
It is harder to deal with big cycles. Let's divide each big cycle into blocks, each having the size in $$$[B, 2B - 1]$$$. Initially, it is possible. After each query of type $$$3$$$ it is possible to reconstruct this division fast:
• Let's split two blocks with $$$i$$$ and $$$j$$$. After that, it is possible to reconstruct the divisions of each new cycle into blocks.
• After that, we should avoid small blocks (with size $$$< B$$$). Let's merge two consecutive blocks if one of them has the size $$$< B$$$. After that, if the total block has size $$$\geq 2B$$$ let's split it into two equal blocks.
• Someone calls this method split-rebuild.
So, maintaining this structure we will have at most $$$\frac{n}{B}$$$ cycle blocks at any moment. If there is a second type query for the big cycle let's add $$$x$$$ to the cycle blocks for this cycle. To consider these values while answering the first type query let's maintain the last structure:
• For each cycle block $$$t$$$ let's calculate the values $$$pref_{t,0}, pref_{t,1}, \ldots, pref_{t,\lceil \frac{n}{B} \rceil}$$$, where $$$pref_{t,i}$$$ is the number of elements from the $$$t$$$-th cycle block in the first $$$i$$$ array's blocks.
Using these values it is easy to consider additions to the cycle blocks in the subsegments sums queries.
And the values of $$$pref$$$ can be recalculated during the cycle's updates because we make $$$O(1)$$$ splits and merges. During each split or merge we should recalculate $$$pref$$$ for $$$O(1)$$$ rows (and this can be done in $$$O(\frac{n}{B})$$$ for one $$$t$$$). Also during each split or merge we should zero additions to blocks in operations just pushing the added value into the elements of the block (their number is smaller than $$$2B$$$).
The total complexity of the solution is $$$O(n \sqrt{n})$$$.