#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);
    pref[0]=0;
    for(int i=1;i<N;i++)
    {
        pref[i]=pref[i-1]^h[i-1];
    }
    // for(int i=0;i<N;i++)
    // {
    //     cout<<pref[i]<<endl;
    // }
    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);
                    if(pref[tt]==0 && pref[min(tr-1,0)]==0)
                    {
                        answer+=h[tt];
                    }
                    else answer+=pref[tt]^pref[min(tr-1,0)];
                }
                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);
                    if(pref[tt]==0 && pref[min(tr-1,0)]==0)
                    {
                        answer-=h[tt];
                    }
                    else answer-=pref[tt]^pref[min(tr-1,0)];
                }
                ans.push_back(answer);
            }
        }
    }

    return ans;
}
