Skip to content

Commit e3a5551

Browse files
authored
Create block-placement-queries.cpp
1 parent 320bda0 commit e3a5551

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

C++/block-placement-queries.cpp

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Time: O(qlogq)
2+
// Space: O(q)
3+
4+
// bst, bit, fenwick tree
5+
class Solution {
6+
public:
7+
vector<bool> getResults(vector<vector<int>>& queries) {
8+
set<int> bst;
9+
for (const auto& q: queries) {
10+
if (q[0] == 1) {
11+
bst.emplace(q[1]);
12+
}
13+
}
14+
unordered_map<int, int> val_to_idx;
15+
int i = 0;
16+
for (const auto& x : bst) {
17+
val_to_idx[x] = i++;
18+
}
19+
const auto& fn = [](const auto& a, const auto& b) {
20+
return max(a, b);
21+
};
22+
23+
BIT<int64_t> bit(size(val_to_idx), 0, fn);
24+
for (auto it = begin(bst); it != end(bst); ++it) {
25+
bit.update(val_to_idx[*it], *it - (it != begin(bst) ? *prev(it) : 0));
26+
}
27+
vector<bool> result;
28+
for (int i = size(queries) - 1; i >= 0; --i) {
29+
auto it = bst.lower_bound(queries[i][1]);
30+
if (queries[i][0] == 1) {
31+
if (next(it) != end(bst)) {
32+
bit.update(val_to_idx[*next(it)], *next(it) - (it != begin(bst) ? *prev(it) : 0));
33+
}
34+
bst.erase(it);
35+
} else {
36+
result.emplace_back(queries[i][1] - (it != begin(bst) ? *prev(it) : 0) >= queries[i][2] ||
37+
(it != begin(bst) && bit.query(val_to_idx[*prev(it)]) >= queries[i][2]));
38+
}
39+
}
40+
reverse(begin(result), end(result));
41+
return result;
42+
}
43+
44+
private:
45+
template<typename T>
46+
class BIT {
47+
public:
48+
BIT(int n, T val, const function<T (T, T)> fn)
49+
: bit_(n + 1, val),
50+
fn_(fn) { // 0-indexed
51+
}
52+
53+
void update(int i, T val) {
54+
++i;
55+
for (; i < size(bit_); i += lower_bit(i)) {
56+
bit_[i] = fn_(bit_[i], val);
57+
}
58+
}
59+
60+
T query(int i) const {
61+
++i;
62+
auto total = bit_[0];
63+
for (; i > 0; i -= lower_bit(i)) {
64+
total = fn_(total, bit_[i]);
65+
}
66+
return total;
67+
}
68+
69+
private:
70+
int lower_bit(int i) const {
71+
return i & -i;
72+
}
73+
74+
vector<T> bit_;
75+
const function<T (T, T)> fn_;
76+
};
77+
};
78+
79+
// Time: O(qlogq)
80+
// Space: O(q)
81+
// bst, segment tree
82+
class Solution2 {
83+
public:
84+
vector<bool> getResults(vector<vector<int>>& queries) {
85+
set<int> bst;
86+
for (const auto& q: queries) {
87+
if (q[0] == 1) {
88+
bst.emplace(q[1]);
89+
}
90+
}
91+
unordered_map<int, int> val_to_idx;
92+
int i = 0;
93+
for (const auto& x : bst) {
94+
val_to_idx[x] = i++;
95+
}
96+
SegmentTree st(size(val_to_idx));
97+
const auto& update = [&](const auto& x) {
98+
bst.emplace(x);
99+
auto it = bst.find(x);
100+
st.update(val_to_idx[x], x - (it != begin(bst) ? *prev(it) : 0));
101+
if (next(it) != end(bst)) {
102+
st.update(val_to_idx[*next(it)], *next(it) - x);
103+
}
104+
};
105+
106+
vector<bool> result;
107+
bst.clear();
108+
for (int i = 0; i < size(queries); ++i) {
109+
if (queries[i][0] == 1) {
110+
update(queries[i][1]);
111+
} else {
112+
auto it = bst.lower_bound(queries[i][1]);
113+
result.emplace_back(queries[i][1] - (it != begin(bst) ? *prev(it) : 0) >= queries[i][2] ||
114+
(it != begin(bst) && st.query(0, val_to_idx[*prev(it)]) >= queries[i][2]));
115+
}
116+
}
117+
return result;
118+
}
119+
120+
private:
121+
class SegmentTree {
122+
public:
123+
explicit SegmentTree(int N)
124+
: tree(N > 1 ? 1 << (__lg(N - 1) + 2) : 2),
125+
base(N > 1 ? 1 << (__lg(N - 1) + 1) : 1) {
126+
}
127+
128+
void update(int i, const auto& h) {
129+
int x = base + i;
130+
tree[x] = h;
131+
while (x > 1) {
132+
x >>= 1;
133+
tree[x] = max(tree[x << 1], tree[(x << 1) + 1]);
134+
}
135+
}
136+
137+
int64_t query(int L, int R) {
138+
L += base;
139+
R += base;
140+
int64_t result = 0;
141+
for (; L <= R; L >>= 1, R >>= 1) {
142+
if (L & 1) {
143+
result = max(result, tree[L]);
144+
++L;
145+
}
146+
if ((R & 1) == 0) {
147+
result = max(result, tree[R]);
148+
--R;
149+
}
150+
}
151+
return result;
152+
}
153+
154+
vector<int64_t> tree;
155+
const int base;
156+
};
157+
};

0 commit comments

Comments
 (0)