#line 1 "verify/ds/dynamic_array/persistent_dynamic_lazy_monoid_array.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "ds/dynamic_array/persistent_dynamic_lazy_monoid_array.hpp"
#include<cassert>
#include<chrono>
#include<concepts>
#include<cstddef>
#include<cstdint>
#include<initializer_list>
#include<memory>
#include<utility>
#include<vector>#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 1 "ds/detail/persistent_binary_node_pool.hpp"
#line 6 "ds/detail/persistent_binary_node_pool.hpp"
#include<deque>
#include<limits>
#include<optional>
#line 11 "ds/detail/persistent_binary_node_pool.hpp"
namespacem1une{namespaceds{namespacedetail{// Node must have integer `l` and `r` members. New nodes initially have no// owner; discard_unreferenced() removes temporary path-copy nodes after the// result roots have been retained.template<classNode,intnull_node=-1>structPersistentBinaryNodePool{private:std::deque<std::optional<Node>>_nodes;std::vector<int>_references;std::vector<int>_next_free;std::vector<int>_unowned;int_first_free=-1;std::size_t_live_nodes=0;voidrelease_zero(intnode){assert(node!=null_node&&_nodes[node].has_value());intleft=(*_nodes[node]).l;intright=(*_nodes[node]).r;_nodes[node].reset();_next_free[node]=_first_free;_first_free=node;--_live_nodes;if(left!=null_node&&--_references[left]==0)release_zero(left);if(right!=null_node&&--_references[right]==0)release_zero(right);}public:PersistentBinaryNodePool(){ifconstexpr(null_node==0){_nodes.emplace_back();_references.push_back(0);_next_free.push_back(-1);}}Node&operator[](intnode){assert(node!=null_node&&_nodes[node].has_value());return*_nodes[node];}constNode&operator[](intnode)const{assert(node!=null_node&&_nodes[node].has_value());return*_nodes[node];}template<class...Args>intemplace(Args&&...args){intresult;if(_first_free==-1){assert(_nodes.size()<std::size_t(std::numeric_limits<int>::max()));result=int(_nodes.size());_nodes.emplace_back(std::in_place,std::forward<Args>(args)...);_references.push_back(0);_next_free.push_back(-1);}else{result=_first_free;_first_free=_next_free[result];_nodes[result].emplace(std::forward<Args>(args)...);_references[result]=0;}retain((*_nodes[result]).l);retain((*_nodes[result]).r);_unowned.push_back(result);++_live_nodes;returnresult;}voidretain(intnode){if(node!=null_node){assert(_nodes[node].has_value());++_references[node];}}voidrelease(intnode){if(node==null_node)return;assert(_nodes[node].has_value()&&_references[node]>0);if(--_references[node]==0)release_zero(node);}boolunique(intnode)const{returnnode==null_node||_references[node]==1;}intclone(intnode){assert(node!=null_node&&_nodes[node].has_value());returnemplace(*_nodes[node]);}// Returns node itself when it has one owner, otherwise an unowned clone.// A returned clone becomes owned when a root or parent edge retains it.intclone_if_shared(intnode){if(unique(node))returnnode;returnclone(node);}voidreplace(int&edge,intnode){if(edge==node)return;retain(node);intold=edge;edge=node;release(old);}voiddiscard_unreferenced(){while(!_unowned.empty()){intnode=_unowned.back();_unowned.pop_back();if(_nodes[node].has_value()&&_references[node]==0)release_zero(node);}}voidreserve(std::size_t){}intnext_index()const{return_first_free==-1?int(_nodes.size()):_first_free;}std::size_tsize()const{return_live_nodes;}};}// namespace detail}// namespace ds}// namespace m1une#line 16 "ds/dynamic_array/persistent_dynamic_lazy_monoid_array.hpp"
namespacem1une{namespaceds{template<m1une::acted_monoid::IsActedMonoidActedMonoid>structPersistentDynamicLazyMonoidArray{usingT=typenameActedMonoid::value_type;usingF=typenameActedMonoid::operator_type;private:structNode{Tval,prod,rprod;Flazy;intpriority;intcount;intl,r;boolrev;boolhas_lazy;Node(Tvalue,Tproduct,Treverse_product,Flazy_value,intnode_priority,intnode_count,intleft,intright,boolreversed,boollazy_flag):val(std::move(value)),prod(std::move(product)),rprod(std::move(reverse_product)),lazy(std::move(lazy_value)),priority(node_priority),count(node_count),l(left),r(right),rev(reversed),has_lazy(lazy_flag){}};structBuildNode{Tval;intpriority;intl,r;BuildNode(Tvalue,intnode_priority):val(std::move(value)),priority(node_priority),l(-1),r(-1){}};introot;std::uint32_trng_state;usingPool=detail::PersistentBinaryNodePool<Node>;std::shared_ptr<Pool>pool;intsubtree_size(intt)const{returnt==-1?0:(*pool)[t].count;}Tnode_prod(intt)const{returnt==-1?ActedMonoid::id():(*pool)[t].prod;}Tnode_rprod(intt)const{returnt==-1?ActedMonoid::id():(*pool)[t].rprod;}staticstd::uint32_tnext_state(std::uint32_tstate){state^=state<<13;state^=state>>17;state^=state<<5;returnstate==0?1:state;}staticintnext_priority(std::uint32_t&state){state=next_state(state);returnint(state);}template<typenameU>staticTmake_value(constU&value){ifconstexpr(requires(Ux){ActedMonoid::make(x);}){returnActedMonoid::make(value);}else{returnstatic_cast<T>(value);}}staticTmapping_at(constF&f,constT&value,longlongord){ifconstexpr(requires(Fg,Tx,longlongi){ActedMonoid::mapping(g,x,i);}){returnActedMonoid::mapping(f,value,ord);}else{returnActedMonoid::mapping(f,value);}}staticFshift_operator(constF&f,longlongord){ifconstexpr(requires(Fg,longlongi){ActedMonoid::op_shift(g,i);}){returnActedMonoid::op_shift(f,ord);}else{returnf;}}staticFreverse_operator(constF&f,longlongsize){ifconstexpr(requires(Fg,longlongn){ActedMonoid::op_reverse(g,n);}){returnActedMonoid::op_reverse(f,size);}else{returnf;}}Fcompose_for_child(constF&inherited,intt,longlongord)const{Fshifted=shift_operator(inherited,ord);constNode&node=(*pool)[t];if(!node.has_lazy)returnshifted;returnActedMonoid::op_comp(shifted,shift_operator(node.lazy,ord));}intmake_raw_node(Tval,Tprod,Trprod,Flazy,intpriority,intcount,boolrev,boolhas_lazy,intl,intr)const{returnpool->emplace(std::move(val),std::move(prod),std::move(rprod),std::move(lazy),priority,count,l,r,rev,has_lazy);}intmake_node(Tval,intpriority,boolrev,intl,intr)const{Tprod=ActedMonoid::op(ActedMonoid::op(node_prod(l),val),node_prod(r));Trprod=ActedMonoid::op(ActedMonoid::op(node_rprod(r),val),node_rprod(l));if(rev)std::swap(prod,rprod);intcount=1+subtree_size(l)+subtree_size(r);returnmake_raw_node(std::move(val),std::move(prod),std::move(rprod),ActedMonoid::op_id(),priority,count,rev,false,l,r);}intreversed_node(intt)const{if(t==-1)return-1;Nodenode=(*pool)[t];Flazy=node.has_lazy?reverse_operator(node.lazy,node.count):node.lazy;returnmake_raw_node(std::move(node.val),std::move(node.rprod),std::move(node.prod),std::move(lazy),node.priority,node.count,!node.rev,node.has_lazy,node.l,node.r);}voidall_apply_to_node(intt,constF&f)const{Node&node=(*pool)[t];intleft_count=node.rev?subtree_size(node.r):subtree_size(node.l);node.val=mapping_at(f,node.val,left_count);node.prod=mapping_at(f,node.prod,0);node.rprod=mapping_at(reverse_operator(f,node.count),node.rprod,0);node.lazy=ActedMonoid::op_comp(f,node.lazy);node.has_lazy=true;}intall_apply(intt,constF&f)const{if(t==-1)return-1;intresult=pool->clone(t);all_apply_to_node(result,f);returnresult;}intpush(intt)const{if(t==-1)return-1;constNode&stored=(*pool)[t];if(!stored.rev&&!stored.has_lazy)returnt;Nodenode=stored;intl=node.l;intr=node.r;if(node.rev){std::swap(l,r);l=reversed_node(l);r=reversed_node(r);}if(node.has_lazy){l=all_apply(l,node.lazy);r=all_apply(r,shift_operator(node.lazy,subtree_size(l)+1));}returnmake_node(std::move(node.val),node.priority,false,l,r);}intmerge(intl,intr)const{if(l==-1||r==-1)returnl==-1?r:l;if((*pool)[l].priority>(*pool)[r].priority){Nodenode=(*pool)[push(l)];intright=merge(node.r,r);returnmake_node(std::move(node.val),node.priority,false,node.l,right);}Nodenode=(*pool)[push(r)];intleft=merge(l,node.l);returnmake_node(std::move(node.val),node.priority,false,left,node.r);}std::pair<int,int>split_node(intt,intpos)const{if(t==-1)return{-1,-1};Nodenode=(*pool)[push(t)];intleft_count=subtree_size(node.l);if(pos<=left_count){auto[a,b]=split_node(node.l,pos);return{a,make_node(std::move(node.val),node.priority,false,b,node.r)};}auto[a,b]=split_node(node.r,pos-left_count-1);return{make_node(std::move(node.val),node.priority,false,node.l,a),b};}intset_node(intt,intpos,Tval)const{Nodenode=(*pool)[push(t)];intleft_count=subtree_size(node.l);if(pos<left_count){intl=set_node(node.l,pos,std::move(val));returnmake_node(std::move(node.val),node.priority,false,l,node.r);}if(pos==left_count){returnmake_node(std::move(val),node.priority,false,node.l,node.r);}intr=set_node(node.r,pos-left_count-1,std::move(val));returnmake_node(std::move(node.val),node.priority,false,node.l,r);}voidreverse_node_inplace(intt)const{Node&node=(*pool)[t];std::swap(node.prod,node.rprod);if(node.has_lazy)node.lazy=reverse_operator(node.lazy,node.count);node.rev=!node.rev;}intreverse_node_cow(intt)const{if(t==-1)return-1;t=pool->clone_if_shared(t);reverse_node_inplace(t);returnt;}intall_apply_cow(intt,constF&f)const{if(t==-1)return-1;t=pool->clone_if_shared(t);all_apply_to_node(t,f);returnt;}voidpull(intt)const{Node&node=(*pool)[t];node.prod=ActedMonoid::op(ActedMonoid::op(node_prod(node.l),node.val),node_prod(node.r));node.rprod=ActedMonoid::op(ActedMonoid::op(node_rprod(node.r),node.val),node_rprod(node.l));}voidpush_inplace(intt)const{if(!(*pool)[t].rev&&!(*pool)[t].has_lazy)return;constboolreversed=(*pool)[t].rev;constboolhas_lazy=(*pool)[t].has_lazy;Flazy=(*pool)[t].lazy;intleft=(*pool)[t].l;intright=(*pool)[t].r;if(reversed){intnew_left=reverse_node_cow(right);intnew_right=reverse_node_cow(left);// Keep both children alive while the two owning edges are swapped.pool->retain(new_left);pool->retain(new_right);pool->replace((*pool)[t].l,new_left);pool->replace((*pool)[t].r,new_right);pool->release(new_left);pool->release(new_right);}if(has_lazy){left=all_apply_cow((*pool)[t].l,lazy);pool->replace((*pool)[t].l,left);right=all_apply_cow((*pool)[t].r,shift_operator(lazy,subtree_size(left)+1));pool->replace((*pool)[t].r,right);}Node&node=(*pool)[t];node.lazy=ActedMonoid::op_id();node.rev=false;node.has_lazy=false;pull(t);}intset_node_inplace(intt,intpos,Tval)const{t=pool->clone_if_shared(t);push_inplace(t);intleft_count=subtree_size((*pool)[t].l);if(pos<left_count){intchild=set_node_inplace((*pool)[t].l,pos,std::move(val));pool->replace((*pool)[t].l,child);}elseif(pos==left_count){(*pool)[t].val=std::move(val);}else{intchild=set_node_inplace((*pool)[t].r,pos-left_count-1,std::move(val));pool->replace((*pool)[t].r,child);}pull(t);returnt;}intapply_node_inplace(intt,intoffset,intquery_left,intquery_right,constF&f)const{if(t==-1||query_right<=offset||offset+subtree_size(t)<=query_left)returnt;t=pool->clone_if_shared(t);if(query_left<=offset&&offset+subtree_size(t)<=query_right){all_apply_to_node(t,shift_operator(f,offset-query_left));returnt;}push_inplace(t);intleft_count=subtree_size((*pool)[t].l);intchild=apply_node_inplace((*pool)[t].l,offset,query_left,query_right,f);pool->replace((*pool)[t].l,child);intposition=offset+left_count;if(query_left<=position&&position<query_right){(*pool)[t].val=mapping_at(shift_operator(f,position-query_left),(*pool)[t].val,0);}child=apply_node_inplace((*pool)[t].r,position+1,query_left,query_right,f);pool->replace((*pool)[t].r,child);pull(t);returnt;}Tget_value(intt,intpos,Finherited,boolreversed=false)const{while(t!=-1){constNode&node=(*pool)[t];boolcur_reversed=reversed^node.rev;intl=cur_reversed?node.r:node.l;intr=cur_reversed?node.l:node.r;intleft_count=subtree_size(l);if(pos<left_count){inherited=compose_for_child(inherited,t,0);t=l;reversed=cur_reversed;}elseif(pos==left_count){returnmapping_at(inherited,node.val,left_count);}else{pos-=left_count+1;inherited=compose_for_child(inherited,t,left_count+1);t=r;reversed=cur_reversed;}}returnActedMonoid::id();}Tprod_dfs(intt,intql,intqr,intoffset,constF&inherited,boolreversed=false)const{if(t==-1||qr<=offset||offset+(*pool)[t].count<=ql)returnActedMonoid::id();constNode&node=(*pool)[t];boolcur_reversed=reversed^node.rev;if(ql<=offset&&offset+node.count<=qr){returnmapping_at(inherited,reversed?node.rprod:node.prod,0);}intl=cur_reversed?node.r:node.l;intr=cur_reversed?node.l:node.r;intleft_count=subtree_size(l);intnode_pos=offset+left_count;Tres=prod_dfs(l,ql,qr,offset,compose_for_child(inherited,t,0),cur_reversed);if(ql<=node_pos&&node_pos<qr)res=ActedMonoid::op(res,mapping_at(inherited,node.val,left_count));returnActedMonoid::op(res,prod_dfs(r,ql,qr,node_pos+1,compose_for_child(inherited,t,left_count+1),cur_reversed));}voiddump_dfs(intt,std::vector<T>&res,constF&inherited,boolreversed=false)const{if(t==-1)return;constNode&node=(*pool)[t];boolcur_reversed=reversed^node.rev;intl=cur_reversed?node.r:node.l;intr=cur_reversed?node.l:node.r;intleft_count=subtree_size(l);dump_dfs(l,res,compose_for_child(inherited,t,0),cur_reversed);res.push_back(mapping_at(inherited,node.val,left_count));dump_dfs(r,res,compose_for_child(inherited,t,left_count+1),cur_reversed);}voiddump_range_dfs(intt,intql,intqr,intoffset,std::vector<T>&res,constF&inherited,boolreversed=false)const{if(t==-1||qr<=offset||offset+(*pool)[t].count<=ql)return;constNode&node=(*pool)[t];boolcur_reversed=reversed^node.rev;intl=cur_reversed?node.r:node.l;intr=cur_reversed?node.l:node.r;intleft_count=subtree_size(l);intnode_pos=offset+left_count;dump_range_dfs(l,ql,qr,offset,res,compose_for_child(inherited,t,0),cur_reversed);if(ql<=node_pos&&node_pos<qr)res.push_back(mapping_at(inherited,node.val,left_count));dump_range_dfs(r,ql,qr,node_pos+1,res,compose_for_child(inherited,t,left_count+1),cur_reversed);}intbuild_from_nodes(std::vector<BuildNode>&nodes,intt)const{if(t==-1)return-1;intl=build_from_nodes(nodes,nodes[t].l);intr=build_from_nodes(nodes,nodes[t].r);returnmake_node(std::move(nodes[t].val),nodes[t].priority,false,l,r);}intbuild_cartesian(std::vector<BuildNode>&nodes)const{if(nodes.empty())return-1;std::vector<int>stack;stack.reserve(nodes.size());for(inti=0;i<int(nodes.size());i++){intleft_child=-1;while(!stack.empty()&&nodes[stack.back()].priority<nodes[i].priority){left_child=stack.back();stack.pop_back();}nodes[i].l=left_child;if(!stack.empty())nodes[stack.back()].r=i;stack.push_back(i);}returnbuild_from_nodes(nodes,stack.front());}intbuild_from_vector(conststd::vector<T>&v,std::uint32_t&state)const{std::vector<BuildNode>nodes;nodes.reserve(v.size());for(constT&x:v)nodes.emplace_back(x,next_priority(state));returnbuild_cartesian(nodes);}intbuild_from_vector(std::vector<T>&&v,std::uint32_t&state)const{std::vector<BuildNode>nodes;nodes.reserve(v.size());for(T&x:v)nodes.emplace_back(std::move(x),next_priority(state));returnbuild_cartesian(nodes);}template<typenameU>intbuild_from_values(conststd::vector<U>&v,std::uint32_t&state)const{std::vector<BuildNode>nodes;nodes.reserve(v.size());for(constU&x:v)nodes.emplace_back(make_value(x),next_priority(state));returnbuild_cartesian(nodes);}intimport_node(constPersistentDynamicLazyMonoidArray&other,intt)const{if(t==-1)return-1;if(pool==other.pool)returnt;constNode&node=(*other.pool)[t];intl=import_node(other,node.l);intr=import_node(other,node.r);returnmake_raw_node(node.val,node.prod,node.rprod,node.lazy,node.priority,node.count,node.rev,node.has_lazy,l,r);}explicitPersistentDynamicLazyMonoidArray(intnode,std::uint32_tstate,std::shared_ptr<Pool>node_pool):root(node),rng_state(state),pool(std::move(node_pool)){pool->retain(root);}PersistentDynamicLazyMonoidArraymake_version(intnode,std::uint32_tstate)const{PersistentDynamicLazyMonoidArrayresult(node,state,pool);pool->discard_unreferenced();returnresult;}public:PersistentDynamicLazyMonoidArray():root(-1),rng_state(std::uint32_t(std::chrono::steady_clock::now().time_since_epoch().count())),pool(std::make_shared<Pool>()){if(rng_state==0)rng_state=1;}explicitPersistentDynamicLazyMonoidArray(intn):PersistentDynamicLazyMonoidArray(n,ActedMonoid::id()){}PersistentDynamicLazyMonoidArray(intn,constT&value):PersistentDynamicLazyMonoidArray(){assert(0<=n);pool->reserve(n);std::vector<T>v(n,value);root=build_from_vector(std::move(v),rng_state);pool->retain(root);pool->discard_unreferenced();}explicitPersistentDynamicLazyMonoidArray(conststd::vector<T>&v):PersistentDynamicLazyMonoidArray(){pool->reserve(v.size());root=build_from_vector(v,rng_state);pool->retain(root);pool->discard_unreferenced();}explicitPersistentDynamicLazyMonoidArray(std::vector<T>&&v):PersistentDynamicLazyMonoidArray(){pool->reserve(v.size());root=build_from_vector(std::move(v),rng_state);pool->retain(root);pool->discard_unreferenced();}template<typenameU>requires(!std::same_as<U,T>)&&(requires(Ux){ActedMonoid::make(x);}||std::convertible_to<U,T>)explicitPersistentDynamicLazyMonoidArray(conststd::vector<U>&v):PersistentDynamicLazyMonoidArray(){pool->reserve(v.size());root=build_from_values(v,rng_state);pool->retain(root);pool->discard_unreferenced();}PersistentDynamicLazyMonoidArray(std::initializer_list<T>init):PersistentDynamicLazyMonoidArray(std::vector<T>(init)){}PersistentDynamicLazyMonoidArray(constPersistentDynamicLazyMonoidArray&other):root(other.root),rng_state(other.rng_state),pool(other.pool){if(pool)pool->retain(root);}PersistentDynamicLazyMonoidArray(PersistentDynamicLazyMonoidArray&&other)noexcept:root(other.root),rng_state(other.rng_state),pool(std::move(other.pool)){other.root=-1;}PersistentDynamicLazyMonoidArray&operator=(constPersistentDynamicLazyMonoidArray&other){if(this==&other)return*this;if(other.pool)other.pool->retain(other.root);if(pool)pool->release(root);root=other.root;rng_state=other.rng_state;pool=other.pool;return*this;}PersistentDynamicLazyMonoidArray&operator=(PersistentDynamicLazyMonoidArray&&other)noexcept{if(this==&other)return*this;if(pool)pool->release(root);root=other.root;rng_state=other.rng_state;pool=std::move(other.pool);other.root=-1;return*this;}~PersistentDynamicLazyMonoidArray(){if(pool)pool->release(root);}intsize()const{returnsubtree_size(root);}boolempty()const{returnsize()==0;}voidrelease(){if(pool)pool->release(root);root=-1;pool=std::make_shared<Pool>();}std::size_tnode_count()const{returnpool?pool->size():0;}PersistentDynamicLazyMonoidArrayclear()const{returnmake_version(-1,rng_state);}PersistentDynamicLazyMonoidArrayinsert(intpos,Tvalue)const{assert(0<=pos&&pos<=size());std::uint32_tnext=next_state(rng_state);intnode=make_node(std::move(value),int(next),false,-1,-1);auto[l,r]=split_node(root,pos);returnmake_version(merge(merge(l,node),r),next);}PersistentDynamicLazyMonoidArrayinsert(intpos,conststd::vector<T>&v)const{assert(0<=pos&&pos<=size());if(v.empty())return*this;std::uint32_tnext=rng_state;intmid=build_from_vector(v,next);auto[l,r]=split_node(root,pos);returnmake_version(merge(merge(l,mid),r),next);}PersistentDynamicLazyMonoidArrayinsert(intpos,std::vector<T>&&v)const{assert(0<=pos&&pos<=size());if(v.empty())return*this;std::uint32_tnext=rng_state;intmid=build_from_vector(std::move(v),next);auto[l,r]=split_node(root,pos);returnmake_version(merge(merge(l,mid),r),next);}PersistentDynamicLazyMonoidArrayinsert(intpos,std::initializer_list<T>init)const{returninsert(pos,std::vector<T>(init));}PersistentDynamicLazyMonoidArrayinsert(intpos,constPersistentDynamicLazyMonoidArray&other)const{assert(0<=pos&&pos<=size());if(other.empty())return*this;intmid=import_node(other,other.root);auto[l,r]=split_node(root,pos);returnmake_version(merge(merge(l,mid),r),rng_state);}PersistentDynamicLazyMonoidArraypush_back(Tvalue)const{returninsert(size(),std::move(value));}PersistentDynamicLazyMonoidArraypush_front(Tvalue)const{returninsert(0,std::move(value));}PersistentDynamicLazyMonoidArrayappend(conststd::vector<T>&v)const{returninsert(size(),v);}PersistentDynamicLazyMonoidArrayappend(std::vector<T>&&v)const{returninsert(size(),std::move(v));}PersistentDynamicLazyMonoidArrayappend(constPersistentDynamicLazyMonoidArray&other)const{returninsert(size(),other);}PersistentDynamicLazyMonoidArrayerase(intpos)const{assert(0<=pos&&pos<size());auto[a,b]=split_node(root,pos);auto[mid,c]=split_node(b,1);(void)mid;returnmake_version(merge(a,c),rng_state);}PersistentDynamicLazyMonoidArrayerase(intl,intr)const{assert(0<=l&&l<=r&&r<=size());if(l==r)return*this;auto[a,b]=split_node(root,l);auto[mid,c]=split_node(b,r-l);(void)mid;returnmake_version(merge(a,c),rng_state);}PersistentDynamicLazyMonoidArraypop_back()const{assert(!empty());returnerase(size()-1);}PersistentDynamicLazyMonoidArraypop_front()const{assert(!empty());returnerase(0);}Tget(intpos)const{assert(0<=pos&&pos<size());returnget_value(root,pos,ActedMonoid::op_id());}Toperator[](intpos)const{returnget(pos);}Tfront()const{assert(!empty());returnget(0);}Tback()const{assert(!empty());returnget(size()-1);}PersistentDynamicLazyMonoidArrayset(intpos,Tvalue)const{assert(0<=pos&&pos<size());returnmake_version(set_node(root,pos,std::move(value)),rng_state);}voidset_inplace(intpos,Tvalue){assert(0<=pos&&pos<size());intnext_root=set_node_inplace(root,pos,std::move(value));pool->replace(root,next_root);pool->discard_unreferenced();}PersistentDynamicLazyMonoidArrayreverse(intl,intr)const{assert(0<=l&&l<=r&&r<=size());if(l==r)return*this;auto[a,b]=split_node(root,l);auto[mid,c]=split_node(b,r-l);returnmake_version(merge(merge(a,reversed_node(mid)),c),rng_state);}PersistentDynamicLazyMonoidArrayreverse()const{returnmake_version(reversed_node(root),rng_state);}PersistentDynamicLazyMonoidArrayrotate(intl,intm,intr)const{assert(0<=l&&l<=m&&m<=r&&r<=size());if(l==m||m==r)return*this;auto[a,b]=split_node(root,l);auto[c,d]=split_node(b,m-l);auto[e,f]=split_node(d,r-m);returnmake_version(merge(merge(a,e),merge(c,f)),rng_state);}PersistentDynamicLazyMonoidArrayapply(intpos,constF&f)const{assert(0<=pos&&pos<size());returnapply(pos,pos+1,f);}PersistentDynamicLazyMonoidArrayapply(intl,intr,constF&f)const{assert(0<=l&&l<=r&&r<=size());if(l==r)return*this;auto[a,b]=split_node(root,l);auto[mid,c]=split_node(b,r-l);returnmake_version(merge(merge(a,all_apply(mid,f)),c),rng_state);}voidapply_inplace(intpos,constF&f){assert(0<=pos&&pos<size());apply_inplace(pos,pos+1,f);}voidapply_inplace(intl,intr,constF&f){assert(0<=l&&l<=r&&r<=size());if(l==r)return;intnext_root=apply_node_inplace(root,0,l,r,f);pool->replace(root,next_root);pool->discard_unreferenced();}Tprod(intl,intr)const{assert(0<=l&&l<=r&&r<=size());if(l==r)returnActedMonoid::id();returnprod_dfs(root,l,r,0,ActedMonoid::op_id());}Tall_prod()const{returnroot==-1?ActedMonoid::id():(*pool)[root].prod;}std::pair<PersistentDynamicLazyMonoidArray,PersistentDynamicLazyMonoidArray>split(intpos)const{assert(0<=pos&&pos<=size());auto[l,r]=split_node(root,pos);PersistentDynamicLazyMonoidArrayleft(l,rng_state,pool);PersistentDynamicLazyMonoidArrayright(r,rng_state,pool);pool->discard_unreferenced();return{std::move(left),std::move(right)};}PersistentDynamicLazyMonoidArraysplit_off(intpos)const{assert(0<=pos&&pos<=size());returnmake_version(split_node(root,pos).second,rng_state);}std::vector<T>to_vector()const{std::vector<T>res;res.reserve(size());dump_dfs(root,res,ActedMonoid::op_id());returnres;}std::vector<T>to_vector(intl,intr)const{assert(0<=l&&l<=r&&r<=size());std::vector<T>res;res.reserve(r-l);dump_range_dfs(root,l,r,0,res,ActedMonoid::op_id());returnres;}};}// namespace ds}// namespace m1une#line 4 "verify/ds/dynamic_array/persistent_dynamic_lazy_monoid_array.test.cpp"
#include<algorithm>
#line 1 "utilities/fast_io.hpp"
#line 5 "utilities/fast_io.hpp"
#include<array>
#include<cerrno>
#include<charconv>
#line 9 "utilities/fast_io.hpp"
#include<cstdio>
#include<cstdlib>
#line 12 "utilities/fast_io.hpp"
#include<cstring>
#include<iterator>
#include<string>
#include<sys/stat.h>
#include<type_traits>
#line 18 "utilities/fast_io.hpp"
#include<unistd.h>
#line 20 "utilities/fast_io.hpp"
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 8 "verify/ds/dynamic_array/persistent_dynamic_lazy_monoid_array.test.cpp"
#include<numeric>
#include<random>
#line 13 "verify/ds/dynamic_array/persistent_dynamic_lazy_monoid_array.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 15 "verify/ds/dynamic_array/persistent_dynamic_lazy_monoid_array.test.cpp"
usingAM=m1une::acted_monoid::RangeAddRangeSum<longlong>;usingNode=AM::value_type;usingArray=m1une::ds::PersistentDynamicLazyMonoidArray<AM>;structStringNoopActedMonoid{usingvalue_type=std::string;usingoperator_type=int;staticstd::stringid(){return"";}staticstd::stringop(conststd::string&a,conststd::string&b){returna+b;}staticintop_id(){return0;}staticintop_comp(intf,intg){returnf+g;}staticstd::stringmapping(int,conststd::string&x){returnx;}};std::vector<Node>make_nodes(conststd::vector<longlong>&a){std::vector<Node>res;res.reserve(a.size());for(longlongx:a)res.push_back(AM::make(x));returnres;}std::vector<longlong>sums(conststd::vector<Node>&a){std::vector<longlong>res;res.reserve(a.size());for(Nodex:a)res.push_back(x.sum);returnres;}longlongsum_range(conststd::vector<longlong>&a,intl,intr){returnstd::accumulate(a.begin()+l,a.begin()+r,0LL);}intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;usingStringArray=m1une::ds::PersistentDynamicLazyMonoidArray<StringNoopActedMonoid>;StringArrays={"a","b","c","d"};StringArraysr=s.reverse(1,4);assert(s.prod(0,4)=="abcd");assert(sr.to_vector()==(std::vector<std::string>{"a","d","c","b"}));assert(sr.prod(1,4)=="dcb");Arraya(std::vector<longlong>{1,2,3,4,5});Arrayb=a.apply(1,4,10);Arrayc=b.reverse(1,5);Arrayd=c.insert(2,AM::make(100));Arraye=d.set(0,AM::make(-1));Arrayf=e.erase(3);assert(sums(a.to_vector())==(std::vector<longlong>{1,2,3,4,5}));assert(sums(b.to_vector())==(std::vector<longlong>{1,12,13,14,5}));assert(sums(c.to_vector())==(std::vector<longlong>{1,5,14,13,12}));assert(sums(d.to_vector())==(std::vector<longlong>{1,5,100,14,13,12}));assert(sums(e.to_vector())==(std::vector<longlong>{-1,5,100,14,13,12}));assert(sums(f.to_vector())==(std::vector<longlong>{-1,5,100,13,12}));assert(b.prod(0,5).sum==45);assert(c.prod(1,4).sum==32);assert(f.all_prod().sum==129);assert(f.get(2).sum==100);assert(f.front().sum==-1);assert(f.back().sum==12);auto[left,right]=f.split(3);assert(sums(left.to_vector())==(std::vector<longlong>{-1,5,100}));assert(sums(right.to_vector())==(std::vector<longlong>{13,12}));assert(sums(left.append(right).to_vector())==sums(f.to_vector()));assert(sums(f.split_off(4).to_vector())==(std::vector<longlong>{12}));assert(f.clear().empty());std::mt19937rng(2);std::vector<std::pair<Array,std::vector<longlong>>>versions;versions.push_back({Array(),{}});for(intstep=0;step<300;step++){intid=int(rng()%versions.size());Arraycur=versions[id].first;std::vector<longlong>expected=versions[id].second;assert(sums(cur.to_vector())==expected);Arraynext=cur;std::vector<longlong>next_expected=expected;intop=int(rng()%9);if(op==0||next_expected.empty()){intpos=int(rng()%(next_expected.size()+1));longlongvalue=int(rng()%100)-50;next=cur.insert(pos,AM::make(value));next_expected.insert(next_expected.begin()+pos,value);}elseif(op==1){intpos=int(rng()%next_expected.size());next=cur.erase(pos);next_expected.erase(next_expected.begin()+pos);}elseif(op==2){intl=int(rng()%(next_expected.size()+1));intr=int(rng()%(next_expected.size()+1));if(l>r)std::swap(l,r);next=cur.erase(l,r);next_expected.erase(next_expected.begin()+l,next_expected.begin()+r);}elseif(op==3){intpos=int(rng()%next_expected.size());longlongvalue=int(rng()%100)-50;next=cur.set(pos,AM::make(value));next_expected[pos]=value;}elseif(op==4){intl=int(rng()%(next_expected.size()+1));intr=int(rng()%(next_expected.size()+1));if(l>r)std::swap(l,r);longlongadd=int(rng()%41)-20;next=cur.apply(l,r,add);for(inti=l;i<r;i++)next_expected[i]+=add;}elseif(op==5){intl=int(rng()%(next_expected.size()+1));intr=int(rng()%(next_expected.size()+1));if(l>r)std::swap(l,r);next=cur.reverse(l,r);std::reverse(next_expected.begin()+l,next_expected.begin()+r);}elseif(op==6){intl=int(rng()%(next_expected.size()+1));intm=int(rng()%(next_expected.size()+1));intr=int(rng()%(next_expected.size()+1));if(l>m)std::swap(l,m);if(m>r)std::swap(m,r);if(l>m)std::swap(l,m);next=cur.rotate(l,m,r);std::rotate(next_expected.begin()+l,next_expected.begin()+m,next_expected.begin()+r);}elseif(op==7&&next_expected.size()<80){intother_id=int(rng()%versions.size());next=cur.append(versions[other_id].first);next_expected.insert(next_expected.end(),versions[other_id].second.begin(),versions[other_id].second.end());}else{next=cur.reverse();std::reverse(next_expected.begin(),next_expected.end());}assert(sums(cur.to_vector())==expected);assert(sums(next.to_vector())==next_expected);assert(next.all_prod().sum==sum_range(next_expected,0,int(next_expected.size())));for(intl=0;l<=int(next_expected.size());l++){for(intr=l;r<=int(next_expected.size());r++){assert(next.prod(l,r).sum==sum_range(next_expected,l,r));}}if(!next_expected.empty()){intpos=int(rng()%next_expected.size());assert(next[pos].sum==next_expected[pos]);}intsplit_pos=int(rng()%(next_expected.size()+1));auto[prefix,suffix]=next.split(split_pos);assert(sums(prefix.to_vector())==std::vector<longlong>(next_expected.begin(),next_expected.begin()+split_pos));assert(sums(suffix.to_vector())==std::vector<longlong>(next_expected.begin()+split_pos,next_expected.end()));if(next_expected.size()<=100)versions.push_back({next,next_expected});}longlongx,y;fast_input>>x>>y;fast_output<<x+y<<'\n';}