#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include<cassert>
#include"../../../utilities/fast_io.hpp"
#include<random>
#include<utility>
#include<vector>#include"../../../acted_monoid/range_add_range_sum.hpp"
#include"../../../ds/dynamic_tree/lazy_link_cut_tree.hpp"usingAddSum=m1une::acted_monoid::RangeAddRangeSum<longlong>;usingNode=AddSum::value_type;voidtest_vertex_path_and_subtree_updates(){m1une::ds::LazyLinkCutTree<AddSum>lct(std::vector<longlong>{1,2,3,4,5});assert(lct.link(0,1));assert(lct.link(1,2));assert(lct.link(1,3));assert(lct.link(3,4));Nodesubtree=lct.subtree_prod(0,1);assert(subtree.sum==14);assert(subtree.size==4);assert(lct.subtree_size(0,1)==4);lct.apply(2,4,10);Nodepath=lct.path_prod(2,4);assert(path.sum==54);assert(path.size==4);subtree=lct.subtree_prod(0,1);assert(subtree.sum==54);assert(subtree.size==4);assert(lct.get(0).sum==1);assert(lct.subtree_prod(1,3).sum==29);lct.apply(0,3,5);subtree=lct.subtree_prod(0,1);assert(subtree.sum==64);assert(subtree.size==4);assert(lct.path_prod(2,4).sum==64);subtree=lct.subtree_prod(2,1);assert(subtree.sum==57);assert(subtree.size==4);}voidtest_edge_path_and_subtree_updates(){m1une::ds::LazyLinkCutTree<AddSum>lct(3);inte01=lct.link_edge(0,1,5);inte12=lct.link_edge(1,2,7);assert(e01==0);assert(e12==1);Nodefull=lct.subtree_prod(0,0);assert(full.sum==12);assert(full.size==2);Nodechild=lct.subtree_prod(0,1);assert(child.sum==7);assert(child.size==1);assert(lct.subtree_size(0,1)==3);lct.apply(0,2,3);assert(lct.path_prod(0,2).sum==18);assert(lct.subtree_prod(0,1).sum==10);lct.apply(1,2,2);assert(lct.get_edge(e01).sum==8);assert(lct.get_edge(e12).sum==12);assert(lct.subtree_prod(0,0).sum==20);assert(lct.cut_edge(e01));assert(!lct.connected(0,2));}voidtest_rooted_tree_utility_apis(){m1une::ds::LazyLinkCutTree<AddSum>lct(std::vector<longlong>{1,2,3,4,5});assert(lct.link_parent(1,0));assert(lct.link_parent(2,0));assert(lct.link_parent(3,2));assert(lct.link_parent(4,2));assert(lct.root(4)==0);lct.reroot(0);Nodecomponent=lct.component_prod(0);assert(component.sum==15);assert(component.size==5);assert(lct.component_size(0)==5);assert(lct.child_toward(0,4)==2);assert(lct.child_toward(2,4)==4);assert(lct.child_toward(4,0)==2);Nodebranch=lct.branch_prod(0,4);assert(branch.sum==12);assert(branch.size==3);assert(lct.branch_size(0,4)==3);assert(lct.parent(0,4)==2);assert(lct.parent(0,2)==0);assert(lct.parent(0,0)==-1);Nodesubtree=lct.subtree_prod(0,2);assert(subtree.sum==12);assert(subtree.size==3);assert(lct.subtree_size(0,2)==3);Nodeexcluding=lct.subtree_prod_excluding_child(0,2,4);assert(excluding.sum==7);assert(excluding.size==2);assert(lct.subtree_size_excluding_child(0,2,4)==2);lct.reroot(0);assert(lct.cut_parent(4));assert(!lct.connected(4,0));component=lct.component_prod(0);assert(component.sum==10);assert(component.size==4);assert(lct.component_size(0)==4);component=lct.component_prod(4);assert(component.sum==5);assert(component.size==1);assert(lct.component_size(4)==1);m1une::ds::LazyLinkCutTree<AddSum>lct2(std::vector<longlong>{1,2,3,4,5});assert(lct2.link_parent(1,0));assert(lct2.link_parent(2,0));assert(lct2.link_parent(3,2));assert(lct2.link_parent(4,2));lct2.apply(3,4,10);lct2.reroot(0);assert(lct2.cut_parent(2));assert(!lct2.connected(2,0));component=lct2.component_prod(0);assert(component.sum==3);assert(component.size==2);assert(lct2.component_size(0)==2);component=lct2.component_prod(2);assert(component.sum==42);assert(component.size==3);assert(lct2.component_size(2)==3);}boolnaive_connected(conststd::vector<std::vector<int>>&adj,ints,intt){std::vector<int>parent(adj.size(),-1);std::vector<int>stack;parent[s]=s;stack.push_back(s);for(intit=0;it<int(stack.size());it++){intv=stack[it];if(v==t)returntrue;for(intto:adj[v]){if(parent[to]!=-1)continue;parent[to]=v;stack.push_back(to);}}returnfalse;}std::vector<int>naive_path_vertices(conststd::vector<std::vector<int>>&adj,ints,intt){std::vector<int>parent(adj.size(),-1);std::vector<int>stack;parent[s]=s;stack.push_back(s);for(intit=0;it<int(stack.size());it++){intv=stack[it];if(v==t)break;for(intto:adj[v]){if(parent[to]!=-1)continue;parent[to]=v;stack.push_back(to);}}assert(parent[t]!=-1);std::vector<int>path;for(intv=t;v!=s;v=parent[v])path.push_back(v);path.push_back(s);returnpath;}longlongnaive_path_sum(conststd::vector<std::vector<int>>&adj,conststd::vector<longlong>&value,ints,intt){longlongres=0;for(intv:naive_path_vertices(adj,s,t))res+=value[v];returnres;}longlongnaive_subtree_sum(conststd::vector<std::vector<int>>&adj,conststd::vector<longlong>&value,introot,intv){std::vector<int>parent(adj.size(),-1);std::vector<int>stack;parent[root]=root;stack.push_back(root);for(intit=0;it<int(stack.size());it++){intx=stack[it];for(intto:adj[x]){if(parent[to]!=-1)continue;parent[to]=x;stack.push_back(to);}}assert(parent[v]!=-1);longlongres=0;stack.clear();stack.push_back(v);while(!stack.empty()){intx=stack.back();stack.pop_back();res+=value[x];for(intto:adj[x]){if(to==parent[x])continue;stack.push_back(to);}}returnres;}voidtest_random_vertex_path_updates(){constexprintn=8;std::vector<longlong>initial;std::vector<longlong>value;for(inti=0;i<n;i++){initial.push_back(i+1);value.push_back(i+1);}m1une::ds::LazyLinkCutTree<AddSum>lct(initial);std::vector<std::vector<int>>adj(n);std::vector<std::pair<int,int>>edges;std::mt19937rng(2);for(intstep=0;step<700;step++){intop=int(rng()%6);intu=int(rng()%n);intv=int(rng()%n);if(op==0){if(u!=v&&!naive_connected(adj,u,v)){assert(lct.link(u,v));adj[u].push_back(v);adj[v].push_back(u);edges.emplace_back(u,v);}}elseif(op==1){if(!edges.empty()){intid=int(rng()%edges.size());auto[a,b]=edges[id];assert(lct.cut(a,b));for(inti=0;i<int(adj[a].size());i++){if(adj[a][i]==b){adj[a][i]=adj[a].back();adj[a].pop_back();break;}}for(inti=0;i<int(adj[b].size());i++){if(adj[b][i]==a){adj[b][i]=adj[b].back();adj[b].pop_back();break;}}edges[id]=edges.back();edges.pop_back();}}elseif(op==2){longlongx=int(rng()%200)-100;value[u]=x;lct.set(u,x);}elseif(u!=v&&naive_connected(adj,u,v)){if(op==3){longlongadd=int(rng()%21)-10;lct.apply(u,v,add);for(intx:naive_path_vertices(adj,u,v))value[x]+=add;}elseif(op==4){assert(lct.path_prod(u,v).sum==naive_path_sum(adj,value,u,v));}else{assert(lct.subtree_prod(u,v).sum==naive_subtree_sum(adj,value,u,v));assert(lct.subtree_prod(v,u).sum==naive_subtree_sum(adj,value,v,u));}}}}intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;test_vertex_path_and_subtree_updates();test_edge_path_and_subtree_updates();test_rooted_tree_utility_apis();test_random_vertex_path_updates();longlonga,b;fast_input>>a>>b;fast_output<<a+b<<'\n';}
#line 1 "verify/ds/dynamic_tree/lazy_link_cut_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include<cassert>
#line 1 "utilities/fast_io.hpp"
#include<algorithm>
#include<array>
#include<cerrno>
#include<charconv>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstdint>
#include<cstring>
#include<iterator>
#include<string>
#include<sys/stat.h>
#include<type_traits>
#include<utility>
#include<unistd.h>
#include<vector>namespacem1une{namespaceutilities{structFastOutput;namespaceinternal{// Shared with the convenience helpers in template.hpp.inlineFastOutput*standard_output_instance=nullptr;// Detect std::begin(x), std::end(x).template<classT,class=void>structis_range:std::false_type{};template<classT>structis_range<T,std::void_t<decltype(std::begin(std::declval<T&>())),decltype(std::end(std::declval<T&>()))>>:std::true_type{};template<classT>inlineconstexprboolis_range_v=is_range<T>::value;template<classT>usingrange_reference_t=decltype(*std::begin(std::declval<T&>()));template<classT>usingrange_value_t=std::remove_cv_t<std::remove_reference_t<range_reference_t<T>>>;template<classT,class=void>structrange_stored_value{usingtype=range_value_t<T>;};template<classT>structrange_stored_value<T,std::void_t<typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type>>{usingtype=typenamestd::remove_cv_t<std::remove_reference_t<T>>::value_type;};template<classT>usingrange_stored_value_t=typenamerange_stored_value<T>::type;// Treat strings and C strings as scalar output objects, not as ranges.template<classT>structis_char_array:std::false_type{};template<classT,std::size_tN>structis_char_array<T[N]>:std::bool_constant<std::is_same_v<std::remove_cv_t<T>,char>>{};template<classT>structis_string_like:std::bool_constant<std::is_same_v<std::decay_t<T>,std::string>||std::is_same_v<std::decay_t<T>,constchar*>||std::is_same_v<std::decay_t<T>,char*>||is_char_array<std::remove_reference_t<T>>::value>{};template<classT>inlineconstexprboolis_string_like_v=is_string_like<T>::value;// ModInt-like type: x.val() is printable, and x can be assigned from long long.template<classT,class=void>structhas_val_method:std::false_type{};template<classT>structhas_val_method<T,std::void_t<decltype(std::declval<constT&>().val())>>:std::true_type{};template<classT>inlineconstexprboolhas_val_method_v=has_val_method<T>::value;template<classT,class=void>structhas_static_mod_raw:std::false_type{};template<classT>structhas_static_mod_raw<T,std::void_t<decltype(T::mod()),decltype(T::raw(std::declval<uint32_t>()))>>:std::true_type{};template<classT>inlineconstexprboolhas_static_mod_raw_v=has_static_mod_raw<T>::value;// libstdc++ before GCC 16 does not classify __int128 as an integral type in// strict ISO modes such as -std=c++23. Keep the fast-I/O interface independent// of that implementation detail.template<classT>inlineconstexprboolis_integral_v=std::is_integral_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>||std::is_same_v<std::remove_cv_t<T>,__uint128_t>;template<classT>inlineconstexprboolis_signed_v=std::is_signed_v<T>||std::is_same_v<std::remove_cv_t<T>,__int128_t>;template<classT>structmake_unsigned{usingtype=std::make_unsigned_t<T>;};template<>structmake_unsigned<__int128_t>{usingtype=__uint128_t;};template<>structmake_unsigned<__uint128_t>{usingtype=__uint128_t;};template<classT>usingmake_unsigned_t=typenamemake_unsigned<std::remove_cv_t<T>>::type;}// namespace internalstructFastInput{staticconstexprintbuffer_size=1<<20;private:std::FILE*_stream;char_buffer[buffer_size];int_position;int_length;int_file_descriptor;bool_streaming;boolrefill(){_position=0;if(_streaming){ssize_tlength;do{length=::read(_file_descriptor,_buffer,buffer_size);}while(length<0&&errno==EINTR);if(length<=0){_length=0;returnfalse;}_length=int(length);}else{_length=int(std::fread(_buffer,1,buffer_size,_stream));}return_length!=0;}template<classT>boolread_integer_from_stream(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'){negative=true;c=read_char_raw();}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){result=negative?result*10-(c-'0'):result*10+(c-'0');c=read_char_raw();}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){result=result*10+T(c-'0');c=read_char_raw();}value=negative?T(0)-result:result;}returntrue;}boolprepare_number(){if(_length-_position>=64)returntrue;constintremaining=_length-_position;if(remaining>0)std::memmove(_buffer,_buffer+_position,remaining);constintadded=int(std::fread(_buffer+remaining,1,buffer_size-remaining,_stream));_position=0;_length=remaining+added;if(_length<buffer_size)_buffer[_length]='\0';return_length!=0;}public:explicitFastInput(std::FILE*stream=stdin):_stream(stream),_position(0),_length(0),_file_descriptor(::fileno(stream)),_streaming([&]{structstatstatus;return_file_descriptor>=0&&::fstat(_file_descriptor,&status)==0&&!S_ISREG(status.st_mode);}()){}FastInput(constFastInput&)=delete;FastInput&operator=(constFastInput&)=delete;intread_char_raw(){if(_position==_length&&!refill())returnEOF;return_buffer[_position++];}boolskip_spaces(){intc=read_char_raw();while(c!=EOF&&c<=' ')c=read_char_raw();if(c==EOF)returnfalse;--_position;returntrue;}boolread(char&value){if(!skip_spaces())returnfalse;value=char(read_char_raw());returntrue;}boolread(std::string&value){if(!skip_spaces())returnfalse;value.clear();while(true){constintbegin=_position;while(_position<_length&&static_cast<unsignedchar>(_buffer[_position])>' '){++_position;}value.append(_buffer+begin,_position-begin);if(_position<_length){++_position;returntrue;}if(!refill())returntrue;}}boolread(bool&value){intx;if(!read(x))returnfalse;value=x!=0;returntrue;}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>,bool>read(T&value){if(_streaming)returnread_integer_from_stream(value);if(!prepare_number())returnfalse;intc=static_cast<unsignedchar>(_buffer[_position++]);while(c<=' ')c=static_cast<unsignedchar>(_buffer[_position++]);boolnegative=false;if(c=='-'){negative=true;c=static_cast<unsignedchar>(_buffer[_position++]);}ifconstexpr(internal::is_signed_v<T>){Tresult=0;while('0'<=c&&c<='9'){constintfirst=c-'0';constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=negative?result*100-(first*10+second):result*100+(first*10+second);++_position;}else{result=negative?result*10-first:result*10+first;}c=static_cast<unsignedchar>(_buffer[_position++]);}value=result;}else{Tresult=0;while('0'<=c&&c<='9'){constunsignedfirst=unsigned(c-'0');constintsecond=static_cast<unsignedchar>(_buffer[_position])-'0';if(0<=second&&second<=9){result=result*100+T(first*10+unsigned(second));++_position;}else{result=result*10+T(first);}c=static_cast<unsignedchar>(_buffer[_position++]);}value=negative?T(0)-result:result;}if(_position>_length)_position=_length;returntrue;}template<classT>std::enable_if_t<std::is_floating_point_v<T>,bool>read(T&value){if(!skip_spaces())returnfalse;intc=read_char_raw();boolnegative=false;if(c=='-'||c=='+'){negative=c=='-';c=read_char_raw();}longdoubleresult=0;while('0'<=c&&c<='9'){result=result*10+(c-'0');c=read_char_raw();}if(c=='.'){longdoubleplace=0.1L;c=read_char_raw();while('0'<=c&&c<='9'){result+=(c-'0')*place;place*=0.1L;c=read_char_raw();}}if(c=='e'||c=='E'){c=read_char_raw();boolexponent_negative=false;if(c=='-'||c=='+'){exponent_negative=c=='-';c=read_char_raw();}intexponent=0;while('0'<=c&&c<='9'){exponent=exponent*10+(c-'0');c=read_char_raw();}longdoublescale=1;longdoublepower=10;while(exponent>0){if(exponent&1)scale*=power;power*=power;exponent>>=1;}result=exponent_negative?result/scale:result*scale;}value=static_cast<T>(negative?-result:result);returntrue;}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>,bool>read(T&value){longlongx;if(!read(x))returnfalse;ifconstexpr(internal::has_static_mod_raw_v<T>){if(x>=0&&uint64_t(x)<uint64_t(T::mod())){value=T::raw(uint32_t(x));}else{value=T(x);}}else{value=T(x);}returntrue;}template<classFirst,classSecond>boolread(std::pair<First,Second>&value){if(!read(value.first))returnfalse;returnread(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>,bool>read(Range&range){usingStoredValue=internal::range_stored_value_t<Range>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;for(auto&&value:range){ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){boolx;if(!read(x))returnfalse;value=x;}else{if(!read(value))returnfalse;}}returntrue;}template<classFirst,classSecond,class...Rest>boolread(First&first,Second&second,Rest&...rest){if(!read(first))returnfalse;returnread(second,rest...);}template<classT>FastInput&operator>>(T&value){if(!read(value))std::abort();return*this;}};structFastOutput{staticconstexprintbuffer_size=1<<20;private:inlinestaticconstautodigit_quads=[]{std::array<char,40000>result{};for(inti=0;i<10000;i++){intvalue=i;for(intj=3;j>=0;j--){result[4*i+j]=char('0'+value%10);value/=10;}}returnresult;}();std::FILE*_stream;char_buffer[buffer_size];int_position;int_precision;std::chars_format_float_format;char_range_separator;std::string*_capture=nullptr;template<classT>std::stringformat_cell(constT&value){std::stringresult;structCaptureGuard{std::string*⌖std::string*previous;~CaptureGuard(){target=previous;}}guard{_capture,_capture};_capture=&result;write(value);returnresult;}template<classMatrix>voidwrite_aligned_matrix(constMatrix&matrix){std::vector<std::vector<std::string>>rows;std::vector<std::size_t>widths;for(constauto&row:matrix){auto&cells=rows.emplace_back();std::size_tcolumn=0;for(constauto&value:row){cells.push_back(format_cell(value));if(column==widths.size())widths.push_back(0);widths[column]=std::max(widths[column],cells.back().size());++column;}}boolfirst=true;for(constauto&row:rows){if(!first)write_char('\n');first=false;for(std::size_tcolumn=0;column<row.size();++column){if(column!=0)write_char(_range_separator);for(std::size_tpadding=row[column].size();padding<widths[column];++padding){write_char(' ');}write(row[column]);}}}public:explicitFastOutput(std::FILE*stream=stdout):_stream(stream),_position(0),_precision(6),_float_format(std::chars_format::general),_range_separator(' '){if(_stream==stdout&&internal::standard_output_instance==nullptr){internal::standard_output_instance=this;}}FastOutput(constFastOutput&)=delete;FastOutput&operator=(constFastOutput&)=delete;~FastOutput(){flush();if(internal::standard_output_instance==this){internal::standard_output_instance=nullptr;}}voidflush(){if(_position!=0){std::fwrite(_buffer,1,_position,_stream);_position=0;}std::fflush(_stream);}voidwrite_char(charc){if(_capture!=nullptr){_capture->push_back(c);return;}if(_position==buffer_size)flush();_buffer[_position++]=c;}voidwrite(constchar*s){while(*s!='\0')write_char(*s++);}voidwrite(conststd::string&s){if(_capture!=nullptr){_capture->append(s);return;}std::size_tposition=0;while(position<s.size()){if(_position==buffer_size)flush();conststd::size_tcopied=std::min<std::size_t>(buffer_size-_position,s.size()-position);std::memcpy(_buffer+_position,s.data()+position,copied);_position+=int(copied);position+=copied;}}voidwrite(charc){write_char(c);}voidwrite(boolvalue){write_char(value?'1':'0');}template<classT>std::enable_if_t<std::is_floating_point_v<T>>write(Tvalue){chardigits[128];auto[end,error]=std::to_chars(digits,digits+sizeof(digits),value,_float_format,_precision);if(error!=std::errc())std::abort();for(constchar*pointer=digits;pointer!=end;pointer++){write_char(*pointer);}}template<classT>std::enable_if_t<internal::is_integral_v<T>&&!std::is_same_v<std::remove_cv_t<T>,bool>&&!std::is_same_v<std::remove_cv_t<T>,char>>write(Tvalue){usingRaw=std::remove_cv_t<T>;usingUnsigned=internal::make_unsigned_t<Raw>;Unsignedmagnitude;ifconstexpr(internal::is_signed_v<Raw>){if(value<0){write_char('-');magnitude=Unsigned(0)-Unsigned(value);}else{magnitude=Unsigned(value);}}else{magnitude=value;}if(magnitude==0){write_char('0');return;}unsignedchunks[16];intcount=0;while(magnitude>=10000){constUnsignedquotient=magnitude/10000;chunks[count++]=unsigned(magnitude-quotient*10000);magnitude=quotient;}if(_capture==nullptr&&_position>buffer_size-64)flush();charcaptured[64];char*constbegin=_capture!=nullptr?captured:_buffer+_position;char*destination=begin;constunsignedleading=unsigned(magnitude);constchar*first=digit_quads.data()+4*leading;intskip=leading<10?3:leading<100?2:leading<1000?1:0;for(;skip<4;skip++)*destination++=first[skip];while(count--){constchar*digits=digit_quads.data()+4*chunks[count];std::memcpy(destination,digits,4);destination+=4;}if(_capture!=nullptr){_capture->append(begin,destination-begin);}else{_position+=int(destination-begin);}}template<classT>std::enable_if_t<internal::has_val_method_v<T>&&!internal::is_integral_v<T>&&!internal::is_range_v<T>>write(constT&value){write(value.val());}template<classFirst,classSecond>voidwrite(conststd::pair<First,Second>&value){write(value.first);write_char(' ');write(value.second);}template<classRange>std::enable_if_t<internal::is_range_v<Range>&&!internal::is_string_like_v<Range>>write(constRange&range){usingStoredValue=internal::range_stored_value_t<constRange>;constexprboolnested=internal::is_range_v<StoredValue>&&!internal::is_string_like_v<StoredValue>;boolfirst=true;for(constauto&value:range){if(!first)write_char(nested?'\n':_range_separator);first=false;ifconstexpr(std::is_same_v<StoredValue,bool>&&!nested){write(static_cast<bool>(value));}else{write(value);}}}template<classFirst,class...Rest>voidprint(constFirst&first,constRest&...rest){write(first);((write_char(' '),write(rest)),...);}voidprintln(){write_char('\n');}voidset_precision(intprecision){_precision=precision;}voidset_fixed(intprecision=6){_float_format=std::chars_format::fixed;_precision=precision;}voidset_general(intprecision=6){_float_format=std::chars_format::general;_precision=precision;}voidset_range_separator(charseparator){_range_separator=separator;}template<classMatrix>voidwrite_aligned(constMatrix&matrix){usingRow=internal::range_stored_value_t<constMatrix>;usingCell=internal::range_stored_value_t<constRow>;static_assert(internal::is_range_v<Row>&&!internal::is_string_like_v<Row>,"write_aligned requires a two-dimensional range");static_assert(!internal::is_range_v<Cell>||internal::is_string_like_v<Cell>,"write_aligned requires scalar cells");write_aligned_matrix(matrix);}template<classMatrix>voidprintln_aligned(constMatrix&matrix){write_aligned(matrix);write_char('\n');}template<class...Args>voidprintln(constArgs&...args){print(args...);write_char('\n');}template<classT>FastOutput&operator<<(constT&value){write(value);return*this;}};}// namespace utilities}// namespace m1une#line 5 "verify/ds/dynamic_tree/lazy_link_cut_tree.test.cpp"
#include<random>
#line 8 "verify/ds/dynamic_tree/lazy_link_cut_tree.test.cpp"
#line 1 "acted_monoid/range_add_range_sum.hpp"
namespacem1une{namespaceacted_monoid{template<typenameT>structRangeAddRangeSumNode{Tsum;longlongsize;};template<typenameT>structRangeAddRangeSum{usingvalue_type=RangeAddRangeSumNode<T>;usingoperator_type=T;staticconstexprboolcommutative=true;staticconstexprbooloperator_commutative=true;// Value Monoid (Sum)staticconstexprvalue_typeid(){return{T(0),0};}staticconstexprvalue_typeop(constvalue_type&a,constvalue_type&b){return{a.sum+b.sum,a.size+b.size};}staticconstexprvalue_typeinv(constvalue_type&x){return{-x.sum,-x.size};}// Operator Monoid (Add)staticconstexproperator_typeop_id(){return0;}staticconstexproperator_typeop_comp(constoperator_type&f,constoperator_type&g){returnf+g;}// Mapping (sum + f * size)staticconstexprvalue_typemapping(constoperator_type&f,constvalue_type&x){return{x.sum+f*x.size,x.size};}// Helper for initializing a leaf nodestaticconstexprvalue_typemake(constT&val){return{val,1};}};}// namespace acted_monoid}// namespace m1une#line 1 "ds/dynamic_tree/lazy_link_cut_tree.hpp"
#line 5 "ds/dynamic_tree/lazy_link_cut_tree.hpp"
#include<concepts>
#line 9 "ds/dynamic_tree/lazy_link_cut_tree.hpp"
#line 1 "acted_monoid/concept.hpp"
#line 5 "acted_monoid/concept.hpp"
namespacem1une{namespaceacted_monoid{// Concept defining the requirements for an Acted Monoid.template<typenameAM>conceptIsActedMonoid=requires(typenameAM::value_typea,typenameAM::value_typeb,typenameAM::operator_typef,typenameAM::operator_typeg){// 1. Value MonoidtypenameAM::value_type;{AM::id()}->std::same_as<typenameAM::value_type>;{AM::op(a,b)}->std::same_as<typenameAM::value_type>;// 2. Operator MonoidtypenameAM::operator_type;{AM::op_id()}->std::same_as<typenameAM::operator_type>;{AM::op_comp(f,g)}->std::same_as<typenameAM::operator_type>;// Composition order: f(g(x))// 3. Mapping: Operator x Value -> Value{AM::mapping(f,a)}->std::same_as<typenameAM::value_type>;};// Concept for acted monoids whose value monoid is a commutative group.// The value operation must obey commutativity and inverse laws.template<typenameAM>conceptIsCommutativeActedGroup=IsActedMonoid<AM>&&requires(typenameAM::value_typea){{AM::inv(a)}->std::same_as<typenameAM::value_type>;};}// namespace acted_monoid}// namespace m1une#line 11 "ds/dynamic_tree/lazy_link_cut_tree.hpp"
namespacem1une{namespaceds{template<m1une::acted_monoid::IsCommutativeActedGroupActedGroup>structLazyLinkCutTree{usingT=typenameActedGroup::value_type;usingF=typenameActedGroup::operator_type;private:structNode{intleft=-1;intright=-1;intparent=-1;boolrev=false;intsize=1;intvirtual_size=0;intrake_size=0;intall_size=1;Tvalue=ActedGroup::id();Tprod=ActedGroup::id();Trev_prod=ActedGroup::id();Tvirtual_prod=ActedGroup::id();Trake_prod=ActedGroup::id();Tall_prod=ActedGroup::id();Flazy=ActedGroup::op_id();};structEdgeInfo{intu=-1;intv=-1;intnode=-1;boolalive=false;};std::vector<Node>_nodes;std::vector<EdgeInfo>_edges;std::vector<int>_path_buffer;staticTmake_node_value(constT&value,int){returnvalue;}staticTmake_node_value(T&&value,int){returnstd::move(value);}template<classU>requires(!std::same_as<U,T>)&&(requires(Ux){ActedGroup::make(x);}||requires(Ux,inti){ActedGroup::make(x,i);}||std::convertible_to<U,T>)staticTmake_node_value(constU&value,intindex){ifconstexpr(requires(Ux){ActedGroup::make(x);}){returnActedGroup::make(value);}elseifconstexpr(requires(Ux,inti){ActedGroup::make(x,i);}){returnActedGroup::make(value,index);}else{returnstatic_cast<T>(value);}}intchild_size(intnode)const{returnnode==-1?0:_nodes[node].size;}intchild_all_size(intnode)const{returnnode==-1?0:_nodes[node].all_size;}intchild_rake_size(intnode)const{returnnode==-1?0:_nodes[node].rake_size;}Tchild_prod(intnode)const{returnnode==-1?ActedGroup::id():_nodes[node].prod;}Tchild_rev_prod(intnode)const{returnnode==-1?ActedGroup::id():_nodes[node].rev_prod;}Tchild_all_prod(intnode)const{returnnode==-1?ActedGroup::id():_nodes[node].all_prod;}Tchild_rake_prod(intnode)const{returnnode==-1?ActedGroup::id():_nodes[node].rake_prod;}Tnode_subtree_prod(intnode)const{constNode&x=_nodes[node];returnActedGroup::op(x.value,x.virtual_prod);}intnode_subtree_size(intnode)const{return1+_nodes[node].virtual_size;}boolis_splay_root(intnode)const{intparent=_nodes[node].parent;returnparent==-1||(_nodes[parent].left!=node&&_nodes[parent].right!=node);}voidupdate(intnode){Node&x=_nodes[node];x.size=1+child_size(x.left)+child_size(x.right);x.rake_size=x.virtual_size+child_rake_size(x.left)+child_rake_size(x.right);x.all_size=x.size+x.rake_size;x.prod=ActedGroup::op(ActedGroup::op(child_prod(x.left),x.value),child_prod(x.right));x.rev_prod=ActedGroup::op(ActedGroup::op(child_rev_prod(x.right),x.value),child_rev_prod(x.left));x.rake_prod=ActedGroup::op(ActedGroup::op(child_rake_prod(x.left),x.virtual_prod),child_rake_prod(x.right));x.all_prod=ActedGroup::op(x.prod,x.rake_prod);}voidadd_virtual_child(intnode,intchild){if(child==-1)return;Node&x=_nodes[node];x.virtual_size+=_nodes[child].all_size;x.virtual_prod=ActedGroup::op(x.virtual_prod,_nodes[child].all_prod);}voidremove_virtual_child(intnode,intchild){if(child==-1)return;Node&x=_nodes[node];x.virtual_size-=_nodes[child].all_size;x.virtual_prod=ActedGroup::op(x.virtual_prod,ActedGroup::inv(_nodes[child].all_prod));}voidapply_reverse(intnode){if(node==-1)return;Node&x=_nodes[node];std::swap(x.left,x.right);std::swap(x.prod,x.rev_prod);x.rev=!x.rev;}voidapply_operator(intnode,constF&f){if(node==-1)return;Node&x=_nodes[node];x.value=ActedGroup::mapping(f,x.value);x.prod=ActedGroup::mapping(f,x.prod);x.rev_prod=ActedGroup::mapping(f,x.rev_prod);x.all_prod=ActedGroup::op(x.prod,x.rake_prod);x.lazy=ActedGroup::op_comp(f,x.lazy);}voidpush(intnode){if(node==-1)return;Node&x=_nodes[node];if(x.rev){apply_reverse(x.left);apply_reverse(x.right);x.rev=false;}apply_operator(x.left,x.lazy);apply_operator(x.right,x.lazy);x.lazy=ActedGroup::op_id();}voidpush_to(intnode){_path_buffer.clear();intcur=node;_path_buffer.push_back(cur);while(!is_splay_root(cur)){cur=_nodes[cur].parent;_path_buffer.push_back(cur);}for(inti=int(_path_buffer.size())-1;i>=0;i--)push(_path_buffer[i]);}voidrotate(intnode){intparent=_nodes[node].parent;intgrand=_nodes[parent].parent;boolis_right=_nodes[parent].right==node;intmiddle=is_right?_nodes[node].left:_nodes[node].right;if(!is_splay_root(parent)){if(_nodes[grand].left==parent){_nodes[grand].left=node;}else{_nodes[grand].right=node;}}_nodes[node].parent=grand;if(is_right){_nodes[node].left=parent;_nodes[parent].right=middle;}else{_nodes[node].right=parent;_nodes[parent].left=middle;}if(middle!=-1)_nodes[middle].parent=parent;_nodes[parent].parent=node;update(parent);update(node);}voidsplay(intnode){push_to(node);while(!is_splay_root(node)){intparent=_nodes[node].parent;intgrand=_nodes[parent].parent;if(!is_splay_root(parent)){boolzig_zig=(_nodes[parent].left==node)==(_nodes[grand].left==parent);rotate(zig_zig?parent:node);}rotate(node);}}intaccess(intnode){intlast=-1;for(intcur=node;cur!=-1;cur=_nodes[cur].parent){splay(cur);add_virtual_child(cur,_nodes[cur].right);remove_virtual_child(cur,last);_nodes[cur].right=last;if(last!=-1)_nodes[last].parent=cur;update(cur);last=cur;}splay(node);returnlast;}voidcheck_vertex(intv)const{assert(0<=v&&v<int(_nodes.size()));}voidcheck_edge(intedge_id)const{assert(0<=edge_id&&edge_id<int(_edges.size()));}public:LazyLinkCutTree()=default;explicitLazyLinkCutTree(intn){assert(0<=n);_nodes.reserve(n);for(inti=0;i<n;i++)add_vertex();}explicitLazyLinkCutTree(conststd::vector<T>&values){_nodes.reserve(values.size());for(inti=0;i<int(values.size());i++)add_vertex(values[i]);}explicitLazyLinkCutTree(std::vector<T>&&values){_nodes.reserve(values.size());for(inti=0;i<int(values.size());i++)add_vertex(std::move(values[i]));}template<classU>requires(!std::same_as<U,T>)&&(requires(Ux){ActedGroup::make(x);}||requires(Ux,inti){ActedGroup::make(x,i);}||std::convertible_to<U,T>)explicitLazyLinkCutTree(conststd::vector<U>&values){_nodes.reserve(values.size());for(inti=0;i<int(values.size());i++)add_vertex(make_node_value(values[i],i));}intsize()const{returnint(_nodes.size());}boolempty()const{return_nodes.empty();}intadd_vertex(constT&value=ActedGroup::id()){Nodenode;node.value=value;node.prod=value;node.rev_prod=value;node.all_prod=value;_nodes.push_back(std::move(node));returnint(_nodes.size())-1;}intadd_vertex(T&&value){Nodenode;node.value=std::move(value);node.prod=node.value;node.rev_prod=node.value;node.all_prod=node.value;_nodes.push_back(std::move(node));returnint(_nodes.size())-1;}template<classU>requires(!std::same_as<std::remove_cvref_t<U>,T>)&&(requires(Ux){ActedGroup::make(x);}||requires(Ux,inti){ActedGroup::make(x,i);}||std::convertible_to<U,T>)intadd_vertex(constU&value){returnadd_vertex(make_node_value(value,size()));}intedge_count()const{returnint(_edges.size());}booledge_alive(intedge_id)const{check_edge(edge_id);return_edges[edge_id].alive;}intedge_node(intedge_id)const{check_edge(edge_id);return_edges[edge_id].node;}std::pair<int,int>edge_endpoints(intedge_id)const{check_edge(edge_id);return{_edges[edge_id].u,_edges[edge_id].v};}Tget(intv){check_vertex(v);access(v);return_nodes[v].value;}Toperator[](intv){returnget(v);}voidset(intv,constT&value){check_vertex(v);access(v);_nodes[v].value=value;update(v);}voidset(intv,T&&value){check_vertex(v);access(v);_nodes[v].value=std::move(value);update(v);}template<classU>requires(!std::same_as<std::remove_cvref_t<U>,T>)&&(requires(Ux){ActedGroup::make(x);}||requires(Ux,inti){ActedGroup::make(x,i);}||std::convertible_to<U,T>)voidset(intv,constU&value){set(v,make_node_value(value,v));}// Applies `f` to one vertex.voidapply(intv,constF&f){check_vertex(v);access(v);_nodes[v].value=ActedGroup::mapping(f,_nodes[v].value);update(v);}// Applies `f` to the path from `u` to `v`. Internally calls `evert(u)`,// so the represented root may change.voidapply(intu,intv,constF&f){check_vertex(u);check_vertex(v);assert(connected(u,v));evert(u);access(v);apply_operator(v,f);}// Makes `v` the represented root of its component.voidevert(intv){check_vertex(v);access(v);apply_reverse(v);}// Alias for `evert(v)`; changes the represented root to `v`.voidreroot(intv){evert(v);}// Returns the current represented root of `v`'s component.intcomponent_root(intv){check_vertex(v);access(v);intcur=v;push(cur);while(_nodes[cur].left!=-1){cur=_nodes[cur].left;push(cur);}splay(cur);returncur;}// Alias for `component_root(v)`.introot(intv){returncomponent_root(v);}boolconnected(intu,intv){check_vertex(u);check_vertex(v);if(u==v)returntrue;returncomponent_root(u)==component_root(v);}boolsame(intu,intv){returnconnected(u,v);}// Links two components. Internally calls `evert(u)`, so the represented root may change.boollink(intu,intv){check_vertex(u);check_vertex(v);if(u==v)returnfalse;evert(u);if(component_root(v)==u)returnfalse;access(v);_nodes[u].parent=v;add_virtual_child(v,u);update(v);returntrue;}// Links `child` under `parent`. This is the same operation as `link(child, parent)`;// it internally calls `evert(child)`, so that side's represented root may change.boollink_parent(intchild,intparent){returnlink(child,parent);}intlink_edge(intu,intv,constT&value=ActedGroup::id()){check_vertex(u);check_vertex(v);if(u==v||connected(u,v))return-1;intedge_id=int(_edges.size());intnode=add_vertex(value);_edges.push_back(EdgeInfo{u,v,node,true});boolok1=link(u,node);boolok2=link(node,v);assert(ok1&&ok2);returnedge_id;}intlink_edge(intu,intv,T&&value){check_vertex(u);check_vertex(v);if(u==v||connected(u,v))return-1;intedge_id=int(_edges.size());intnode=add_vertex(std::move(value));_edges.push_back(EdgeInfo{u,v,node,true});boolok1=link(u,node);boolok2=link(node,v);assert(ok1&&ok2);returnedge_id;}template<classU>requires(!std::same_as<std::remove_cvref_t<U>,T>)&&(requires(Ux){ActedGroup::make(x);}||requires(Ux,inti){ActedGroup::make(x,i);}||std::convertible_to<U,T>)intlink_edge(intu,intv,constU&value){check_vertex(u);check_vertex(v);if(u==v||connected(u,v))return-1;returnlink_edge(u,v,make_node_value(value,size()));}// Cuts edge `(u, v)`. Internally calls `evert(u)`, so the represented root may change.boolcut(intu,intv){check_vertex(u);check_vertex(v);if(u==v)returnfalse;evert(u);access(v);if(_nodes[v].left!=u||_nodes[u].right!=-1)returnfalse;_nodes[v].left=-1;_nodes[u].parent=-1;update(v);returntrue;}// Cuts the parent edge of `v` in the current represented-root orientation.// Unlike `cut(u, v)`, this does not call `evert`.boolcut_parent(intv){check_vertex(v);access(v);intleft=_nodes[v].left;if(left==-1)returnfalse;_nodes[v].left=-1;_nodes[left].parent=-1;update(v);returntrue;}boolcut_edge(intedge_id){check_edge(edge_id);EdgeInfo&edge=_edges[edge_id];if(!edge.alive)returnfalse;boolok1=cut(edge.u,edge.node);boolok2=cut(edge.node,edge.v);if(ok1&&ok2)edge.alive=false;returnok1&&ok2;}Tget_edge(intedge_id){returnget(edge_node(edge_id));}voidset_edge(intedge_id,constT&value){set(edge_node(edge_id),value);}voidset_edge(intedge_id,T&&value){set(edge_node(edge_id),std::move(value));}template<classU>requires(!std::same_as<std::remove_cvref_t<U>,T>)&&(requires(Ux){ActedGroup::make(x);}||requires(Ux,inti){ActedGroup::make(x,i);}||std::convertible_to<U,T>)voidset_edge(intedge_id,constU&value){set(edge_node(edge_id),make_node_value(value,edge_node(edge_id)));}voidapply_edge(intedge_id,constF&f){apply(edge_node(edge_id),f);}// Returns the path product from `u` to `v`. Internally calls `evert(u)`,// so the represented root may change.Tprod(intu,intv){check_vertex(u);check_vertex(v);assert(connected(u,v));evert(u);access(v);return_nodes[v].prod;}// Alias for `prod(u, v)`. Internally calls `evert(u)`,// so the represented root may change.Tpath_prod(intu,intv){returnprod(u,v);}// Returns the number of vertices on path `u`-`v`. Internally calls `evert(u)`,// so the represented root may change.intpath_size(intu,intv){check_vertex(u);check_vertex(v);assert(connected(u,v));evert(u);access(v);return_nodes[v].size;}// Returns the `k`-th vertex on path `u`-`v`. Internally calls `evert(u)`,// so the represented root may change.intkth_vertex(intu,intv,intk){check_vertex(u);check_vertex(v);assert(connected(u,v));evert(u);access(v);assert(0<=k&&k<_nodes[v].size);intcur=v;while(true){push(cur);intleft_size=child_size(_nodes[cur].left);if(k<left_size){cur=_nodes[cur].left;}elseif(k==left_size){splay(cur);returncur;}else{k-=left_size+1;cur=_nodes[cur].right;}}}intlca(intu,intv){check_vertex(u);check_vertex(v);if(!connected(u,v))return-1;if(u==v)returnu;access(u);returnaccess(v);}// Returns the aggregate of `v`'s subtree when the represented tree is rooted at `root`.// Internally calls `evert(root)`, so the represented root may change.Tsubtree_prod(introot,intv){check_vertex(root);check_vertex(v);assert(connected(root,v));evert(root);access(v);returnnode_subtree_prod(v);}// Returns the aggregate of `v`'s subtree with respect to the current represented root.Tsubtree_prod(intv){check_vertex(v);access(v);returnnode_subtree_prod(v);}// Returns the size of `v`'s subtree when the represented tree is rooted at `root`.// Internally calls `evert(root)`, so the represented root may change.intsubtree_size(introot,intv){check_vertex(root);check_vertex(v);assert(connected(root,v));evert(root);access(v);returnnode_subtree_size(v);}// Returns the size of `v`'s subtree with respect to the current represented root.intsubtree_size(intv){check_vertex(v);access(v);returnnode_subtree_size(v);}// Returns the aggregate of the whole connected component containing `v`.Tcomponent_prod(intv){intr=root(v);returnsubtree_prod(r,r);}// Returns the number of vertices in the connected component containing `v`.intcomponent_size(intv){intr=root(v);returnsubtree_size(r,r);}// Returns the child of `root` that lies on path `root`-`v`.intchild_toward(introot,intv){check_vertex(root);check_vertex(v);assert(root!=v);assert(connected(root,v));returnkth_vertex(root,v,1);}// Returns the aggregate of the entire branch of `root` that contains `v`.Tbranch_prod(introot,intv){check_vertex(root);check_vertex(v);assert(root!=v);intchild=child_toward(root,v);returnsubtree_prod(root,child);}// Returns the size of the entire branch of `root` that contains `v`.intbranch_size(introot,intv){check_vertex(root);check_vertex(v);assert(root!=v);intchild=child_toward(root,v);returnsubtree_size(root,child);}// Returns the parent of `v` when rooted at `root`, or `-1` if `v == root`.intparent(introot,intv){check_vertex(root);check_vertex(v);if(root==v)return-1;assert(connected(root,v));intd=path_size(root,v);assert(2<=d);returnkth_vertex(root,v,d-2);}// Returns `v`'s rooted subtree aggregate excluding the child-side subtree.Tsubtree_prod_excluding_child(introot,intv,intchild){check_vertex(root);check_vertex(v);check_vertex(child);assert(parent(root,child)==v);Twhole=subtree_prod(root,v);Tsub=subtree_prod(root,child);returnActedGroup::op(whole,ActedGroup::inv(sub));}// Returns `v`'s rooted subtree size excluding the child-side subtree.intsubtree_size_excluding_child(introot,intv,intchild){check_vertex(root);check_vertex(v);check_vertex(child);assert(parent(root,child)==v);returnsubtree_size(root,v)-subtree_size(root,child);}};}// namespace ds}// namespace m1une#line 11 "verify/ds/dynamic_tree/lazy_link_cut_tree.test.cpp"
usingAddSum=m1une::acted_monoid::RangeAddRangeSum<longlong>;usingNode=AddSum::value_type;voidtest_vertex_path_and_subtree_updates(){m1une::ds::LazyLinkCutTree<AddSum>lct(std::vector<longlong>{1,2,3,4,5});assert(lct.link(0,1));assert(lct.link(1,2));assert(lct.link(1,3));assert(lct.link(3,4));Nodesubtree=lct.subtree_prod(0,1);assert(subtree.sum==14);assert(subtree.size==4);assert(lct.subtree_size(0,1)==4);lct.apply(2,4,10);Nodepath=lct.path_prod(2,4);assert(path.sum==54);assert(path.size==4);subtree=lct.subtree_prod(0,1);assert(subtree.sum==54);assert(subtree.size==4);assert(lct.get(0).sum==1);assert(lct.subtree_prod(1,3).sum==29);lct.apply(0,3,5);subtree=lct.subtree_prod(0,1);assert(subtree.sum==64);assert(subtree.size==4);assert(lct.path_prod(2,4).sum==64);subtree=lct.subtree_prod(2,1);assert(subtree.sum==57);assert(subtree.size==4);}voidtest_edge_path_and_subtree_updates(){m1une::ds::LazyLinkCutTree<AddSum>lct(3);inte01=lct.link_edge(0,1,5);inte12=lct.link_edge(1,2,7);assert(e01==0);assert(e12==1);Nodefull=lct.subtree_prod(0,0);assert(full.sum==12);assert(full.size==2);Nodechild=lct.subtree_prod(0,1);assert(child.sum==7);assert(child.size==1);assert(lct.subtree_size(0,1)==3);lct.apply(0,2,3);assert(lct.path_prod(0,2).sum==18);assert(lct.subtree_prod(0,1).sum==10);lct.apply(1,2,2);assert(lct.get_edge(e01).sum==8);assert(lct.get_edge(e12).sum==12);assert(lct.subtree_prod(0,0).sum==20);assert(lct.cut_edge(e01));assert(!lct.connected(0,2));}voidtest_rooted_tree_utility_apis(){m1une::ds::LazyLinkCutTree<AddSum>lct(std::vector<longlong>{1,2,3,4,5});assert(lct.link_parent(1,0));assert(lct.link_parent(2,0));assert(lct.link_parent(3,2));assert(lct.link_parent(4,2));assert(lct.root(4)==0);lct.reroot(0);Nodecomponent=lct.component_prod(0);assert(component.sum==15);assert(component.size==5);assert(lct.component_size(0)==5);assert(lct.child_toward(0,4)==2);assert(lct.child_toward(2,4)==4);assert(lct.child_toward(4,0)==2);Nodebranch=lct.branch_prod(0,4);assert(branch.sum==12);assert(branch.size==3);assert(lct.branch_size(0,4)==3);assert(lct.parent(0,4)==2);assert(lct.parent(0,2)==0);assert(lct.parent(0,0)==-1);Nodesubtree=lct.subtree_prod(0,2);assert(subtree.sum==12);assert(subtree.size==3);assert(lct.subtree_size(0,2)==3);Nodeexcluding=lct.subtree_prod_excluding_child(0,2,4);assert(excluding.sum==7);assert(excluding.size==2);assert(lct.subtree_size_excluding_child(0,2,4)==2);lct.reroot(0);assert(lct.cut_parent(4));assert(!lct.connected(4,0));component=lct.component_prod(0);assert(component.sum==10);assert(component.size==4);assert(lct.component_size(0)==4);component=lct.component_prod(4);assert(component.sum==5);assert(component.size==1);assert(lct.component_size(4)==1);m1une::ds::LazyLinkCutTree<AddSum>lct2(std::vector<longlong>{1,2,3,4,5});assert(lct2.link_parent(1,0));assert(lct2.link_parent(2,0));assert(lct2.link_parent(3,2));assert(lct2.link_parent(4,2));lct2.apply(3,4,10);lct2.reroot(0);assert(lct2.cut_parent(2));assert(!lct2.connected(2,0));component=lct2.component_prod(0);assert(component.sum==3);assert(component.size==2);assert(lct2.component_size(0)==2);component=lct2.component_prod(2);assert(component.sum==42);assert(component.size==3);assert(lct2.component_size(2)==3);}boolnaive_connected(conststd::vector<std::vector<int>>&adj,ints,intt){std::vector<int>parent(adj.size(),-1);std::vector<int>stack;parent[s]=s;stack.push_back(s);for(intit=0;it<int(stack.size());it++){intv=stack[it];if(v==t)returntrue;for(intto:adj[v]){if(parent[to]!=-1)continue;parent[to]=v;stack.push_back(to);}}returnfalse;}std::vector<int>naive_path_vertices(conststd::vector<std::vector<int>>&adj,ints,intt){std::vector<int>parent(adj.size(),-1);std::vector<int>stack;parent[s]=s;stack.push_back(s);for(intit=0;it<int(stack.size());it++){intv=stack[it];if(v==t)break;for(intto:adj[v]){if(parent[to]!=-1)continue;parent[to]=v;stack.push_back(to);}}assert(parent[t]!=-1);std::vector<int>path;for(intv=t;v!=s;v=parent[v])path.push_back(v);path.push_back(s);returnpath;}longlongnaive_path_sum(conststd::vector<std::vector<int>>&adj,conststd::vector<longlong>&value,ints,intt){longlongres=0;for(intv:naive_path_vertices(adj,s,t))res+=value[v];returnres;}longlongnaive_subtree_sum(conststd::vector<std::vector<int>>&adj,conststd::vector<longlong>&value,introot,intv){std::vector<int>parent(adj.size(),-1);std::vector<int>stack;parent[root]=root;stack.push_back(root);for(intit=0;it<int(stack.size());it++){intx=stack[it];for(intto:adj[x]){if(parent[to]!=-1)continue;parent[to]=x;stack.push_back(to);}}assert(parent[v]!=-1);longlongres=0;stack.clear();stack.push_back(v);while(!stack.empty()){intx=stack.back();stack.pop_back();res+=value[x];for(intto:adj[x]){if(to==parent[x])continue;stack.push_back(to);}}returnres;}voidtest_random_vertex_path_updates(){constexprintn=8;std::vector<longlong>initial;std::vector<longlong>value;for(inti=0;i<n;i++){initial.push_back(i+1);value.push_back(i+1);}m1une::ds::LazyLinkCutTree<AddSum>lct(initial);std::vector<std::vector<int>>adj(n);std::vector<std::pair<int,int>>edges;std::mt19937rng(2);for(intstep=0;step<700;step++){intop=int(rng()%6);intu=int(rng()%n);intv=int(rng()%n);if(op==0){if(u!=v&&!naive_connected(adj,u,v)){assert(lct.link(u,v));adj[u].push_back(v);adj[v].push_back(u);edges.emplace_back(u,v);}}elseif(op==1){if(!edges.empty()){intid=int(rng()%edges.size());auto[a,b]=edges[id];assert(lct.cut(a,b));for(inti=0;i<int(adj[a].size());i++){if(adj[a][i]==b){adj[a][i]=adj[a].back();adj[a].pop_back();break;}}for(inti=0;i<int(adj[b].size());i++){if(adj[b][i]==a){adj[b][i]=adj[b].back();adj[b].pop_back();break;}}edges[id]=edges.back();edges.pop_back();}}elseif(op==2){longlongx=int(rng()%200)-100;value[u]=x;lct.set(u,x);}elseif(u!=v&&naive_connected(adj,u,v)){if(op==3){longlongadd=int(rng()%21)-10;lct.apply(u,v,add);for(intx:naive_path_vertices(adj,u,v))value[x]+=add;}elseif(op==4){assert(lct.path_prod(u,v).sum==naive_path_sum(adj,value,u,v));}else{assert(lct.subtree_prod(u,v).sum==naive_subtree_sum(adj,value,u,v));assert(lct.subtree_prod(v,u).sum==naive_subtree_sum(adj,value,v,u));}}}}intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;test_vertex_path_and_subtree_updates();test_edge_path_and_subtree_updates();test_rooted_tree_utility_apis();test_random_vertex_path_updates();longlonga,b;fast_input>>a>>b;fast_output<<a+b<<'\n';}