#include "happy.h"
#include<bits/stdc++.h>
using namespace std;

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) 
{
    vector<long long>ans;
    vector<long long>pref(N+1);
    for(int i=1;i<=N;i++)
    {
        pref[i]=pref[i-1]^h[i-1];
    }
    set<int>opened;
    long long answer=0;
    for(int i=0;i<Q;i++)
    {
        if(t[i]==1)
        {
            if(opened.count(l[i]))
            {
                ans.push_back(answer);
            }
            else
            {
                for(auto it:opened)
                {
                    int tr=it;
                    int tt=l[i];
                    if(tr>tt)swap(tr,tt);
                    answer+=pref[tt+1]^pref[tr];
                }
                opened.insert(l[i]);
                ans.push_back(answer);
            }
        }
        else
        {
            if(!opened.count(l[i]))
            {
                ans.push_back(answer);
            }
            else
            {
                opened.erase(l[i]);
                for(auto it:opened)
                {
                    int tr=it;
                    int tt=l[i];
                    if(tr>tt)swap(tr,tt);
                    answer-=pref[tt+1]^pref[tr];
                }
                ans.push_back(answer);
            }
        }
    }

    return ans;
}
