forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
135 lines (123 loc) · 3.03 KB
/
Solution.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
class Node {
public:
int l, r;
long s, mx;
};
class SegmentTree {
public:
SegmentTree(int n, int m) {
this->m = m;
tr.resize(n << 2);
for (int i = 0; i < n << 2; ++i) {
tr[i] = new Node();
}
build(1, 1, n);
}
void modify(int u, int x, int v) {
if (tr[u]->l == x && tr[u]->r == x) {
tr[u]->s = tr[u]->mx = v;
return;
}
int mid = (tr[u]->l + tr[u]->r) >> 1;
if (x <= mid) {
modify(u << 1, x, v);
} else {
modify(u << 1 | 1, x, v);
}
pushup(u);
}
long querySum(int u, int l, int r) {
if (tr[u]->l >= l && tr[u]->r <= r) {
return tr[u]->s;
}
int mid = (tr[u]->l + tr[u]->r) >> 1;
long v = 0;
if (l <= mid) {
v += querySum(u << 1, l, r);
}
if (r > mid) {
v += querySum(u << 1 | 1, l, r);
}
return v;
}
int queryIdx(int u, int l, int r, int k) {
if (tr[u]->mx < k) {
return 0;
}
if (tr[u]->l == tr[u]->r) {
return tr[u]->l;
}
int mid = (tr[u]->l + tr[u]->r) >> 1;
if (tr[u << 1]->mx >= k) {
return queryIdx(u << 1, l, r, k);
}
if (r > mid) {
return queryIdx(u << 1 | 1, l, r, k);
}
return 0;
}
private:
vector<Node*> tr;
int m;
void build(int u, int l, int r) {
tr[u]->l = l;
tr[u]->r = r;
if (l == r) {
tr[u]->s = m;
tr[u]->mx = m;
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void pushup(int u) {
tr[u]->s = tr[u << 1]->s + tr[u << 1 | 1]->s;
tr[u]->mx = max(tr[u << 1]->mx, tr[u << 1 | 1]->mx);
}
};
class BookMyShow {
public:
BookMyShow(int n, int m) {
this->n = n;
this->m = m;
tree = new SegmentTree(n, m);
}
vector<int> gather(int k, int maxRow) {
++maxRow;
int i = tree->queryIdx(1, 1, maxRow, k);
if (i == 0) {
return {};
}
long s = tree->querySum(1, i, i);
tree->modify(1, i, s - k);
return {i - 1, (int) (m - s)};
}
bool scatter(int k, int maxRow) {
++maxRow;
if (tree->querySum(1, 1, maxRow) < k) {
return false;
}
int i = tree->queryIdx(1, 1, maxRow, 1);
for (int j = i; j <= n; ++j) {
long s = tree->querySum(1, j, j);
if (s >= k) {
tree->modify(1, j, s - k);
return true;
}
k -= s;
tree->modify(1, j, 0);
}
return true;
}
private:
SegmentTree* tree;
int m, n;
};
/**
* 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);
*/