A concert hall has n rows numbered from 0 to n - 1, each with m seats, numbered from 0 to m - 1. You need to design a ticketing system that can allocate seats in the following cases:
If a group of k spectators can sit together in a row.
If every member of a group of k spectators can get a seat. They may or may not sit together.
Note that the spectators are very picky. Hence:
They will book seats only if each member of their group can get a seat with row number less than or equal to maxRow. maxRow can vary from group to group.
In case there are multiple rows to choose from, the row with the smallest number is chosen. If there are multiple seats to choose in the same row, the seat with the smallest number is chosen.
Implement the BookMyShow class:
BookMyShow(int n, int m) Initializes the object with n as number of rows and m as number of seats per row.
int[] gather(int k, int maxRow) Returns an array of length 2 denoting the row and seat number (respectively) of the first seat being allocated to the k members of the group, who must sit together. In other words, it returns the smallest possible r and c such that all [c, c + k - 1] seats are valid and empty in row r, and r <= maxRow. Returns [] in case it is not possible to allocate seats to the group.
boolean scatter(int k, int maxRow) Returns true if all k members of the group can be allocated seats in rows 0 to maxRow, who may or may not sit together. If the seats can be allocated, it allocates k seats to the group with the smallest row numbers, and the smallest possible seat numbers in each row. Otherwise, returns false.
Example 1:
Input
["BookMyShow", "gather", "gather", "scatter", "scatter"]
[[2, 5], [4, 0], [2, 0], [5, 1], [5, 1]]
Output
[null, [0, 0], [], true, false]
Explanation
BookMyShow bms = new BookMyShow(2, 5); // There are 2 rows with 5 seats each
bms.gather(4, 0); // return [0, 0]
// The group books seats [0, 3] of row 0.
bms.gather(2, 0); // return []
// There is only 1 seat left in row 0,
// so it is not possible to book 2 consecutive seats.
bms.scatter(5, 1); // return True
// The group books seat 4 of row 0 and seats [0, 3] of row 1.
bms.scatter(5, 1); // return False
// There is only one seat left in the hall.
class BookMyShow {
int occ = -1;
vector<long long> sumTree, maxTree;
int treeSize = 0, rowSeats = 0;
public:
BookMyShow(int n, int m) {
this->treeSize = (int) 2 * pow(2, ceil((double)log2(n)));
sumTree.resize(treeSize);
maxTree.resize(treeSize);
this->rowSeats = m;
constructSumTree(m);
constructMaxTree(m);
}
void constructSumTree(int m){
int n = this->treeSize/ 2;
for(int i=n; i<2*n;i++)
this->sumTree[i] = (long long) m;
for(int i=n-1;i>=1;i--)
this->sumTree[i] = this->sumTree[2*i] + this->sumTree[2*i+1];
}
void constructMaxTree(int m){
int n = this->treeSize/ 2;
for(int i=n; i<2*n;i++)
this->maxTree[i] = (long long) m;
for(int i=n-1;i>=1;i--)
this->maxTree[i] = max(this->maxTree[2*i], this->maxTree[2*i+1]);
}
long long rangeSum(int minRow, int maxRow){
long long sum = 0;
int n = treeSize / 2;
minRow += n; maxRow += n;
while(minRow <= maxRow){
if(minRow % 2 == 1) sum += this->sumTree[minRow++];
if(maxRow % 2 == 0) sum += this->sumTree[maxRow--];
minRow /= 2; maxRow /= 2;
}
return sum;
}
void updateSumTree(int index, int newValue){
int n = this->treeSize / 2;
int temp = index;
index += n;
this->sumTree[index] = newValue;
index /= 2;
while(index > 0){
this->sumTree[index] = this->sumTree[2*index] + this->sumTree[2*index + 1];
index /= 2;
}
}
long long rangeMax(int minRow, int maxRow){
long long ans = 0;
int n = this->treeSize / 2;
minRow += n; maxRow += n;
while(minRow <= maxRow){
if(minRow % 2 == 1) ans = max(ans, this->maxTree[minRow++]);
if(maxRow % 2 == 0) ans = max(ans, this->maxTree[maxRow--]);
minRow /= 2; maxRow /= 2;
}
return ans;
}
void updateMaxTree(int index, int newValue){
int n = this->treeSize / 2;
int temp = index;
index += n;
this->maxTree[index] = newValue;
index /= 2;
while(index > 0){
this->maxTree[index] = max(this->maxTree[2*index], this->maxTree[2*index + 1]);
index /= 2;
}
}
vector<int> gather(int k, int maxRow) {
int minRow = occ + 1;
if(maxRow < minRow) return {};
if(rangeMax(minRow, maxRow) < k) return {};
int minIndex = maxRow;
int seats = 0;
int low = minRow, high = maxRow;
while(low <= high){
int midRow = (low + high)/2;
int maxSeats = rangeMax(minRow, midRow);
if(maxSeats >= k){
high = midRow - 1;
seats = maxSeats;
minIndex = midRow;
}
else low = midRow + 1;
}
int r = minIndex, c = this->rowSeats - seats;
this->updateMaxTree(minIndex, seats - k);
this->updateSumTree(minIndex, seats - k);
return {r,c};
}
bool scatter(int k, int maxRow) {
int minRow = occ + 1;
if(maxRow < minRow) return false;
if(rangeSum(minRow, maxRow) < k) return false;
int minIndex = maxRow;
long long seats = 0;
int low = minRow, high = maxRow;
while(low <= high){
int midRow = (low + high)/2;
long long rangeSeats = rangeSum(minRow, midRow);
if(rangeSeats >= k){
high = midRow - 1;
seats = rangeSeats;
minIndex = midRow;
}
else low = midRow + 1;
}
occ = minIndex - 1;
this->updateSumTree(minIndex, seats - k);
this->updateMaxTree(minIndex, seats - k);
return true;
}
};