cho.sh
Notes
Loading...

Tree and Queries 20

Time limit

5s

Memory limit

512 MB

Problem

There is a forest with NNN vertices. The vertices are numbered from 111 to NNN, and initially there are no edges. Each vertex vvv has an integer XvX_vXv​, and every initial value is 111.

Write a program that processes the following queries.

  • 1 a b c: connect vertices aaa and bbb with an edge of weight ccc. This query is given only when the graph remains a forest after it is performed.
  • 2 a b: remove the edge connecting vertices aaa and bbb. This query is given only when that edge exists.
  • 3 a: first replace XaX_aXa​ with 1−Xa1-X_a1−Xa​. Then, for the tree containing vertex aaa, let its vertices be v1,v2,…,vkv_1, v_2, \dots, v_kv1​,v2​,…,vk​ and output the minimum value below.
min⁡1≤i≤k{∑1≤j≤kdist(vi,vj)×Xvj}\min_{1 \le i \le k}\left\{ \sum_{1 \le j \le k} dist(v_i, v_j) \times X_{v_j} \right\}1≤i≤kmin​⎩⎨⎧​1≤j≤k∑​dist(vi​,vj​)×Xvj​​⎭⎬⎫​

Here, dist(vi,vj)dist(v_i, v_j)dist(vi​,vj​) is the sum of the weights of all edges on the path from viv_ivi​ to vjv_jvj​.

Input

The first line contains the number of vertices NNN and the number of queries QQQ. Each of the next QQQ lines contains one query.

The vertex numbers appearing in queries are encrypted and must be decoded before the query is executed. If the input vertex number is xxx and the sum of all outputs from type 333 queries processed so far is SSS, then the actual vertex number is

(x−1+S) mod N+1(x - 1 + S) \bmod N + 1(x−1+S)modN+1

Before the first type 333 query, use S=0S=0S=0. The edge weight ccc in a type 111 query is not encrypted.

Output

For each type 333 query, output the computed value on its own line, in the order the queries are given.

Constraints

  • 1≤N≤1051 \le N \le 10^51≤N≤105
  • 1≤Q≤3×1051 \le Q \le 3 \times 10^51≤Q≤3×105
  • 1≤a,b≤N1 \le a, b \le N1≤a,b≤N
  • a≠ba \ne ba=b
  • 1≤c≤1081 \le c \le 10^81≤c≤108