Under construction

Still being built. Things may change

1D Spreadsheet

Time limit10sMemory limit512 MB

Summary
Given n cells that hold either a number or a link to another cell, process set, link, and range-sum queries on the resolved cell values.
Level

Hard8 of 10

Topics
Tree, Array, Implementation
Solved
No attempts yet

Problem

You have a summer internship at a large software company. Your assigned project is a new application: a 1D spreadsheet.

A 1D spreadsheet is an array of nn cells numbered from 1 to nn. Cell ii either contains an integer aia_i or a link to another cell lil_i, in which case cell ii is linked to cell lil_i. Cyclic link dependencies are not allowed. At no moment is there a sequence of cells i1,i2,…,iki_1, i_2, \ldots, i_k where i1i_1 is linked to i2i_2, ..., and iki_k is linked to i1i_1.

The value of a cell is defined as follows. If cell ii contains a number aia_i, then value(i)=aivalue(i) = a_i. Otherwise, value(i)=value(li)value(i) = value(l_i). To evaluate a cell, follow the chain of links from it until you reach a cell that contains a number. Since there are no cycles, value(i)value(i) is uniquely defined for every cell ii at all times.

Bill, a colleague, added a range sum feature. Users can ask for ∑i=lrvalue(i)\sum_{i = l}^{r} value(i) and can change cell contents. Testing showed that Bill's implementation is slow. Your task is to write a program that processes a sequence of queries on the spreadsheet faster.

Input

The first line contains two integers nn and qq (1≤n,q≤2⋅1051 \le n, q \le 2 \cdot 10^5), the number of cells and the number of queries.

The second line contains nn integers a1,…,ana_1, \ldots, a_n (−104≤ai≤104-10^4 \le a_i \le 10^4), the initial cell values. Initially no cell contains a link.

Each of the next qq lines is one of the following queries.

  • set i x: cell ii is assigned the number xx (−104≤x≤104-10^4 \le x \le 10^4).
  • link i j: cell ii is assigned a link to cell jj.
  • sum l r: compute ∑i=lrvalue(i)\sum_{i = l}^{r} value(i).

It is guaranteed that no cyclic dependencies occur at any point in time.

Output

For each sum l r query, print the answer on its own line, in the same order as the queries appear in the input.

Examples1

  1. Example 1

    Input
    5 7
    1 2 3 4 5
    sum 1 5
    link 3 5
    link 1 3
    link 4 2
    sum 1 5
    set 5 -1
    sum 1 5
    
    Expected output
    15
    19
    1