#include "happy.h"
std::vector<int>adj[2005];
long long dist[2005][2005];
bool deschis[2005];
void dfs(int nod,int ant,int sursa){
    dist[sursa][nod]=dist[sursa][ant]^dist[ant][nod];
    for(auto e:adj[nod]){
        if(e!=ant) dfs(e,nod,sursa);
    }
}
std::vector <long long int> 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) {
    int n=N,q=Q;
    for(int i=0;i<n-1;i++){
        adj[u[i]].push_back(v[i]);
        adj[v[i]].push_back(u[i]);
        dist[u[i]][v[i]]=dist[v[i]][u[i]]=h[i];
    }
    for(int i=0;i<n;i++){
        dfs(i,-1,i);
    }
    int k=0;
    std::vector<long long>ans;
    while(q--){
        for(int i=l[k];i<=r[k];i++){
            if(t[k]==1) deschis[i]=1;
            else deschis[i]=0;
        }
        long long r=0;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                if(deschis[i]==1&&deschis[j]==1){
                    r+=dist[i][j];
                }
            }
        }
        k++;
        ans.push_back(r);
    }
    return ans;
}
/**
6 5
0 1 1
1 2 2
2 3 3
2 4 4
4 5 2

1 1 2
1 2 3
2 2 2
2 4 5
1 0 5
**/
