具体思路:
线段树区间最大值问题;
具体代码:
struct node{
int ln,rn,maxn,val,add;
node(){
ln=0;
rn=0;
add=0;
val=0;
}
};
class MyCalendarTwo {
public:
MyCalendarTwo() {
n=1e9;
m=1e6;
cnt=1;
tree.resize(m);
}
void create(int root){
if(tree[root]==nullptr){
tree[root]=new node();
}
if(tree[root]->ln==0){
//左节点未开;
tree[root]->ln=++cnt;
tree[tree[root]->ln]=new node();
}
if(tree[root]->rn==0){
tree[root]->rn=++cnt;
tree[tree[root]->rn]=new node();
}
}
void pushup(int root){
tree[root]->val=max(tree[tree[root]->ln]->val,tree[tree[root]->rn]->val);
}
void pushdown(int root){
tree[tree[root]->ln]->add+=tree[root]->add;
tree[tree[root]->rn]->add+=tree[root]->add;
tree[tree[root]->ln]->val+=tree[root]->add;
tree[tree[root]->rn]->val+=tree[root]->add;
tree[root]->add=0;
}
int query(int root,int l,int r,int lc,int rc){
if(l<=lc&&rc<=r){
return tree[root]->val;
}
create(root);
pushdown(root);
int mid=lc+(rc-lc)/2;
int ret=0;
if(l<=mid){
ret=max(ret,query(tree[root]->ln, l, r, lc, mid));
}
if(mid<r){
ret=max(ret,query(tree[root]->rn, l, r, mid+1, rc));
}
return ret;
}
void update(int root,int l,int r,int lc,int rc){
if(l<=lc&&rc<=r){
tree[root]->val++;
tree[root]->add++;
return;
}
create(root);
pushdown(root);
int mid=lc+(rc-lc)/2;
if(l<=mid)
update(tree[root]->ln, l, r, lc, mid);
if(r>mid)
update(tree[root]->rn,l, r, mid+1, rc);
pushup(root);
}
bool book(int start, int end) {
if(query(1, start+1, end, 1, n+1)>=2)
return false;
update(1, start+1, end,1, n+1);
return true;
}
private:
vector<node*> tree;
int n;
int m;
int cnt;
};
/**
* Your MyCalendarTwo object will be instantiated and called as such:
* MyCalendarTwo* obj = new MyCalendarTwo();
* bool param_1 = obj->book(start,end);
*/```