PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000101. Cây động của Yuezheng Ling (Yuezheng Ling and Dynamic Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cây động của Yuezheng Ling (Yuezheng Ling and Dynamic Tree)

Yuezheng Ling and Dynamic Tree

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A tree has nn vertices rooted at 11. For each vertex ii with 2≤i≤n2\le i\le n, its parent is aia_i and 1≤ai<i1\le a_i<i. The tree changes over time. Process the following operations in order:

  • 1 l r x: For every index ii with l≤i≤rl\le i\le r, change the parent of vertex ii to max⁡(ai−x,1)\max(a_i-x,1). This changes parent links, not vertex weights.
  • 2 u v: Print the lowest common ancestor (LCA) of uu and vv in the current tree, after all preceding updates.

The parent index of every non-root vertex always remains smaller than its index; therefore the structure remains a rooted tree after each update.

Input

  • The first line contains n,qn,q.
  • The second line contains a2,a3,…,ana_2,a_3,\ldots,a_n.
  • Each of the next qq lines has the format 1 l r x or 2 u v. At least one type-2 query is guaranteed.

Output

For each type-2 operation, print the vertex index of the LCA on its own line.

Subtasks

  • Subtask 1 (10 points): 2≤n≤1002\le n\le100, 2≤q≤1502\le q\le150; 1≤ai<i1\le a_i<i; updates have 2≤l≤r≤n2\le l\le r\le n, 1≤x≤1051\le x\le10^5; queries have 1≤u,v≤n1\le u,v\le n; at least one type-2 query.
  • Subtask 2 (20 points): 2≤n,q≤1052\le n,q\le10^5; 1≤ai<i1\le a_i<i; only type-2 queries with 1≤u,v≤n1\le u,v\le n.
  • Subtask 3 (30 points): 2≤n,q≤300002\le n,q\le30000; 1≤ai<i1\le a_i<i; updates have 2≤l≤r≤n2\le l\le r\le n, 1≤x≤1051\le x\le10^5; queries have 1≤u,v≤n1\le u,v\le n; at least one type-2 query.
  • Subtask 4 (40 points): 2≤n,q≤1052\le n,q\le10^5; 1≤ai<i1\le a_i<i; updates have 2≤l≤r≤n2\le l\le r\le n, 1≤x≤1051\le x\le10^5; queries have 1≤u,v≤n1\le u,v\le n; at least one type-2 query.

Examples

Input

6 4
1 2 3 3 4
2 3 4
1 2 3 1
2 5 6
2 2 3

Output

3
3
1

Explanation

Initially the parents are a2=1,a3=2,a4=3,a5=3,a6=4a_2=1,a_3=2,a_4=3,a_5=3,a_6=4. Vertex 3 is an ancestor of vertex 4, hence LCA⁡(3,4)=3\operatorname{LCA}(3,4)=3. After 1 2 3 1, the parents of 2 and 3 are both 1. The paths from vertices 5 and 6 to the root are 5→3→15\to3\to1 and 6→4→3→16\to4\to3\to1, so their LCA is 3. Finally, LCA⁡(2,3)=1\operatorname{LCA}(2,3)=1.