Skip to content

Commit 9745d44

Browse files
committed
Runtime: 390 ms (Top 96.30%) | Memory: 112.1 MB (Top 64.81%)
1 parent bc750b2 commit 9745d44

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1+
// Runtime: 390 ms (Top 96.30%) | Memory: 112.1 MB (Top 64.81%)
12
class Solution {
23
public:
34
vector<bitset<101>> tree;
45
vector<int> minDifference(vector<int>& nums, vector<vector<int>>& queries) {
56
int n = nums.size();
6-
7+
78
tree = vector<bitset<101>>(4*(n+1) + 1);
89
buildtree(1, 0, n-1, nums);
9-
10+
1011
vector<int> ans;
1112
for(auto &e: queries){
1213
auto finalOnesRepresentations = query(1, 0, n-1, e[0], e[1]);
13-
14+
1415
// find the first 1
1516
int i = 0;
1617
while(i < 101 and finalOnesRepresentations[i] != 1){
1718
i++;
1819
}
19-
20+
2021
int gap = INT_MAX;
2122
int prev = i;
2223
i++;
@@ -27,37 +28,37 @@ class Solution {
2728
prev = i;
2829
}
2930
}
30-
ans.push_back(gap == INT_MAX ? -1 : gap);
31+
ans.push_back(gap == INT_MAX ? -1 : gap);
3132
}
3233
return ans;
3334
}
34-
35+
3536
bitset<101> query(int index, int s, int e, int qs, int qe){
36-
37+
3738
if(s > e) return bitset<101>();
3839
if(e < qs || s > qe) return bitset<101>();
3940
if(qs <= s and e <= qe) return tree[index];
40-
41+
4142
int mid = (s+e)/2;
4243
auto left = query(index*2, s, mid, qs, qe);
4344
auto right = query(index*2+1, mid+1, e, qs , qe);
4445
return left | right;
4546
}
46-
47+
4748
void buildtree(int index, int s, int e, vector<int>& a){
48-
49+
4950
if(s > e)return;
50-
51+
5152
if(s==e){
5253
bitset<101> b;
5354
b[a[s]] = 1;
5455
tree[index] = b;
5556
return;
5657
}
57-
58+
5859
int mid = (s+e)/2;
5960
buildtree(index*2, s, mid, a);
6061
buildtree(index*2+1, mid+1, e, a);
6162
tree[index] = tree[index*2] | tree[index*2+1];
6263
}
63-
};
64+
};

0 commit comments

Comments
 (0)