-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_Block_Placement_Queries.cpp
More file actions
61 lines (50 loc) · 1.62 KB
/
Copy pathLeetCode_Block_Placement_Queries.cpp
File metadata and controls
61 lines (50 loc) · 1.62 KB
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
//Problem Link: https://leetcode.com/problems/block-placement-queries/
class Solution {
public:
static const int maxN = 50001;
vector<int> tree;
int query(int v, int tl, int tr, int l, int r){
if(l>r) return 0;
if(l==tl and r==tr) return tree[v];
int tm = (tl+tr)/2;
return max(query(v*2, tl, tm, l, min(r,tm)), query(v*2+1, tm+1, tr, max(l,tm+1), r));
}
void update(int v, int tl, int tr, int pos, int newVal){
if(tl == tr) tree[v] = newVal;
else{
int tm = (tl+tr)/2;
if(pos<=tm) update(v*2,tl, tm, pos,newVal);
else update(v*2+1,tm+1,tr,pos,newVal);
tree[v] = max(tree[v*2], tree[v*2+1]);
}
}
vector<bool> getResults(vector<vector<int>>& q) {
set<int> st;
st.insert(0);
st.insert(maxN-1);
tree.assign(4*maxN, 0);
update(1, 0, maxN-1, maxN-1, maxN-1);
vector<bool> result;
for(auto it: q){
if(it[0] == 1){
int x = it[1];
auto itr = st.upper_bound(x);
int r = *itr;
int l = *prev(itr);
update(1, 0, maxN-1, x, x-l);
update(1, 0, maxN-1, r, r-x);
st.insert(x);
}
else{
int x = it[1];
int sz = it[2];
auto itr = st.upper_bound(x);
int l = *prev(itr);
int mxSz = query(1, 0, maxN-1, 0, l);
mxSz = max(mxSz, x-l);
result.push_back(mxSz >= sz);
}
}
return result;
}
};