File size: 4,779 Bytes
1fd0050 | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | #include <bits/stdc++.h>
using namespace std;
struct Node {
vector<pair<int,int>> out; // (to, weight)
};
static int bitlen(int x) {
return 32 - __builtin_clz((unsigned)x);
}
static vector<int> toBits(int x, int k) {
vector<int> b(k);
for (int i = 0; i < k; i++) b[i] = (x >> (k - 1 - i)) & 1;
return b;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int L, R;
cin >> L >> R;
int maxLen = bitlen(R);
vector<Node> nodes(1); // 1-indexed
auto newNode = [&]() -> int {
nodes.push_back(Node{});
return (int)nodes.size() - 1;
};
int start = newNode();
int end = newNode();
vector<int> Gen(maxLen, -1); // Gen[t] generates exactly t bits then reaches end
Gen[0] = end;
for (int t = 1; t < maxLen; t++) {
int id = newNode();
Gen[t] = id;
nodes[id].out.push_back({Gen[t-1], 0});
nodes[id].out.push_back({Gen[t-1], 1});
}
auto addEdge = [&](int u, int v, int w) {
nodes[u].out.push_back({v, w});
};
auto buildGE = [&](int k, const vector<int>& aBits) -> int {
vector<int> memo(k + 1, -1);
function<int(int)> dfs = [&](int pos) -> int {
int rem = k - pos;
if (rem == 0) return end;
int &res = memo[pos];
if (res != -1) return res;
int id = newNode();
res = id;
int lb = aBits[pos];
if (lb == 0) {
addEdge(id, dfs(pos + 1), 0);
addEdge(id, Gen[rem - 1], 1);
} else {
addEdge(id, dfs(pos + 1), 1);
}
return id;
};
return dfs(1);
};
auto buildLE = [&](int k, const vector<int>& bBits) -> int {
vector<int> memo(k + 1, -1);
function<int(int)> dfs = [&](int pos) -> int {
int rem = k - pos;
if (rem == 0) return end;
int &res = memo[pos];
if (res != -1) return res;
int id = newNode();
res = id;
int hb = bBits[pos];
if (hb == 0) {
addEdge(id, dfs(pos + 1), 0);
} else {
addEdge(id, Gen[rem - 1], 0);
addEdge(id, dfs(pos + 1), 1);
}
return id;
};
return dfs(1);
};
auto buildBetween = [&](int k, const vector<int>& aBits, const vector<int>& bBits) -> int {
static int memo[25][2][2];
for (int i = 0; i <= k; i++)
for (int tl = 0; tl < 2; tl++)
for (int th = 0; th < 2; th++)
memo[i][tl][th] = -1;
function<int(int,int,int)> dfs = [&](int pos, int tL, int tH) -> int {
int rem = k - pos;
if (rem == 0) return end;
if (!tL && !tH) return Gen[rem];
int &res = memo[pos][tL][tH];
if (res != -1) return res;
int id = newNode();
res = id;
int lo = tL ? aBits[pos] : 0;
int hi = tH ? bBits[pos] : 1;
for (int x = lo; x <= hi; x++) {
int ntL = tL && (x == aBits[pos]);
int ntH = tH && (x == bBits[pos]);
int to = dfs(pos + 1, ntL, ntH);
addEdge(id, to, x);
}
return id;
};
return dfs(1, 1, 1);
};
int lenL = bitlen(L), lenR = bitlen(R);
for (int k = lenL; k <= lenR; k++) {
long long minK = 1LL << (k - 1);
long long maxK = (1LL << k) - 1;
long long low = max<long long>(L, minK);
long long high = min<long long>(R, maxK);
if (low > high) continue;
int entry = -1;
if (low == minK && high == maxK) {
entry = Gen[k - 1];
} else if (high == maxK) {
auto aBits = toBits((int)low, k);
entry = buildGE(k, aBits);
} else if (low == minK) {
auto bBits = toBits((int)high, k);
entry = buildLE(k, bBits);
} else {
auto aBits = toBits((int)low, k);
auto bBits = toBits((int)high, k);
entry = buildBetween(k, aBits, bBits);
}
addEdge(start, entry, 1);
}
int n = (int)nodes.size() - 1;
if (n > 100) {
// Should not happen with this construction for given constraints.
// If it does, fall back to a trivial (but invalid by constraints) output to avoid UB.
// However, expected never to trigger.
return 0;
}
cout << n << "\n";
for (int i = 1; i <= n; i++) {
cout << nodes[i].out.size();
for (auto [to, w] : nodes[i].out) {
cout << ' ' << to << ' ' << w;
}
cout << "\n";
}
return 0;
} |