tree-multimap-typed 2.4.1 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
`),i.isShowNull&&(s+=`N for null
|
|
3
3
|
`),i.isShowRedBlackNIL&&(s+=`S for Sentinel Node(NIL)
|
|
4
4
|
`),f(n=>{let[h]=this._displayAux(n,i),o="";for(let l of h)o+=l+`
|
|
5
|
-
`;s+=o},"display")(t)),s}print(t,e=this._root){console.log(this.toVisual(e,t))}_dfs(t=this._DEFAULT_NODE_CALLBACK,e="IN",i=!1,s=this._root,r=this.iterationType,n=!1,h=_=>!!_,o=_=>!!_,l=_=>n?this.isRealNodeOrNull(_):this.isRealNode(_),a=_=>this.isRealNodeOrNull(_)){if(s=this.ensureNode(s),!s)return[];let _=[];if(r==="RECURSIVE"){let d=f(p=>{if(!l(p))return;let m=f(()=>{h(p)&&(p==null?void 0:p.left)!==void 0&&d(p==null?void 0:p.left)},"visitLeft"),x=f(()=>{o(p)&&(p==null?void 0:p.right)!==void 0&&d(p==null?void 0:p.right)},"visitRight");switch(e){case"IN":if(m(),a(p)&&(_.push(t(p)),i))return;x();break;case"PRE":if(a(p)&&(_.push(t(p)),i))return;m(),x();break;case"POST":if(m(),x(),a(p)&&(_.push(t(p)),i))return;break}},"dfs");d(s)}else{let d=[{opt:0,node:s}],p=f(b=>{var w;h(b.node)&&d.push({opt:0,node:(w=b.node)==null?void 0:w.left})},"pushLeft"),m=f(b=>{var w;o(b.node)&&d.push({opt:0,node:(w=b.node)==null?void 0:w.right})},"pushRight"),x=f(b=>{l(b.node)&&d.push({opt:1,node:b.node})},"pushRoot");for(;d.length>0;){let b=d.pop();if(b!==void 0&&l(b.node))if(b.opt===1){if(a(b.node)&&b.node!==void 0&&(_.push(t(b.node)),i))return _}else switch(e){case"IN":m(b),x(b),p(b);break;case"PRE":m(b),p(b),x(b);break;case"POST":x(b),m(b),p(b);break}}}return _}*_getIterator(t=this._root){if(t)if(this.iterationType==="ITERATIVE"){let e=[],i=t;for(;i||e.length>0;){for(;this.isRealNode(i);)e.push(i),i=i.left;i=e.pop(),this.isRealNode(i)&&(yield[i.key,i.value],i=i.right)}}else t.left&&this.isRealNode(t)&&(yield*this[Symbol.iterator](t.left)),yield[t.key,t.value],t.right&&this.isRealNode(t)&&(yield*this[Symbol.iterator](t.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(t){let e=this.constructor;return new e([],{...this._snapshotOptions(),...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,{...this._snapshotOptions(),...e!=null?e:{}})}_keyValueNodeOrEntryToNodeAndValue(t,e){if(t===void 0)return[void 0,void 0];if(t===null)return[null,void 0];if(this.isNode(t))return[t,e];if(this.isEntry(t)){let[i,s]=t;if(i===void 0)return[void 0,void 0];if(i===null)return[null,void 0];let r=e!=null?e:s;return[this.createNode(i,r),r]}return[this.createNode(t,e),e]}_clone(t){this.bfs(e=>{e===null?t.set(null):t.set([e.key,e.value])},this._root,this.iterationType,!0)}_displayAux(t,e){let{isShowNull:i,isShowUndefined:s,isShowRedBlackNIL:r}=e,n=[["\u2500"],1,0,0];if(t===null&&!i)return n;if(t===void 0&&!s)return n;if(this.isNIL(t)&&!r)return n;if(t!=null){let o=t.key,l=this.isNIL(t)?"S":String(o),a=l.length;return h(l,a,this._displayAux(t.left,e),this._displayAux(t.right,e))}else{let o=t===void 0?"U":"N",l=o.length;return h(o,l,[[""],1,0,0],[[""],1,0,0])}function h(o,l,a,_){let[d,p,m,x]=a,[b,w,M,k]=_,v=" ".repeat(Math.max(0,x+1))+"_".repeat(Math.max(0,p-x-1))+o+"_".repeat(Math.max(0,k))+" ".repeat(Math.max(0,w-k)),y=(m>0?" ".repeat(x)+"/"+" ".repeat(p-x-1):" ".repeat(p))+" ".repeat(l)+(M>0?" ".repeat(k)+"\\"+" ".repeat(w-k-1):" ".repeat(w)),N=[v,y];for(let E=0;E<Math.max(m,M);E++){let T=E<m?d[E]:" ".repeat(p),C=E<M?b[E]:" ".repeat(w);N.push(T+" ".repeat(l)+C)}return[N,p+l+w,Math.max(m,M)+2,p+Math.floor(l/2)]}}_swapProperties(t,e){if(t=this.ensureNode(t),e=this.ensureNode(e),t&&e){let{key:i,value:s}=e,r=this.createNode(i,s);return r&&(e.key=t.key,this._isMapMode||(e.value=t.value),t.key=r.key,this._isMapMode||(t.value=r.value)),e}}_replaceNode(t,e){return t.parent&&(t.parent.left===t?t.parent.left=e:t.parent.right===t&&(t.parent.right=e)),e.left=t.left,e.right=t.right,e.parent=t.parent,this._root===t&&this._setRoot(e),e}_setRoot(t){t&&(t.parent=void 0),this._root=t}_ensurePredicate(t){if(t==null)return e=>!1;if(this._isPredicate(t))return t;if(this.isRealNode(t))return e=>e===t;if(this.isEntry(t)){let[e]=t;return i=>i?i.key===e:!1}return e=>e?e.key===t:!1}_isPredicate(t){return typeof t=="function"}_extractKey(t){if(t===null)return null;if(t!==void 0&&t!==this._NIL)return this.isNode(t)?t.key:this.isEntry(t)?t[0]:t}_setValue(t,e){if(t==null)return!1;let i=this._store.get(t);return i?(i.value=e,!0):!1}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};f(fe,"BinaryTree");var ni=fe,ce=class{constructor(t,e){u(this,"key"),u(this,"value"),u(this,"parent"),u(this,"_left"),u(this,"_right"),u(this,"_height",0),u(this,"_color","BLACK"),u(this,"_count",1),this.key=t,this.value=e}get left(){return this._left}set left(t){t&&(t.parent=this),this._left=t}get right(){return this._right}set right(t){t&&(t.parent=this),this._right=t}get height(){return this._height}set height(t){this._height=t}get color(){return this._color}set color(t){this._color=t}get count(){return this._count}set count(t){this._count=t}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}};f(ce,"BSTNode");var kt=ce,_e=class extends ni{constructor(t=[],e){super([],e),u(this,"_root"),u(this,"_comparator"),e?"comparator"in e&&e.comparator!==void 0?this._comparator=e.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),t&&this.setMany(t)}get root(){return this._root}get comparator(){return this._comparator}createNode(t,e){return new kt(t,e)}ensureNode(t,e=this.iterationType){var i;return(i=super.ensureNode(t,e))!=null?i:void 0}isNode(t){return t instanceof kt}isValidKey(t){return U(t)}dfs(t=this._DEFAULT_NODE_CALLBACK,e="IN",i=!1,s=this._root,r=this.iterationType){return super.dfs(t,e,i,s,r)}bfs(t=this._DEFAULT_NODE_CALLBACK,e=this._root,i=this.iterationType){return super.bfs(t,e,i,!1)}listLevels(t=this._DEFAULT_NODE_CALLBACK,e=this._root,i=this.iterationType){return super.listLevels(t,e,i,!1)}getNode(t,e=this._root,i=this.iterationType){var s,r;if(t==null)return;if(this._isPredicate(t))return(s=this.getNodes(t,!0,e,i)[0])!=null?s:void 0;if(t instanceof G)return(r=this.getNodes(t,!0,e,i)[0])!=null?r:void 0;let n;if(this.isNode(t))n=t.key;else if(this.isEntry(t)){let _=t[0];if(_==null)return;n=_}else n=t;let h=this.ensureNode(e);if(!h)return;let o=this._NIL,l=h,a=this._comparator;for(;l&&l!==o;){let _=a(n,l.key);if(_===0)return l;l=_<0?l._left:l._right}}search(t,e=!1,i=this._DEFAULT_NODE_CALLBACK,s=this._root,r=this.iterationType){if(t===void 0)return[];if(t===null)return[];if(s=this.ensureNode(s),!s)return[];let n=this.isRange(t),h=!n&&this._isPredicate(t);if(!n&&!h){let _;if(this.isNode(t))_=t.key;else if(this.isEntry(t)){let x=t[0];x!=null&&(_=x)}else _=t;if(_===void 0)return[];let d=this._NIL,p=this._comparator,m=s;for(;m&&m!==d;){let x=p(_,m.key);if(x===0)return[i(m)];m=x<0?m._left:m._right}return[]}let o;n?o=f(_=>_?t.isInRange(_.key,this._comparator):!1,"predicate"):o=this._ensurePredicate(t);let l=f(_=>{if(!_||!this.isRealNode(_.left))return!1;if(n){let d=t,p=d.low,m=d.includeLow;return m&&this._compare(_.key,p)>=0||!m&&this._compare(_.key,p)>0}if(!n&&!this._isPredicate(t)){let d=this._extractKey(t);return d!=null&&this._compare(_.key,d)>0}return!0},"shouldVisitLeft"),a=f(_=>{if(!_||!this.isRealNode(_.right))return!1;if(n){let d=t,p=d.high,m=d.includeHigh;return m&&this._compare(_.key,p)<=0||!m&&this._compare(_.key,p)<0}if(!n&&!this._isPredicate(t)){let d=this._extractKey(t);return d!=null&&this._compare(_.key,d)<0}return!0},"shouldVisitRight");return super._dfs(i,"IN",e,s,r,!1,l,a,()=>!0,_=>!!_&&o(_))}rangeSearch(t,e=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){let r=t instanceof G?t:new G(t[0],t[1]);return this.search(r,!1,e,i,s)}set(t,e){let[i]=this._keyValueNodeOrEntryToNodeAndValue(t,e);if(i===void 0)return!1;if(this._root===void 0)return this._setRoot(i),this._isMapMode&&this.isRealNode(i)&&this._store.set(i.key,i),this._size++,!0;let s=this._root;for(;s!==void 0;){if(this._compare(s.key,i.key)===0)return this._replaceNode(s,i),this._isMapMode&&this.isRealNode(i)&&this._store.set(s.key,i),!0;if(this._compare(s.key,i.key)>0){if(s.left===void 0)return s.left=i,this._isMapMode&&this.isRealNode(i)&&this._store.set(i.key,i),this._size++,!0;s.left!==null&&(s=s.left)}else{if(s.right===void 0)return s.right=i,this._isMapMode&&this.isRealNode(i)&&this._store.set(i.key,i),this._size++,!0;s.right!==null&&(s=s.right)}}return!1}setMany(t,e,i=!0,s=this.iterationType){let r=[],n=e==null?void 0:e[Symbol.iterator]();if(!i){for(let d of t){let p=n==null?void 0:n.next().value;this.isRaw(d)&&(d=this._toEntryFn(d)),r.push(this.set(d,p))}return r}let h=[],o=0;for(let d of t)h.push({key:d,value:n==null?void 0:n.next().value,orgIndex:o++});let l=h.sort(({key:d},{key:p})=>{let m,x;return this.isRaw(d)?m=this._toEntryFn(d)[0]:this.isEntry(d)?m=d[0]:this.isRealNode(d)?m=d.key:m=d,this.isRaw(p)?x=this._toEntryFn(p)[0]:this.isEntry(p)?x=p[0]:this.isRealNode(p)?x=p.key:x=p,m!=null&&x!=null?this._compare(m,x):0}),a=f(d=>{if(d.length===0)return;let p=Math.floor((d.length-1)/2),{key:m,value:x,orgIndex:b}=d[p];if(this.isRaw(m)){let w=this._toEntryFn(m);r[b]=this.set(w)}else r[b]=this.set(m,x);a(d.slice(0,p)),a(d.slice(p+1))},"_dfs");return s==="RECURSIVE"?a(l):f(()=>{let p=[[0,l.length-1]];for(;p.length>0;){let m=p.pop();if(!m)continue;let[x,b]=m;if(x>b)continue;let w=x+Math.floor((b-x)/2),{key:M,value:k,orgIndex:v}=l[w];if(this.isRaw(M)){let y=this._toEntryFn(M);r[v]=this.set(y)}else r[v]=this.set(M,k);p.push([w+1,b]),p.push([x,w-1])}},"_iterate")(),r}ceiling(t,e=this._DEFAULT_NODE_CALLBACK,i){let s,r=this.iterationType;typeof e=="string"?r=e:e&&(s=e,i&&(r=i));let n=this._bound(t,!0,r);return s?n?s(n):void 0:n==null?void 0:n.key}higher(t,e=this._DEFAULT_NODE_CALLBACK,i){let s,r=this.iterationType;typeof e=="string"?r=e:e&&(s=e,i&&(r=i));let n=this._bound(t,!1,r);return s?n?s(n):void 0:n==null?void 0:n.key}floor(t,e=this._DEFAULT_NODE_CALLBACK,i){if(t==null)return void 0;let s,r=this.iterationType;if(typeof e=="string"?r=e:e&&(s=e,i&&(r=i)),this._isPredicate(t)){let h=this._floorByPredicate(t,r);return s?h?s(h):void 0:h==null?void 0:h.key}let n;if(this.isNode(t))n=t.key;else if(this.isEntry(t)){let h=t[0];if(h==null)return void 0;n=h}else n=t;if(n!==void 0){let h=this._floorByKey(n,r);return s?h?s(h):void 0:h==null?void 0:h.key}}lower(t,e,i){if(t==null)return void 0;let s,r=this.iterationType;if(typeof e=="string"?r=e:e&&(s=e,i&&(r=i)),this._isPredicate(t)){let h=this._lowerByPredicate(t,r);return s?h?s(h):void 0:h==null?void 0:h.key}let n;if(this.isNode(t))n=t.key;else if(this.isEntry(t)){let h=t[0];if(h==null)return void 0;n=h}else n=t;if(n!==void 0){let h=this._lowerByKey(n,r);return s?h?s(h):void 0:h==null?void 0:h.key}}lesserOrGreaterTraverse(t=this._DEFAULT_NODE_CALLBACK,e=-1,i=this._root,s=this.iterationType){let r=this.ensureNode(i),n=[];if(!this._root||!r)return n;let h=r.key;if(s==="RECURSIVE"){let o=f(l=>{let a=this._compare(l.key,h);Math.sign(a)==e&&n.push(t(l)),this.isRealNode(l.left)&&o(l.left),this.isRealNode(l.right)&&o(l.right)},"dfs");return o(this._root),n}else{let o=new H([this._root]);for(;o.length>0;){let l=o.shift();if(this.isRealNode(l)){let a=this._compare(l.key,h);Math.sign(a)==e&&n.push(t(l)),this.isRealNode(l.left)&&o.push(l.left),this.isRealNode(l.right)&&o.push(l.right)}}return n}}perfectlyBalance(t=this.iterationType){let e=this.dfs(n=>n,"IN",!1,this._root,t),i=e.length;if(this._clearNodes(),i===0)return!1;let s=f((n,h,o)=>{if(n>h)return;let l=n+(h-n>>1),a=e[l],_=s(n,l-1,a),d=s(l+1,h,a);return a.left=_,a.right=d,a.parent=o,a},"build"),r=s(0,i-1,void 0);return this._setRoot(r),this._size=i,!0}isAVLBalanced(t=this.iterationType){if(!this._root)return!0;let e=!0;if(t==="RECURSIVE"){let i=f(s=>{if(!s)return 0;let r=i(s.left),n=i(s.right);return Math.abs(r-n)>1&&(e=!1),Math.max(r,n)+1},"_height");i(this._root)}else{let i=[],s=this._root,r,n=new Map;for(;i.length>0||s;)if(s)i.push(s),s.left!==null&&(s=s.left);else if(s=i[i.length-1],!s.right||r===s.right){if(s=i.pop(),s){let h=s.left?n.get(s.left):-1,o=s.right?n.get(s.right):-1;if(Math.abs(h-o)>1)return!1;n.set(s,1+Math.max(h,o)),r=s,s=void 0}}else s=s.right}return e}map(t,e,i){let s=this._createLike([],e),r=0;for(let[n,h]of this)s.set(t.call(i,h,n,r++,this));return s}deleteWhere(t,e=!1,i=this._root,s=this.iterationType){let r=this.search(t,e,h=>h,i,s),n=[];for(let h of r){let o=this.delete(h);n=n.concat(o)}return n}_createDefaultComparator(){return(t,e)=>{if(U(t)&&U(e))return t>e?1:t<e?-1:0;if(typeof t=="object"||typeof e=="object")throw TypeError("When comparing object type keys, a custom comparator must be provided in the constructor's options!");return 0}}_floorByKey(t,e){var i,s;if(e==="RECURSIVE"){let r=f(n=>{if(!this.isRealNode(n))return;if(this.comparator(n.key,t)<=0){let o=r(n.right);return o!=null?o:n}else return r(n.left)},"dfs");return r(this.root)}else{let r=this.root,n;for(;this.isRealNode(r);)this.comparator(r.key,t)<=0?(n=r,r=(i=r.right)!=null?i:void 0):r=(s=r.left)!=null?s:void 0;return n}}_floorByPredicate(t,e){if(e==="RECURSIVE"){let i,s=f(r=>{this.isRealNode(r)&&(this.isRealNode(r.left)&&s(r.left),t(r)&&(i=r),this.isRealNode(r.right)&&s(r.right))},"dfs");return s(this.root),i}else{let i=[],s=this.root,r;for(;i.length>0||this.isRealNode(s);)if(this.isRealNode(s))i.push(s),s=s.left;else{let n=i.pop();if(!this.isRealNode(n))break;t(n)&&(r=n),s=n.right}return r}}_lowerByKey(t,e){var i,s;if(e==="RECURSIVE"){let r=f(n=>{if(!this.isRealNode(n))return;if(this.comparator(n.key,t)<0){let o=r(n.right);return o!=null?o:n}else return r(n.left)},"dfs");return r(this.root)}else{let r=this.root,n;for(;this.isRealNode(r);)this.comparator(r.key,t)<0?(n=r,r=(i=r.right)!=null?i:void 0):r=(s=r.left)!=null?s:void 0;return n}}_lowerByPredicate(t,e){if(e==="RECURSIVE"){let i,s=f(r=>{this.isRealNode(r)&&(this.isRealNode(r.left)&&s(r.left),t(r)&&(i=r),this.isRealNode(r.right)&&s(r.right))},"dfs");return s(this.root),i}else{let i=[],s=this.root,r;for(;i.length>0||this.isRealNode(s);)if(this.isRealNode(s))i.push(s),s=s.left;else{let n=i.pop();if(!this.isRealNode(n))break;t(n)&&(r=n),s=n.right}return r}}_bound(t,e,i){if(t==null)return;if(this._isPredicate(t))return this._boundByPredicate(t,i);let s;if(this.isNode(t))s=t.key;else if(this.isEntry(t)){let r=t[0];if(r==null)return;s=r}else s=t;if(s!==void 0)return this._boundByKey(s,e,i)}_boundByKey(t,e,i){var s,r;if(i==="RECURSIVE"){let n=f(h=>{if(!this.isRealNode(h))return;let o=this.comparator(h.key,t);if(e?o>=0:o>0){let a=n(h.left);return a!=null?a:h}else return n(h.right)},"dfs");return n(this.root)}else{let n=this.root,h;for(;this.isRealNode(n);){let o=this.comparator(n.key,t);(e?o>=0:o>0)?(h=n,n=(s=n.left)!=null?s:void 0):n=(r=n.right)!=null?r:void 0}return h}}_boundByPredicate(t,e){if(e==="RECURSIVE"){let i,s=f(r=>{i||!this.isRealNode(r)||(this.isRealNode(r.left)&&s(r.left),!i&&t(r)&&(i=r),!i&&this.isRealNode(r.right)&&s(r.right))},"dfs");return s(this.root),i}else{let i=[],s=this.root;for(;i.length>0||this.isRealNode(s);)if(this.isRealNode(s))i.push(s),s=s.left;else{let r=i.pop();if(!this.isRealNode(r))break;if(t(r))return r;s=r.right}return}}_createInstance(t){let e=this.constructor;return new e([],{...this._snapshotOptions(),...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,{...this._snapshotOptions(),...e!=null?e:{}})}_snapshotOptions(){return{...super._snapshotOptions(),comparator:this._comparator}}_keyValueNodeOrEntryToNodeAndValue(t,e){let[i,s]=super._keyValueNodeOrEntryToNodeAndValue(t,e);return i===null?[void 0,void 0]:[i,e!=null?e:s]}_setRoot(t){t&&(t.parent=void 0),this._root=t}_compare(t,e){return this._comparator(t,e)}_deleteByKey(t){let e=this._root;for(;e;){let r=this._compare(e.key,t);if(r===0)break;e=r>0?e.left:e.right}if(!e)return!1;let i=f((r,n)=>{let h=r==null?void 0:r.parent;h?h.left===r?h.left=n:h.right=n:this._setRoot(n),n&&(n.parent=h)},"transplant"),s=f(r=>{if(r){for(;r.left!==void 0&&r.left!==null;)r=r.left;return r}},"minNode");if(e.left===void 0)i(e,e.right);else if(e.right===void 0)i(e,e.left);else{let r=s(e.right);r.parent!==e&&(i(r,r.right),r.right=e.right,r.right&&(r.right.parent=r)),i(e,r),r.left=e.left,r.left&&(r.left.parent=r)}return this._size=Math.max(0,this._size-1),!0}};f(_e,"BST");var de=_e,hi=class{constructor({frequency:t=0,max:e}){u(this,"_freq"),u(this,"_max"),u(this,"_freqMap"),u(this,"_msb"),u(this,"_negativeCount"),this._freq=t,this._max=e,this._freqMap={0:0},this._msb=De(e),this._negativeCount=t<0?e:0}get freqMap(){return this._freqMap}get msb(){return this._msb}get negativeCount(){return this._negativeCount}get freq(){return this._freq}get max(){return this._max}readSingle(t){return this._checkIndex(t),this._readSingle(t)}update(t,e){this._checkIndex(t);let i=this._readSingle(t);this._update(t,e),this._updateNegativeCount(i,i+e)}writeSingle(t,e){this._checkIndex(t),this._writeSingle(t,e)}read(t){if(!Number.isInteger(t))throw new Error("Invalid count");return this._read(Math.max(Math.min(t,this.max),0))}lowerBound(t){if(this.negativeCount>0)throw new Error("Sequence is not non-descending");return this._binarySearch(t,(e,i)=>e<i)}upperBound(t){if(this.negativeCount>0)throw new Error("Must not be descending");return this._binarySearch(t,(e,i)=>e<=i)}getPrefixSum(t){this._checkIndex(t),t++;let e=0;for(;t>0;)e+=this._getFrequency(t),t-=t&-t;return e}_getFrequency(t){return t in this.freqMap?this.freqMap[t]:this.freq*(t&-t)}_updateFrequency(t,e){this.freqMap[t]=this._getFrequency(t)+e}_checkIndex(t){if(!Number.isInteger(t))throw new Error("Invalid index: Index must be an integer.");if(t<0||t>=this.max)throw new Error("Index out of range: Index must be within the range [0, this.max).")}_readSingle(t){t=t+1;let e=this._getFrequency(t),i=t-(t&-t);for(t--;t!==i;)e-=this._getFrequency(t),t-=t&-t;return e}_updateNegativeCount(t,e){t<0&&e>=0?this._negativeCount--:t>=0&&e<0&&this._negativeCount++}_update(t,e){for(t=t+1;t<=this.max;)this._updateFrequency(t,e),t+=t&-t}_writeSingle(t,e){let i=this._readSingle(t);this._update(t,e-i),this._updateNegativeCount(i,e)}_read(t){let e=t,i=0;for(;e;)i+=this._getFrequency(e),e-=e&-e;return i}_binarySearch(t,e){let i=0,s=this.msb<<1,r=t;for(;s>i+1;){let n=i+s>>1,h=this._getFrequency(n);n<=this.max&&e(h,r)?(r-=h,i=n):s=n}return i}};f(hi,"BinaryIndexedTree");var ge=class{constructor(t,e,i,s){u(this,"_start",0),u(this,"_end",0),u(this,"_value"),u(this,"_sum",0),u(this,"_left"),u(this,"_right"),this._start=t,this._end=e,this._sum=i,this._value=s||void 0}get start(){return this._start}set start(t){this._start=t}get end(){return this._end}set end(t){this._end=t}get value(){return this._value}set value(t){this._value=t}get sum(){return this._sum}set sum(t){this._sum=t}get left(){return this._left}set left(t){this._left=t}get right(){return this._right}set right(t){this._right=t}};f(ge,"SegmentTreeNode");var ut=ge,oi=class{constructor(t,e,i){u(this,"_values",[]),u(this,"_start",0),u(this,"_end"),u(this,"_root"),e=e||0,i=i||t.length-1,this._values=t,this._start=e,this._end=i,t.length>0?this._root=this.build(e,i):(this._root=void 0,this._values=[])}get values(){return this._values}get start(){return this._start}get end(){return this._end}get root(){return this._root}build(t,e){if(t>e)return new ut(t,e,0);if(t===e)return new ut(t,e,this._values[t]);let i=t+Math.floor((e-t)/2),s=this.build(t,i),r=this.build(i+1,e),n=new ut(t,e,s.sum+r.sum);return n.left=s,n.right=r,n}updateNode(t,e,i){let s=this.root||void 0;if(!s)return;let r=f((n,h,o,l)=>{if(n.start===n.end&&n.start===h){n.sum=o,l!==void 0&&(n.value=l);return}let a=n.start+Math.floor((n.end-n.start)/2);h<=a?n.left&&r(n.left,h,o,l):n.right&&r(n.right,h,o,l),n.left&&n.right&&(n.sum=n.left.sum+n.right.sum)},"dfs");r(s,t,e,i)}querySumByRange(t,e){let i=this.root||void 0;if(!i)return 0;if(t<0||e>=this.values.length||t>e)return NaN;let s=f((r,n,h)=>{if(n<=r.start&&h>=r.end)return r.sum;let o=r.start+Math.floor((r.end-r.start)/2);if(h<=o)return r.left?s(r.left,n,h):NaN;if(n>o)return r.right?s(r.right,n,h):NaN;{let l=0,a=0;return r.left&&(l=s(r.left,n,o)),r.right&&(a=s(r.right,o+1,h)),l+a}},"dfs");return s(i,t,e)}};f(oi,"SegmentTree");var pe=class{constructor(t,e){u(this,"key"),u(this,"value"),u(this,"parent"),u(this,"_left"),u(this,"_right"),u(this,"_height",0),u(this,"_color","BLACK"),u(this,"_count",1),this.key=t,this.value=e}get left(){return this._left}set left(t){t&&(t.parent=this),this._left=t}get right(){return this._right}set right(t){t&&(t.parent=this),this._right=t}get height(){return this._height}set height(t){this._height=t}get color(){return this._color}set color(t){this._color=t}get count(){return this._count}set count(t){this._count=t}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}};f(pe,"AVLTreeNode");var Nt=pe,li=class extends de{constructor(t=[],e){super([],e),t&&super.setMany(t)}createNode(t,e){return new Nt(t,e)}isNode(t){return t instanceof Nt}set(t,e){if(t===null)return!1;let i=super.set(t,e);return i&&this._balancePath(t),i}delete(t){let e=super.delete(t);for(let{needBalanced:i}of e)i&&this._balancePath(i);return e}perfectlyBalance(t=this.iterationType){let e=this.dfs(n=>n,"IN",!1,this._root,t),i=e.length;if(i===0)return!1;this._clearNodes();let s=f((n,h,o)=>{if(n>h)return;let l=n+(h-n>>1),a=e[l];a.left=s(n,l-1,a),a.right=s(l+1,h,a),a.parent=o;let _=a.left?a.left.height:-1,d=a.right?a.right.height:-1;return a.height=Math.max(_,d)+1,a},"build"),r=s(0,i-1,void 0);return this._setRoot(r),this._size=i,!0}map(t,e,i){let s=this._createLike([],e),r=0;for(let[n,h]of this)s.set(t.call(i,h,n,r++,this));return s}_createInstance(t){let e=this.constructor;return new e([],{...this._snapshotOptions(),...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(t,e){let i=this.ensureNode(t),s=this.ensureNode(e);if(i&&s){let{key:r,value:n,height:h}=s,o=this.createNode(r,n);return o&&(o.height=h,s.key=i.key,this._isMapMode||(s.value=i.value),s.height=i.height,i.key=o.key,this._isMapMode||(i.value=o.value),i.height=o.height),s}}_balanceFactor(t){let e=t.left?t.left.height:-1;return(t.right?t.right.height:-1)-e}_updateHeight(t){let e=t.left?t.left.height:-1,i=t.right?t.right.height:-1;t.height=1+Math.max(e,i)}_balanceLL(t){let e=t.parent,i=t.left;i!==null&&(t.parent=i),i&&i.right&&(i.right.parent=t),i&&(i.parent=e),t===this.root?i&&this._setRoot(i):(e==null?void 0:e.left)===t?e.left=i:e&&(e.right=i),i&&(t.left=i.right,i.right=t),this._updateHeight(t),i&&this._updateHeight(i)}_balanceLR(t){let e=t.parent,i=t.left,s;i&&(s=i.right),t&&s!==null&&(t.parent=s),i&&s!==null&&(i.parent=s),s&&(s.left&&i!==null&&(s.left.parent=i),s.right&&(s.right.parent=t),s.parent=e),t===this.root?s&&this._setRoot(s):e&&(e.left===t?e.left=s:e.right=s),s&&(t.left=s.right,i&&(i.right=s.left),s.left=i,s.right=t),this._updateHeight(t),i&&this._updateHeight(i),s&&this._updateHeight(s)}_balanceRR(t){let e=t.parent,i=t.right;i!==null&&(t.parent=i),i&&(i.left&&(i.left.parent=t),i.parent=e),t===this.root?i&&this._setRoot(i):e&&(e.left===t?e.left=i:e.right=i),i&&(t.right=i.left,i.left=t),this._updateHeight(t),i&&this._updateHeight(i)}_balanceRL(t){let e=t.parent,i=t.right,s;i&&(s=i.left),s!==null&&(t.parent=s),i&&s!==null&&(i.parent=s),s&&(s.left&&(s.left.parent=t),s.right&&i!==null&&(s.right.parent=i),s.parent=e),t===this.root?s&&this._setRoot(s):e&&(e.left===t?e.left=s:e.right=s),s&&(t.right=s.left),i&&s&&(i.left=s.right),s&&(s.left=t),s&&(s.right=i),this._updateHeight(t),i&&this._updateHeight(i),s&&this._updateHeight(s)}_balancePath(t){t=this.ensureNode(t);let e=this.getPathToRoot(t,i=>i,!1);for(let i=0;i<e.length;i++){let s=e[i];if(s)switch(this._updateHeight(s),this._balanceFactor(s)){case-2:s&&s.left&&(this._balanceFactor(s.left)<=0?this._balanceLL(s):this._balanceLR(s));break;case 2:s&&s.right&&(this._balanceFactor(s.right)>=0?this._balanceRR(s):this._balanceRL(s))}}}_replaceNode(t,e){return e.height=t.height,super._replaceNode(t,e)}};f(li,"AVLTree");var ve=class{constructor(t,e,i="BLACK"){u(this,"key"),u(this,"value"),u(this,"parent"),u(this,"_left"),u(this,"_right"),u(this,"_height",0),u(this,"_color","BLACK"),u(this,"_count",1),this.key=t,this.value=e,this.color=i}get left(){return this._left}set left(t){t&&(t.parent=this),this._left=t}get right(){return this._right}set right(t){t&&(t.parent=this),this._right=t}get height(){return this._height}set height(t){this._height=t}get color(){return this._color}set color(t){this._color=t}get count(){return this._count}set count(t){this._count=t}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}};f(ve,"RedBlackTreeNode");var W=ve,me=class extends de{constructor(t=[],e){super([],e),u(this,"_root"),u(this,"_header"),u(this,"_minNode"),u(this,"_maxNode"),this._root=this.NIL,this._header=new W(void 0,void 0,"BLACK"),this._header.parent=this.NIL,this._header._left=this.NIL,this._header._right=this.NIL,t&&this.setMany(t)}get root(){return this._root}createNode(t,e,i="BLACK"){return new W(t,e,i)}isNode(t){return t instanceof W}clear(){super.clear(),this._root=this.NIL,this._header.parent=this.NIL,this._setMinCache(void 0),this._setMaxCache(void 0)}_findNodeByKey(t){var e,i,s;let r=this.NIL,n=this._compare.bind(this),h=(e=this._header.parent)!=null?e:r;for(;h!==r;){let o=n(t,h.key);if(o<0)h=(i=h.left)!=null?i:r;else if(o>0)h=(s=h.right)!=null?s:r;else return h}}_predecessorOf(t){let e=this.NIL;if(t.left&&t.left!==e){let r=t.left;for(;r.right&&r.right!==e;)r=r.right;return r}let i=t,s=t.parent;for(;s&&i===s.left;)i=s,s=s.parent;return s}_successorOf(t){let e=this.NIL;if(t.right&&t.right!==e){let r=t.right;for(;r.left&&r.left!==e;)r=r.left;return r}let i=t,s=t.parent;for(;s&&i===s.right;)i=s,s=s.parent;return s}_attachNewNode(t,e,i){let s=this.NIL;i.parent=t,e==="left"?t.left=i:t.right=i,i.left=s,i.right=s,i.color="RED",this._insertFixup(i),this.isRealNode(this._root)&&(this._root.color="BLACK")}_setMinCache(t){this._minNode=t,this._header._left=t!=null?t:this.NIL}_setMaxCache(t){this._maxNode=t,this._header._right=t!=null?t:this.NIL}_setKVNode(t,e){var i,s,r,n,h,o,l;let a=this.NIL,_=this._comparator,d=this._header,p=(i=d._left)!=null?i:a;if(p!==a){let E=_(t,p.key);if(E===0)return p.value=e,this._isMapMode&&this._store.set(t,p),{node:p,created:!1};let T=p.left;if(E<0&&(T===a||T===null||T===void 0)){let C=this.createNode(t,e);return this._attachNewNode(p,"left",C),this._isMapMode&&this._store.set(C.key,C),this._size++,this._setMinCache(C),d._right===a&&this._setMaxCache(C),{node:C,created:!0}}if(E>0){let C=(s=d._right)!=null?s:a,Mt=_(t,C.key);if(Mt===0)return C.value=e,this._isMapMode&&this._store.set(t,C),{node:C,created:!1};let ot=C.right;if(Mt>0&&(ot===a||ot===null||ot===void 0)){let D=this.createNode(t,e);return this._attachNewNode(C,"right",D),this._isMapMode&&this._store.set(D.key,D),this._size++,this._setMaxCache(D),d._left===a&&this._setMinCache(D),{node:D,created:!0}}}}let m=_,x=this._isMapMode,b=this._store,w=(r=this._header.parent)!=null?r:a,M,k=0;for(;w!==a;)if(M=w,k=m(t,w.key),k<0)w=(n=w.left)!=null?n:a;else if(k>0)w=(h=w.right)!=null?h:a;else return w.value=e,x&&b.set(t,w),{node:w,created:!1};let v=this.createNode(t,e);if(v.parent=M,M?k<0?M.left=v:M.right=v:this._setRoot(v),v.left=a,v.right=a,v.color="RED",this._insertFixup(v),this.isRealNode(this._root))this._root.color="BLACK";else return;x&&b.set(v.key,v),this._size++;let y=(o=this._header._left)!=null?o:a,N=(l=this._header._right)!=null?l:a;return y===a||N===a?(this._setMinCache(v),this._setMaxCache(v)):M===N&&k>0?this._setMaxCache(v):M===y&&k<0?this._setMinCache(v):(m(v.key,y.key)<0&&this._setMinCache(v),m(v.key,N.key)>0&&this._setMaxCache(v)),{node:v,created:!0}}_setKV(t,e){if(this._isMapMode){let s=this._store.get(t);if(s)return s.value=e,!0}return this._setKVNode(t,e)!==void 0}setWithHintNode(t,e,i){var s,r,n,h,o,l,a,_,d,p,m,x,b;if(!i||!this.isRealNode(i))return(s=this._setKVNode(t,e))==null?void 0:s.node;let w=this._compare.bind(this),M=w(t,i.key);if(M===0)return i.value=e,this._isMapMode&&this._store.set(t,i),i;if(M<0){if(!this.isRealNode(i.left)){let y=this.createNode(t,e);if(!this.isRealNode(y))return;this._attachNewNode(i,"left",y),this._isMapMode&&this._store.set(t,y),this._size++;let N=this.NIL,E=(r=this._header._left)!=null?r:N;(E===N||this._compare(y.key,E.key)<0)&&this._setMinCache(y);let T=(n=this._header._right)!=null?n:N;return(T===N||this._compare(y.key,T.key)>0)&&this._setMaxCache(y),y}let v=this._predecessorOf(i);if(v&&w(v.key,t)>=0)return(h=this._setKVNode(t,e))==null?void 0:h.node;if(v&&!this.isRealNode(v.right)){let y=this.createNode(t,e);if(!this.isRealNode(y))return;this._attachNewNode(v,"right",y),this._isMapMode&&this._store.set(t,y),this._size++;let N=this.NIL,E=(o=this._header._left)!=null?o:N;(E===N||this._compare(y.key,E.key)<0)&&this._setMinCache(y);let T=(l=this._header._right)!=null?l:N;return(T===N||this._compare(y.key,T.key)>0)&&this._setMaxCache(y),y}return(a=this._setKVNode(t,e))==null?void 0:a.node}if(!this.isRealNode(i.right)){let v=this.createNode(t,e);if(!this.isRealNode(v))return;this._attachNewNode(i,"right",v),this._isMapMode&&this._store.set(t,v),this._size++;let y=this.NIL,N=(_=this._header._left)!=null?_:y;(N===y||this._compare(v.key,N.key)<0)&&this._setMinCache(v);let E=(d=this._header._right)!=null?d:y;return(E===y||this._compare(v.key,E.key)>0)&&this._setMaxCache(v),v}let k=this._successorOf(i);if(k&&w(k.key,t)<=0)return(p=this._setKVNode(t,e))==null?void 0:p.node;if(k&&!this.isRealNode(k.left)){let v=this.createNode(t,e);if(!this.isRealNode(v))return;this._attachNewNode(k,"left",v),this._isMapMode&&this._store.set(t,v),this._size++;let y=this.NIL,N=(m=this._header._left)!=null?m:y;(N===y||this._compare(v.key,N.key)<0)&&this._setMinCache(v);let E=(x=this._header._right)!=null?x:y;return(E===y||this._compare(v.key,E.key)>0)&&this._setMaxCache(v),v}return(b=this._setKVNode(t,e))==null?void 0:b.node}setWithHint(t,e,i){return this.setWithHintNode(t,e,i)!==void 0}set(t,e){if(!this.isNode(t)){if(t==null)return!1;if(this.isEntry(t)){let n=t[0];if(n==null)return!1;let h=e!=null?e:t[1];return this._setKV(n,h)}return this._setKV(t,e)}let[i,s]=this._keyValueNodeOrEntryToNodeAndValue(t,e);if(!this.isRealNode(i))return!1;let r=this._insert(i);if(r==="CREATED"){if(this.isRealNode(this._root))this._root.color="BLACK";else return!1;if(this._isMapMode){let n=this.getNode(i.key);this.isRealNode(n)&&(n.value=s,this._store.set(n.key,n))}return this._size++,!0}if(r==="UPDATED"){if(this._isMapMode){let n=this.getNode(i.key);this.isRealNode(n)&&(n.value=s,this._store.set(n.key,n))}return!0}return!1}delete(t){if(t===null)return[];let e=[],i;if(this._isPredicate(t)?i=this.getNode(t):i=this.isRealNode(t)?t:this.getNode(t),!i)return e;let s=i===this._minNode,r=i===this._maxNode,n=s?this._successorOf(i):void 0,h=r?this._predecessorOf(i):void 0,o=i.color,l;if(!this.isRealNode(i.left))i.right!==null&&(l=i.right,this._transplant(i,i.right));else if(!this.isRealNode(i.right))l=i.left,this._transplant(i,i.left);else{let a=this.getLeftMost(_=>_,i.right);a&&(o=a.color,a.right!==null&&(l=a.right),a.parent===i?this.isRealNode(l)&&(l.parent=a):(a.right!==null&&(this._transplant(a,a.right),a.right=i.right),this.isRealNode(a.right)&&(a.right.parent=a)),this._transplant(i,a),a.left=i.left,this.isRealNode(a.left)&&(a.left.parent=a),a.color=i.color)}return this._isMapMode&&this._store.delete(i.key),this._size--,this._size<=0?(this._setMinCache(void 0),this._setMaxCache(void 0)):(s&&this._setMinCache(n),r&&this._setMaxCache(h),(!this._minNode||!this.isRealNode(this._minNode))&&this._setMinCache(this.isRealNode(this._root)?this.getLeftMost(a=>a,this._root):void 0),(!this._maxNode||!this.isRealNode(this._maxNode))&&this._setMaxCache(this.isRealNode(this._root)?this.getRightMost(a=>a,this._root):void 0)),o==="BLACK"&&this._deleteFixup(l),e.push({deleted:i,needBalanced:void 0}),e}map(t,e,i){let s=this._createLike([],e),r=0;for(let[n,h]of this)s.set(t.call(i,h,n,r++,this));return s}_createInstance(t){let e=this.constructor;return new e([],{...this._snapshotOptions(),...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,{...this._snapshotOptions(),...e!=null?e:{}})}_setRoot(t){let e=this.NIL;t&&(t.parent=void 0),this._root=t,this._header.parent=t!=null?t:e}_replaceNode(t,e){return e.color=t.color,super._replaceNode(t,e)}_insert(t){var e,i,s;let r=this.NIL,n=this._compare.bind(this),h=(e=this._header.parent)!=null?e:r,o,l=0;for(;h!==r;)if(o=h,l=n(t.key,h.key),l<0)h=(i=h.left)!=null?i:r;else if(l>0)h=(s=h.right)!=null?s:r;else return this._replaceNode(h,t),"UPDATED";return t.parent=o,o?l<0?o.left=t:o.right=t:this._setRoot(t),t.left=r,t.right=r,t.color="RED",this._insertFixup(t),"CREATED"}_transplant(t,e){t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._setRoot(e),e&&(e.parent=t.parent)}_insertFixup(t){let e=this._leftRotate.bind(this),i=this._rightRotate.bind(this);for(;t;){let s=t.parent;if(!s||s.color!=="RED")break;let r=s.parent;if(!r)break;if(s===r.left){let n=r.right;if((n==null?void 0:n.color)==="RED"){s.color="BLACK",n.color="BLACK",r.color="RED",t=r;continue}t===s.right&&(t=s,e(t));let h=t==null?void 0:t.parent,o=h==null?void 0:h.parent;h&&o&&(h.color="BLACK",o.color="RED",i(o))}else{let n=r.left;if((n==null?void 0:n.color)==="RED"){s.color="BLACK",n.color="BLACK",r.color="RED",t=r;continue}t===s.left&&(t=s,i(t));let h=t==null?void 0:t.parent,o=h==null?void 0:h.parent;h&&o&&(h.color="BLACK",o.color="RED",e(o))}break}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(t){var e,i,s,r;if(!t||t===this.root||t.color==="BLACK"){t&&(t.color="BLACK");return}for(;t&&t!==this.root&&t.color==="BLACK";){let n=t.parent;if(!n)break;if(t===n.left){let h=n.right;(h==null?void 0:h.color)==="RED"&&(h.color="BLACK",n.color="RED",this._leftRotate(n),h=n.right),((i=(e=h==null?void 0:h.left)==null?void 0:e.color)!=null?i:"BLACK")==="BLACK"?(h&&(h.color="RED"),t=n):(h!=null&&h.left&&(h.left.color="BLACK"),h&&(h.color=n.color),n.color="BLACK",this._rightRotate(n),t=this.root)}else{let h=n.left;(h==null?void 0:h.color)==="RED"&&(h.color="BLACK",n&&(n.color="RED"),this._rightRotate(n),n&&(h=n.left)),((r=(s=h==null?void 0:h.right)==null?void 0:s.color)!=null?r:"BLACK")==="BLACK"?(h&&(h.color="RED"),t=n):(h!=null&&h.right&&(h.right.color="BLACK"),h&&(h.color=n.color),n&&(n.color="BLACK"),this._leftRotate(n),t=this.root)}}t&&(t.color="BLACK")}_leftRotate(t){if(!t||!t.right)return;let e=t.right;t.right=e.left,e.left&&e.left!==this.NIL&&(e.left.parent=t),e.parent=t.parent,t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._setRoot(e),e.left=t,t.parent=e}_rightRotate(t){if(!t||!t.left)return;let e=t.left;t.left=e.right,e.right&&e.right!==this.NIL&&(e.right.parent=t),e.parent=t.parent,t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._setRoot(e),e.right=t,t.parent=e}};f(me,"RedBlackTree");var ht=me,I,P,O,Me=class Q{constructor(t=[],e={}){A(this,I),A(this,P),A(this,O);var i;B(this,O,e.comparator);let s=e.toElementFn,r=(i=e.comparator)!=null?i:Q.createDefaultComparator();B(this,P,e.comparator===void 0),B(this,I,new ht([],{comparator:r,isMapMode:e.isMapMode}));for(let n of t){let h=s?s(n):n;this.add(h)}}static createDefaultComparator(){return(t,e)=>{if(typeof t=="number"&&typeof e=="number"){if(Number.isNaN(t)||Number.isNaN(e))throw new TypeError("TreeSet: NaN is not a valid key");let i=Object.is(t,-0)?0:t,s=Object.is(e,-0)?0:e;return i>s?1:i<s?-1:0}if(typeof t=="string"&&typeof e=="string")return t>e?1:t<e?-1:0;if(t instanceof Date&&e instanceof Date){let i=t.getTime(),s=e.getTime();if(Number.isNaN(i)||Number.isNaN(s))throw new TypeError("TreeSet: invalid Date key");return i>s?1:i<s?-1:0}throw new TypeError("TreeSet: comparator is required for non-number/non-string/non-Date keys")}}get size(){return g(this,I).size}isEmpty(){return this.size===0}_validateKey(t){if(g(this,P)){if(typeof t=="number"){if(Number.isNaN(t))throw new TypeError("TreeSet: NaN is not a valid key");return}if(typeof t!="string"){if(t instanceof Date){if(Number.isNaN(t.getTime()))throw new TypeError("TreeSet: invalid Date key");return}throw new TypeError("TreeSet: comparator is required for non-number/non-string/non-Date keys")}}}add(t){return this._validateKey(t),g(this,I).set(t,void 0),this}has(t){return this._validateKey(t),g(this,I).has(t)}delete(t){var e;this._validateKey(t);let i=g(this,I).delete(t);return Array.isArray(i)&&i.length>0&&!!((e=i[0])!=null&&e.deleted)}clear(){g(this,I).clear()}keys(){return g(this,I).keys()}values(){return this.keys()}*entries(){for(let t of this.keys())yield[t,t]}[Symbol.iterator](){return this.keys()}forEach(t,e){for(let i of this)t.call(e,i,i,this)}map(t,e={},i){let s=new Q([],e),r=0;for(let n of this){let h=i===void 0?t(n,r++,this):t.call(i,n,r++,this);s.add(h)}return s}filter(t,e){let i=new Q([],{comparator:g(this,O)}),s=0;for(let r of this)(e===void 0?t(r,s++,this):t.call(e,r,s++,this))&&i.add(r);return i}reduce(t,e){let i=e,s=0;for(let r of this)i=t(i,r,s++,this);return i}every(t,e){let i=0;for(let s of this)if(!(e===void 0?t(s,i++,this):t.call(e,s,i++,this)))return!1;return!0}some(t,e){let i=0;for(let s of this)if(e===void 0?t(s,i++,this):t.call(e,s,i++,this))return!0;return!1}find(t,e){let i=0;for(let s of this)if(e===void 0?t(s,i++,this):t.call(e,s,i++,this))return s}toArray(){return[...this]}print(){g(this,I).print()}first(){return g(this,I).getLeftMost()}last(){return g(this,I).getRightMost()}pollFirst(){let t=this.first();if(t!==void 0)return this.delete(t),t}pollLast(){let t=this.last();if(t!==void 0)return this.delete(t),t}ceiling(t){return this._validateKey(t),g(this,I).ceiling(t)}floor(t){return this._validateKey(t),g(this,I).floor(t)}higher(t){return this._validateKey(t),g(this,I).higher(t)}lower(t){return this._validateKey(t),g(this,I).lower(t)}rangeSearch(t,e={}){let{lowInclusive:i=!0,highInclusive:s=!0}=e,[r,n]=t;this._validateKey(r),this._validateKey(n);let h=g(this,I).rangeSearch([r,n]),o=[],l=g(this,I).comparator;for(let a of h)a!==void 0&&(!i&&l(a,r)===0||!s&&l(a,n)===0||o.push(a));return o}};I=new WeakMap;P=new WeakMap;O=new WeakMap;f(Me,"TreeSet");var we=Me,ai=class extends W{constructor(t,e=[]){super(t,e)}};f(ai,"TreeMultiMapNode");var L,X,ye=class ${constructor(t=[],e={}){A(this,L),A(this,X);var i;let s=(i=e.comparator)!=null?i:we.createDefaultComparator();B(this,X,e.comparator===void 0);let r=e.toEntryFn;B(this,L,new ht([],{...e,comparator:s,isMapMode:e.isMapMode}));for(let n of t)if(n!=null){if(r){let[h,o]=r(n);if(h==null)continue;o!==void 0?g(this,L).set(h,Array.isArray(o)?[...o]:[o]):g(this,L).set(h,[]);continue}if(Array.isArray(n)){let[h,o]=n;if(h==null)continue;o!==void 0?g(this,L).set(h,[...o]):g(this,L).set(h,[]);continue}g(this,L).set(n,[])}}_validateKey(t){if(g(this,X)){if(typeof t=="number"){if(Number.isNaN(t))throw new TypeError("TreeMultiMap: NaN is not a valid key");return}if(typeof t!="string"){if(t instanceof Date){if(Number.isNaN(t.getTime()))throw new TypeError("TreeMultiMap: invalid Date key");return}throw new TypeError("TreeMultiMap: comparator is required for non-number/non-string/non-Date keys")}}}get size(){return g(this,L).size}isEmpty(){return this.size===0}clear(){g(this,L).clear()}count(t){let e=this.get(t);return Array.isArray(e)?e.length:0}get totalSize(){let t=0;for(let[,e]of this)t+=e.length;return t}has(t){return this._validateKey(t),g(this,L).has(t)}get(t){return this._validateKey(t),g(this,L).get(t)}add(t,e){this._validateKey(t);let i=g(this,L).get(t);return i?(i.push(e),!0):g(this,L).set(t,[e])}set(t,e){if(t==null)return!1;if(Array.isArray(t)){let[i,s]=t;if(i==null)return!1;if(e!==void 0)return this.add(i,e);if(s===void 0)return g(this,L).set(i,[]);let r=g(this,L).get(i);return r?(r.push(...s),!0):g(this,L).set(i,[...s])}return e!==void 0?this.add(t,e):g(this,L).set(t,[])}delete(t){return this._validateKey(t),g(this,L).delete(t).length>0}hasEntry(t,e,i=Object.is){let s=this.get(t);return Array.isArray(s)?s.some(r=>i(r,e)):!1}deleteValue(t,e,i=Object.is){let s=this.get(t);if(!Array.isArray(s))return!1;let r=s.findIndex(n=>i(n,e));return r===-1?!1:(s.splice(r,1),s.length===0&&this.delete(t),!0)}deleteValues(t,e,i=Object.is){let s=this.get(t);if(!Array.isArray(s)||s.length===0)return 0;let r=0;for(let n=s.length-1;n>=0;n--)i(s[n],e)&&(s.splice(n,1),r++);return s.length===0&&r>0&&this.delete(t),r}*[Symbol.iterator](){for(let[t,e]of g(this,L))yield[t,e!=null?e:[]]}*keys(){yield*g(this,L).keys()}*values(){for(let[,t]of this)yield t}*entriesOf(t){let e=this.get(t);if(Array.isArray(e))for(let i of e)yield[t,i]}*valuesOf(t){let e=this.get(t);Array.isArray(e)&&(yield*e)}*flatEntries(){for(let[t,e]of this)for(let i of e)yield[t,i]}first(){let t=g(this,L).getLeftMost();if(t===void 0)return;let e=this.get(t);return e===void 0?void 0:[t,e]}last(){let t=g(this,L).getRightMost();if(t===void 0)return;let e=this.get(t);return e===void 0?void 0:[t,e]}pollFirst(){let t=this.first();if(t)return this.delete(t[0]),t}pollLast(){let t=this.last();if(t)return this.delete(t[0]),t}ceiling(t){this._validateKey(t);let e=g(this,L).ceiling(t);if(e===void 0)return;let i=this.get(e);return i===void 0?void 0:[e,i]}floor(t){this._validateKey(t);let e=g(this,L).floor(t);if(e===void 0)return;let i=this.get(e);return i===void 0?void 0:[e,i]}higher(t){this._validateKey(t);let e=g(this,L).higher(t);if(e===void 0)return;let i=this.get(e);return i===void 0?void 0:[e,i]}lower(t){this._validateKey(t);let e=g(this,L).lower(t);if(e===void 0)return;let i=this.get(e);return i===void 0?void 0:[e,i]}print(){g(this,L).print()}forEach(t){for(let[e,i]of this)t(i,e,this)}filter(t){let e=[];for(let[i,s]of this)t(s,i,this)&&e.push([i,s]);return new $(e,{comparator:this.comparator})}map(t){let e=[];for(let[i,s]of this)e.push(t(s,i,this));return new $(e,{comparator:this.comparator})}reduce(t,e){let i=e;for(let[s,r]of this)i=t(i,r,s,this);return i}setMany(t){let e=[];for(let i of t)e.push(this.set(i));return e}rangeSearch(t,e){return g(this,L).rangeSearch(t,e)}clone(){return new $(this,{comparator:this.comparator,isMapMode:g(this,L).isMapMode})}get comparator(){return g(this,L).comparator}};L=new WeakMap;X=new WeakMap;f(ye,"TreeMultiMap");var be=ye,F,J,Y,ui=class Z{constructor(t=[],e={}){A(this,F),A(this,J),A(this,Y);var i;B(this,Y,e.comparator);let s=e.toEntryFn,r=(i=e.comparator)!=null?i:Z.createDefaultComparator();B(this,J,e.comparator===void 0),B(this,F,new ht([],{comparator:r,isMapMode:e.isMapMode}));for(let n of t){let h,o;if(s)[h,o]=s(n);else{if(!Array.isArray(n)||n.length<2)throw new TypeError("TreeMap: each entry must be a [key, value] tuple");h=n[0],o=n[1]}this.set(h,o)}}static createDefaultComparator(){return(t,e)=>{if(typeof t=="number"&&typeof e=="number"){if(Number.isNaN(t)||Number.isNaN(e))throw new TypeError("TreeMap: NaN is not a valid key");let i=Object.is(t,-0)?0:t,s=Object.is(e,-0)?0:e;return i>s?1:i<s?-1:0}if(typeof t=="string"&&typeof e=="string")return t>e?1:t<e?-1:0;if(t instanceof Date&&e instanceof Date){let i=t.getTime(),s=e.getTime();if(Number.isNaN(i)||Number.isNaN(s))throw new TypeError("TreeMap: invalid Date key");return i>s?1:i<s?-1:0}throw new TypeError("TreeMap: comparator is required for non-number/non-string/non-Date keys")}}_validateKey(t){if(g(this,J)){if(typeof t=="number"){if(Number.isNaN(t))throw new TypeError("TreeMap: NaN is not a valid key");return}if(typeof t!="string"){if(t instanceof Date){if(Number.isNaN(t.getTime()))throw new TypeError("TreeMap: invalid Date key");return}throw new TypeError("TreeMap: comparator is required for non-number/non-string/non-Date keys")}}}get size(){return g(this,F).size}isEmpty(){return this.size===0}set(t,e){return this._validateKey(t),g(this,F).set(t,e),this}get(t){return this._validateKey(t),g(this,F).get(t)}has(t){return this._validateKey(t),g(this,F).has(t)}delete(t){var e;this._validateKey(t);let i=g(this,F).delete(t);return Array.isArray(i)&&i.length>0&&!!((e=i[0])!=null&&e.deleted)}clear(){g(this,F).clear()}keys(){return g(this,F).keys()}_entryFromKey(t){return[t,g(this,F).get(t)]}*values(){for(let t of this.keys())yield this._entryFromKey(t)[1]}*entries(){for(let t of this.keys())yield this._entryFromKey(t)}[Symbol.iterator](){return this.entries()}forEach(t,e){for(let[i,s]of this)t.call(e,s,i,this)}map(t,e={},i){let s=new Z([],e),r=0;for(let[n,h]of this){let[o,l]=i===void 0?t(h,n,r++,this):t.call(i,h,n,r++,this);s.set(o,l)}return s}filter(t,e){let i=new Z([],{comparator:g(this,Y)}),s=0;for(let[r,n]of this)(e===void 0?t(n,r,s++,this):t.call(e,n,r,s++,this))&&i.set(r,n);return i}reduce(t,e){let i=e,s=0;for(let[r,n]of this)i=t(i,n,r,s++,this);return i}every(t,e){let i=0;for(let[s,r]of this)if(!(e===void 0?t(r,s,i++,this):t.call(e,r,s,i++,this)))return!1;return!0}some(t,e){let i=0;for(let[s,r]of this)if(e===void 0?t(r,s,i++,this):t.call(e,r,s,i++,this))return!0;return!1}find(t,e){let i=0;for(let[s,r]of this)if(e===void 0?t(r,s,i++,this):t.call(e,r,s,i++,this))return[s,r]}toArray(){return[...this]}print(){g(this,F).print()}first(){let t=g(this,F).getLeftMost();return t===void 0?void 0:this._entryFromKey(t)}last(){let t=g(this,F).getRightMost();return t===void 0?void 0:this._entryFromKey(t)}pollFirst(){let t=this.first();if(t)return this.delete(t[0]),t}pollLast(){let t=this.last();if(t)return this.delete(t[0]),t}ceiling(t){this._validateKey(t);let e=g(this,F).ceiling(t);return e===void 0?void 0:this._entryFromKey(e)}floor(t){this._validateKey(t);let e=g(this,F).floor(t);return e===void 0?void 0:this._entryFromKey(e)}higher(t){this._validateKey(t);let e=g(this,F).higher(t);return e===void 0?void 0:this._entryFromKey(e)}lower(t){this._validateKey(t);let e=g(this,F).lower(t);return e===void 0?void 0:this._entryFromKey(e)}rangeSearch(t,e={}){let{lowInclusive:i=!0,highInclusive:s=!0}=e,[r,n]=t;this._validateKey(r),this._validateKey(n);let h=g(this,F).rangeSearch([r,n]),o=[],l=g(this,F).comparator;for(let a of h)a!==void 0&&(!i&&l(a,r)===0||!s&&l(a,n)===0||o.push(this._entryFromKey(a)));return o}};F=new WeakMap;J=new WeakMap;Y=new WeakMap;f(ui,"TreeMap");var R,K,fi=class tt{constructor(t=[],e={}){A(this,R),A(this,K),u(this,"_size",0);var i;let s=e.toElementFn,r=(i=e.comparator)!=null?i:we.createDefaultComparator();B(this,K,e.comparator===void 0),B(this,R,new ht([],{comparator:r,isMapMode:e.isMapMode}));for(let n of t){let h=s?s(n):n;this.add(h)}}_validateKey(t){if(g(this,K)){if(typeof t=="number"){if(Number.isNaN(t))throw new TypeError("TreeMultiSet: NaN is not a valid key");return}if(typeof t!="string"){if(t instanceof Date){if(Number.isNaN(t.getTime()))throw new TypeError("TreeMultiSet: invalid Date key");return}throw new TypeError("TreeMultiSet: comparator is required for non-number/non-string/non-Date keys")}}}_validateCount(t){if(!Number.isSafeInteger(t)||t<0)throw new RangeError("TreeMultiSet: count must be a safe integer >= 0")}get size(){return this._size}get distinctSize(){return g(this,R).size}isEmpty(){return this.size===0}has(t){return this._validateKey(t),this.count(t)>0}count(t){var e;return this._validateKey(t),(e=g(this,R).get(t))!=null?e:0}add(t,e=1){var i;if(this._validateKey(t),this._validateCount(e),e===0)return!1;let r=((i=g(this,R).get(t))!=null?i:0)+e;return g(this,R).set(t,r),this._size+=e,!0}setCount(t,e){var i;this._validateKey(t),this._validateCount(e);let s=(i=g(this,R).get(t))!=null?i:0;return s===e?!1:(e===0?s!==0&&g(this,R).delete(t):g(this,R).set(t,e),this._size+=e-s,!0)}delete(t,e=1){var i;if(this._validateKey(t),this._validateCount(e),e===0)return!1;let s=(i=g(this,R).get(t))!=null?i:0;if(s===0)return!1;let r=Math.min(s,e),n=s-r;return n===0?g(this,R).delete(t):g(this,R).set(t,n),this._size-=r,!0}deleteAll(t){var e;this._validateKey(t);let i=(e=g(this,R).get(t))!=null?e:0;return i===0?!1:(g(this,R).delete(t),this._size-=i,!0)}*keysDistinct(){yield*g(this,R).keys()}*entries(){for(let[t,e]of g(this,R))yield[t,e!=null?e:0]}*[Symbol.iterator](){for(let[t,e]of this.entries())for(let i=0;i<e;i++)yield t}toArray(){return[...this]}toDistinctArray(){return[...this.keysDistinct()]}toEntries(){return[...this.entries()]}get comparator(){return g(this,R)._comparator}clear(){g(this,R).clear(),this._size=0}first(){return g(this,R).getLeftMost()}last(){return g(this,R).getRightMost()}pollFirst(){let t=this.first();if(t!==void 0)return this.deleteAll(t),t}pollLast(){let t=this.last();if(t!==void 0)return this.deleteAll(t),t}ceiling(t){return this._validateKey(t),g(this,R).ceiling(t)}floor(t){return this._validateKey(t),g(this,R).floor(t)}higher(t){return this._validateKey(t),g(this,R).higher(t)}lower(t){return this._validateKey(t),g(this,R).lower(t)}forEach(t){for(let[e,i]of this.entries())t(e,i)}filter(t){let e=new tt([],{comparator:g(this,K)?void 0:this.comparator,isMapMode:g(this,R)._isMapMode});for(let[i,s]of this.entries())t(i,s)&&e.add(i,s);return e}reduce(t,e){let i=e;for(let[s,r]of this.entries())i=t(i,s,r);return i}map(t,e){let i=new tt([],{comparator:e==null?void 0:e.comparator,isMapMode:g(this,R)._isMapMode});for(let[s,r]of this.entries()){let[n,h]=t(s,r);i.add(n,h)}return i}clone(){let t=new tt([],{comparator:g(this,K)?void 0:this.comparator,isMapMode:g(this,R)._isMapMode});for(let[e,i]of this.entries())t.add(e,i);return t}rangeSearch(t,e){let i=e!=null?e:(s=>s);return g(this,R).rangeSearch(t,s=>i(s.key))}print(){g(this,R).print()}};R=new WeakMap;K=new WeakMap;f(fi,"TreeMultiSet");var xe=class extends nt{constructor(t=[],e){super(t,e)}};f(xe,"PriorityQueue");var ke=xe,ci=class extends ke{constructor(t=[],e){super(t,e)}};f(ci,"MinPriorityQueue");var _i=class extends ke{constructor(t=[],e){super(t,{comparator:f((i,s)=>{if(typeof i=="object"||typeof s=="object")throw TypeError("When comparing object types, a custom comparator must be defined in the constructor's options parameter.");return i<s?1:i>s?-1:0},"comparator"),...e})}};f(_i,"MaxPriorityQueue");var di=class V{constructor(t,e){u(this,"_rows",0),u(this,"_cols",0),u(this,"_data");var i,s,r;if(e){let{rows:n,cols:h,addFn:o,subtractFn:l,multiplyFn:a}=e;typeof n=="number"&&n>0?this._rows=n:this._rows=t.length,typeof h=="number"&&h>0?this._cols=h:this._cols=((i=t[0])==null?void 0:i.length)||0,o&&(this._addFn=o),l&&(this._subtractFn=l),a&&(this._multiplyFn=a)}else this._rows=t.length,this._cols=(r=(s=t[0])==null?void 0:s.length)!=null?r:0;if(t.length>0)this._data=t;else{this._data=[];for(let n=0;n<this.rows;n++)this._data[n]=new Array(this.cols).fill(0)}}get rows(){return this._rows}get cols(){return this._cols}get data(){return this._data}get addFn(){return this._addFn}get subtractFn(){return this._subtractFn}get multiplyFn(){return this._multiplyFn}get(t,e){if(this.isValidIndex(t,e))return this.data[t][e]}set(t,e,i){return this.isValidIndex(t,e)?(this.data[t][e]=i,!0):!1}isMatchForCalculate(t){return this.rows===t.rows&&this.cols===t.cols}add(t){if(!this.isMatchForCalculate(t))throw new Error("Matrix dimensions must match for addition.");let e=[];for(let i=0;i<this.rows;i++){e[i]=[];for(let s=0;s<this.cols;s++){let r=this.get(i,s),n=t.get(i,s);if(r!==void 0&&n!==void 0){let h=this._addFn(r,n);h&&(e[i][s]=h)}}}return new V(e,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}subtract(t){if(!this.isMatchForCalculate(t))throw new Error("Matrix dimensions must match for subtraction.");let e=[];for(let i=0;i<this.rows;i++){e[i]=[];for(let s=0;s<this.cols;s++){let r=this.get(i,s),n=t.get(i,s);if(r!==void 0&&n!==void 0){let h=this._subtractFn(r,n);h&&(e[i][s]=h)}}}return new V(e,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}multiply(t){if(this.cols!==t.rows)throw new Error("Matrix dimensions must be compatible for multiplication (A.cols = B.rows).");let e=[];for(let i=0;i<this.rows;i++){e[i]=[];for(let s=0;s<t.cols;s++){let r;for(let n=0;n<this.cols;n++){let h=this.get(i,n),o=t.get(n,s);if(h!==void 0&&o!==void 0){let l=this.multiplyFn(h,o);l!==void 0&&(r=this.addFn(r,l))}}r!==void 0&&(e[i][s]=r)}}return new V(e,{rows:this.rows,cols:t.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}transpose(){if(this.data.some(e=>e.length!==this.rows))throw new Error("Matrix must be rectangular for transposition.");let t=[];for(let e=0;e<this.cols;e++){t[e]=[];for(let i=0;i<this.rows;i++){let s=this.get(i,e);s!==void 0&&(t[e][i]=s)}}return new V(t,{rows:this.cols,cols:this.rows,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}inverse(){var t;if(this.rows!==this.cols)throw new Error("Matrix must be square for inversion.");let e=[];for(let r=0;r<this.rows;r++){e[r]=this.data[r].slice();for(let n=0;n<this.cols;n++)e[r][this.cols+n]=r===n?1:0}let i=new V(e,{rows:this.rows,cols:this.cols*2,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn});for(let r=0;r<this.rows;r++){let n=r;for(;n<this.rows&&i.get(n,r)===0;)n++;if(n===this.rows)throw new Error("Matrix is singular, and its inverse does not exist.");i._swapRows(r,n);let h=(t=i.get(r,r))!=null?t:1;if(h===0)throw new Error("Matrix is singular, and its inverse does not exist (division by zero).");i._scaleRow(r,1/h);for(let o=0;o<this.rows;o++)if(o!==r){let l=i.get(o,r);l===void 0&&(l=0),i._addScaledRow(o,r,-l)}}let s=[];for(let r=0;r<this.rows;r++)s[r]=i.data[r].slice(this.cols);return new V(s,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}dot(t){if(this.cols!==t.rows)throw new Error("Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.");let e=[];for(let i=0;i<this.rows;i++){e[i]=[];for(let s=0;s<t.cols;s++){let r;for(let n=0;n<this.cols;n++){let h=this.get(i,n),o=t.get(n,s);if(h!==void 0&&o!==void 0){let l=this.multiplyFn(h,o);l!==void 0&&(r=this.addFn(r,l))}}r!==void 0&&(e[i][s]=r)}}return new V(e,{rows:this.rows,cols:t.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}isValidIndex(t,e){return t>=0&&t<this.rows&&e>=0&&e<this.cols}clone(){return new V(this.data,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}_addFn(t,e){return t===void 0?e:t+e}_subtractFn(t,e){return t-e}_multiplyFn(t,e){return t*e}_swapRows(t,e){let i=this.data[t];this.data[t]=this.data[e],this.data[e]=i}_scaleRow(t,e){for(let i=0;i<this.cols;i++){let s=this.multiplyFn(this.data[t][i],e);s===void 0&&(s=0),this.data[t][i]=s}}_addScaledRow(t,e,i){for(let s=0;s<this.cols;s++){let r=this.multiplyFn(this.data[e][s],i);r===void 0&&(r=0);let n=r,h=this.addFn(this.data[t][s],n);h===void 0&&(h=0),this.data[t][s]=h}}};f(di,"Matrix");var Ne=class Ee{constructor(t,e){u(this,"direction"),u(this,"turn"),this.direction=t,this.turn=()=>new Ee(e[t],e)}};f(Ne,"Character");var gi=Ne,pi=class{constructor({matrix:t,turning:e,onMove:i,init:{cur:s,charDir:r,VISITED:n}}){u(this,"onMove"),u(this,"_matrix"),u(this,"_cur"),u(this,"_character"),u(this,"_VISITED"),this._matrix=t,this._cur=s,this._character=new gi(r,e),this.onMove=i,this.onMove&&this.onMove(this._cur),this._VISITED=n,this._matrix[this._cur[0]][this._cur[1]]=this._VISITED}start(){for(;this.check(this._character.direction)||this.check(this._character.turn().direction);){let{direction:t}=this._character;this.check(t)?this.move(t):this.check(this._character.turn().direction)&&(this._character=this._character.turn())}}check(t){let e,i,s=this._matrix,[r,n]=this._cur;switch(t){case"up":if(i=s[r-1],!i)return!1;e=i[n];break;case"right":e=s[r][n+1];break;case"down":if(i=s[r+1],!i)return!1;e=i[n];break;case"left":e=s[r][n-1];break}return e!==void 0&&e!==this._VISITED}move(t){switch(t){case"up":this._cur[0]--;break;case"right":this._cur[1]++;break;case"down":this._cur[0]++;break;case"left":this._cur[1]--;break}let[e,i]=this._cur;this._matrix[e][i]=this._VISITED,this.onMove&&this.onMove(this._cur)}};f(pi,"Navigator");var Le=class{constructor(t){u(this,"_key"),u(this,"_children"),u(this,"_isEnd"),this._key=t,this._isEnd=!1,this._children=new Map}get key(){return this._key}set key(t){this._key=t}get children(){return this._children}set children(t){this._children=t}get isEnd(){return this._isEnd}set isEnd(t){this._isEnd=t}};f(Le,"TrieNode");var ft=Le,vi=class extends rt{constructor(t=[],e){if(super(e),u(this,"_size",0),u(this,"_caseSensitive",!0),u(this,"_root",new ft("")),e){let{caseSensitive:i}=e;i!==void 0&&(this._caseSensitive=i)}t&&this.addMany(t)}get size(){return this._size}get caseSensitive(){return this._caseSensitive}get root(){return this._root}get _total(){return this._size}add(t){t=this._caseProcess(t);let e=this.root,i=!1;for(let s of t){let r=e.children.get(s);r||(r=new ft(s),e.children.set(s,r)),e=r}return e.isEnd||(i=!0,e.isEnd=!0,this._size++),i}addMany(t){let e=[];for(let i of t)this.toElementFn?e.push(this.add(this.toElementFn(i))):e.push(this.add(i));return e}has(t){t=this._caseProcess(t);let e=this.root;for(let i of t){let s=e.children.get(i);if(!s)return!1;e=s}return e.isEnd}isEmpty(){return this._size===0}clear(){this._size=0,this._root=new ft("")}delete(t){t=this._caseProcess(t);let e=!1,i=f((s,r)=>{let n=t[r],h=s.children.get(n);return h?r===t.length-1?h.isEnd?(h.children.size>0?h.isEnd=!1:s.children.delete(n),e=!0,!0):!1:i(h,r+1)&&!s.isEnd&&h.children.size===0?(s.children.delete(n),!0):!1:!1},"dfs");return i(this.root,0),e&&this._size--,e}getHeight(){let t=this.root,e=0;if(t){let i=f((s,r)=>{r>e&&(e=r);let{children:n}=s;if(n)for(let h of n.entries())i(h[1],r+1)},"bfs");i(t,0)}return e}hasPurePrefix(t){t=this._caseProcess(t);let e=this.root;for(let i of t){let s=e.children.get(i);if(!s)return!1;e=s}return!e.isEnd}hasPrefix(t){t=this._caseProcess(t);let e=this.root;for(let i of t){let s=e.children.get(i);if(!s)return!1;e=s}return!0}hasCommonPrefix(t){t=this._caseProcess(t);let e="",i=f(s=>{if(e+=s.key,e!==t&&!s.isEnd)if(s&&s.children&&s.children.size===1)i(Array.from(s.children.values())[0]);else return},"dfs");return i(this.root),e===t}getLongestCommonPrefix(){let t="",e=f(i=>{if(t+=i.key,!i.isEnd)if(i&&i.children&&i.children.size===1)e(Array.from(i.children.values())[0]);else return},"dfs");return e(this.root),t}getWords(t="",e=Number.MAX_SAFE_INTEGER,i=!1){t=this._caseProcess(t);let s=[],r=0;function n(o,l){for(let a of o.children.keys()){let _=o.children.get(a);_!==void 0&&n(_,l.concat(a))}if(o.isEnd){if(r>e-1)return;s.push(l),r++}}f(n,"dfs");let h=this.root;if(t)for(let o of t){let l=h.children.get(o);if(l)h=l;else return[]}return(i||h!==this.root)&&n(h,t),s}clone(){let t=this._createInstance();for(let e of this)t.add(e);return t}filter(t,e){let i=this._createInstance(),s=0;for(let r of this)t.call(e,r,s,this)&&i.add(r),s++;return i}map(t,e,i){let s=this._createLike([],e),r=0;for(let n of this){let h=i===void 0?t(n,r++,this):t.call(i,n,r++,this);if(typeof h!="string")throw new TypeError(`Trie.map callback must return string; got ${typeof h}`);s.add(h)}return s}mapSame(t,e){let i=this._createInstance(),s=0;for(let r of this){let n=e===void 0?t(r,s++,this):t.call(e,r,s++,this);i.add(n)}return i}_createInstance(t){let e=this.constructor;return new e([],{toElementFn:this.toElementFn,caseSensitive:this.caseSensitive,...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,e)}_spawnLike(t){return this._createLike([],t)}*_getIterator(){function*t(e,i){e.isEnd&&(yield i);for(let[s,r]of e.children)yield*t(r,i+s)}f(t,"_dfs"),yield*t(this.root,"")}_caseProcess(t){return this._caseSensitive||(t=t.toLowerCase()),t}};f(vi,"Trie");var mi=class Re{constructor(t,e,i){u(this,"_key"),u(this,"_value"),u(this,"_children"),this._key=t,this._value=e||void 0,i&&(this._children=i)}get key(){return this._key}set key(t){this._key=t}get value(){return this._value}set value(t){this._value=t}get children(){return this._children}set children(t){this._children=t}addChildren(t){this._children||(this._children=[]),t instanceof Re?this._children.push(t):this._children=this._children.concat(t)}getHeight(){let t=0;if(this){let e=f((i,s)=>{s>t&&(t=s);let{_children:r}=i;if(r)for(let n=0,h=r.length;n<h;n++)e(r[n],s+1)},"bfs");e(this,0)}return t}};f(mi,"TreeNode");return Ae(Mi);})();
|
|
5
|
+
`;s+=o},"display")(t)),s}print(t,e=this._root){console.log(this.toVisual(e,t))}_dfs(t=this._DEFAULT_NODE_CALLBACK,e="IN",i=!1,s=this._root,r=this.iterationType,n=!1,h=_=>!!_,o=_=>!!_,l=_=>n?this.isRealNodeOrNull(_):this.isRealNode(_),a=_=>this.isRealNodeOrNull(_)){if(s=this.ensureNode(s),!s)return[];let _=[];if(r==="RECURSIVE"){let d=f(p=>{if(!l(p))return;let m=f(()=>{h(p)&&(p==null?void 0:p.left)!==void 0&&d(p==null?void 0:p.left)},"visitLeft"),x=f(()=>{o(p)&&(p==null?void 0:p.right)!==void 0&&d(p==null?void 0:p.right)},"visitRight");switch(e){case"IN":if(m(),a(p)&&(_.push(t(p)),i))return;x();break;case"PRE":if(a(p)&&(_.push(t(p)),i))return;m(),x();break;case"POST":if(m(),x(),a(p)&&(_.push(t(p)),i))return;break}},"dfs");d(s)}else{let d=[{opt:0,node:s}],p=f(b=>{var w;h(b.node)&&d.push({opt:0,node:(w=b.node)==null?void 0:w.left})},"pushLeft"),m=f(b=>{var w;o(b.node)&&d.push({opt:0,node:(w=b.node)==null?void 0:w.right})},"pushRight"),x=f(b=>{l(b.node)&&d.push({opt:1,node:b.node})},"pushRoot");for(;d.length>0;){let b=d.pop();if(b!==void 0&&l(b.node))if(b.opt===1){if(a(b.node)&&b.node!==void 0&&(_.push(t(b.node)),i))return _}else switch(e){case"IN":m(b),x(b),p(b);break;case"PRE":m(b),p(b),x(b);break;case"POST":x(b),m(b),p(b);break}}}return _}*_getIterator(t=this._root){if(t)if(this.iterationType==="ITERATIVE"){let e=[],i=t;for(;i||e.length>0;){for(;this.isRealNode(i);)e.push(i),i=i.left;i=e.pop(),this.isRealNode(i)&&(yield[i.key,i.value],i=i.right)}}else t.left&&this.isRealNode(t)&&(yield*this[Symbol.iterator](t.left)),yield[t.key,t.value],t.right&&this.isRealNode(t)&&(yield*this[Symbol.iterator](t.right))}_snapshotOptions(){return{iterationType:this.iterationType,toEntryFn:this.toEntryFn,isMapMode:this.isMapMode,isDuplicate:this.isDuplicate}}_createInstance(t){let e=this.constructor;return new e([],{...this._snapshotOptions(),...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,{...this._snapshotOptions(),...e!=null?e:{}})}_keyValueNodeOrEntryToNodeAndValue(t,e){if(t===void 0)return[void 0,void 0];if(t===null)return[null,void 0];if(this.isNode(t))return[t,e];if(this.isEntry(t)){let[i,s]=t;if(i===void 0)return[void 0,void 0];if(i===null)return[null,void 0];let r=e!=null?e:s;return[this.createNode(i,r),r]}return[this.createNode(t,e),e]}_clone(t){this.bfs(e=>{e===null?t.set(null):t.set([e.key,e.value])},this._root,this.iterationType,!0)}_displayAux(t,e){let{isShowNull:i,isShowUndefined:s,isShowRedBlackNIL:r}=e,n=[["\u2500"],1,0,0];if(t===null&&!i)return n;if(t===void 0&&!s)return n;if(this.isNIL(t)&&!r)return n;if(t!=null){let o=t.key,l=this.isNIL(t)?"S":String(o),a=l.length;return h(l,a,this._displayAux(t.left,e),this._displayAux(t.right,e))}else{let o=t===void 0?"U":"N",l=o.length;return h(o,l,[[""],1,0,0],[[""],1,0,0])}function h(o,l,a,_){let[d,p,m,x]=a,[b,w,M,k]=_,v=" ".repeat(Math.max(0,x+1))+"_".repeat(Math.max(0,p-x-1))+o+"_".repeat(Math.max(0,k))+" ".repeat(Math.max(0,w-k)),y=(m>0?" ".repeat(x)+"/"+" ".repeat(p-x-1):" ".repeat(p))+" ".repeat(l)+(M>0?" ".repeat(k)+"\\"+" ".repeat(w-k-1):" ".repeat(w)),N=[v,y];for(let E=0;E<Math.max(m,M);E++){let T=E<m?d[E]:" ".repeat(p),C=E<M?b[E]:" ".repeat(w);N.push(T+" ".repeat(l)+C)}return[N,p+l+w,Math.max(m,M)+2,p+Math.floor(l/2)]}}_swapProperties(t,e){if(t=this.ensureNode(t),e=this.ensureNode(e),t&&e){let{key:i,value:s}=e,r=this.createNode(i,s);return r&&(e.key=t.key,this._isMapMode||(e.value=t.value),t.key=r.key,this._isMapMode||(t.value=r.value)),e}}_replaceNode(t,e){return t.parent&&(t.parent.left===t?t.parent.left=e:t.parent.right===t&&(t.parent.right=e)),e.left=t.left,e.right=t.right,e.parent=t.parent,this._root===t&&this._setRoot(e),e}_setRoot(t){t&&(t.parent=void 0),this._root=t}_ensurePredicate(t){if(t==null)return e=>!1;if(this._isPredicate(t))return t;if(this.isRealNode(t))return e=>e===t;if(this.isEntry(t)){let[e]=t;return i=>i?i.key===e:!1}return e=>e?e.key===t:!1}_isPredicate(t){return typeof t=="function"}_extractKey(t){if(t===null)return null;if(t!==void 0&&t!==this._NIL)return this.isNode(t)?t.key:this.isEntry(t)?t[0]:t}_setValue(t,e){if(t==null)return!1;let i=this._store.get(t);return i?(i.value=e,!0):!1}_clearNodes(){this._setRoot(void 0),this._size=0}_clearValues(){this._store.clear()}};f(fe,"BinaryTree");var ni=fe,ce=class{constructor(t,e){u(this,"key"),u(this,"value"),u(this,"parent"),u(this,"_left"),u(this,"_right"),u(this,"_height",0),u(this,"_color","BLACK"),u(this,"_count",1),this.key=t,this.value=e}get left(){return this._left}set left(t){t&&(t.parent=this),this._left=t}get right(){return this._right}set right(t){t&&(t.parent=this),this._right=t}get height(){return this._height}set height(t){this._height=t}get color(){return this._color}set color(t){this._color=t}get count(){return this._count}set count(t){this._count=t}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}};f(ce,"BSTNode");var kt=ce,_e=class extends ni{constructor(t=[],e){super([],e),u(this,"_root"),u(this,"_comparator"),e?"comparator"in e&&e.comparator!==void 0?this._comparator=e.comparator:this._comparator=this._createDefaultComparator():this._comparator=this._createDefaultComparator(),t&&this.setMany(t)}get root(){return this._root}get comparator(){return this._comparator}createNode(t,e){return new kt(t,e)}ensureNode(t,e=this.iterationType){var i;return(i=super.ensureNode(t,e))!=null?i:void 0}isNode(t){return t instanceof kt}isValidKey(t){return U(t)}dfs(t=this._DEFAULT_NODE_CALLBACK,e="IN",i=!1,s=this._root,r=this.iterationType){return super.dfs(t,e,i,s,r)}bfs(t=this._DEFAULT_NODE_CALLBACK,e=this._root,i=this.iterationType){return super.bfs(t,e,i,!1)}listLevels(t=this._DEFAULT_NODE_CALLBACK,e=this._root,i=this.iterationType){return super.listLevels(t,e,i,!1)}getNode(t,e=this._root,i=this.iterationType){var s,r;if(t==null)return;if(this._isPredicate(t))return(s=this.getNodes(t,!0,e,i)[0])!=null?s:void 0;if(t instanceof G)return(r=this.getNodes(t,!0,e,i)[0])!=null?r:void 0;let n;if(this.isNode(t))n=t.key;else if(this.isEntry(t)){let _=t[0];if(_==null)return;n=_}else n=t;let h=this.ensureNode(e);if(!h)return;let o=this._NIL,l=h,a=this._comparator;for(;l&&l!==o;){let _=a(n,l.key);if(_===0)return l;l=_<0?l._left:l._right}}search(t,e=!1,i=this._DEFAULT_NODE_CALLBACK,s=this._root,r=this.iterationType){if(t===void 0)return[];if(t===null)return[];if(s=this.ensureNode(s),!s)return[];let n=this.isRange(t),h=!n&&this._isPredicate(t);if(!n&&!h){let _;if(this.isNode(t))_=t.key;else if(this.isEntry(t)){let x=t[0];x!=null&&(_=x)}else _=t;if(_===void 0)return[];let d=this._NIL,p=this._comparator,m=s;for(;m&&m!==d;){let x=p(_,m.key);if(x===0)return[i(m)];m=x<0?m._left:m._right}return[]}let o;n?o=f(_=>_?t.isInRange(_.key,this._comparator):!1,"predicate"):o=this._ensurePredicate(t);let l=f(_=>{if(!_||!this.isRealNode(_.left))return!1;if(n){let d=t,p=d.low,m=d.includeLow;return m&&this._compare(_.key,p)>=0||!m&&this._compare(_.key,p)>0}if(!n&&!this._isPredicate(t)){let d=this._extractKey(t);return d!=null&&this._compare(_.key,d)>0}return!0},"shouldVisitLeft"),a=f(_=>{if(!_||!this.isRealNode(_.right))return!1;if(n){let d=t,p=d.high,m=d.includeHigh;return m&&this._compare(_.key,p)<=0||!m&&this._compare(_.key,p)<0}if(!n&&!this._isPredicate(t)){let d=this._extractKey(t);return d!=null&&this._compare(_.key,d)<0}return!0},"shouldVisitRight");return super._dfs(i,"IN",e,s,r,!1,l,a,()=>!0,_=>!!_&&o(_))}rangeSearch(t,e=this._DEFAULT_NODE_CALLBACK,i=this._root,s=this.iterationType){let r=t instanceof G?t:new G(t[0],t[1]);return this.search(r,!1,e,i,s)}set(t,e){let[i]=this._keyValueNodeOrEntryToNodeAndValue(t,e);if(i===void 0)return!1;if(this._root===void 0)return this._setRoot(i),this._isMapMode&&this.isRealNode(i)&&this._store.set(i.key,i),this._size++,!0;let s=this._root;for(;s!==void 0;){if(this._compare(s.key,i.key)===0)return this._replaceNode(s,i),this._isMapMode&&this.isRealNode(i)&&this._store.set(s.key,i),!0;if(this._compare(s.key,i.key)>0){if(s.left===void 0)return s.left=i,this._isMapMode&&this.isRealNode(i)&&this._store.set(i.key,i),this._size++,!0;s.left!==null&&(s=s.left)}else{if(s.right===void 0)return s.right=i,this._isMapMode&&this.isRealNode(i)&&this._store.set(i.key,i),this._size++,!0;s.right!==null&&(s=s.right)}}return!1}setMany(t,e,i=!0,s=this.iterationType){let r=[],n=e==null?void 0:e[Symbol.iterator]();if(!i){for(let d of t){let p=n==null?void 0:n.next().value;this.isRaw(d)&&(d=this._toEntryFn(d)),r.push(this.set(d,p))}return r}let h=[],o=0;for(let d of t)h.push({key:d,value:n==null?void 0:n.next().value,orgIndex:o++});let l=h.sort(({key:d},{key:p})=>{let m,x;return this.isRaw(d)?m=this._toEntryFn(d)[0]:this.isEntry(d)?m=d[0]:this.isRealNode(d)?m=d.key:m=d,this.isRaw(p)?x=this._toEntryFn(p)[0]:this.isEntry(p)?x=p[0]:this.isRealNode(p)?x=p.key:x=p,m!=null&&x!=null?this._compare(m,x):0}),a=f(d=>{if(d.length===0)return;let p=Math.floor((d.length-1)/2),{key:m,value:x,orgIndex:b}=d[p];if(this.isRaw(m)){let w=this._toEntryFn(m);r[b]=this.set(w)}else r[b]=this.set(m,x);a(d.slice(0,p)),a(d.slice(p+1))},"_dfs");return s==="RECURSIVE"?a(l):f(()=>{let p=[[0,l.length-1]];for(;p.length>0;){let m=p.pop();if(!m)continue;let[x,b]=m;if(x>b)continue;let w=x+Math.floor((b-x)/2),{key:M,value:k,orgIndex:v}=l[w];if(this.isRaw(M)){let y=this._toEntryFn(M);r[v]=this.set(y)}else r[v]=this.set(M,k);p.push([w+1,b]),p.push([x,w-1])}},"_iterate")(),r}ceiling(t,e=this._DEFAULT_NODE_CALLBACK,i){let s,r=this.iterationType;typeof e=="string"?r=e:e&&(s=e,i&&(r=i));let n=this._bound(t,!0,r);return s?n?s(n):void 0:n==null?void 0:n.key}higher(t,e=this._DEFAULT_NODE_CALLBACK,i){let s,r=this.iterationType;typeof e=="string"?r=e:e&&(s=e,i&&(r=i));let n=this._bound(t,!1,r);return s?n?s(n):void 0:n==null?void 0:n.key}floor(t,e=this._DEFAULT_NODE_CALLBACK,i){if(t==null)return void 0;let s,r=this.iterationType;if(typeof e=="string"?r=e:e&&(s=e,i&&(r=i)),this._isPredicate(t)){let h=this._floorByPredicate(t,r);return s?h?s(h):void 0:h==null?void 0:h.key}let n;if(this.isNode(t))n=t.key;else if(this.isEntry(t)){let h=t[0];if(h==null)return void 0;n=h}else n=t;if(n!==void 0){let h=this._floorByKey(n,r);return s?h?s(h):void 0:h==null?void 0:h.key}}lower(t,e,i){if(t==null)return void 0;let s,r=this.iterationType;if(typeof e=="string"?r=e:e&&(s=e,i&&(r=i)),this._isPredicate(t)){let h=this._lowerByPredicate(t,r);return s?h?s(h):void 0:h==null?void 0:h.key}let n;if(this.isNode(t))n=t.key;else if(this.isEntry(t)){let h=t[0];if(h==null)return void 0;n=h}else n=t;if(n!==void 0){let h=this._lowerByKey(n,r);return s?h?s(h):void 0:h==null?void 0:h.key}}lesserOrGreaterTraverse(t=this._DEFAULT_NODE_CALLBACK,e=-1,i=this._root,s=this.iterationType){let r=this.ensureNode(i),n=[];if(!this._root||!r)return n;let h=r.key;if(s==="RECURSIVE"){let o=f(l=>{let a=this._compare(l.key,h);Math.sign(a)==e&&n.push(t(l)),this.isRealNode(l.left)&&o(l.left),this.isRealNode(l.right)&&o(l.right)},"dfs");return o(this._root),n}else{let o=new H([this._root]);for(;o.length>0;){let l=o.shift();if(this.isRealNode(l)){let a=this._compare(l.key,h);Math.sign(a)==e&&n.push(t(l)),this.isRealNode(l.left)&&o.push(l.left),this.isRealNode(l.right)&&o.push(l.right)}}return n}}perfectlyBalance(t=this.iterationType){let e=this.dfs(n=>n,"IN",!1,this._root,t),i=e.length;if(this._clearNodes(),i===0)return!1;let s=f((n,h,o)=>{if(n>h)return;let l=n+(h-n>>1),a=e[l],_=s(n,l-1,a),d=s(l+1,h,a);return a.left=_,a.right=d,a.parent=o,a},"build"),r=s(0,i-1,void 0);return this._setRoot(r),this._size=i,!0}isAVLBalanced(t=this.iterationType){if(!this._root)return!0;let e=!0;if(t==="RECURSIVE"){let i=f(s=>{if(!s)return 0;let r=i(s.left),n=i(s.right);return Math.abs(r-n)>1&&(e=!1),Math.max(r,n)+1},"_height");i(this._root)}else{let i=[],s=this._root,r,n=new Map;for(;i.length>0||s;)if(s)i.push(s),s.left!==null&&(s=s.left);else if(s=i[i.length-1],!s.right||r===s.right){if(s=i.pop(),s){let h=s.left?n.get(s.left):-1,o=s.right?n.get(s.right):-1;if(Math.abs(h-o)>1)return!1;n.set(s,1+Math.max(h,o)),r=s,s=void 0}}else s=s.right}return e}map(t,e,i){let s=this._createLike([],e),r=0;for(let[n,h]of this)s.set(t.call(i,h,n,r++,this));return s}deleteWhere(t,e=!1,i=this._root,s=this.iterationType){let r=this.search(t,e,h=>h,i,s),n=[];for(let h of r){let o=this.delete(h);n=n.concat(o)}return n}_createDefaultComparator(){return(t,e)=>{if(U(t)&&U(e))return t>e?1:t<e?-1:0;if(typeof t=="object"||typeof e=="object")throw TypeError("When comparing object type keys, a custom comparator must be provided in the constructor's options!");return 0}}_floorByKey(t,e){var i,s;if(e==="RECURSIVE"){let r=f(n=>{if(!this.isRealNode(n))return;if(this.comparator(n.key,t)<=0){let o=r(n.right);return o!=null?o:n}else return r(n.left)},"dfs");return r(this.root)}else{let r=this.root,n;for(;this.isRealNode(r);)this.comparator(r.key,t)<=0?(n=r,r=(i=r.right)!=null?i:void 0):r=(s=r.left)!=null?s:void 0;return n}}_floorByPredicate(t,e){if(e==="RECURSIVE"){let i,s=f(r=>{this.isRealNode(r)&&(this.isRealNode(r.left)&&s(r.left),t(r)&&(i=r),this.isRealNode(r.right)&&s(r.right))},"dfs");return s(this.root),i}else{let i=[],s=this.root,r;for(;i.length>0||this.isRealNode(s);)if(this.isRealNode(s))i.push(s),s=s.left;else{let n=i.pop();if(!this.isRealNode(n))break;t(n)&&(r=n),s=n.right}return r}}_lowerByKey(t,e){var i,s;if(e==="RECURSIVE"){let r=f(n=>{if(!this.isRealNode(n))return;if(this.comparator(n.key,t)<0){let o=r(n.right);return o!=null?o:n}else return r(n.left)},"dfs");return r(this.root)}else{let r=this.root,n;for(;this.isRealNode(r);)this.comparator(r.key,t)<0?(n=r,r=(i=r.right)!=null?i:void 0):r=(s=r.left)!=null?s:void 0;return n}}_lowerByPredicate(t,e){if(e==="RECURSIVE"){let i,s=f(r=>{this.isRealNode(r)&&(this.isRealNode(r.left)&&s(r.left),t(r)&&(i=r),this.isRealNode(r.right)&&s(r.right))},"dfs");return s(this.root),i}else{let i=[],s=this.root,r;for(;i.length>0||this.isRealNode(s);)if(this.isRealNode(s))i.push(s),s=s.left;else{let n=i.pop();if(!this.isRealNode(n))break;t(n)&&(r=n),s=n.right}return r}}_bound(t,e,i){if(t==null)return;if(this._isPredicate(t))return this._boundByPredicate(t,i);let s;if(this.isNode(t))s=t.key;else if(this.isEntry(t)){let r=t[0];if(r==null)return;s=r}else s=t;if(s!==void 0)return this._boundByKey(s,e,i)}_boundByKey(t,e,i){var s,r;if(i==="RECURSIVE"){let n=f(h=>{if(!this.isRealNode(h))return;let o=this.comparator(h.key,t);if(e?o>=0:o>0){let a=n(h.left);return a!=null?a:h}else return n(h.right)},"dfs");return n(this.root)}else{let n=this.root,h;for(;this.isRealNode(n);){let o=this.comparator(n.key,t);(e?o>=0:o>0)?(h=n,n=(s=n.left)!=null?s:void 0):n=(r=n.right)!=null?r:void 0}return h}}_boundByPredicate(t,e){if(e==="RECURSIVE"){let i,s=f(r=>{i||!this.isRealNode(r)||(this.isRealNode(r.left)&&s(r.left),!i&&t(r)&&(i=r),!i&&this.isRealNode(r.right)&&s(r.right))},"dfs");return s(this.root),i}else{let i=[],s=this.root;for(;i.length>0||this.isRealNode(s);)if(this.isRealNode(s))i.push(s),s=s.left;else{let r=i.pop();if(!this.isRealNode(r))break;if(t(r))return r;s=r.right}return}}_createInstance(t){let e=this.constructor;return new e([],{...this._snapshotOptions(),...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,{...this._snapshotOptions(),...e!=null?e:{}})}_snapshotOptions(){return{...super._snapshotOptions(),comparator:this._comparator}}_keyValueNodeOrEntryToNodeAndValue(t,e){let[i,s]=super._keyValueNodeOrEntryToNodeAndValue(t,e);return i===null?[void 0,void 0]:[i,e!=null?e:s]}_setRoot(t){t&&(t.parent=void 0),this._root=t}_compare(t,e){return this._comparator(t,e)}_deleteByKey(t){let e=this._root;for(;e;){let r=this._compare(e.key,t);if(r===0)break;e=r>0?e.left:e.right}if(!e)return!1;let i=f((r,n)=>{let h=r==null?void 0:r.parent;h?h.left===r?h.left=n:h.right=n:this._setRoot(n),n&&(n.parent=h)},"transplant"),s=f(r=>{if(r){for(;r.left!==void 0&&r.left!==null;)r=r.left;return r}},"minNode");if(e.left===void 0)i(e,e.right);else if(e.right===void 0)i(e,e.left);else{let r=s(e.right);r.parent!==e&&(i(r,r.right),r.right=e.right,r.right&&(r.right.parent=r)),i(e,r),r.left=e.left,r.left&&(r.left.parent=r)}return this._size=Math.max(0,this._size-1),!0}};f(_e,"BST");var de=_e,hi=class{constructor({frequency:t=0,max:e}){u(this,"_freq"),u(this,"_max"),u(this,"_freqMap"),u(this,"_msb"),u(this,"_negativeCount"),this._freq=t,this._max=e,this._freqMap={0:0},this._msb=De(e),this._negativeCount=t<0?e:0}get freqMap(){return this._freqMap}get msb(){return this._msb}get negativeCount(){return this._negativeCount}get freq(){return this._freq}get max(){return this._max}readSingle(t){return this._checkIndex(t),this._readSingle(t)}update(t,e){this._checkIndex(t);let i=this._readSingle(t);this._update(t,e),this._updateNegativeCount(i,i+e)}writeSingle(t,e){this._checkIndex(t),this._writeSingle(t,e)}read(t){if(!Number.isInteger(t))throw new Error("Invalid count");return this._read(Math.max(Math.min(t,this.max),0))}lowerBound(t){if(this.negativeCount>0)throw new Error("Sequence is not non-descending");return this._binarySearch(t,(e,i)=>e<i)}upperBound(t){if(this.negativeCount>0)throw new Error("Must not be descending");return this._binarySearch(t,(e,i)=>e<=i)}getPrefixSum(t){this._checkIndex(t),t++;let e=0;for(;t>0;)e+=this._getFrequency(t),t-=t&-t;return e}_getFrequency(t){return t in this.freqMap?this.freqMap[t]:this.freq*(t&-t)}_updateFrequency(t,e){this.freqMap[t]=this._getFrequency(t)+e}_checkIndex(t){if(!Number.isInteger(t))throw new Error("Invalid index: Index must be an integer.");if(t<0||t>=this.max)throw new Error("Index out of range: Index must be within the range [0, this.max).")}_readSingle(t){t=t+1;let e=this._getFrequency(t),i=t-(t&-t);for(t--;t!==i;)e-=this._getFrequency(t),t-=t&-t;return e}_updateNegativeCount(t,e){t<0&&e>=0?this._negativeCount--:t>=0&&e<0&&this._negativeCount++}_update(t,e){for(t=t+1;t<=this.max;)this._updateFrequency(t,e),t+=t&-t}_writeSingle(t,e){let i=this._readSingle(t);this._update(t,e-i),this._updateNegativeCount(i,e)}_read(t){let e=t,i=0;for(;e;)i+=this._getFrequency(e),e-=e&-e;return i}_binarySearch(t,e){let i=0,s=this.msb<<1,r=t;for(;s>i+1;){let n=i+s>>1,h=this._getFrequency(n);n<=this.max&&e(h,r)?(r-=h,i=n):s=n}return i}};f(hi,"BinaryIndexedTree");var ge=class{constructor(t,e,i,s){u(this,"_start",0),u(this,"_end",0),u(this,"_value"),u(this,"_sum",0),u(this,"_left"),u(this,"_right"),this._start=t,this._end=e,this._sum=i,this._value=s||void 0}get start(){return this._start}set start(t){this._start=t}get end(){return this._end}set end(t){this._end=t}get value(){return this._value}set value(t){this._value=t}get sum(){return this._sum}set sum(t){this._sum=t}get left(){return this._left}set left(t){this._left=t}get right(){return this._right}set right(t){this._right=t}};f(ge,"SegmentTreeNode");var ut=ge,oi=class{constructor(t,e,i){u(this,"_values",[]),u(this,"_start",0),u(this,"_end"),u(this,"_root"),e=e||0,i=i||t.length-1,this._values=t,this._start=e,this._end=i,t.length>0?this._root=this.build(e,i):(this._root=void 0,this._values=[])}get values(){return this._values}get start(){return this._start}get end(){return this._end}get root(){return this._root}build(t,e){if(t>e)return new ut(t,e,0);if(t===e)return new ut(t,e,this._values[t]);let i=t+Math.floor((e-t)/2),s=this.build(t,i),r=this.build(i+1,e),n=new ut(t,e,s.sum+r.sum);return n.left=s,n.right=r,n}updateNode(t,e,i){let s=this.root||void 0;if(!s)return;let r=f((n,h,o,l)=>{if(n.start===n.end&&n.start===h){n.sum=o,l!==void 0&&(n.value=l);return}let a=n.start+Math.floor((n.end-n.start)/2);h<=a?n.left&&r(n.left,h,o,l):n.right&&r(n.right,h,o,l),n.left&&n.right&&(n.sum=n.left.sum+n.right.sum)},"dfs");r(s,t,e,i)}querySumByRange(t,e){let i=this.root||void 0;if(!i)return 0;if(t<0||e>=this.values.length||t>e)return NaN;let s=f((r,n,h)=>{if(n<=r.start&&h>=r.end)return r.sum;let o=r.start+Math.floor((r.end-r.start)/2);if(h<=o)return r.left?s(r.left,n,h):NaN;if(n>o)return r.right?s(r.right,n,h):NaN;{let l=0,a=0;return r.left&&(l=s(r.left,n,o)),r.right&&(a=s(r.right,o+1,h)),l+a}},"dfs");return s(i,t,e)}};f(oi,"SegmentTree");var pe=class{constructor(t,e){u(this,"key"),u(this,"value"),u(this,"parent"),u(this,"_left"),u(this,"_right"),u(this,"_height",0),u(this,"_color","BLACK"),u(this,"_count",1),this.key=t,this.value=e}get left(){return this._left}set left(t){t&&(t.parent=this),this._left=t}get right(){return this._right}set right(t){t&&(t.parent=this),this._right=t}get height(){return this._height}set height(t){this._height=t}get color(){return this._color}set color(t){this._color=t}get count(){return this._count}set count(t){this._count=t}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}};f(pe,"AVLTreeNode");var Nt=pe,li=class extends de{constructor(t=[],e){super([],e),t&&super.setMany(t)}createNode(t,e){return new Nt(t,e)}isNode(t){return t instanceof Nt}set(t,e){if(t===null)return!1;let i=super.set(t,e);return i&&this._balancePath(t),i}delete(t){let e=super.delete(t);for(let{needBalanced:i}of e)i&&this._balancePath(i);return e}perfectlyBalance(t=this.iterationType){let e=this.dfs(n=>n,"IN",!1,this._root,t),i=e.length;if(i===0)return!1;this._clearNodes();let s=f((n,h,o)=>{if(n>h)return;let l=n+(h-n>>1),a=e[l];a.left=s(n,l-1,a),a.right=s(l+1,h,a),a.parent=o;let _=a.left?a.left.height:-1,d=a.right?a.right.height:-1;return a.height=Math.max(_,d)+1,a},"build"),r=s(0,i-1,void 0);return this._setRoot(r),this._size=i,!0}map(t,e,i){let s=this._createLike([],e),r=0;for(let[n,h]of this)s.set(t.call(i,h,n,r++,this));return s}_createInstance(t){let e=this.constructor;return new e([],{...this._snapshotOptions(),...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,{...this._snapshotOptions(),...e!=null?e:{}})}_swapProperties(t,e){let i=this.ensureNode(t),s=this.ensureNode(e);if(i&&s){let{key:r,value:n,height:h}=s,o=this.createNode(r,n);return o&&(o.height=h,s.key=i.key,this._isMapMode||(s.value=i.value),s.height=i.height,i.key=o.key,this._isMapMode||(i.value=o.value),i.height=o.height),s}}_balanceFactor(t){let e=t.left?t.left.height:-1;return(t.right?t.right.height:-1)-e}_updateHeight(t){let e=t.left?t.left.height:-1,i=t.right?t.right.height:-1;t.height=1+Math.max(e,i)}_balanceLL(t){let e=t.parent,i=t.left;i!==null&&(t.parent=i),i&&i.right&&(i.right.parent=t),i&&(i.parent=e),t===this.root?i&&this._setRoot(i):(e==null?void 0:e.left)===t?e.left=i:e&&(e.right=i),i&&(t.left=i.right,i.right=t),this._updateHeight(t),i&&this._updateHeight(i)}_balanceLR(t){let e=t.parent,i=t.left,s;i&&(s=i.right),t&&s!==null&&(t.parent=s),i&&s!==null&&(i.parent=s),s&&(s.left&&i!==null&&(s.left.parent=i),s.right&&(s.right.parent=t),s.parent=e),t===this.root?s&&this._setRoot(s):e&&(e.left===t?e.left=s:e.right=s),s&&(t.left=s.right,i&&(i.right=s.left),s.left=i,s.right=t),this._updateHeight(t),i&&this._updateHeight(i),s&&this._updateHeight(s)}_balanceRR(t){let e=t.parent,i=t.right;i!==null&&(t.parent=i),i&&(i.left&&(i.left.parent=t),i.parent=e),t===this.root?i&&this._setRoot(i):e&&(e.left===t?e.left=i:e.right=i),i&&(t.right=i.left,i.left=t),this._updateHeight(t),i&&this._updateHeight(i)}_balanceRL(t){let e=t.parent,i=t.right,s;i&&(s=i.left),s!==null&&(t.parent=s),i&&s!==null&&(i.parent=s),s&&(s.left&&(s.left.parent=t),s.right&&i!==null&&(s.right.parent=i),s.parent=e),t===this.root?s&&this._setRoot(s):e&&(e.left===t?e.left=s:e.right=s),s&&(t.right=s.left),i&&s&&(i.left=s.right),s&&(s.left=t),s&&(s.right=i),this._updateHeight(t),i&&this._updateHeight(i),s&&this._updateHeight(s)}_balancePath(t){t=this.ensureNode(t);let e=this.getPathToRoot(t,i=>i,!1);for(let i=0;i<e.length;i++){let s=e[i];if(s)switch(this._updateHeight(s),this._balanceFactor(s)){case-2:s&&s.left&&(this._balanceFactor(s.left)<=0?this._balanceLL(s):this._balanceLR(s));break;case 2:s&&s.right&&(this._balanceFactor(s.right)>=0?this._balanceRR(s):this._balanceRL(s))}}}_replaceNode(t,e){return e.height=t.height,super._replaceNode(t,e)}};f(li,"AVLTree");var ve=class{constructor(t,e,i="BLACK"){u(this,"key"),u(this,"value"),u(this,"parent"),u(this,"_left"),u(this,"_right"),u(this,"_height",0),u(this,"_color","BLACK"),u(this,"_count",1),this.key=t,this.value=e,this.color=i}get left(){return this._left}set left(t){t&&(t.parent=this),this._left=t}get right(){return this._right}set right(t){t&&(t.parent=this),this._right=t}get height(){return this._height}set height(t){this._height=t}get color(){return this._color}set color(t){this._color=t}get count(){return this._count}set count(t){this._count=t}get familyPosition(){return this.parent?this.parent.left===this?this.left||this.right?"ROOT_LEFT":"LEFT":this.parent.right===this?this.left||this.right?"ROOT_RIGHT":"RIGHT":"MAL_NODE":this.left||this.right?"ROOT":"ISOLATED"}};f(ve,"RedBlackTreeNode");var W=ve,me=class extends de{constructor(t=[],e){super([],e),u(this,"_root"),u(this,"_header"),u(this,"_minNode"),u(this,"_maxNode"),this._root=this.NIL,this._header=new W(void 0,void 0,"BLACK"),this._header.parent=this.NIL,this._header._left=this.NIL,this._header._right=this.NIL,t&&this.setMany(t)}get root(){return this._root}createNode(t,e,i="BLACK"){return new W(t,e,i)}isNode(t){return t instanceof W}clear(){super.clear(),this._root=this.NIL,this._header.parent=this.NIL,this._setMinCache(void 0),this._setMaxCache(void 0)}_findNodeByKey(t){var e,i,s;let r=this.NIL,n=this._compare.bind(this),h=(e=this._header.parent)!=null?e:r;for(;h!==r;){let o=n(t,h.key);if(o<0)h=(i=h.left)!=null?i:r;else if(o>0)h=(s=h.right)!=null?s:r;else return h}}_predecessorOf(t){let e=this.NIL;if(t.left&&t.left!==e){let r=t.left;for(;r.right&&r.right!==e;)r=r.right;return r}let i=t,s=t.parent;for(;s&&i===s.left;)i=s,s=s.parent;return s}_successorOf(t){let e=this.NIL;if(t.right&&t.right!==e){let r=t.right;for(;r.left&&r.left!==e;)r=r.left;return r}let i=t,s=t.parent;for(;s&&i===s.right;)i=s,s=s.parent;return s}_attachNewNode(t,e,i){let s=this.NIL;i.parent=t,e==="left"?t.left=i:t.right=i,i.left=s,i.right=s,i.color="RED",this._insertFixup(i),this.isRealNode(this._root)&&(this._root.color="BLACK")}_setMinCache(t){this._minNode=t,this._header._left=t!=null?t:this.NIL}_setMaxCache(t){this._maxNode=t,this._header._right=t!=null?t:this.NIL}_setKVNode(t,e){var i,s,r,n,h,o,l;let a=this.NIL,_=this._comparator,d=this._header,p=(i=d._left)!=null?i:a;if(p!==a){let E=_(t,p.key);if(E===0)return p.value=e,this._isMapMode&&this._store.set(t,p),{node:p,created:!1};let T=p.left;if(E<0&&(T===a||T===null||T===void 0)){let C=this.createNode(t,e);return this._attachNewNode(p,"left",C),this._isMapMode&&this._store.set(C.key,C),this._size++,this._setMinCache(C),d._right===a&&this._setMaxCache(C),{node:C,created:!0}}if(E>0){let C=(s=d._right)!=null?s:a,Mt=_(t,C.key);if(Mt===0)return C.value=e,this._isMapMode&&this._store.set(t,C),{node:C,created:!1};let ot=C.right;if(Mt>0&&(ot===a||ot===null||ot===void 0)){let D=this.createNode(t,e);return this._attachNewNode(C,"right",D),this._isMapMode&&this._store.set(D.key,D),this._size++,this._setMaxCache(D),d._left===a&&this._setMinCache(D),{node:D,created:!0}}}}let m=_,x=this._isMapMode,b=this._store,w=(r=this._header.parent)!=null?r:a,M,k=0;for(;w!==a;)if(M=w,k=m(t,w.key),k<0)w=(n=w.left)!=null?n:a;else if(k>0)w=(h=w.right)!=null?h:a;else return w.value=e,x&&b.set(t,w),{node:w,created:!1};let v=this.createNode(t,e);if(v.parent=M,M?k<0?M.left=v:M.right=v:this._setRoot(v),v.left=a,v.right=a,v.color="RED",this._insertFixup(v),this.isRealNode(this._root))this._root.color="BLACK";else return;x&&b.set(v.key,v),this._size++;let y=(o=this._header._left)!=null?o:a,N=(l=this._header._right)!=null?l:a;return y===a||N===a?(this._setMinCache(v),this._setMaxCache(v)):M===N&&k>0?this._setMaxCache(v):M===y&&k<0?this._setMinCache(v):(m(v.key,y.key)<0&&this._setMinCache(v),m(v.key,N.key)>0&&this._setMaxCache(v)),{node:v,created:!0}}_setKV(t,e){if(this._isMapMode){let s=this._store.get(t);if(s)return s.value=e,!0}return this._setKVNode(t,e)!==void 0}setWithHintNode(t,e,i){var s,r,n,h,o,l,a,_,d,p,m,x,b;if(!i||!this.isRealNode(i))return(s=this._setKVNode(t,e))==null?void 0:s.node;let w=this._compare.bind(this),M=w(t,i.key);if(M===0)return i.value=e,this._isMapMode&&this._store.set(t,i),i;if(M<0){if(!this.isRealNode(i.left)){let y=this.createNode(t,e);if(!this.isRealNode(y))return;this._attachNewNode(i,"left",y),this._isMapMode&&this._store.set(t,y),this._size++;let N=this.NIL,E=(r=this._header._left)!=null?r:N;(E===N||this._compare(y.key,E.key)<0)&&this._setMinCache(y);let T=(n=this._header._right)!=null?n:N;return(T===N||this._compare(y.key,T.key)>0)&&this._setMaxCache(y),y}let v=this._predecessorOf(i);if(v&&w(v.key,t)>=0)return(h=this._setKVNode(t,e))==null?void 0:h.node;if(v&&!this.isRealNode(v.right)){let y=this.createNode(t,e);if(!this.isRealNode(y))return;this._attachNewNode(v,"right",y),this._isMapMode&&this._store.set(t,y),this._size++;let N=this.NIL,E=(o=this._header._left)!=null?o:N;(E===N||this._compare(y.key,E.key)<0)&&this._setMinCache(y);let T=(l=this._header._right)!=null?l:N;return(T===N||this._compare(y.key,T.key)>0)&&this._setMaxCache(y),y}return(a=this._setKVNode(t,e))==null?void 0:a.node}if(!this.isRealNode(i.right)){let v=this.createNode(t,e);if(!this.isRealNode(v))return;this._attachNewNode(i,"right",v),this._isMapMode&&this._store.set(t,v),this._size++;let y=this.NIL,N=(_=this._header._left)!=null?_:y;(N===y||this._compare(v.key,N.key)<0)&&this._setMinCache(v);let E=(d=this._header._right)!=null?d:y;return(E===y||this._compare(v.key,E.key)>0)&&this._setMaxCache(v),v}let k=this._successorOf(i);if(k&&w(k.key,t)<=0)return(p=this._setKVNode(t,e))==null?void 0:p.node;if(k&&!this.isRealNode(k.left)){let v=this.createNode(t,e);if(!this.isRealNode(v))return;this._attachNewNode(k,"left",v),this._isMapMode&&this._store.set(t,v),this._size++;let y=this.NIL,N=(m=this._header._left)!=null?m:y;(N===y||this._compare(v.key,N.key)<0)&&this._setMinCache(v);let E=(x=this._header._right)!=null?x:y;return(E===y||this._compare(v.key,E.key)>0)&&this._setMaxCache(v),v}return(b=this._setKVNode(t,e))==null?void 0:b.node}setWithHint(t,e,i){return this.setWithHintNode(t,e,i)!==void 0}set(t,e){if(!this.isNode(t)){if(t==null)return!1;if(this.isEntry(t)){let n=t[0];if(n==null)return!1;let h=e!=null?e:t[1];return this._setKV(n,h)}return this._setKV(t,e)}let[i,s]=this._keyValueNodeOrEntryToNodeAndValue(t,e);if(!this.isRealNode(i))return!1;let r=this._insert(i);if(r==="CREATED"){if(this.isRealNode(this._root))this._root.color="BLACK";else return!1;if(this._isMapMode){let n=this.getNode(i.key);this.isRealNode(n)&&(n.value=s,this._store.set(n.key,n))}return this._size++,!0}if(r==="UPDATED"){if(this._isMapMode){let n=this.getNode(i.key);this.isRealNode(n)&&(n.value=s,this._store.set(n.key,n))}return!0}return!1}delete(t){if(t===null)return[];let e=[],i;if(this._isPredicate(t)?i=this.getNode(t):i=this.isRealNode(t)?t:this.getNode(t),!i)return e;let s=i===this._minNode,r=i===this._maxNode,n=s?this._successorOf(i):void 0,h=r?this._predecessorOf(i):void 0,o=i.color,l;if(!this.isRealNode(i.left))i.right!==null&&(l=i.right,this._transplant(i,i.right));else if(!this.isRealNode(i.right))l=i.left,this._transplant(i,i.left);else{let a=this.getLeftMost(_=>_,i.right);a&&(o=a.color,a.right!==null&&(l=a.right),a.parent===i?this.isRealNode(l)&&(l.parent=a):(a.right!==null&&(this._transplant(a,a.right),a.right=i.right),this.isRealNode(a.right)&&(a.right.parent=a)),this._transplant(i,a),a.left=i.left,this.isRealNode(a.left)&&(a.left.parent=a),a.color=i.color)}return this._isMapMode&&this._store.delete(i.key),this._size--,this._size<=0?(this._setMinCache(void 0),this._setMaxCache(void 0)):(s&&this._setMinCache(n),r&&this._setMaxCache(h),(!this._minNode||!this.isRealNode(this._minNode))&&this._setMinCache(this.isRealNode(this._root)?this.getLeftMost(a=>a,this._root):void 0),(!this._maxNode||!this.isRealNode(this._maxNode))&&this._setMaxCache(this.isRealNode(this._root)?this.getRightMost(a=>a,this._root):void 0)),o==="BLACK"&&this._deleteFixup(l),e.push({deleted:i,needBalanced:void 0}),e}map(t,e,i){let s=this._createLike([],e),r=0;for(let[n,h]of this)s.set(t.call(i,h,n,r++,this));return s}_createInstance(t){let e=this.constructor;return new e([],{...this._snapshotOptions(),...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,{...this._snapshotOptions(),...e!=null?e:{}})}_setRoot(t){let e=this.NIL;t&&(t.parent=void 0),this._root=t,this._header.parent=t!=null?t:e}_replaceNode(t,e){return e.color=t.color,super._replaceNode(t,e)}_insert(t){var e,i,s;let r=this.NIL,n=this._compare.bind(this),h=(e=this._header.parent)!=null?e:r,o,l=0;for(;h!==r;)if(o=h,l=n(t.key,h.key),l<0)h=(i=h.left)!=null?i:r;else if(l>0)h=(s=h.right)!=null?s:r;else return this._replaceNode(h,t),"UPDATED";return t.parent=o,o?l<0?o.left=t:o.right=t:this._setRoot(t),t.left=r,t.right=r,t.color="RED",this._insertFixup(t),"CREATED"}_transplant(t,e){t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._setRoot(e),e&&(e.parent=t.parent)}_insertFixup(t){let e=this._leftRotate.bind(this),i=this._rightRotate.bind(this);for(;t;){let s=t.parent;if(!s||s.color!=="RED")break;let r=s.parent;if(!r)break;if(s===r.left){let n=r.right;if((n==null?void 0:n.color)==="RED"){s.color="BLACK",n.color="BLACK",r.color="RED",t=r;continue}t===s.right&&(t=s,e(t));let h=t==null?void 0:t.parent,o=h==null?void 0:h.parent;h&&o&&(h.color="BLACK",o.color="RED",i(o))}else{let n=r.left;if((n==null?void 0:n.color)==="RED"){s.color="BLACK",n.color="BLACK",r.color="RED",t=r;continue}t===s.left&&(t=s,i(t));let h=t==null?void 0:t.parent,o=h==null?void 0:h.parent;h&&o&&(h.color="BLACK",o.color="RED",e(o))}break}this.isRealNode(this._root)&&(this._root.color="BLACK")}_deleteFixup(t){var e,i,s,r;if(!t||t===this.root||t.color==="BLACK"){t&&(t.color="BLACK");return}for(;t&&t!==this.root&&t.color==="BLACK";){let n=t.parent;if(!n)break;if(t===n.left){let h=n.right;(h==null?void 0:h.color)==="RED"&&(h.color="BLACK",n.color="RED",this._leftRotate(n),h=n.right),((i=(e=h==null?void 0:h.left)==null?void 0:e.color)!=null?i:"BLACK")==="BLACK"?(h&&(h.color="RED"),t=n):(h!=null&&h.left&&(h.left.color="BLACK"),h&&(h.color=n.color),n.color="BLACK",this._rightRotate(n),t=this.root)}else{let h=n.left;(h==null?void 0:h.color)==="RED"&&(h.color="BLACK",n&&(n.color="RED"),this._rightRotate(n),n&&(h=n.left)),((r=(s=h==null?void 0:h.right)==null?void 0:s.color)!=null?r:"BLACK")==="BLACK"?(h&&(h.color="RED"),t=n):(h!=null&&h.right&&(h.right.color="BLACK"),h&&(h.color=n.color),n&&(n.color="BLACK"),this._leftRotate(n),t=this.root)}}t&&(t.color="BLACK")}_leftRotate(t){if(!t||!t.right)return;let e=t.right;t.right=e.left,e.left&&e.left!==this.NIL&&(e.left.parent=t),e.parent=t.parent,t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._setRoot(e),e.left=t,t.parent=e}_rightRotate(t){if(!t||!t.left)return;let e=t.left;t.left=e.right,e.right&&e.right!==this.NIL&&(e.right.parent=t),e.parent=t.parent,t.parent?t===t.parent.left?t.parent.left=e:t.parent.right=e:this._setRoot(e),e.right=t,t.parent=e}};f(me,"RedBlackTree");var ht=me,I,P,O,Me=class Q{constructor(t=[],e={}){A(this,I),A(this,P),A(this,O);var i;B(this,O,e.comparator);let s=e.toElementFn,r=(i=e.comparator)!=null?i:Q.createDefaultComparator();B(this,P,e.comparator===void 0),B(this,I,new ht([],{comparator:r,isMapMode:e.isMapMode}));for(let n of t){let h=s?s(n):n;this.add(h)}}static createDefaultComparator(){return(t,e)=>{if(typeof t=="number"&&typeof e=="number"){if(Number.isNaN(t)||Number.isNaN(e))throw new TypeError("TreeSet: NaN is not a valid key");let i=Object.is(t,-0)?0:t,s=Object.is(e,-0)?0:e;return i>s?1:i<s?-1:0}if(typeof t=="string"&&typeof e=="string")return t>e?1:t<e?-1:0;if(t instanceof Date&&e instanceof Date){let i=t.getTime(),s=e.getTime();if(Number.isNaN(i)||Number.isNaN(s))throw new TypeError("TreeSet: invalid Date key");return i>s?1:i<s?-1:0}throw new TypeError("TreeSet: comparator is required for non-number/non-string/non-Date keys")}}get size(){return g(this,I).size}isEmpty(){return this.size===0}_validateKey(t){if(g(this,P)){if(typeof t=="number"){if(Number.isNaN(t))throw new TypeError("TreeSet: NaN is not a valid key");return}if(typeof t!="string"){if(t instanceof Date){if(Number.isNaN(t.getTime()))throw new TypeError("TreeSet: invalid Date key");return}throw new TypeError("TreeSet: comparator is required for non-number/non-string/non-Date keys")}}}add(t){return this._validateKey(t),g(this,I).set(t,void 0),this}has(t){return this._validateKey(t),g(this,I).has(t)}delete(t){var e;this._validateKey(t);let i=g(this,I).delete(t);return Array.isArray(i)&&i.length>0&&!!((e=i[0])!=null&&e.deleted)}clear(){g(this,I).clear()}keys(){return g(this,I).keys()}values(){return this.keys()}*entries(){for(let t of this.keys())yield[t,t]}[Symbol.iterator](){return this.keys()}forEach(t,e){for(let i of this)t.call(e,i,i,this)}map(t,e={},i){let s=new Q([],e),r=0;for(let n of this){let h=i===void 0?t(n,r++,this):t.call(i,n,r++,this);s.add(h)}return s}filter(t,e){let i=new Q([],{comparator:g(this,O)}),s=0;for(let r of this)(e===void 0?t(r,s++,this):t.call(e,r,s++,this))&&i.add(r);return i}reduce(t,e){let i=e,s=0;for(let r of this)i=t(i,r,s++,this);return i}every(t,e){let i=0;for(let s of this)if(!(e===void 0?t(s,i++,this):t.call(e,s,i++,this)))return!1;return!0}some(t,e){let i=0;for(let s of this)if(e===void 0?t(s,i++,this):t.call(e,s,i++,this))return!0;return!1}find(t,e){let i=0;for(let s of this)if(e===void 0?t(s,i++,this):t.call(e,s,i++,this))return s}toArray(){return[...this]}print(){g(this,I).print()}first(){return g(this,I).getLeftMost()}last(){return g(this,I).getRightMost()}pollFirst(){let t=this.first();if(t!==void 0)return this.delete(t),t}pollLast(){let t=this.last();if(t!==void 0)return this.delete(t),t}ceiling(t){return this._validateKey(t),g(this,I).ceiling(t)}floor(t){return this._validateKey(t),g(this,I).floor(t)}higher(t){return this._validateKey(t),g(this,I).higher(t)}lower(t){return this._validateKey(t),g(this,I).lower(t)}rangeSearch(t,e={}){let{lowInclusive:i=!0,highInclusive:s=!0}=e,[r,n]=t;this._validateKey(r),this._validateKey(n);let h=g(this,I).rangeSearch([r,n]),o=[],l=g(this,I).comparator;for(let a of h)a!==void 0&&(!i&&l(a,r)===0||!s&&l(a,n)===0||o.push(a));return o}};I=new WeakMap;P=new WeakMap;O=new WeakMap;f(Me,"TreeSet");var we=Me,ai=class extends W{constructor(t,e=[]){super(t,e)}};f(ai,"TreeMultiMapNode");var L,X,ye=class ${constructor(t=[],e={}){A(this,L),A(this,X);var i;let s=(i=e.comparator)!=null?i:we.createDefaultComparator();B(this,X,e.comparator===void 0);let r=e.toEntryFn;B(this,L,new ht([],{...e,comparator:s,isMapMode:e.isMapMode}));for(let n of t)if(n!=null){if(r){let[h,o]=r(n);if(h==null)continue;o!==void 0?g(this,L).set(h,Array.isArray(o)?[...o]:[o]):g(this,L).set(h,[]);continue}if(Array.isArray(n)){let[h,o]=n;if(h==null)continue;o!==void 0?g(this,L).set(h,[...o]):g(this,L).set(h,[]);continue}g(this,L).set(n,[])}}_validateKey(t){if(g(this,X)){if(typeof t=="number"){if(Number.isNaN(t))throw new TypeError("TreeMultiMap: NaN is not a valid key");return}if(typeof t!="string"){if(t instanceof Date){if(Number.isNaN(t.getTime()))throw new TypeError("TreeMultiMap: invalid Date key");return}throw new TypeError("TreeMultiMap: comparator is required for non-number/non-string/non-Date keys")}}}get size(){return g(this,L).size}isEmpty(){return this.size===0}clear(){g(this,L).clear()}count(t){let e=this.get(t);return Array.isArray(e)?e.length:0}get totalSize(){let t=0;for(let[,e]of this)t+=e.length;return t}has(t){return this._validateKey(t),g(this,L).has(t)}get(t){return this._validateKey(t),g(this,L).get(t)}add(t,e){this._validateKey(t);let i=g(this,L).get(t);return i?(i.push(e),!0):g(this,L).set(t,[e])}set(t,e){if(t==null)return!1;if(Array.isArray(t)){let[i,s]=t;if(i==null)return!1;if(e!==void 0)return this.add(i,e);if(s===void 0)return g(this,L).set(i,[]);let r=g(this,L).get(i);return r?(r.push(...s),!0):g(this,L).set(i,[...s])}return e!==void 0?this.add(t,e):g(this,L).set(t,[])}delete(t){return this._validateKey(t),g(this,L).delete(t).length>0}hasEntry(t,e,i=Object.is){let s=this.get(t);return Array.isArray(s)?s.some(r=>i(r,e)):!1}deleteValue(t,e,i=Object.is){let s=this.get(t);if(!Array.isArray(s))return!1;let r=s.findIndex(n=>i(n,e));return r===-1?!1:(s.splice(r,1),s.length===0&&this.delete(t),!0)}deleteValues(t,e,i=Object.is){let s=this.get(t);if(!Array.isArray(s)||s.length===0)return 0;let r=0;for(let n=s.length-1;n>=0;n--)i(s[n],e)&&(s.splice(n,1),r++);return s.length===0&&r>0&&this.delete(t),r}*[Symbol.iterator](){for(let[t,e]of g(this,L))yield[t,e!=null?e:[]]}*keys(){yield*g(this,L).keys()}*values(){for(let[,t]of this)yield t}*entriesOf(t){let e=this.get(t);if(Array.isArray(e))for(let i of e)yield[t,i]}*valuesOf(t){let e=this.get(t);Array.isArray(e)&&(yield*e)}*flatEntries(){for(let[t,e]of this)for(let i of e)yield[t,i]}first(){let t=g(this,L).getLeftMost();if(t===void 0)return;let e=this.get(t);return e===void 0?void 0:[t,e]}last(){let t=g(this,L).getRightMost();if(t===void 0)return;let e=this.get(t);return e===void 0?void 0:[t,e]}pollFirst(){let t=this.first();if(t)return this.delete(t[0]),t}pollLast(){let t=this.last();if(t)return this.delete(t[0]),t}ceiling(t){this._validateKey(t);let e=g(this,L).ceiling(t);if(e===void 0)return;let i=this.get(e);return i===void 0?void 0:[e,i]}floor(t){this._validateKey(t);let e=g(this,L).floor(t);if(e===void 0)return;let i=this.get(e);return i===void 0?void 0:[e,i]}higher(t){this._validateKey(t);let e=g(this,L).higher(t);if(e===void 0)return;let i=this.get(e);return i===void 0?void 0:[e,i]}lower(t){this._validateKey(t);let e=g(this,L).lower(t);if(e===void 0)return;let i=this.get(e);return i===void 0?void 0:[e,i]}print(){g(this,L).print()}forEach(t){for(let[e,i]of this)t(i,e,this)}filter(t){let e=[];for(let[i,s]of this)t(s,i,this)&&e.push([i,s]);return new $(e,{comparator:this.comparator})}map(t){let e=[];for(let[i,s]of this)e.push(t(s,i,this));return new $(e,{comparator:this.comparator})}reduce(t,e){let i=e;for(let[s,r]of this)i=t(i,r,s,this);return i}setMany(t){let e=[];for(let i of t)e.push(this.set(i));return e}rangeSearch(t,e){return g(this,L).rangeSearch(t,e)}clone(){return new $(this,{comparator:this.comparator,isMapMode:g(this,L).isMapMode})}get comparator(){return g(this,L).comparator}};L=new WeakMap;X=new WeakMap;f(ye,"TreeMultiMap");var be=ye,F,J,Y,ui=class Z{constructor(t=[],e={}){A(this,F),A(this,J),A(this,Y);var i;B(this,Y,e.comparator);let s=e.toEntryFn,r=(i=e.comparator)!=null?i:Z.createDefaultComparator();B(this,J,e.comparator===void 0),B(this,F,new ht([],{comparator:r,isMapMode:e.isMapMode}));for(let n of t){let h,o;if(s)[h,o]=s(n);else{if(!Array.isArray(n)||n.length<2)throw new TypeError("TreeMap: each entry must be a [key, value] tuple");h=n[0],o=n[1]}this.set(h,o)}}static createDefaultComparator(){return(t,e)=>{if(typeof t=="number"&&typeof e=="number"){if(Number.isNaN(t)||Number.isNaN(e))throw new TypeError("TreeMap: NaN is not a valid key");let i=Object.is(t,-0)?0:t,s=Object.is(e,-0)?0:e;return i>s?1:i<s?-1:0}if(typeof t=="string"&&typeof e=="string")return t>e?1:t<e?-1:0;if(t instanceof Date&&e instanceof Date){let i=t.getTime(),s=e.getTime();if(Number.isNaN(i)||Number.isNaN(s))throw new TypeError("TreeMap: invalid Date key");return i>s?1:i<s?-1:0}throw new TypeError("TreeMap: comparator is required for non-number/non-string/non-Date keys")}}_validateKey(t){if(g(this,J)){if(typeof t=="number"){if(Number.isNaN(t))throw new TypeError("TreeMap: NaN is not a valid key");return}if(typeof t!="string"){if(t instanceof Date){if(Number.isNaN(t.getTime()))throw new TypeError("TreeMap: invalid Date key");return}throw new TypeError("TreeMap: comparator is required for non-number/non-string/non-Date keys")}}}get size(){return g(this,F).size}isEmpty(){return this.size===0}set(t,e){return this._validateKey(t),g(this,F).set(t,e),this}get(t){return this._validateKey(t),g(this,F).get(t)}has(t){return this._validateKey(t),g(this,F).has(t)}delete(t){var e;this._validateKey(t);let i=g(this,F).delete(t);return Array.isArray(i)&&i.length>0&&!!((e=i[0])!=null&&e.deleted)}clear(){g(this,F).clear()}keys(){return g(this,F).keys()}_entryFromKey(t){return[t,g(this,F).get(t)]}*values(){for(let t of this.keys())yield this._entryFromKey(t)[1]}*entries(){for(let t of this.keys())yield this._entryFromKey(t)}[Symbol.iterator](){return this.entries()}forEach(t,e){for(let[i,s]of this)t.call(e,s,i,this)}map(t,e={},i){let s=new Z([],e),r=0;for(let[n,h]of this){let[o,l]=i===void 0?t(h,n,r++,this):t.call(i,h,n,r++,this);s.set(o,l)}return s}filter(t,e){let i=new Z([],{comparator:g(this,Y)}),s=0;for(let[r,n]of this)(e===void 0?t(n,r,s++,this):t.call(e,n,r,s++,this))&&i.set(r,n);return i}reduce(t,e){let i=e,s=0;for(let[r,n]of this)i=t(i,n,r,s++,this);return i}every(t,e){let i=0;for(let[s,r]of this)if(!(e===void 0?t(r,s,i++,this):t.call(e,r,s,i++,this)))return!1;return!0}some(t,e){let i=0;for(let[s,r]of this)if(e===void 0?t(r,s,i++,this):t.call(e,r,s,i++,this))return!0;return!1}find(t,e){let i=0;for(let[s,r]of this)if(e===void 0?t(r,s,i++,this):t.call(e,r,s,i++,this))return[s,r]}toArray(){return[...this]}print(){g(this,F).print()}first(){let t=g(this,F).getLeftMost();return t===void 0?void 0:this._entryFromKey(t)}last(){let t=g(this,F).getRightMost();return t===void 0?void 0:this._entryFromKey(t)}pollFirst(){let t=this.first();if(t)return this.delete(t[0]),t}pollLast(){let t=this.last();if(t)return this.delete(t[0]),t}ceiling(t){this._validateKey(t);let e=g(this,F).ceiling(t);return e===void 0?void 0:this._entryFromKey(e)}floor(t){this._validateKey(t);let e=g(this,F).floor(t);return e===void 0?void 0:this._entryFromKey(e)}higher(t){this._validateKey(t);let e=g(this,F).higher(t);return e===void 0?void 0:this._entryFromKey(e)}lower(t){this._validateKey(t);let e=g(this,F).lower(t);return e===void 0?void 0:this._entryFromKey(e)}rangeSearch(t,e={}){let{lowInclusive:i=!0,highInclusive:s=!0}=e,[r,n]=t;this._validateKey(r),this._validateKey(n);let h=g(this,F).rangeSearch([r,n]),o=[],l=g(this,F).comparator;for(let a of h)a!==void 0&&(!i&&l(a,r)===0||!s&&l(a,n)===0||o.push(this._entryFromKey(a)));return o}};F=new WeakMap;J=new WeakMap;Y=new WeakMap;f(ui,"TreeMap");var R,K,fi=class tt{constructor(t=[],e={}){A(this,R),A(this,K),u(this,"_size",0);var i;let s=e.toElementFn,r=(i=e.comparator)!=null?i:we.createDefaultComparator();B(this,K,e.comparator===void 0),B(this,R,new ht([],{comparator:r,isMapMode:e.isMapMode}));for(let n of t){let h=s?s(n):n;this.add(h)}}_validateKey(t){if(g(this,K)){if(typeof t=="number"){if(Number.isNaN(t))throw new TypeError("TreeMultiSet: NaN is not a valid key");return}if(typeof t!="string"){if(t instanceof Date){if(Number.isNaN(t.getTime()))throw new TypeError("TreeMultiSet: invalid Date key");return}throw new TypeError("TreeMultiSet: comparator is required for non-number/non-string/non-Date keys")}}}_validateCount(t){if(!Number.isSafeInteger(t)||t<0)throw new RangeError("TreeMultiSet: count must be a safe integer >= 0")}get size(){return this._size}get distinctSize(){return g(this,R).size}isEmpty(){return this.size===0}has(t){return this._validateKey(t),this.count(t)>0}count(t){var e;return this._validateKey(t),(e=g(this,R).get(t))!=null?e:0}add(t,e=1){var i;if(this._validateKey(t),this._validateCount(e),e===0)return!1;let r=((i=g(this,R).get(t))!=null?i:0)+e;return g(this,R).set(t,r),this._size+=e,!0}setCount(t,e){var i;this._validateKey(t),this._validateCount(e);let s=(i=g(this,R).get(t))!=null?i:0;return s===e?!1:(e===0?s!==0&&g(this,R).delete(t):g(this,R).set(t,e),this._size+=e-s,!0)}delete(t,e=1){var i;if(this._validateKey(t),this._validateCount(e),e===0)return!1;let s=(i=g(this,R).get(t))!=null?i:0;if(s===0)return!1;let r=Math.min(s,e),n=s-r;return n===0?g(this,R).delete(t):g(this,R).set(t,n),this._size-=r,!0}deleteAll(t){var e;this._validateKey(t);let i=(e=g(this,R).get(t))!=null?e:0;return i===0?!1:(g(this,R).delete(t),this._size-=i,!0)}*keysDistinct(){yield*g(this,R).keys()}*entries(){for(let[t,e]of g(this,R))yield[t,e!=null?e:0]}*[Symbol.iterator](){for(let[t,e]of this.entries())for(let i=0;i<e;i++)yield t}toArray(){return[...this]}toDistinctArray(){return[...this.keysDistinct()]}toEntries(){return[...this.entries()]}get comparator(){return g(this,R)._comparator}clear(){g(this,R).clear(),this._size=0}first(){return g(this,R).getLeftMost()}last(){return g(this,R).getRightMost()}pollFirst(){let t=this.first();if(t!==void 0)return this.deleteAll(t),t}pollLast(){let t=this.last();if(t!==void 0)return this.deleteAll(t),t}ceiling(t){return this._validateKey(t),g(this,R).ceiling(t)}floor(t){return this._validateKey(t),g(this,R).floor(t)}higher(t){return this._validateKey(t),g(this,R).higher(t)}lower(t){return this._validateKey(t),g(this,R).lower(t)}forEach(t){for(let[e,i]of this.entries())t(e,i)}filter(t){let e=new tt([],{comparator:g(this,K)?void 0:this.comparator,isMapMode:g(this,R)._isMapMode});for(let[i,s]of this.entries())t(i,s)&&e.add(i,s);return e}reduce(t,e){let i=e;for(let[s,r]of this.entries())i=t(i,s,r);return i}map(t,e){let i=new tt([],{comparator:e==null?void 0:e.comparator,isMapMode:g(this,R)._isMapMode});for(let[s,r]of this.entries()){let[n,h]=t(s,r);i.add(n,h)}return i}clone(){let t=new tt([],{comparator:g(this,K)?void 0:this.comparator,isMapMode:g(this,R)._isMapMode});for(let[e,i]of this.entries())t.add(e,i);return t}rangeSearch(t,e){let i=e!=null?e:(s=>s);return g(this,R).rangeSearch(t,s=>i(s.key))}print(){g(this,R).print()}};R=new WeakMap;K=new WeakMap;f(fi,"TreeMultiSet");var xe=class extends nt{constructor(t=[],e){super(t,e)}};f(xe,"PriorityQueue");var ke=xe,ci=class extends ke{constructor(t=[],e){super(t,e)}};f(ci,"MinPriorityQueue");var _i=class extends ke{constructor(t=[],e){super(t,{comparator:f((i,s)=>{if(typeof i=="object"||typeof s=="object")throw TypeError("When comparing object types, a custom comparator must be defined in the constructor's options parameter.");return i<s?1:i>s?-1:0},"comparator"),...e})}};f(_i,"MaxPriorityQueue");var di=class V{constructor(t,e){u(this,"_rows",0),u(this,"_cols",0),u(this,"_data");var i,s,r;if(e){let{rows:n,cols:h,addFn:o,subtractFn:l,multiplyFn:a}=e;typeof n=="number"&&n>0?this._rows=n:this._rows=t.length,typeof h=="number"&&h>0?this._cols=h:this._cols=((i=t[0])==null?void 0:i.length)||0,o&&(this._addFn=o),l&&(this._subtractFn=l),a&&(this._multiplyFn=a)}else this._rows=t.length,this._cols=(r=(s=t[0])==null?void 0:s.length)!=null?r:0;if(t.length>0)this._data=t;else{this._data=[];for(let n=0;n<this.rows;n++)this._data[n]=new Array(this.cols).fill(0)}}get rows(){return this._rows}get cols(){return this._cols}get data(){return this._data}get addFn(){return this._addFn}get subtractFn(){return this._subtractFn}get multiplyFn(){return this._multiplyFn}get(t,e){if(this.isValidIndex(t,e))return this.data[t][e]}set(t,e,i){return this.isValidIndex(t,e)?(this.data[t][e]=i,!0):!1}isMatchForCalculate(t){return this.rows===t.rows&&this.cols===t.cols}add(t){if(!this.isMatchForCalculate(t))throw new Error("Matrix dimensions must match for addition.");let e=[];for(let i=0;i<this.rows;i++){e[i]=[];for(let s=0;s<this.cols;s++){let r=this.get(i,s),n=t.get(i,s);if(r!==void 0&&n!==void 0){let h=this._addFn(r,n);h&&(e[i][s]=h)}}}return new V(e,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}subtract(t){if(!this.isMatchForCalculate(t))throw new Error("Matrix dimensions must match for subtraction.");let e=[];for(let i=0;i<this.rows;i++){e[i]=[];for(let s=0;s<this.cols;s++){let r=this.get(i,s),n=t.get(i,s);if(r!==void 0&&n!==void 0){let h=this._subtractFn(r,n);h&&(e[i][s]=h)}}}return new V(e,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}multiply(t){if(this.cols!==t.rows)throw new Error("Matrix dimensions must be compatible for multiplication (A.cols = B.rows).");let e=[];for(let i=0;i<this.rows;i++){e[i]=[];for(let s=0;s<t.cols;s++){let r;for(let n=0;n<this.cols;n++){let h=this.get(i,n),o=t.get(n,s);if(h!==void 0&&o!==void 0){let l=this.multiplyFn(h,o);l!==void 0&&(r=this.addFn(r,l))}}r!==void 0&&(e[i][s]=r)}}return new V(e,{rows:this.rows,cols:t.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}transpose(){if(this.data.some(e=>e.length!==this.rows))throw new Error("Matrix must be rectangular for transposition.");let t=[];for(let e=0;e<this.cols;e++){t[e]=[];for(let i=0;i<this.rows;i++){let s=this.get(i,e);s!==void 0&&(t[e][i]=s)}}return new V(t,{rows:this.cols,cols:this.rows,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}inverse(){var t;if(this.rows!==this.cols)throw new Error("Matrix must be square for inversion.");let e=[];for(let r=0;r<this.rows;r++){e[r]=this.data[r].slice();for(let n=0;n<this.cols;n++)e[r][this.cols+n]=r===n?1:0}let i=new V(e,{rows:this.rows,cols:this.cols*2,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn});for(let r=0;r<this.rows;r++){let n=r;for(;n<this.rows&&i.get(n,r)===0;)n++;if(n===this.rows)throw new Error("Matrix is singular, and its inverse does not exist.");i._swapRows(r,n);let h=(t=i.get(r,r))!=null?t:1;if(h===0)throw new Error("Matrix is singular, and its inverse does not exist (division by zero).");i._scaleRow(r,1/h);for(let o=0;o<this.rows;o++)if(o!==r){let l=i.get(o,r);l===void 0&&(l=0),i._addScaledRow(o,r,-l)}}let s=[];for(let r=0;r<this.rows;r++)s[r]=i.data[r].slice(this.cols);return new V(s,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}dot(t){if(this.cols!==t.rows)throw new Error("Number of columns in the first matrix must be equal to the number of rows in the second matrix for dot product.");let e=[];for(let i=0;i<this.rows;i++){e[i]=[];for(let s=0;s<t.cols;s++){let r;for(let n=0;n<this.cols;n++){let h=this.get(i,n),o=t.get(n,s);if(h!==void 0&&o!==void 0){let l=this.multiplyFn(h,o);l!==void 0&&(r=this.addFn(r,l))}}r!==void 0&&(e[i][s]=r)}}return new V(e,{rows:this.rows,cols:t.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}isValidIndex(t,e){return t>=0&&t<this.rows&&e>=0&&e<this.cols}clone(){return new V(this.data,{rows:this.rows,cols:this.cols,addFn:this.addFn,subtractFn:this.subtractFn,multiplyFn:this.multiplyFn})}_addFn(t,e){return t===void 0?e:t+e}_subtractFn(t,e){return t-e}_multiplyFn(t,e){return t*e}_swapRows(t,e){let i=this.data[t];this.data[t]=this.data[e],this.data[e]=i}_scaleRow(t,e){for(let i=0;i<this.cols;i++){let s=this.multiplyFn(this.data[t][i],e);s===void 0&&(s=0),this.data[t][i]=s}}_addScaledRow(t,e,i){for(let s=0;s<this.cols;s++){let r=this.multiplyFn(this.data[e][s],i);r===void 0&&(r=0);let n=r,h=this.addFn(this.data[t][s],n);h===void 0&&(h=0),this.data[t][s]=h}}};f(di,"Matrix");var Ne=class Ee{constructor(t,e){u(this,"direction"),u(this,"turn"),this.direction=t,this.turn=()=>new Ee(e[t],e)}};f(Ne,"Character");var gi=Ne,pi=class{constructor({matrix:t,turning:e,onMove:i,init:{cur:s,charDir:r,VISITED:n}}){u(this,"onMove"),u(this,"_matrix"),u(this,"_cur"),u(this,"_character"),u(this,"_VISITED"),this._matrix=t,this._cur=s,this._character=new gi(r,e),this.onMove=i,this.onMove&&this.onMove(this._cur),this._VISITED=n,this._matrix[this._cur[0]][this._cur[1]]=this._VISITED}start(){for(;this.check(this._character.direction)||this.check(this._character.turn().direction);){let{direction:t}=this._character;this.check(t)?this.move(t):this.check(this._character.turn().direction)&&(this._character=this._character.turn())}}check(t){let e,i,s=this._matrix,[r,n]=this._cur;switch(t){case"up":if(i=s[r-1],!i)return!1;e=i[n];break;case"right":e=s[r][n+1];break;case"down":if(i=s[r+1],!i)return!1;e=i[n];break;case"left":e=s[r][n-1];break}return e!==void 0&&e!==this._VISITED}move(t){switch(t){case"up":this._cur[0]--;break;case"right":this._cur[1]++;break;case"down":this._cur[0]++;break;case"left":this._cur[1]--;break}let[e,i]=this._cur;this._matrix[e][i]=this._VISITED,this.onMove&&this.onMove(this._cur)}};f(pi,"Navigator");var Le=class{constructor(t){u(this,"_key"),u(this,"_children"),u(this,"_isEnd"),this._key=t,this._isEnd=!1,this._children=new Map}get key(){return this._key}set key(t){this._key=t}get children(){return this._children}set children(t){this._children=t}get isEnd(){return this._isEnd}set isEnd(t){this._isEnd=t}};f(Le,"TrieNode");var ft=Le,vi=class extends rt{constructor(t=[],e){if(super(e),u(this,"_size",0),u(this,"_caseSensitive",!0),u(this,"_root",new ft("")),e){let{caseSensitive:i}=e;i!==void 0&&(this._caseSensitive=i)}t&&this.addMany(t)}get size(){return this._size}get caseSensitive(){return this._caseSensitive}get root(){return this._root}get _total(){return this._size}add(t){t=this._caseProcess(t);let e=this.root,i=!1;for(let s of t){let r=e.children.get(s);r||(r=new ft(s),e.children.set(s,r)),e=r}return e.isEnd||(i=!0,e.isEnd=!0,this._size++),i}addMany(t){let e=[];for(let i of t)this.toElementFn?e.push(this.add(this.toElementFn(i))):e.push(this.add(i));return e}has(t){t=this._caseProcess(t);let e=this.root;for(let i of t){let s=e.children.get(i);if(!s)return!1;e=s}return e.isEnd}isEmpty(){return this._size===0}clear(){this._size=0,this._root=new ft("")}delete(t){t=this._caseProcess(t);let e=!1,i=f((s,r)=>{let n=t[r],h=s.children.get(n);return h?r===t.length-1?h.isEnd?(h.children.size>0?h.isEnd=!1:s.children.delete(n),e=!0,!0):!1:i(h,r+1)&&!s.isEnd&&h.children.size===0?(s.children.delete(n),!0):!1:!1},"dfs");return i(this.root,0),e&&this._size--,e}getHeight(){let t=this.root,e=0;if(t){let i=f((s,r)=>{r>e&&(e=r);let{children:n}=s;if(n)for(let h of n.entries())i(h[1],r+1)},"bfs");i(t,0)}return e}hasPurePrefix(t){t=this._caseProcess(t);let e=this.root;for(let i of t){let s=e.children.get(i);if(!s)return!1;e=s}return!e.isEnd}hasPrefix(t){t=this._caseProcess(t);let e=this.root;for(let i of t){let s=e.children.get(i);if(!s)return!1;e=s}return!0}hasCommonPrefix(t){t=this._caseProcess(t);let e="",i=f(s=>{if(e+=s.key,e!==t&&!s.isEnd)if(s&&s.children&&s.children.size===1)i(Array.from(s.children.values())[0]);else return},"dfs");return i(this.root),e===t}getLongestCommonPrefix(){let t="",e=f(i=>{if(t+=i.key,!i.isEnd)if(i&&i.children&&i.children.size===1)e(Array.from(i.children.values())[0]);else return},"dfs");return e(this.root),t}getWords(t="",e=Number.MAX_SAFE_INTEGER,i=!1){t=this._caseProcess(t);let s=[],r=0,n=f((o,l)=>{for(let[a,_]of o.children){if(r>=e)return;n(_,l+a)}if(o.isEnd){if(r>=e)return;s.push(l),r++}},"dfs"),h=this.root;if(t)for(let o of t){let l=h.children.get(o);if(l)h=l;else return[]}return(i||h!==this.root)&&n(h,t),s}clone(){let t=this._createInstance();for(let e of this)t.add(e);return t}filter(t,e){let i=this._createInstance(),s=0;for(let r of this)t.call(e,r,s,this)&&i.add(r),s++;return i}map(t,e,i){let s=this._createLike([],e),r=0;for(let n of this){let h=i===void 0?t(n,r++,this):t.call(i,n,r++,this);if(typeof h!="string")throw new TypeError(`Trie.map callback must return string; got ${typeof h}`);s.add(h)}return s}mapSame(t,e){let i=this._createInstance(),s=0;for(let r of this){let n=e===void 0?t(r,s++,this):t.call(e,r,s++,this);i.add(n)}return i}_createInstance(t){let e=this.constructor;return new e([],{toElementFn:this.toElementFn,caseSensitive:this.caseSensitive,...t!=null?t:{}})}_createLike(t=[],e){let i=this.constructor;return new i(t,e)}_spawnLike(t){return this._createLike([],t)}*_getIterator(){function*t(e,i){e.isEnd&&(yield i);for(let[s,r]of e.children)yield*t(r,i+s)}f(t,"_dfs"),yield*t(this.root,"")}_caseProcess(t){return this._caseSensitive||(t=t.toLowerCase()),t}};f(vi,"Trie");var mi=class Re{constructor(t,e,i){u(this,"_key"),u(this,"_value"),u(this,"_children"),this._key=t,this._value=e||void 0,i&&(this._children=i)}get key(){return this._key}set key(t){this._key=t}get value(){return this._value}set value(t){this._value=t}get children(){return this._children}set children(t){this._children=t}addChildren(t){this._children||(this._children=[]),t instanceof Re?this._children.push(t):this._children=this._children.concat(t)}getHeight(){let t=0;if(this){let e=f((i,s)=>{s>t&&(t=s);let{_children:r}=i;if(r)for(let n=0,h=r.length;n<h;n++)e(r[n],s+1)},"bfs");e(this,0)}return t}};f(mi,"TreeNode");return Ae(Mi);})();
|
|
6
6
|
/*! Bundled license information:
|
|
7
7
|
|
|
8
8
|
data-structure-typed/dist/esm-legacy/index.mjs:
|