Under construction

Still being built. Things may change

WCWBTT

Time limit20sMemory limit1024 MB

Summary
Maintain a rooted tree under parent changes and output the vertex set on each path query using set-union operations under strict cost limits.
Level

Hard9 of 10

Topics
Tree, Divide and conquer, Implementation
Solved
No attempts yet

Problem

This is an interactive problem.

You need to maintain a rooted tree with vertex 11 as the root. The tree has nn vertices, and the parent of vertex ii (2≤i≤n2 \le i \le n) is pip_i (1≤pi<i1 \le p_i < i).

You have to process qq queries, each in one of the following forms:

  • ? aa bb: Output the set SS of all vertices on the unique simple path from aa to bb.
  • = aa bb: Change the parent of aa to bb (pa←bp_a \leftarrow b). The vertices are guaranteed to still form a tree after the change, but b<ab<a is not guaranteed.

The set SS can be too large to print every element in each query.

To deal with this, a special computer maintains sets of integers and operates on them quickly. Initially the computer has only n+1n+1 sets S0,S1,…,SnS_0, S_1, \ldots, S_n, where S0=∅S_0 = \varnothing and Si={i}S_i = \{i\} for 1≤i≤n1 \le i \le n.

The computer supports only two operations.

  • + aa bb: Construct a new set Sc=Sa∪SbS_c = S_a \cup S_b with Sa∩Sb=∅S_a \cap S_b = \varnothing. The new ID cc is one more than the maximum ID of all sets. The cost is ∣Sa∣+∣Sb∣|S_a| + |S_b|.
  • ! kk x1x_1 x2x_2 …\ldots xkx_k: Print Sx1∪Sx2∪⋯∪SxkS_{x_1} \cup S_{x_2} \cup \cdots \cup S_{x_k} as the answer. You must ensure Sxi∩Sxj=∅S_{x_i} \cap S_{x_j} = \varnothing for all 1≤i<j≤k1 \le i < j \le k. The cost is kk.

The following limits apply.

  • The cost of each operation cannot exceed 70007000.
  • The total cost of all operations cannot exceed 7.5⋅1077.5 \cdot 10^7.
  • The total number of operations cannot exceed 5⋅1065 \cdot 10^6.

Input

The first line contains two integers nn and qq (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, 1≤q≤2.5⋅1041 \le q \le 2.5 \cdot 10^4).

The next line contains n−1n-1 integers p2,p3,…,pnp_2, p_3, \ldots, p_n (1≤pi<i1 \le p_i < i), the initial parent of each vertex.

Hint

The sample input and output only illustrate the interaction protocol. The blank lines and flush markers are there for readability, and you should not print them.

The figure corresponds to the sample test case.

  • S0=∅S_0 = \varnothing
  • S1={1}S_1 = \{1\}
  • S2={2}S_2 = \{2\}
  • S3={3}S_3 = \{3\}
  • S4={1,2}S_4 = \{1, 2\}
  • S5={1,2,3}S_5 = \{1, 2, 3\}
  • S6={1,2}S_6 = \{1, 2\}
  • S7={1,3}S_7 = \{1, 3\}

Examples1

  1. Example 1

    Input
    3 3
    1 2
    
    ? 2 3
    
    
    
    = 3 1
    
    
    ? 2 3
    
    
    
    Expected output
    + 1 2
    
    + 3 4
    ! 2 2 3
    <flush the output>
    
    + 1 2
    + 1 3
    
    ! 2 2 7
    <flush the output>