File tree 1 file changed +61
-0
lines changed
1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution{
2
+ private:
3
+ long long cnts[3][64];
4
+ public:
5
+ void precompute()
6
+ {
7
+ // build searching table
8
+ cnts[0][63] = cnts[1][63] = cnts[2][63] = 0ll;
9
+
10
+ // Code Here
11
+ for (int i = 0; i < 63; i++) {
12
+ cnts[0][i] = i + 1;
13
+
14
+ if (i < 2) {
15
+ cnts[1][i] = 0;
16
+ } else {
17
+ cnts[1][i] = i - 1 + cnts[1][i-1];
18
+ }
19
+
20
+ if (i < 3) {
21
+ cnts[2][i] = 0;
22
+ } else {
23
+ cnts[2][i] = cnts[1][i-1] + cnts[2][i-1];
24
+ }
25
+ }
26
+ }
27
+
28
+ long long solve(long long l, long long r){
29
+ // Code Here
30
+ long long res = 0;
31
+
32
+ if (l < 1 || r < l || r > 1000000000000000000) {
33
+ return -1;
34
+ }
35
+
36
+ long long l_cnt = 0, r_cnt = 0;
37
+
38
+ // [l, r] ~ (l-1, r]
39
+ l--;
40
+
41
+ // searching all possible 3bits numbers from table
42
+ for (int i = 2; i >= 0; i--) {
43
+ if (l > 0) {
44
+ int cl = 63 - __builtin_clzll(l);
45
+ l_cnt += cnts[i][cl];
46
+
47
+ l ^= 1ll << cl;
48
+ }
49
+
50
+ if (r > 0) {
51
+ int cr = 63 - __builtin_clzll(r);
52
+ r_cnt += cnts[i][cr];
53
+
54
+ r ^= 1ll << cr;
55
+ }
56
+ }
57
+
58
+ return (r_cnt - l_cnt);
59
+ }
60
+
61
+ };
You can’t perform that action at this time.
0 commit comments