#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include"../../../ds/dynamic_array/persistent_dynamic_array.hpp"#include<algorithm>
#include<cassert>
#include"../../../utilities/fast_io.hpp"
#include<random>
#include<utility>
#include<vector>intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;usingArray=m1une::ds::PersistentDynamicArray<int>;Arraya={1,2,3,4,5};Arrayb=a.insert(2,10);Arrayc=b.erase(4);Arrayd=c.reverse(1,4);Arraye=d.rotate(1,3,5);Arrayf=e.set(0,-1);Arrayg=f.push_front(-2).push_back(6);Arrayh=g.erase(2,5);Arrayi=h.append(Array({7,8}));assert(a.to_vector()==(std::vector<int>{1,2,3,4,5}));assert(b.to_vector()==(std::vector<int>{1,2,10,3,4,5}));assert(c.to_vector()==(std::vector<int>{1,2,10,3,5}));assert(d.to_vector()==(std::vector<int>{1,3,10,2,5}));assert(e.to_vector()==(std::vector<int>{1,2,5,3,10}));assert(f.to_vector()==(std::vector<int>{-1,2,5,3,10}));assert(g.to_vector()==(std::vector<int>{-2,-1,2,5,3,10,6}));assert(h.to_vector()==(std::vector<int>{-2,-1,10,6}));assert(i.to_vector()==(std::vector<int>{-2,-1,10,6,7,8}));assert(a.to_vector(1,4)==(std::vector<int>{2,3,4}));assert(i.size()==6);assert(!i.empty());assert(i.front()==-2);assert(i.back()==8);assert(i[2]==10);assert(i.get(3)==6);auto[left,right]=i.split(3);assert(left.to_vector()==(std::vector<int>{-2,-1,10}));assert(right.to_vector()==(std::vector<int>{6,7,8}));assert(i.split_off(4).to_vector()==(std::vector<int>{7,8}));assert(left.append(right).to_vector()==i.to_vector());assert(i.clear().empty());Arraybase={1,2,3};constint&stable_reference=base[1];Arraypointer_growth=base;for(intx=0;x<1000;x++)pointer_growth=pointer_growth.push_back(x);Arrayj=base.append(base);assert(j.to_vector()==(std::vector<int>{1,2,3,1,2,3}));assert(j.reverse().to_vector()==(std::vector<int>{3,2,1,3,2,1}));assert(base.to_vector()==(std::vector<int>{1,2,3}));assert(stable_reference==2);assert(pointer_growth.size()==1003);std::mt19937rng(0);std::vector<std::pair<Array,std::vector<int>>>versions;versions.push_back({Array(),{}});for(intstep=0;step<300;step++){intid=int(rng()%versions.size());constArraycur=versions[id].first;std::vector<int>expected=versions[id].second;assert(cur.to_vector()==expected);Arraynext=cur;std::vector<int>next_expected=expected;intop=int(rng()%8);if(op==0||next_expected.empty()){intpos=int(rng()%(next_expected.size()+1));intval=int(rng()%1000)-500;next=cur.insert(pos,val);next_expected.insert(next_expected.begin()+pos,val);}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());intval=int(rng()%1000)-500;next=cur.set(pos,val);next_expected[pos]=val;}elseif(op==4){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==5){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==6&&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(cur.to_vector()==expected);assert(next.to_vector()==next_expected);if(!next_expected.empty()){intpos=int(rng()%next_expected.size());assert(next[pos]==next_expected[pos]);assert(next.get(pos)==next_expected[pos]);}intsplit_pos=int(rng()%(next_expected.size()+1));auto[prefix,suffix]=next.split(split_pos);assert(prefix.to_vector()==std::vector<int>(next_expected.begin(),next_expected.begin()+split_pos));assert(suffix.to_vector()==std::vector<int>(next_expected.begin()+split_pos,next_expected.end()));assert(prefix.append(suffix).to_vector()==next_expected);if(next_expected.size()<=100){versions.push_back({next,next_expected});}}longlongx,y;fast_input>>x>>y;fast_output<<x+y<<'\n';}
#line 1 "verify/ds/dynamic_array/persistent_dynamic_array.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 1 "ds/dynamic_array/persistent_dynamic_array.hpp"
#include<cassert>
#include<chrono>
#include<cstddef>
#include<cstdint>
#include<deque>
#include<initializer_list>
#include<memory>
#include<utility>
#include<vector>#line 1 "ds/detail/persistent_binary_node_pool.hpp"
#line 7 "ds/detail/persistent_binary_node_pool.hpp"
#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 15 "ds/dynamic_array/persistent_dynamic_array.hpp"
namespacem1une{namespaceds{template<typenameT>structPersistentDynamicArray{private:structNode{Tval;intpriority;intcount;intl,r;boolrev;Node(Tvalue,intnode_priority,intnode_count,intleft,intright,boolreversed):val(std::move(value)),priority(node_priority),count(node_count),l(left),r(right),rev(reversed){}};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;}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);}intmake_node(Tval,intpriority,boolrev,intl,intr)const{intcount=1+subtree_size(l)+subtree_size(r);returnpool->emplace(std::move(val),priority,count,l,r,rev);}intreversed_node(intt)const{if(t==-1)return-1;constNode&node=(*pool)[t];returnmake_node(node.val,node.priority,!node.rev,node.l,node.r);}intpush(intt)const{if(t==-1||!(*pool)[t].rev)returnt;Nodenode=(*pool)[t];intl=reversed_node(node.r);intr=reversed_node(node.l);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);}intset_node_inplace(intt,intpos,Tval,boolinherited_reversed=false)const{t=pool->clone_if_shared(t);constboolreversed=inherited_reversed^(*pool)[t].rev;constintlogical_left=reversed?(*pool)[t].r:(*pool)[t].l;constintleft_count=subtree_size(logical_left);if(pos<left_count){intchild=set_node_inplace(logical_left,pos,std::move(val),reversed);if(reversed){pool->replace((*pool)[t].r,child);}else{pool->replace((*pool)[t].l,child);}}elseif(pos==left_count){(*pool)[t].val=std::move(val);}else{constintlogical_right=reversed?(*pool)[t].l:(*pool)[t].r;intchild=set_node_inplace(logical_right,pos-left_count-1,std::move(val),reversed);if(reversed){pool->replace((*pool)[t].l,child);}else{pool->replace((*pool)[t].r,child);}}returnt;}intfind_node(intt,intpos)const{boolreversed=false;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){t=l;reversed=cur_reversed;}elseif(pos==left_count){returnt;}else{pos-=left_count+1;t=r;reversed=cur_reversed;}}return-1;}voiddump_dfs(intt,std::vector<T>&res,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;dump_dfs(l,res,cur_reversed);res.push_back(node.val);dump_dfs(r,res,cur_reversed);}voiddump_range_dfs(intt,intql,intqr,intoffset,std::vector<T>&res,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,cur_reversed);if(ql<=node_pos&&node_pos<qr)res.push_back(node.val);dump_range_dfs(r,ql,qr,node_pos+1,res,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);}intimport_node(constPersistentDynamicArray&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_node(node.val,node.priority,node.rev,l,r);}explicitPersistentDynamicArray(intnode,std::uint32_tstate,std::shared_ptr<Pool>node_pool):root(node),rng_state(state),pool(std::move(node_pool)){pool->retain(root);}PersistentDynamicArraymake_version(intnode,std::uint32_tstate)const{PersistentDynamicArrayresult(node,state,pool);pool->discard_unreferenced();returnresult;}public:PersistentDynamicArray():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;}explicitPersistentDynamicArray(intn):PersistentDynamicArray(n,T()){}PersistentDynamicArray(intn,constT&value):PersistentDynamicArray(){assert(0<=n);std::vector<T>v(n,value);root=build_from_vector(std::move(v),rng_state);pool->retain(root);pool->discard_unreferenced();}explicitPersistentDynamicArray(conststd::vector<T>&v):PersistentDynamicArray(){root=build_from_vector(v,rng_state);pool->retain(root);pool->discard_unreferenced();}explicitPersistentDynamicArray(std::vector<T>&&v):PersistentDynamicArray(){root=build_from_vector(std::move(v),rng_state);pool->retain(root);pool->discard_unreferenced();}PersistentDynamicArray(std::initializer_list<T>init):PersistentDynamicArray(std::vector<T>(init)){}PersistentDynamicArray(constPersistentDynamicArray&other):root(other.root),rng_state(other.rng_state),pool(other.pool){if(pool)pool->retain(root);}PersistentDynamicArray(PersistentDynamicArray&&other)noexcept:root(other.root),rng_state(other.rng_state),pool(std::move(other.pool)){other.root=-1;}PersistentDynamicArray&operator=(constPersistentDynamicArray&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;}PersistentDynamicArray&operator=(PersistentDynamicArray&&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;}~PersistentDynamicArray(){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;}PersistentDynamicArrayclear()const{returnmake_version(-1,rng_state);}PersistentDynamicArrayinsert(intpos,Tval)const{assert(0<=pos&&pos<=size());std::uint32_tnext=next_state(rng_state);intnode=make_node(std::move(val),int(next),false,-1,-1);auto[l,r]=split_node(root,pos);returnmake_version(merge(merge(l,node),r),next);}PersistentDynamicArrayinsert(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);}PersistentDynamicArrayinsert(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);}PersistentDynamicArrayinsert(intpos,std::initializer_list<T>init)const{returninsert(pos,std::vector<T>(init));}PersistentDynamicArrayinsert(intpos,constPersistentDynamicArray&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);}PersistentDynamicArraypush_back(Tval)const{returninsert(size(),std::move(val));}PersistentDynamicArraypush_front(Tval)const{returninsert(0,std::move(val));}PersistentDynamicArrayappend(conststd::vector<T>&v)const{returninsert(size(),v);}PersistentDynamicArrayappend(std::vector<T>&&v)const{returninsert(size(),std::move(v));}PersistentDynamicArrayappend(constPersistentDynamicArray&other)const{returninsert(size(),other);}PersistentDynamicArrayerase(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);}PersistentDynamicArrayerase(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);}PersistentDynamicArraypop_back()const{assert(!empty());returnerase(size()-1);}PersistentDynamicArraypop_front()const{assert(!empty());returnerase(0);}constT&at(intpos)const{assert(0<=pos&&pos<size());return(*pool)[find_node(root,pos)].val;}constT&operator[](intpos)const{returnat(pos);}constT&front()const{assert(!empty());returnat(0);}constT&back()const{assert(!empty());returnat(size()-1);}Tget(intpos)const{returnat(pos);}PersistentDynamicArrayset(intpos,Tval)const{assert(0<=pos&&pos<size());returnmake_version(set_node(root,pos,std::move(val)),rng_state);}voidset_inplace(intpos,Tval){assert(0<=pos&&pos<size());intnext_root=set_node_inplace(root,pos,std::move(val));pool->replace(root,next_root);pool->discard_unreferenced();}PersistentDynamicArrayreverse(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);}PersistentDynamicArrayreverse()const{returnmake_version(reversed_node(root),rng_state);}PersistentDynamicArrayrotate(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);}std::pair<PersistentDynamicArray,PersistentDynamicArray>split(intpos)const{assert(0<=pos&&pos<=size());auto[l,r]=split_node(root,pos);PersistentDynamicArrayleft(l,rng_state,pool);PersistentDynamicArrayright(r,rng_state,pool);pool->discard_unreferenced();return{std::move(left),std::move(right)};}PersistentDynamicArraysplit_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);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);returnres;}};}// namespace ds}// namespace m1une#line 4 "verify/ds/dynamic_array/persistent_dynamic_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_array.test.cpp"
#include<random>
#line 11 "verify/ds/dynamic_array/persistent_dynamic_array.test.cpp"
intmain(){m1une::utilities::FastInputfast_input;m1une::utilities::FastOutputfast_output;usingArray=m1une::ds::PersistentDynamicArray<int>;Arraya={1,2,3,4,5};Arrayb=a.insert(2,10);Arrayc=b.erase(4);Arrayd=c.reverse(1,4);Arraye=d.rotate(1,3,5);Arrayf=e.set(0,-1);Arrayg=f.push_front(-2).push_back(6);Arrayh=g.erase(2,5);Arrayi=h.append(Array({7,8}));assert(a.to_vector()==(std::vector<int>{1,2,3,4,5}));assert(b.to_vector()==(std::vector<int>{1,2,10,3,4,5}));assert(c.to_vector()==(std::vector<int>{1,2,10,3,5}));assert(d.to_vector()==(std::vector<int>{1,3,10,2,5}));assert(e.to_vector()==(std::vector<int>{1,2,5,3,10}));assert(f.to_vector()==(std::vector<int>{-1,2,5,3,10}));assert(g.to_vector()==(std::vector<int>{-2,-1,2,5,3,10,6}));assert(h.to_vector()==(std::vector<int>{-2,-1,10,6}));assert(i.to_vector()==(std::vector<int>{-2,-1,10,6,7,8}));assert(a.to_vector(1,4)==(std::vector<int>{2,3,4}));assert(i.size()==6);assert(!i.empty());assert(i.front()==-2);assert(i.back()==8);assert(i[2]==10);assert(i.get(3)==6);auto[left,right]=i.split(3);assert(left.to_vector()==(std::vector<int>{-2,-1,10}));assert(right.to_vector()==(std::vector<int>{6,7,8}));assert(i.split_off(4).to_vector()==(std::vector<int>{7,8}));assert(left.append(right).to_vector()==i.to_vector());assert(i.clear().empty());Arraybase={1,2,3};constint&stable_reference=base[1];Arraypointer_growth=base;for(intx=0;x<1000;x++)pointer_growth=pointer_growth.push_back(x);Arrayj=base.append(base);assert(j.to_vector()==(std::vector<int>{1,2,3,1,2,3}));assert(j.reverse().to_vector()==(std::vector<int>{3,2,1,3,2,1}));assert(base.to_vector()==(std::vector<int>{1,2,3}));assert(stable_reference==2);assert(pointer_growth.size()==1003);std::mt19937rng(0);std::vector<std::pair<Array,std::vector<int>>>versions;versions.push_back({Array(),{}});for(intstep=0;step<300;step++){intid=int(rng()%versions.size());constArraycur=versions[id].first;std::vector<int>expected=versions[id].second;assert(cur.to_vector()==expected);Arraynext=cur;std::vector<int>next_expected=expected;intop=int(rng()%8);if(op==0||next_expected.empty()){intpos=int(rng()%(next_expected.size()+1));intval=int(rng()%1000)-500;next=cur.insert(pos,val);next_expected.insert(next_expected.begin()+pos,val);}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());intval=int(rng()%1000)-500;next=cur.set(pos,val);next_expected[pos]=val;}elseif(op==4){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==5){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==6&&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(cur.to_vector()==expected);assert(next.to_vector()==next_expected);if(!next_expected.empty()){intpos=int(rng()%next_expected.size());assert(next[pos]==next_expected[pos]);assert(next.get(pos)==next_expected[pos]);}intsplit_pos=int(rng()%(next_expected.size()+1));auto[prefix,suffix]=next.split(split_pos);assert(prefix.to_vector()==std::vector<int>(next_expected.begin(),next_expected.begin()+split_pos));assert(suffix.to_vector()==std::vector<int>(next_expected.begin()+split_pos,next_expected.end()));assert(prefix.append(suffix).to_vector()==next_expected);if(next_expected.size()<=100){versions.push_back({next,next_expected});}}longlongx,y;fast_input>>x>>y;fast_output<<x+y<<'\n';}