#pragma once
/**
* Author: Teetat T.
* Description: Link Cut Tree (1-indexed)
*/template<intN,classT>structLinkCutTree{intch[N][2],par[N],lz[N],rev[N];Tval[N],sum[N],rsum[N];voidtoggle(intv){if(!v)return;swap(ch[v][0],ch[v][1]);swap(sum[v],rsum[v]);rev[v]^=1;}voidpush(intv){if(!v||!rev[v])return;toggle(ch[v][0]);toggle(ch[v][1]);rev[v]=0;}voidpull(intv){if(!v)return;sum[v]=sum[ch[v][0]]+val[v]+sum[ch[v][1]];rsum[v]=rsum[ch[v][0]]+val[v]+rsum[ch[v][1]];}boolis_root(intv){returnch[par[v]][0]!=v&&ch[par[v]][1]!=v;}boolpos(intv){returnch[par[v]][1]==v;}voidrotate(intv){intu=par[v],g=par[u];boolx=pos(v);if(!is_root(u))ch[g][pos(u)]=v;ch[u][x]=ch[v][!x];if(ch[u][x])par[ch[u][x]]=u;ch[v][!x]=u,par[u]=v,par[v]=g;pull(u),pull(v);}voidsplay(intv){if(!v)return;for(push(v);!is_root(v);rotate(v)){intu=par[v];if(is_root(u))push(u),push(v);elsepush(par[u]),push(u),push(v),rotate(pos(u)==pos(v)?u:v);}}voidaccess(intv){for(intu=v,c=0;u;u=par[u]){splay(u);ch[u][1]=c;pull(c=u);}splay(v);}voidevert(intv){access(v),toggle(v);}voidlink(intu,intv){evert(u);access(v);par[u]=v;}voidcut(intu,intv){evert(u);access(v);assert(par[u]==v);ch[v][0]=par[u]=0;pull(v);}Taggregate(intu,intv){evert(u);access(v);returnsum[v];}voidset(intu,Tv){evert(u);val[u]=v;pull(u);}};
#line 2 "src/tree/link-cut-tree.hpp"
/**
* Author: Teetat T.
* Description: Link Cut Tree (1-indexed)
*/template<intN,classT>structLinkCutTree{intch[N][2],par[N],lz[N],rev[N];Tval[N],sum[N],rsum[N];voidtoggle(intv){if(!v)return;swap(ch[v][0],ch[v][1]);swap(sum[v],rsum[v]);rev[v]^=1;}voidpush(intv){if(!v||!rev[v])return;toggle(ch[v][0]);toggle(ch[v][1]);rev[v]=0;}voidpull(intv){if(!v)return;sum[v]=sum[ch[v][0]]+val[v]+sum[ch[v][1]];rsum[v]=rsum[ch[v][0]]+val[v]+rsum[ch[v][1]];}boolis_root(intv){returnch[par[v]][0]!=v&&ch[par[v]][1]!=v;}boolpos(intv){returnch[par[v]][1]==v;}voidrotate(intv){intu=par[v],g=par[u];boolx=pos(v);if(!is_root(u))ch[g][pos(u)]=v;ch[u][x]=ch[v][!x];if(ch[u][x])par[ch[u][x]]=u;ch[v][!x]=u,par[u]=v,par[v]=g;pull(u),pull(v);}voidsplay(intv){if(!v)return;for(push(v);!is_root(v);rotate(v)){intu=par[v];if(is_root(u))push(u),push(v);elsepush(par[u]),push(u),push(v),rotate(pos(u)==pos(v)?u:v);}}voidaccess(intv){for(intu=v,c=0;u;u=par[u]){splay(u);ch[u][1]=c;pull(c=u);}splay(v);}voidevert(intv){access(v),toggle(v);}voidlink(intu,intv){evert(u);access(v);par[u]=v;}voidcut(intu,intv){evert(u);access(v);assert(par[u]==v);ch[v][0]=par[u]=0;pull(v);}Taggregate(intu,intv){evert(u);access(v);returnsum[v];}voidset(intu,Tv){evert(u);val[u]=v;pull(u);}};