Under construction

Still being built. Things may change

Distributed Matrix

Time limit1sMemory limit1024 MB

Summary
Nodes join a tree rooted at node 1 while others fail and recover; for each query, sum the time since last recovery over both root paths if both endpoints are powered.
Level

Medium7 of 10

Topics
Tree, Implementation
Solved
No attempts yet

Problem

The Matrix was modified to use as little energy as possible because of an energy shortage. There are nn Matrix nodes, numbered uniquely from 11 to nn. Initially, only the generator, node 11, has energy.

Nodes that need power are connected one by one to nodes that already have power, and they receive energy from them. The connections form a power network shaped as a tree. Some nodes can fail and recover after some time. A node that is already in the network never reconnects to another node, even if a node that indirectly powers it has failed.

The unreliability of a node is the time elapsed since its last recovery. If the node has never failed, it is the time since it joined the network. The generator joins at time 00.

You must process mm events. Event ii happens exactly ii seconds after the start and is one of the following types:

  • ! xix_i yiy_i: node yiy_i connects to node xix_i and starts receiving energy from it.
  • - xix_i: node xix_i fails and stops passing energy on.
  • + xix_i: node xix_i, which failed earlier, recovers and passes energy on again.
  • ? xix_i yiy_i: find how reliable the pair of nodes xix_i and yiy_i is.

For a query, check whether both nodes receive energy. A node receives energy if it is connected to the network and every node on the path from the generator to it, including itself, is working. If both nodes receive energy, output the total unreliability of all nodes that the work of at least one of xix_i or yiy_i depends on, that is, the nodes on the paths from the generator to them. A node shared by both paths is counted once.

Input

The first line contains two integers nn and mm (2⩽n,m⩽2×1052 \leqslant n, m \leqslant 2 \times 10^5), the number of nodes that need power and the number of events to process.

The ii-th of the next mm lines describes the query that happens at time ii. The query type symbol is followed by either one integer xix_i or two integers xix_i and yiy_i, separated by a space, depending on the type (1⩽xi,yi⩽n1 \leqslant x_i, y_i \leqslant n; xi≠yix_i \neq y_i).

It is guaranteed that in a type 1 query, node xix_i is already connected to the network and node yiy_i is not. For type 2 and type 3 queries, node xix_i fails only if it was working before, and it recovers only after the corresponding failure.

Output

After each type 4 query, print the answer on its own line. If at least one of xix_i and yiy_i does not receive energy, print -1. Otherwise, print the sum of the unreliabilities of all nodes on the paths from the generator to xix_i and yiy_i.

Hint

In the first example, the failure of the third node makes the answer to the second query ? 2 3 equal to -1. The answer to the first query is 66, because 33, 22, and 11 seconds have passed since the generator, the second node, and the third node joined the network, respectively. At the third query, 77 and 66 seconds have passed since the generator and the second node joined. The third node recovered exactly 11 second ago, giving the answer 1414.

In the second example, the failure of the second node makes the answer to the second query -1. The answer to the first query is calculated as in the first example. The answer to the third query is 7+1+5=137 + 1 + 5 = 13.

Examples2

  1. Example 1

    Input
    3 7
    ! 1 2
    ! 1 3
    ? 2 3
    - 3
    ? 2 3
    + 3
    ? 2 3
    
    Expected output
    6
    -1
    14
    
  2. Example 2

    Input
    3 7
    ! 1 2
    ! 2 3
    ? 1 3
    - 2
    ? 1 3
    + 2
    ? 1 3
    
    Expected output
    6
    -1
    13