#ifdef EVAL
#include <bits/stdc++.h>
#define debug(x)
#else
#define debug(x) cout << __LINE__ << ": " << (#x) << " = " << x << endl;
#endif
#include <iostream>
#include <vector>
#include <algorithm>
#include "happy.h"
using namespace std;
using ll = long long;


const int MAX_N = 3e5 + 10;
const int MAX_LOG = 21;
vector<vector<pair<int, int>>> g(MAX_N);
vector<vector<int>> binup(MAX_LOG, vector<int>(MAX_N));
vector<int> values(MAX_N), tin(MAX_N), tout(MAX_N), d(MAX_N);
int timer = 0;


void dfs(int v, int p, int val) {
    binup[0][v] = p;
    values[v] = val;
    d[v] = d[p] + 1;
    for (int k = 1; k < MAX_LOG; k++) {
        binup[k][v] = binup[k - 1][ binup[k - 1][v] ];
    }

    tin[v] = ++timer;
    for (auto [u, h] : g[v]) {
        if (u == p) continue;
        dfs(u, v, h ^ val);
    }
    tout[v] = timer;
}


bool isParent(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}


int lca(int u, int v) {
    if (isParent(u, v)) return u;
    if (isParent(v, u)) return v;
    if (d[u] > d[v]) swap(u, v);

    for (int k = MAX_LOG - 1; k >= 0; k--) {
        if (d[v] - d[u] >= (1 << k)) {
            v = binup[k][v];
        }
    }

    for (int k = MAX_LOG - 1; k >= 0; k--) {
        if (binup[k][v] != binup[k][u]) {
            v = binup[k][v];
            u = binup[k][u];
        }
    }
    return binup[0][v];
}


struct Val {
    ll pref0, pref1, suf0, suf1, ans, val;
};


struct Node {
    Val current, original;
};


struct Segtree {
    
};


vector<ll> happy(int N, int Q, std::vector<int> u, std::vector<int> v, std::vector<int> h, std::vector<int> t, std::vector<int> l, std::vector<int> r) {
    vector<ll> a(N), pref(N + 1);

    for (int i = 0; i < u.size(); i++) {
        int U = u[i];
        int V = v[i];
        int H = h[i];
        g[U].push_back({V, H});
        g[V].push_back({U, H});
    }
    dfs(0, 0, 0);

    for (int i = 0; i < N; i++) {
        int next = i + 1;
        //int c = lca(i, next);
        a[i] = values[i] ^ values[next];
    }

    for (int i = 1; i <= N; i++) {
        pref[i] = a[i - 1] ^ pref[i - 1];
    } 

    vector<ll> answers(Q);
    vector<bool> begin(N, false), end(N, false);
    for (int i = 0; i < Q; i++) {
        int L = l[i];
        int R = r[i];
        int T = t[i];

        if (T == 1) {
            for (int v = L; v < R; v++) {
                begin[v] = true;
                end[v] = true;
            }
            begin[R] = true;
            if (L > 0) end[L - 1] = true;
        } else {
            for (int v = L; v < R; v++) {
                begin[v] = false;
                end[v] = false;
            }
            begin[R] = false;
            if (L > 0) end[L - 1] = false;
        }

        ll res = 0;
        for (int l = 0; l < N; l++) {
            if (!begin[l]) continue;
            for (int r = l; r < N; r++) {
                if (!end[r]) continue;
                res += pref[r + 1] ^ pref[l]; 
            }
        }
        answers[i] = res;
    } 

    return answers;
}


#ifndef EVAL
extern vector <long long int> happy (int N, int Q, vector <int> u, vector <int> v, vector <int> h, vector <int> t, vector <int> l, vector <int> r);
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,q;
    cin >> n >> q ;
    vector <int> u(n-1),v(n-1),h(n-1);
    for (int i=0; i<n-1; i++) {
        cin >> u[i] >> v[i] >> h[i] ;
    }
    vector <int> t(q),l(q),r(q);
    for (int i=0; i<q; i++) {
        cin >> t[i] >> l[i] >> r[i] ;
    }
    for (auto sum : happy(n,q,u,v,h,t,l,r)) {
        cout << sum << "\n";
    }
    return 0;
}
#endif
