# [c++ solution] beats 91.92%

• MyCalendarTwo() {

}
vector <pair<int, int>> theBooks;
bool book(int start, int end) {
int mark = -1;
int stage = 0; int theC = 0,theD=0;
for (int i = 0; i < theBooks.size(); i++)
{
if (theBooks[i].first < start)
{
if (theBooks[i].second > start)
{
if (stage == 0)
{
stage = 1; theC = theBooks[i].second;
}
else
if (stage == 1)
{
return false;
}
}
}
else
{
if(mark==-1) mark = i;
if (theBooks[i].first < end)
{
if (stage == 0)
{
stage = 2; theC = theBooks[i].first; theD= theBooks[i].second;
}
else
{
if (stage == 2)
{
if (theBooks[i].first >= theD)
{
stage = 2;
theC = theBooks[i].first; theD = theBooks[i].second;
}
else
{
return false;
}
}
if (stage == 1)
{
if (theBooks[i].first < theC)
{
return false;
}
else
{
stage = 2;
theC = theBooks[i].first; theD = theBooks[i].second;
}
}
}
}
else
{
theBooks.insert(theBooks.begin() + mark, pair<int, int>(start, end));
return true;
}
}
}

``````if (mark == -1)
{
theBooks.push_back(pair<int, int>(start, end));
return true;
}
else
{
theBooks.insert(theBooks.begin() + mark, pair<int, int>(start, end));
return true;
}
``````

}
};

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.