classMyCalendarTwo:def__init__(self):self.sd=SortedDict()defbook(self,startTime:int,endTime:int)->bool:self.sd[startTime]=self.sd.get(startTime,0)+1self.sd[endTime]=self.sd.get(endTime,0)-1s=0forvinself.sd.values():s+=vifs>2:self.sd[startTime]-=1self.sd[endTime]+=1returnFalsereturnTrue# Your MyCalendarTwo object will be instantiated and called as such:# obj = MyCalendarTwo()# param_1 = obj.book(startTime,endTime)
classMyCalendarTwo{privatefinalMap<Integer,Integer>tm=newTreeMap<>();publicMyCalendarTwo(){}publicbooleanbook(intstartTime,intendTime){tm.merge(startTime,1,Integer::sum);tm.merge(endTime,-1,Integer::sum);ints=0;for(intv:tm.values()){s+=v;if(s>2){tm.merge(startTime,-1,Integer::sum);tm.merge(endTime,1,Integer::sum);returnfalse;}}returntrue;}}/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo obj = new MyCalendarTwo(); * boolean param_1 = obj.book(startTime,endTime); */
classMyCalendarTwo{public:MyCalendarTwo(){}boolbook(intstartTime,intendTime){++m[startTime];--m[endTime];ints=0;for(auto&[_,v]:m){s+=v;if(s>2){--m[startTime];++m[endTime];returnfalse;}}returntrue;}private:map<int,int>m;};/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo* obj = new MyCalendarTwo(); * bool param_1 = obj->book(startTime,endTime); */
typeMyCalendarTwostruct{rbt*redblacktree.Tree[int,int]}funcConstructor()MyCalendarTwo{returnMyCalendarTwo{rbt:redblacktree.New[int,int]()}}func(this*MyCalendarTwo)Book(startTimeint,endTimeint)bool{merge:=func(x,vint){c,_:=this.rbt.Get(x)ifc+v==0{this.rbt.Remove(x)}else{this.rbt.Put(x,c+v)}}merge(startTime,1)merge(endTime,-1)s:=0for_,v:=rangethis.rbt.Values(){s+=vifs>2{merge(startTime,-1)merge(endTime,1)returnfalse}}returntrue}/** * Your MyCalendarTwo object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Book(startTime,endTime); */
classMyCalendarTwo{privatetm:Record<number,number>={};constructor(){}book(startTime:number,endTime:number):boolean{this.tm[startTime]=(this.tm[startTime]??0)+1;this.tm[endTime]=(this.tm[endTime]??0)-1;lets=0;for(constvofObject.values(this.tm)){s+=v;if(s>2){if(--this.tm[startTime]===0){deletethis.tm[startTime];}if(++this.tm[endTime]===0){deletethis.tm[endTime];}returnfalse;}}returntrue;}}/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(startTime,endTime) */
varMyCalendarTwo=function(){this.tm={};};/** * @param {number} startTime * @param {number} endTime * @return {boolean} */MyCalendarTwo.prototype.book=function(startTime,endTime){this.tm[startTime]=(this.tm[startTime]||0)+1;this.tm[endTime]=(this.tm[endTime]||0)-1;lets=0;for(constvofObject.values(this.tm)){s+=v;if(s>2){if(--this.tm[startTime]===0){deletethis.tm[startTime];}if(++this.tm[endTime]===0){deletethis.tm[endTime];}returnfalse;}}returntrue;};/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(startTime,endTime) */
classNode:def__init__(self,l:int,r:int):self.left=Noneself.right=Noneself.l=lself.r=rself.mid=(l+r)>>1self.v=0self.add=0classSegmentTree:def__init__(self):self.root=Node(1,10**9+1)defmodify(self,l:int,r:int,v:int,node:Node=None):ifl>r:returnifnodeisNone:node=self.rootifnode.l>=landnode.r<=r:node.v+=vnode.add+=vreturnself.pushdown(node)ifl<=node.mid:self.modify(l,r,v,node.left)ifr>node.mid:self.modify(l,r,v,node.right)self.pushup(node)defquery(self,l:int,r:int,node:Node=None)->int:ifl>r:return0ifnodeisNone:node=self.rootifnode.l>=landnode.r<=r:returnnode.vself.pushdown(node)v=0ifl<=node.mid:v=max(v,self.query(l,r,node.left))ifr>node.mid:v=max(v,self.query(l,r,node.right))returnvdefpushup(self,node:Node):node.v=max(node.left.v,node.right.v)defpushdown(self,node:Node):ifnode.leftisNone:node.left=Node(node.l,node.mid)ifnode.rightisNone:node.right=Node(node.mid+1,node.r)ifnode.add:node.left.v+=node.addnode.right.v+=node.addnode.left.add+=node.addnode.right.add+=node.addnode.add=0classMyCalendarTwo:def__init__(self):self.tree=SegmentTree()defbook(self,startTime:int,endTime:int)->bool:ifself.tree.query(startTime+1,endTime)>=2:returnFalseself.tree.modify(startTime+1,endTime,1)returnTrue# Your MyCalendarTwo object will be instantiated and called as such:# obj = MyCalendarTwo()# param_1 = obj.book(startTime,endTime)
classNode{Nodeleft;Noderight;intl;intr;intmid;intv;intadd;publicNode(intl,intr){this.l=l;this.r=r;this.mid=(l+r)>>1;}}classSegmentTree{privateNoderoot=newNode(1,(int)1e9+1);publicSegmentTree(){}publicvoidmodify(intl,intr,intv){modify(l,r,v,root);}publicvoidmodify(intl,intr,intv,Nodenode){if(l>r){return;}if(node.l>=l&&node.r<=r){node.v+=v;node.add+=v;return;}pushdown(node);if(l<=node.mid){modify(l,r,v,node.left);}if(r>node.mid){modify(l,r,v,node.right);}pushup(node);}publicintquery(intl,intr){returnquery(l,r,root);}publicintquery(intl,intr,Nodenode){if(l>r){return0;}if(node.l>=l&&node.r<=r){returnnode.v;}pushdown(node);intv=0;if(l<=node.mid){v=Math.max(v,query(l,r,node.left));}if(r>node.mid){v=Math.max(v,query(l,r,node.right));}returnv;}publicvoidpushup(Nodenode){node.v=Math.max(node.left.v,node.right.v);}publicvoidpushdown(Nodenode){if(node.left==null){node.left=newNode(node.l,node.mid);}if(node.right==null){node.right=newNode(node.mid+1,node.r);}if(node.add!=0){Nodeleft=node.left,right=node.right;left.add+=node.add;right.add+=node.add;left.v+=node.add;right.v+=node.add;node.add=0;}}}classMyCalendarTwo{privateSegmentTreetree=newSegmentTree();publicMyCalendarTwo(){}publicbooleanbook(intstartTime,intendTime){if(tree.query(startTime+1,endTime)>=2){returnfalse;}tree.modify(startTime+1,endTime,1);returntrue;}}/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo obj = new MyCalendarTwo(); * boolean param_1 = obj.book(startTime,endTime); */
classNode{public:intl,r,mid,v,add;Node*left;Node*right;Node(intl,intr):l(l),r(r),mid((l+r)>>1),v(0),add(0),left(nullptr),right(nullptr){}};classSegmentTree{public:Node*root;SegmentTree(){root=newNode(1,1e9+1);}voidmodify(intl,intr,intv,Node*node=nullptr){if(l>r){return;}if(node==nullptr){node=root;}if(node->l>=l&&node->r<=r){node->v+=v;node->add+=v;return;}pushdown(node);if(l<=node->mid){modify(l,r,v,node->left);}if(r>node->mid){modify(l,r,v,node->right);}pushup(node);}intquery(intl,intr,Node*node=nullptr){if(l>r){return0;}if(node==nullptr){node=root;}if(node->l>=l&&node->r<=r){returnnode->v;}pushdown(node);intv=0;if(l<=node->mid){v=max(v,query(l,r,node->left));}if(r>node->mid){v=max(v,query(l,r,node->right));}returnv;}private:voidpushup(Node*node){node->v=max(node->left->v,node->right->v);}voidpushdown(Node*node){if(node->left==nullptr){node->left=newNode(node->l,node->mid);}if(node->right==nullptr){node->right=newNode(node->mid+1,node->r);}if(node->add){node->left->v+=node->add;node->right->v+=node->add;node->left->add+=node->add;node->right->add+=node->add;node->add=0;}}};classMyCalendarTwo{public:SegmentTreetree;MyCalendarTwo(){}boolbook(intstartTime,intendTime){if(tree.query(startTime+1,endTime)>=2){returnfalse;}tree.modify(startTime+1,endTime,1);returntrue;}};/** * Your MyCalendarTwo object will be instantiated and called as such: * MyCalendarTwo* obj = new MyCalendarTwo(); * bool param_1 = obj->book(startTime,endTime); */
typenodestruct{left*noderight*nodel,mid,rintv,addint}funcnewNode(l,rint)*node{return&node{l:l,r:r,mid:int(uint(l+r)>>1),}}typesegmentTreestruct{root*node}funcnewSegmentTree()*segmentTree{return&segmentTree{root:newNode(1,1e9+1),}}func(t*segmentTree)modify(l,r,vint,n*node){ifl>r{return}ifn.l>=l&&n.r<=r{n.v+=vn.add+=vreturn}t.pushdown(n)ifl<=n.mid{t.modify(l,r,v,n.left)}ifr>n.mid{t.modify(l,r,v,n.right)}t.pushup(n)}func(t*segmentTree)query(l,rint,n*node)int{ifl>r{return0}ifn.l>=l&&n.r<=r{returnn.v}t.pushdown(n)v:=0ifl<=n.mid{v=max(v,t.query(l,r,n.left))}ifr>n.mid{v=max(v,t.query(l,r,n.right))}returnv}func(t*segmentTree)pushup(n*node){n.v=max(n.left.v,n.right.v)}func(t*segmentTree)pushdown(n*node){ifn.left==nil{n.left=newNode(n.l,n.mid)}ifn.right==nil{n.right=newNode(n.mid+1,n.r)}ifn.add!=0{n.left.add+=n.addn.right.add+=n.addn.left.v+=n.addn.right.v+=n.addn.add=0}}typeMyCalendarTwostruct{tree*segmentTree}funcConstructor()MyCalendarTwo{returnMyCalendarTwo{newSegmentTree()}}func(this*MyCalendarTwo)Book(startTimeint,endTimeint)bool{ifthis.tree.query(startTime+1,endTime,this.tree.root)>=2{returnfalse}this.tree.modify(startTime+1,endTime,1,this.tree.root)returntrue}/** * Your MyCalendarTwo object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Book(startTime,endTime); */
classNode{left:Node|null=null;right:Node|null=null;l:number;r:number;mid:number;v:number=0;add:number=0;constructor(l:number,r:number){this.l=l;this.r=r;this.mid=(l+r)>>1;}}classSegmentTree{privateroot:Node=newNode(1,1e9+1);modify(l:number,r:number,v:number,node:Node|null=this.root):void{if(l>r||!node){return;}if(node.l>=l&&node.r<=r){node.v+=v;node.add+=v;return;}this.pushdown(node);if(l<=node.mid){this.modify(l,r,v,node.left);}if(r>node.mid){this.modify(l,r,v,node.right);}this.pushup(node);}query(l:number,r:number,node:Node|null=this.root):number{if(l>r||!node){return0;}if(node.l>=l&&node.r<=r){returnnode.v;}this.pushdown(node);letv=0;if(l<=node.mid){v=Math.max(v,this.query(l,r,node.left));}if(r>node.mid){v=Math.max(v,this.query(l,r,node.right));}returnv;}privatepushup(node:Node):void{if(node.left&&node.right){node.v=Math.max(node.left.v,node.right.v);}}privatepushdown(node:Node):void{if(!node.left){node.left=newNode(node.l,node.mid);}if(!node.right){node.right=newNode(node.mid+1,node.r);}if(node.add){letleft=node.left;letright=node.right;left.add+=node.add;right.add+=node.add;left.v+=node.add;right.v+=node.add;node.add=0;}}}classMyCalendarTwo{privatetree:SegmentTree=newSegmentTree();constructor(){}book(startTime:number,endTime:number):boolean{if(this.tree.query(startTime+1,endTime)>=2){returnfalse;}this.tree.modify(startTime+1,endTime,1);returntrue;}}/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(startTime,endTime) */
classNode{constructor(l,r){this.left=null;this.right=null;this.l=l;this.r=r;this.mid=(l+r)>>1;this.v=0;this.add=0;}}classSegmentTree{constructor(){this.root=newNode(1,1e9+1);}modify(l,r,v,node=this.root){if(l>r||!node){return;}if(node.l>=l&&node.r<=r){node.v+=v;node.add+=v;return;}this.pushdown(node);if(l<=node.mid){this.modify(l,r,v,node.left);}if(r>node.mid){this.modify(l,r,v,node.right);}this.pushup(node);}query(l,r,node=this.root){if(l>r||!node){return0;}if(node.l>=l&&node.r<=r){returnnode.v;}this.pushdown(node);letv=0;if(l<=node.mid){v=Math.max(v,this.query(l,r,node.left));}if(r>node.mid){v=Math.max(v,this.query(l,r,node.right));}returnv;}pushup(node){if(node.left&&node.right){node.v=Math.max(node.left.v,node.right.v);}}pushdown(node){if(!node.left){node.left=newNode(node.l,node.mid);}if(!node.right){node.right=newNode(node.mid+1,node.r);}if(node.add){constleft=node.left;constright=node.right;left.add+=node.add;right.add+=node.add;left.v+=node.add;right.v+=node.add;node.add=0;}}}varMyCalendarTwo=function(){this.tree=newSegmentTree();};/** * @param {number} startTime * @param {number} endTime * @return {boolean} */MyCalendarTwo.prototype.book=function(startTime,endTime){if(this.tree.query(startTime+1,endTime)>=2){returnfalse;}this.tree.modify(startTime+1,endTime,1);returntrue;};/** * Your MyCalendarTwo object will be instantiated and called as such: * var obj = new MyCalendarTwo() * var param_1 = obj.book(startTime,endTime) */