|
| 1 | +```cpp |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <vector> |
| 5 | +#include <algorithm> |
| 6 | +#include <cmath> |
| 7 | +using namespace std; |
| 8 | +using ll = long long; |
| 9 | + |
| 10 | +int D[32]{1}; |
| 11 | + |
| 12 | +int sol(int d, int s, int e); |
| 13 | +int ans(int x, int y); |
| 14 | + |
| 15 | +int main() |
| 16 | +{ |
| 17 | + cin.tie(0)->sync_with_stdio(0); |
| 18 | + |
| 19 | + for (int i = 1; i < 32; i++) D[i] = D[i - 1] + i; |
| 20 | + int T, x, y; |
| 21 | + for (cin >> T; T--;) { |
| 22 | + cin >> x >> y; |
| 23 | + cout << ans(x, y) << '\n'; |
| 24 | + } |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | +int J(int x) { |
| 29 | + if (x <= 1) return x; |
| 30 | + int lg = log2(x); |
| 31 | + if (x == (1 << (lg + 1)) - 1) return lg + 1; |
| 32 | + if (x == (1 << (lg + 1)) - 2) return lg * 2; |
| 33 | + return lg + J(x - (1 << lg) + 1); |
| 34 | +} |
| 35 | + |
| 36 | +int sol(int d, int s, int e) { |
| 37 | + if (s == e) return J(s + (1 << d) - 1); |
| 38 | + if (e == (1 << d)) { |
| 39 | + int res = max(d * 2, d + 1); |
| 40 | + if (s == (1 << d) - 1) return res; |
| 41 | + return max(res, d + ans(s, e - 2)); |
| 42 | + } |
| 43 | + return d + ans(s, e); |
| 44 | +} |
| 45 | + |
| 46 | +int ans(int x, int y) { |
| 47 | + if (y <= 5) { |
| 48 | + int arr[6] = { 0,1,2,2,3,4 }; |
| 49 | + int mx = 0; |
| 50 | + for (int i = x; i <= y; i++) mx = max(mx, arr[i]); |
| 51 | + return mx; |
| 52 | + } |
| 53 | + int depX = log2(x); |
| 54 | + int depY = log2(y); |
| 55 | + int idxX = x - (1 << depX) + 1; |
| 56 | + int idxY = y - (1 << depY) + 1; |
| 57 | + if (depY - depX > 1) return max(D[depY - 1], sol(depY, 1, idxY)); |
| 58 | + if (depY - depX == 1) return max(sol(depY, 1, idxY), sol(depX, idxX, (1 << depX))); |
| 59 | + return sol(depX, idxX, idxY); |
| 60 | +} |
| 61 | + |
| 62 | +``` |
0 commit comments