-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.h
68 lines (60 loc) · 1.77 KB
/
solution.h
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
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
template <class T>
auto vect(const T& v, int n) { return vector<T>(n, v); }
template <class T, class... D>
auto vect(const T& v, int n, D... m) {
return vector<decltype(vect(v, m...))>(n, vect(v, m...));
}
template <typename T>
static constexpr T inf = numeric_limits<T>::max() / 2;
mt19937_64 mrand(random_device{}());
long long rnd(long long x) { return mrand() % x; }
int lg2(long long x) { return sizeof(long long) * 8 - 1 - __builtin_clzll(x); }
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef pair<int, int> pii;
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
class Solution {
public:
vector<int> b;
int n;
Solution(int _n, vector<int>& blacklist) {
sort(blacklist.begin(), blacklist.end());
b = blacklist;
b.push_back(_n);
n = _n;
}
int pick() {
int num = n - b.size() + 1;
int idx = rnd(num) + 1;
int l = 0, r = b.size() - 1;
int ans = -1;
while (l <= r) {
int m = l + r >> 1;
int val = b[m] - m;
if (val >= idx) {
ans = m;
r = m - 1;
} else {
l = m + 1;
}
}
int val = b[ans] - ans - idx;
return b[ans] - 1 - val;
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(n, blacklist);
* int param_1 = obj->pick();
*/