757_G. Can Bash Save the Day?

Whoa! You did a great job helping Team Rocket who managed to capture all the Pokemons sent by Bash. Meowth, part of Team Rocket, having already mastered the human language, now wants to become a master in programming as well. He agrees to free the Pokemons if Bash can answer his questions.

Initially, Meowth gives Bash a weighted tree containing n nodes and a sequence a1, a2..., an which is a permutation of 1, 2, ..., n. Now, Mewoth makes q queries of one of the following forms:

  * 1 l r v: meaning Bash should report <image>, where dist(a, b) is the length of the shortest path from node a to node b in the given tree. 
  * 2 x: meaning Bash should swap ax and ax + 1 in the given sequence. This new sequence is used for later queries. 



Help Bash to answer the questions!

Input

The first line contains two integers n and q (1 ≤ n ≤ 2·105, 1 ≤ q ≤ 2·105) — the number of nodes in the tree and the number of queries, respectively.

The next line contains n space-separated integers — the sequence a1, a2, ..., an which is a permutation of 1, 2, ..., n.

Each of the next n - 1 lines contain three space-separated integers u, v, and w denoting that there exists an undirected edge between node u and node v of weight w, (1 ≤ u, v ≤ n, u ≠ v, 1 ≤ w ≤ 106). It is guaranteed that the given graph is a tree.

Each query consists of two lines. First line contains single integer t, indicating the type of the query. Next line contains the description of the query: 

  * t = 1: Second line contains three integers a, b and c (1 ≤ a, b, c < 230) using which l, r and v can be generated using the formula given below: 
    * <image>, 
    * <image>, 
    * <image>. 
  * t = 2: Second line contains single integer a (1 ≤ a < 230) using which x can be generated using the formula given below: 
    * <image>. 



The ansi is the answer for the i-th query, assume that ans0 = 0. If the i-th query is of type 2 then ansi = ansi - 1. It is guaranteed that: 

  * for each query of type 1: 1 ≤ l ≤ r ≤ n, 1 ≤ v ≤ n, 
  * for each query of type 2: 1 ≤ x ≤ n - 1. 



The <image> operation means bitwise exclusive OR.

Output

For each query of type 1, output a single integer in a separate line, denoting the answer to the query.

Example

Input

5 5
4 5 1 3 2
4 2 4
1 3 9
4 1 4
4 5 2
1
1 5 4
1
22 20 20
2
38
2
39
1
36 38 38


Output

23
37
28

Note

In the sample, the actual queries are the following: 

  * 1 1 5 4
  * 1 1 3 3
  * 2 3
  * 2 2
  * 1 1 3 3

Example input:
5 5
4 5 1 3 2
4 2 4
1 3 9
4 1 4
4 5 2
1
1 5 4
1
22 20 20
2
38
2
39
1
36 38 38


Example output:
                                                              23
                                                              37
                                                              28


Tags: data structures
divide and conquer
graphs
trees
Difficulty rating: 3400

Solution:
#include <bits/stdc++.h>
using namespace std;
int loc[200005][20], grd[20];
int tar[400005], nex[400005], len[400005], fir[200005], cnt;
int vis[200005], siz[200005], A[200005];
long long dis[200005][20];
void Add(int a, int b, int c) {
  ++cnt;
  tar[cnt] = b;
  nex[cnt] = fir[a];
  len[cnt] = c;
  fir[a] = cnt;
}
void Dfs(int u, int p, int d) {
  siz[u] = 1;
  for (int i = fir[u]; i; i = nex[i]) {
    int v = tar[i];
    if (v == p || vis[v]) continue;
    dis[v][d] = dis[u][d] + len[i];
    Dfs(v, u, d);
    siz[u] += siz[v];
  }
}
void Divide(int u, int d) {
  Dfs(u, 0, d);
  int numz = siz[u], p = -1;
  bool flg = 1;
  while (flg) {
    flg = 0;
    for (int i = fir[u]; i; i = nex[i]) {
      int v = tar[i];
      if (vis[v] || v == p) continue;
      if (siz[v] * 2 > numz) {
        p = u;
        u = v;
        flg = 1;
        break;
      }
    }
  }
  dis[u][d] = 0;
  Dfs(u, 0, d);
  grd[d] = u;
  vis[u] = d + 1;
  for (int i = 0; i <= d; i++) loc[u][i] = grd[i];
  for (int i = fir[u]; i; i = nex[i]) {
    int v = tar[i];
    if (vis[v]) continue;
    Divide(v, d + 1);
  }
}
vector<long long> sum[200005];
vector<int> num[200005];
int pos[200005][20];
int n, q, u, v, x, t, l, r;
int main() {
  long long ans = 0, msk = (1ll << 30) - 1;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) scanf("%d", &A[i]);
  for (int i = 1; i < n; i++) {
    int u, v, x;
    scanf("%d%d%d", &u, &v, &x);
    Add(u, v, x), Add(v, u, x);
  }
  Divide(1, 0);
  for (int i = 1; i <= n; i++) {
    sum[i].push_back(0);
    num[i].push_back(-1);
  }
  for (int i = 0; i < n; i++) {
    int u = A[i];
    for (int j = 0; j < vis[u]; j++) {
      sum[loc[u][j]].push_back(sum[loc[u][j]].back() + dis[u][j] -
                               (j > 0 ? dis[u][j - 1] : 0));
      pos[u][j] = num[loc[u][j]].size();
      num[loc[u][j]].push_back(i);
    }
  }
  while (q--) {
    scanf("%d", &t);
    if (t == 1) {
      scanf("%d%d%d", &l, &r, &u);
      l = (msk & ans) ^ l;
      r = (msk & ans) ^ r;
      u = (msk & ans) ^ u;
      l--, r--;
      ans = 0;
      for (int i = 0; i < vis[u]; i++) {
        v = loc[u][i];
        int pr =
            upper_bound(num[v].begin(), num[v].end(), r) - num[v].begin() - 1;
        int pl =
            lower_bound(num[v].begin(), num[v].end(), l) - num[v].begin() - 1;
        if (pl == pr) break;
        ans += sum[v][pr] - sum[v][pl] +
               (pr - pl) * (dis[u][i] - (i > 0 ? dis[u][i - 1] : 0));
      }
      printf("%I64d\n", ans);
    } else {
      scanf("%d", &x);
      x = (msk & ans) ^ x;
      u = A[x], v = A[x - 1];
      for (int i = 0; i < max(vis[u], vis[v]); i++) {
        if (i < min(vis[u], vis[v]) && loc[u][i] == loc[v][i]) {
          sum[loc[u][i]][pos[v][i]] = sum[loc[u][i]][pos[v][i] + 1] -
                                      sum[loc[u][i]][pos[v][i]] +
                                      sum[loc[u][i]][pos[v][i] - 1];
          swap(pos[u][i], pos[v][i]);
        } else {
          if (i < vis[u]) num[loc[u][i]][pos[u][i]]--;
          if (i < vis[v]) num[loc[v][i]][pos[v][i]]++;
        }
      }
      swap(A[x], A[x - 1]);
    }
  }
}