-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathBooking Concert Tickets in Groups.cpp
156 lines (132 loc) · 4.49 KB
/
Booking Concert Tickets in Groups.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Runtime: 639 ms (Top 96.14%) | Memory: 288.6 MB (Top 75.29%)
class BookMyShow {
int occ = -1; // No row occupied initially
vector<long long> sumTree, maxTree;
int treeSize = 0, rowSeats = 0;
public:
BookMyShow(int n, int m) {
// Seg tree size
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;
// Filling the leaves
for(int i=n; i<2*n;i++)
this->sumTree[i] = (long long) m;
// Forming the higher nodes
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;
// Filling the leaves
for(int i=n; i<2*n;i++)
this->maxTree[i] = (long long) m;
// Forming the higher nodes
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++]; // Right child
if(maxRow % 2 == 0) sum += this->sumTree[maxRow--]; // Left child
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; // Update leaf
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++]); // Right child
if(maxRow % 2 == 0) ans = max(ans, this->maxTree[maxRow--]); // Left child
minRow /= 2; maxRow /= 2;
}
// cout<<"range max over"<<'\n';
return ans;
}
void updateMaxTree(int index, int newValue){
int n = this->treeSize / 2;
int temp = index;
index += n;
this->maxTree[index] = newValue; // Update leaf
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;
// Updatng the segment trees
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;
}
// Updating the occupied rows
occ = minIndex - 1;
// Updating the segment trees
this->updateSumTree(minIndex, seats - k);
this->updateMaxTree(minIndex, seats - k);
return true;
}
};
/**
* Your BookMyShow object will be instantiated and called as such:
* BookMyShow* obj = new BookMyShow(n, m);
* vector<int> param_1 = obj->gather(k,maxRow);
* bool param_2 = obj->scatter(k,maxRow);
*/