题面
来源:USACO 官网。模型收到的是同一份题面的文字。
There is a line of cows, initially (i.e. at time $t = 0$) containing only cow $0$ at position $0$ (here, a cow is at position $k$ if there are $k$ cows in front of it). At time $t$ for $t=1,2,3,\dots$, the cow at position 0 moves to position $\lfloor t/2\rfloor$, every cow in positions $1\dots \lfloor t/2\rfloor$ moves forward one position, and cow $t$ joins the line at the end of the line (position $t$).
Answer $Q$ ($1\le Q\le 10^5$) independent queries each of the following form:
- Out of cows $l_1\dots r_1$, how many are located at positions $l_2\dots r_2$ immediately after time $t$? ($0\le l_1\le r_1\le t, 0\le l_2\le r_2 \le t, t\le 10^{18}$)
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $Q$, the number of queries.The next $Q$ lines each contain five integers specifying a query of the form "$l_1$ $r_1$ $l_2$ $r_2$ $t$."
OUTPUT FORMAT (print output to the terminal / stdout):
Output the answer to each query on a separate line.
SAMPLE INPUT:
4 0 9 0 9 9 3 5 4 5 9 4 5 3 5 9 1 1 3 3 9
SAMPLE OUTPUT:
10 2 1 1
Lineups at various times:
t = 0 | 0 t = 1 | 0 1 t = 2 | 1 0 2 t = 3 | 0 1 2 3 t = 4 | 1 2 0 3 4 t = 5 | 2 0 1 3 4 5 t = 6 | 0 1 3 2 4 5 6 t = 7 | 1 3 2 0 4 5 6 7 t = 8 | 3 2 0 4 1 5 6 7 8 t = 9 | 2 0 4 1 3 5 6 7 8 9
At $t=9$ the cows from front to back are $[2,0,4,1,3,5,6,7,8,9]$.
To answer the third query, the cows at positions $3\dots 5$ are $[1,3,5]$, and only one of them is in the range $4\dots 5$.
SAMPLE INPUT:
1 0 1000000000000000000 0 1000000000000000000 1000000000000000000
SAMPLE OUTPUT:
1000000000000000001
SCORING:
- Input 3: $Q\le 1000, t\le 100$
- Inputs 4-7: $l_1 = r_1$ for all queries
- Inputs 8-14: $r_1 \leq 2 \cdot l_1$ for all queries
- Inputs 15-21: No additional constraints
Problem credits: Agastya Goel and Benjamin Qi
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 15.8k平均输出 token(含思考) ↓
- 27.4输出速度 tokens/s ↑
- 557 秒延迟 ↓
- 0.31×CPU 时间 / 官方解 ↓
- 同阶 2、更优 1时间复杂度(相对官方解)
Kimi K3.1 Pro(high)Kimi high
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 15.8k平均输出 token(含思考) ↓
- 27.4输出速度 tokens/s ↑
- 557 秒延迟 ↓
- 0.31×CPU 时间 / 官方解 ↓
- 同阶 2、更优 1时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 55.0k平均输出 token(含思考) ↓
- 127.3输出速度 tokens/s ↑
- 397 秒延迟 ↓
- 0.41×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
2/3
解出的采样 ↑
- 28.6采样 1 · 答案错误
- 100采样 2 · 解出
- 100采样 3 · 解出
- 51.0k平均输出 token(含思考) ↓
- 119.9输出速度 tokens/s ↑
- 450 秒延迟 ↓
- 0.20×CPU 时间 / 官方解 ↓
- 更优 2时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; using int64 = long long; // f(r) is the next time at which a cow returns to position 0, // given that its previous return was at time r. static inline int64 next_reset(int64 r) { return (3 * (r + 1)) / 2; } // Minimum r such that f(r) >= y. static inline int64 inverse_threshold(int64 y) { if (y <= 0) return 0; return (2 * y + 2) / 3 - 1; } // Minimum k >= 1 such that 3*k - 1 >= x. static inline int64 min_cow(int64 x) { return (x + 3) / 3; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; if (t == 0) { cout << ((l1 == 0 && l2 == 0) ? 1 : 0) << '\n'; continue; } int64 answer = 0; // Cows which have never entered the moving prefix remain at // position equal to their cow number. int64 active_max = (t + 1) / 2; int64 lo = max({l1, active_max + 1, l2}); int64 hi = min({r1, t, r2}); if (lo <= hi) answer += hi - lo + 1; // Cow 0 has the same reset-time process, starting at time 0. int64 reset = 0; while (reset < t) reset = next_reset(reset); int64 pos0 = reset - t; if (l1 == 0 && l2 <= pos0 && pos0 <= r2) ++answer; if (r1 >= 1 && l1 <= active_max) { // For cow k >= 1, write x = 3*k - 1. Its reset times are // x, f(x), f^2(x), ... . At time t, if f^i(x) is the first // reset time >= t, its position is f^i(x) - t. // // b, c, d are inverse thresholds: // b: minimum initial x with f^i(x) >= t // c: minimum initial x with f^i(x) >= t+l2 // d: minimum initial x with f^i(x) >= t+r2+1 int64 b = t; int64 c = t + l2; int64 d = t + r2 + 1; int64 previous_b = 0; for (int i = 0;; ++i) { int64 lower = max({int64(1), l1, min_cow(b), min_cow(c)}); int64 upper = min({active_max, r1, d / 3}); // For i >= 1, this group also requires // f^(i-1)(x) < t, equivalently x < previous_b. if (i > 0) upper = min(upper, previous_b / 3); if (lower <= upper) answer += upper - lower + 1; // Once min_cow(b) == 1, this is the final group and all // remaining active cows have been covered. if (min_cow(b) <= 1) break; previous_b = b; b = inverse_threshold(b); c = inverse_threshold(c); d = inverse_threshold(d); } } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; static inline int64 nextFront(int64 f) { return (3 * f + 3) / 2; } // Number of n in [a,b] whose associated cow lies in [L,R]. int64 countTimes(int64 a, int64 b, int64 L, int64 R) { if (a > b || L > R) return 0; int64 result = 0; // Cow 0 is represented by the base case n = 0. if (L == 0 && a == 0) ++result; // Initial front times are n = 3*c - 1 for cows c >= 1. int64 cowLo = max<int64>(1, L); int64 cowHi = R; cowLo = max(cowLo, (a + 3) / 3); cowHi = min(cowHi, (b + 1) / 3); if (cowLo <= cowHi) result += cowHi - cowLo + 1; if (b == 0) return result; // All other values n arise as nextFront(f). Since nextFront is // increasing, the possible f values form one interval. int64 lo = max<int64>(0, 2 * a / 3 - 2); while (nextFront(lo) < a) ++lo; int64 hi = 2 * b / 3 + 2; while (nextFront(hi) > b) --hi; if (lo <= hi) result += countTimes(lo, hi, L, R); return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; int64 m = t / 2; int64 answer = 0; // Cows 0..m occupy positions 0..m. For such a cow at position p, // set n = t+p and recursively determine its cow number. int64 L = l1, R = min(r1, m); int64 pLo = l2, pHi = min(r2, m); if (L <= R && pLo <= pHi) { answer += countTimes(t + pLo, t + pHi, L, R); } // Every cow c > m is still at position c. int64 a = max(max(l1, m + 1), l2); int64 b = min(min(r1, t), r2); if (a <= b) answer += b - a + 1; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; // Number of integers s in [0, n] for which the cow at the front // at time s has a label in [a, b]. int64 prefixCount(int64 n, int64 a, int64 b) { if (n < 0) return 0; int64 result = 0; const bool includeZero = (a == 0); while (n >= 0) { if (includeZero) ++result; if (n == 0) break; int64 k = (n + 1) / 3; int64 lo = max<int64>(1, a); int64 hi = min(k, b); if (lo <= hi) result += hi - lo + 1; n = 2 * (n - 1) / 3; } return result; } int64 rangeCount(int64 l, int64 r, int64 a, int64 b) { return prefixCount(r, a, b) - prefixCount(l - 1, a, b); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; const int64 h = t / 2; int64 answer = 0; // Cows 0..h form the nontrivial prefix of the line. int64 cowL = l1, cowR = min(r1, h); int64 posL = l2, posR = min(r2, h); if (cowL <= cowR && posL <= posR) { // The cow at position p is the cow that was/will be at // the front at time t+p. answer += rangeCount(t + posL, t + posR, cowL, cowR); } // Every cow i>h remains at position i. int64 lo = max(max(l1, l2), h + 1); int64 hi = min(r1, r2); if (lo <= hi) answer += hi - lo + 1; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; // f(r) is the next time at which a cow returns to position 0, // given that its previous return was at time r. static inline int64 next_reset(int64 r) { return (3 * (r + 1)) / 2; } // Minimum r such that f(r) >= y. static inline int64 inverse_threshold(int64 y) { if (y <= 0) return 0; return (2 * y + 2) / 3 - 1; } // Minimum k >= 1 such that 3*k - 1 >= x. static inline int64 min_cow(int64 x) { return (x + 3) / 3; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; if (t == 0) { cout << ((l1 == 0 && l2 == 0) ? 1 : 0) << '\n'; continue; } int64 answer = 0; // Cows which have never entered the moving prefix remain at // position equal to their cow number. int64 active_max = (t + 1) / 2; int64 lo = max({l1, active_max + 1, l2}); int64 hi = min({r1, t, r2}); if (lo <= hi) answer += hi - lo + 1; // Cow 0 has the same reset-time process, starting at time 0. int64 reset = 0; while (reset < t) reset = next_reset(reset); int64 pos0 = reset - t; if (l1 == 0 && l2 <= pos0 && pos0 <= r2) ++answer; if (r1 >= 1 && l1 <= active_max) { // For cow k >= 1, write x = 3*k - 1. Its reset times are // x, f(x), f^2(x), ... . At time t, if f^i(x) is the first // reset time >= t, its position is f^i(x) - t. // // b, c, d are inverse thresholds: // b: minimum initial x with f^i(x) >= t // c: minimum initial x with f^i(x) >= t+l2 // d: minimum initial x with f^i(x) >= t+r2+1 int64 b = t; int64 c = t + l2; int64 d = t + r2 + 1; int64 previous_b = 0; for (int i = 0;; ++i) { int64 lower = max({int64(1), l1, min_cow(b), min_cow(c)}); int64 upper = min({active_max, r1, d / 3}); // For i >= 1, this group also requires // f^(i-1)(x) < t, equivalently x < previous_b. if (i > 0) upper = min(upper, previous_b / 3); if (lower <= upper) answer += upper - lower + 1; // Once min_cow(b) == 1, this is the final group and all // remaining active cows have been covered. if (min_cow(b) <= 1) break; previous_b = b; b = inverse_threshold(b); c = inverse_threshold(c); d = inverse_threshold(d); } } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; static inline int64 nextFront(int64 f) { return (3 * f + 3) / 2; } // Number of n in [a,b] whose associated cow lies in [L,R]. int64 countTimes(int64 a, int64 b, int64 L, int64 R) { if (a > b || L > R) return 0; int64 result = 0; // Cow 0 is represented by the base case n = 0. if (L == 0 && a == 0) ++result; // Initial front times are n = 3*c - 1 for cows c >= 1. int64 cowLo = max<int64>(1, L); int64 cowHi = R; cowLo = max(cowLo, (a + 3) / 3); cowHi = min(cowHi, (b + 1) / 3); if (cowLo <= cowHi) result += cowHi - cowLo + 1; if (b == 0) return result; // All other values n arise as nextFront(f). Since nextFront is // increasing, the possible f values form one interval. int64 lo = max<int64>(0, 2 * a / 3 - 2); while (nextFront(lo) < a) ++lo; int64 hi = 2 * b / 3 + 2; while (nextFront(hi) > b) --hi; if (lo <= hi) result += countTimes(lo, hi, L, R); return result; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; int64 m = t / 2; int64 answer = 0; // Cows 0..m occupy positions 0..m. For such a cow at position p, // set n = t+p and recursively determine its cow number. int64 L = l1, R = min(r1, m); int64 pLo = l2, pHi = min(r2, m); if (L <= R && pLo <= pHi) { answer += countTimes(t + pLo, t + pHi, L, R); } // Every cow c > m is still at position c. int64 a = max(max(l1, m + 1), l2); int64 b = min(min(r1, t), r2); if (a <= b) answer += b - a + 1; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; // Number of integers s in [0, n] for which the cow at the front // at time s has a label in [a, b]. int64 prefixCount(int64 n, int64 a, int64 b) { if (n < 0) return 0; int64 result = 0; const bool includeZero = (a == 0); while (n >= 0) { if (includeZero) ++result; if (n == 0) break; int64 k = (n + 1) / 3; int64 lo = max<int64>(1, a); int64 hi = min(k, b); if (lo <= hi) result += hi - lo + 1; n = 2 * (n - 1) / 3; } return result; } int64 rangeCount(int64 l, int64 r, int64 a, int64 b) { return prefixCount(r, a, b) - prefixCount(l - 1, a, b); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; const int64 h = t / 2; int64 answer = 0; // Cows 0..h form the nontrivial prefix of the line. int64 cowL = l1, cowR = min(r1, h); int64 posL = l2, posR = min(r2, h); if (cowL <= cowR && posL <= posR) { // The cow at position p is the cow that was/will be at // the front at time t+p. answer += rangeCount(t + posL, t + posR, cowL, cowR); } // Every cow i>h remains at position i. int64 lo = max(max(l1, l2), h + 1); int64 hi = min(r1, r2); if (lo <= hi) answer += hi - lo + 1; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int THRESH = 200; vector<vector<long long>> smallPerm; void precomputeSmall() { smallPerm.assign(THRESH + 1, {}); if (THRESH >= 1) smallPerm[1] = {0}; if (THRESH >= 2) smallPerm[2] = {0, 1}; for (int n = 3; n <= THRESH; n++) { auto &prev = smallPerm[n - 1]; auto &cur = smallPerm[n]; cur.reserve(n); // C_n = C_{n-1}[2..] + [n-1, C_{n-1}[0], C_{n-1}[1]] for (int i = 2; i < (int)prev.size(); i++) cur.push_back(prev[i]); cur.push_back(n - 1); cur.push_back(prev[0]); cur.push_back(prev[1]); } } // Count entries of C_N whose value is in [vl, vr] and whose position is in [pl, pr]. // odd = 1: A_t = C_N // odd = 0: A_t = B_N, where B_N is C_N rotated right by one. long long solvePrefix(long long N, long long vl, long long vr, long long pl, long long pr, int odd) { if (N <= 0) return 0; if (vl < 0) vl = 0; if (vr > N - 1) vr = N - 1; if (vl > vr) return 0; if (pl < 0) pl = 0; if (pr > N - 1) pr = N - 1; if (pl > pr) return 0; if (N <= THRESH) { auto &v = smallPerm[N]; long long ans = 0; if (odd) { for (long long pos = pl; pos <= pr; pos++) { long long val = v[pos]; if (vl <= val && val <= vr) ans++; } } else { for (long long pos = pl; pos <= pr; pos++) { long long val; if (pos == 0) val = v[N - 1]; else val = v[pos - 1]; if (vl <= val && val <= vr) ans++; } } return ans; } const int MAXD = 300; long long Ns[MAXD]; long long suf[MAXD][6]; int sufLen[MAXD]; int D = 0; Ns[0] = N; while (Ns[D] > THRESH) { long long cur = Ns[D]; long long nxt = cur - cur / 3; Ns[++D] = nxt; } // Base suffix. { long long Nb = Ns[D]; auto &v = smallPerm[Nb]; int len = (int)v.size(); int L = min(6, len); sufLen[D] = L; for (int i = 0; i < L; i++) { suf[D][i] = v[len - L + i]; } } // Compute last 6 values of every C_{Ns[j]}. for (int j = D - 1; j >= 0; j--) { long long cur = Ns[j]; long long r = cur % 3; long long vals[6]; // vals[o] = element at offset o from the end for (int o = 0; o < 6; o++) { int t = o + 1; if (t % 3 == 0) { vals[o] = cur - t / 3; } else if (t % 3 == 1) { int s = (t - 1) / 3; int childOffset = (int)r + 2 * s; int idx = sufLen[j + 1] - 1 - childOffset; vals[o] = suf[j + 1][idx]; } else { int s = (t - 2) / 3; int childOffset = (int)r + 2 * s + 1; int idx = sufLen[j + 1] - 1 - childOffset; vals[o] = suf[j + 1][idx]; } } sufLen[j] = 6; for (int idx = 0; idx < 6; idx++) { suf[j][idx] = vals[5 - idx]; // increasing order } } auto processC = [&](long long Lpos, long long Rpos) -> long long { if (Lpos < 0) Lpos = 0; if (Rpos > N - 1) Rpos = N - 1; if (Lpos > Rpos) return 0; long long ans = 0; long long curL = Lpos; long long curR = Rpos; for (int i = 0; i <= D; i++) { long long curN = Ns[i]; if (curL > curR) break; if (curN <= THRESH) { auto &v = smallPerm[curN]; for (long long pos = curL; pos <= curR; pos++) { long long val = v[pos]; if (vl <= val && val <= vr) ans++; } break; } long long k = curN / 3; long long r = curN % 3; long long n = Ns[i + 1]; // Special prefix: positions 0 .. r-1. for (int p = 0; p < (int)r; p++) { if (curL <= p && p <= curR) { int off = (int)r - 1 - p; int idx = sufLen[i + 1] - 1 - off; long long val = suf[i + 1][idx]; if (vl <= val && val <= vr) ans++; } } // New labels inside the block. long long Lblock = max(curL, r); long long Rblock = curR; if (Lblock <= Rblock) { long long lo = 0; long long hi = k - 1; long long tL = Lblock - r; long long iL = (tL + 2) / 3; long long iR = (Rblock - r) / 3; lo = max(lo, iL); hi = min(hi, iR); lo = max(lo, vl - n); hi = min(hi, vr - n); if (lo <= hi) ans += hi - lo + 1; } // Old part of the block. long long y = curL - r - 1; long long pL; if (y <= 0) pL = 0; else pL = (2 * y + 2) / 3; long long z = curR - r - 1; long long pR; if (z < 0) pR = -1; else pR = (2 * z + 1) / 3; long long maxP = 2 * k - 1; if (pL < 0) pL = 0; if (pR > maxP) pR = maxP; if (pL > maxP) pL = maxP + 1; if (pL > pR) break; curL = pL; curR = pR; } return ans; }; if (odd) { return processC(pl, pr); } else { long long ans = 0; // B[0] = C[N-1] if (pl <= 0 && 0 <= pr) { ans += processC(N - 1, N - 1); } // B[i] = C[i-1] for i >= 1 long long L = max(1LL, pl); long long R = pr; if (L <= R) { ans += processC(L - 1, R - 1); } return ans; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); precomputeSmall(); int Q; cin >> Q; while (Q--) { long long l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; long long m = t / 2; long long N = m + 1; long long ans = 0; // Prefix positions 0..m contain labels 0..m in A_t. long long vl = max(l1, 0LL); long long vr = min(r1, m); long long pl = max(l2, 0LL); long long pr = min(r2, m); if (vl <= vr && pl <= pr) { int odd = (t % 2 == 1); if (odd) { // A_t = C_N ans += solvePrefix(N, vl, vr, pl, pr, 1); } else { // A_t = B_N ans += solvePrefix(N, vl, vr, pl, pr, 0); } } // Suffix labels m+1..t stay at their own positions. long long sl = max({l1, m + 1, l2}); long long sr = min({r1, t, r2}); if (sl <= sr) { ans += sr - sl + 1; } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; static const int TH = 200; static const int K = 8; static const int MAXD = 200; vector<vector<long long>> smallB; struct Solver { long long N; int depth; long long ns[MAXD]; long long first[MAXD][K]; Solver(long long n) : N(n), depth(0) { if (N > TH) buildPath(); } static long long countRange(long long a, long long b, long long ql, long long qr) { if (a > b) return 0; long long L = max(a, ql); long long R = min(b, qr); if (L > R) return 0; return R - L + 1; } void buildPath() { long long cur = N; int len = 0; while (cur > TH) { ns[len++] = cur; long long M = cur - (cur % 3); cur = 2 * (M / 3); } ns[len] = cur; depth = len + 1; int baseIdx = depth - 1; long long baseN = ns[baseIdx]; for (int k = 0; k < K; k++) { if (k < (int)smallB[baseN].size()) first[baseIdx][k] = smallB[baseN][k]; else first[baseIdx][k] = -1; } for (int j = depth - 2; j >= 0; j--) { long long curN = ns[j]; int r = curN % 3; int d = 2 * r; long long L = ns[j + 1]; for (int k = 0; k < K; k++) { long long p = d + k; int s = p / 3; int t = p % 3; long long lab; if (t == 0) lab = first[j + 1][2 * s]; else if (t == 1) lab = L + s; else lab = first[j + 1][2 * s + 1]; first[j][k] = lab; } } } long long getBMlabel(int j, int k, long long L) const { int s = k / 3; int t = k % 3; if (t == 0) return first[j + 1][2 * s]; if (t == 1) return L + s; return first[j + 1][2 * s + 1]; } long long firstDcount(int j, int d, long long L, long long ql, long long qr) const { long long cnt = 0; for (int k = 0; k < d; k++) { int s = k / 3; int t = k % 3; long long lab; if (t == 0) lab = first[j + 1][2 * s]; else if (t == 1) lab = L + s; else lab = first[j + 1][2 * s + 1]; if (lab >= ql && lab <= qr) cnt++; } return cnt; } long long countSPrefix(int j, int r, long long M, long long L, long long remS, long long ql, long long qr) const { long long cnt = 0; for (int i = 0; i < remS; i++) { long long lab; if (r == 1) { if (i == 0) lab = getBMlabel(j, 0, L); else if (i == 1) lab = M; else lab = getBMlabel(j, 1, L); } else { // r == 2 if (i == 0) lab = getBMlabel(j, 0, L); else if (i == 1) lab = M; else if (i == 2) lab = getBMlabel(j, 1, L); else if (i == 3) lab = getBMlabel(j, 2, L); else if (i == 4) lab = M + 1; else lab = getBMlabel(j, 3, L); } if (lab >= ql && lab <= qr) cnt++; } return cnt; } long long evalPrefix(long long P, long long ql, long long qr) const { if (P <= 0) return 0; if (P > N) P = N; long long ans = 0; for (int j = 0; j < depth - 1; j++) { long long cur = ns[j]; int r = cur % 3; long long M = cur - r; int d = 2 * r; long long L = ns[j + 1]; if (r == 0) { long long Sfull = P / 3; int rem = P % 3; if (Sfull > 0) { ans += countRange(L, L + Sfull - 1, ql, qr); } if (rem >= 2) { long long lab = L + Sfull; if (lab >= ql && lab <= qr) ans++; } P = 2 * Sfull + (rem >= 1 ? 1 : 0); } else { long long lenBase = M - d; if (P <= lenBase) { long long PM = d + P; long long Sfull = PM / 3; int rem = PM % 3; if (Sfull > 0) { ans += countRange(L, L + Sfull - 1, ql, qr); } if (rem >= 2) { long long lab = L + Sfull; if (lab >= ql && lab <= qr) ans++; } ans -= firstDcount(j, d, L, ql, qr); P = 2 * Sfull + (rem >= 1 ? 1 : 0); } else { ans += countRange(0, M - 1, ql, qr); ans -= firstDcount(j, d, L, ql, qr); long long remS = P - lenBase; ans += countSPrefix(j, r, M, L, remS, ql, qr); return ans; } } } long long baseN = ns[depth - 1]; if (P > baseN) P = baseN; const vector<long long>& v = smallB[baseN]; for (int i = 0; i < P; i++) { if (v[i] >= ql && v[i] <= qr) ans++; } return ans; } long long countInterval(long long pl, long long pr, long long ql, long long qr) const { if (pl < 0) pl = 0; if (pr >= N) pr = N - 1; if (pl > pr) return 0; if (ql < 0) ql = 0; if (qr >= N) qr = N - 1; if (ql > qr) return 0; if (N <= TH) { long long cnt = 0; const vector<long long>& v = smallB[N]; for (long long i = pl; i <= pr; i++) { if (v[i] >= ql && v[i] <= qr) cnt++; } return cnt; } return evalPrefix(pr + 1, ql, qr) - evalPrefix(pl, ql, qr); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); smallB.assign(TH + 1, vector<long long>()); smallB[1] = {0}; if (TH >= 2) smallB[2] = {1, 0}; for (int n = 2; n < TH; n++) { vector<long long> v = smallB[n]; long long a = v[0]; long long b = v[1]; vector<long long> nv; nv.reserve(n + 1); for (int i = 2; i < n; i++) nv.push_back(v[i]); nv.push_back(a); nv.push_back(n); nv.push_back(b); smallB[n + 1] = move(nv); } int Q; cin >> Q; while (Q--) { long long l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; long long N = t / 2 + 1; long long ans = 0; long long a = max(l2, 0LL); long long b = min(r2, N - 1); if (a <= b) { Solver solver(N); if (t % 2 == 0) { ans += solver.countInterval(a, b, l1, r1); } else { // Active prefix is left rotation of B_N by one. if (b < N - 1) { ans += solver.countInterval(a + 1, b + 1, l1, r1); } else { if (a <= N - 2) { ans += solver.countInterval(a + 1, N - 1, l1, r1); } ans += solver.countInterval(0, 0, l1, r1); } } } // Suffix: positions N ... t contain labels equal to their positions. long long A = max(l2, N); long long B = min(r2, t); if (A <= B) { long long L = max(A, l1); long long R = min(B, r1); if (L <= R) ans += R - L + 1; } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; static inline int64 invF(int64 q) { // Maximum p >= 0 such that floor(3p/2) + 2 <= q if (q < 3) return 0; return (2 * q - 3) / 3; } static inline int64 countP0Interval( int64 lo, int64 hi, int64 L, int64 R, int64 m ) { // Count x in [L,R] ∩ [0,m-1] with lo < p0(x) <= hi, // where p0(0)=2, p0(1)=1, p0(x)=3x-2 for x>=2. if (lo >= hi) return 0; int64 ans = 0; if (L <= 0 && 0 <= R && m >= 1) { if (lo < 2 && 2 <= hi) ++ans; } if (L <= 1 && 1 <= R && m >= 2) { if (lo < 1 && 1 <= hi) ++ans; } int64 xL = max(L, 2LL); int64 xR = min(R, m - 1); if (xL <= xR) { // lo < 3x - 2 <= hi int64 lowX = (lo + 2) / 3 + 1; int64 highX = (hi + 2) / 3; int64 left = max(xL, lowX); int64 right = min(xR, highX); if (left <= right) ans += right - left + 1; } return ans; } // Count x in [L,R] whose position in P_m is in [a,b]. // P_m is the active permutation for even time 2(m-1). static int64 countP( int64 m, int64 L, int64 R, int64 a, int64 b ) { if (m <= 0 || L > R || a > b) return 0; L = max(L, 0LL); R = min(R, m - 1); a = max(a, 0LL); b = min(b, m - 1); if (L > R || a > b) return 0; if (m == 1) { return (L <= 0 && 0 <= R && a <= 0 && 0 <= b) ? 1 : 0; } // For m >= 2: // P_m is the suffix of length m of the generated sequence E. int64 S = 2 * m - 3; int64 N = 3 * m - 4; int64 A = a + S; int64 B = b + S; int64 cur = N; // H^k(N) int64 low = A - 1; // H^k(A-1) int64 high = B; // H^k(B) int64 ans = 0; while (cur > 0) { int64 nxt = invF(cur); int64 lo = max(nxt, low); int64 hi = min(cur, high); if (lo < hi) { ans += countP0Interval(lo, hi, L, R, m); } low = invF(low); high = invF(high); cur = nxt; if (low >= high) break; } return ans; } // For odd time, the active permutation is P_m rotated left by one. static int64 countPCyclic( int64 m, int64 L, int64 R, int64 a, int64 b ) { if (m <= 0 || L > R || a > b) return 0; a = max(a, 0LL); b = min(b, m - 1); if (a > b) return 0; int64 len = b - a + 1; int64 start = (a + 1) % m; int64 firstLen = min(len, m - start); int64 ans = 0; if (firstLen > 0) { ans += countP(m, L, R, start, start + firstLen - 1); } int64 rem = len - firstLen; if (rem > 0) { ans += countP(m, L, R, 0, rem - 1); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; // Active cows are 0 ... m-1, occupying positions 0 ... m-1. int64 m = t / 2 + 1; int64 ans = 0; // Active part. int64 L = max(l1, 0LL); int64 R = min(r1, m - 1); int64 a = max(l2, 0LL); int64 b = min(r2, m - 1); if (L <= R && a <= b) { if (t % 2 == 0) { ans += countP(m, L, R, a, b); } else { ans += countPCyclic(m, L, R, a, b); } } // Fixed part: cows x >= m have position x. int64 lo = max(l1, m); lo = max(lo, l2); int64 hi = min(r1, r2); if (lo <= hi) { ans += hi - lo + 1; } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Solver { int64 t, m, R, nextZero; explicit Solver(int64 time) : t(time), m(time / 2), R((3 * time + 3) / 2), nextZero(0) { // The service times of cow 0 are h^k(0), for k >= 1. // Find its first service time strictly larger than t. while (nextZero <= t) { nextZero = nextZero + nextZero / 2 + 1; } } // Largest nonnegative z such that h(z) <= x. static int64 G(int64 x) { return (2 * x - 1) / 3; } // Number of active cows whose next service time is at most X. int64 activeCount(int64 X) const { int64 roots = X / 3; if (X <= R) { return X - t - max(0LL, roots - m); } return min(roots, m) + 1; } // Time of the k-th next service among the active cows. int64 kthEvent(int64 k) const { int64 low = t, high = R; while (low < high) { int64 mid = low + (high - low) / 2; if (activeCount(mid) >= k) { high = mid; } else { low = mid + 1; } } return low; } // Count cows with labels in [a, b] whose next service time is <= X. // The label interval passed here is contained in [0, m]. int64 selectedCount(int64 X, int64 a, int64 b) const { if (X < t || a > b) return 0; int64 answer = 0; // Depth 0: the first service of cow s is at time 3s. int64 lo = max(a, t / 3 + 1); int64 hi = min(b, X / 3); if (lo <= hi) answer += hi - lo + 1; int64 gt = t; int64 gx = X; int64 previousA = t / 3; // Positive-depth first services. while (previousA > 0) { gt = G(gt); if (gx > 0) gx = G(gx); int64 currentA = gt / 3; int64 currentB = gx / 3; lo = max(a, currentA + 1); hi = min(b, min(previousA, currentB)); if (lo <= hi) answer += hi - lo + 1; previousA = currentA; // No positive label can have a deeper service time <= X. if (gx == 0) break; } // Cow 0 has a separate chain rooted at 0. if (a <= 0 && 0 <= b && nextZero <= X) { ++answer; } return answer; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; Solver solver(t); // Active cows have labels 0..m and occupy positions 0..m. int64 activeLabelsLeft = l1; int64 activeLabelsRight = min(r1, solver.m); int64 activePositionsLeft = l2; int64 activePositionsRight = min(r2, solver.m); int64 activeAnswer = 0; if (activeLabelsLeft <= activeLabelsRight && activePositionsLeft <= activePositionsRight) { int64 before; if (activePositionsLeft == 0) { before = t; } else { before = solver.kthEvent(activePositionsLeft); } int64 through; if (activePositionsRight == solver.m) { through = solver.R; } else { through = solver.kthEvent(activePositionsRight + 1); } activeAnswer = solver.selectedCount( through, activeLabelsLeft, activeLabelsRight ) - solver.selectedCount( before, activeLabelsLeft, activeLabelsRight ); } // Every inactive cow s is still at position s. int64 inactiveLeft = max(l1, solver.m + 1); int64 inactiveRight = min(min(r1, r2), t); int64 inactiveAnswer = 0; if (inactiveLeft <= inactiveRight) { inactiveAnswer = inactiveRight - inactiveLeft + 1; } cout << activeAnswer + inactiveAnswer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int MAX_ITER = 256; // G(z) = floor(3z / 2). static inline int64 G(int64 z) { return z + z / 2; } // H(v) = max { z : G(z) <= v } = floor((2v + 1) / 3). static inline int64 H(int64 v) { return (2 * v + 1) / 3; } static inline int64 ceilDiv3(int64 v) { return (v + 2) / 3; } // Stores H^0(v), H^1(v), ... static int buildH(int64 v, array<int64, MAX_ITER>& a) { int n = 1; a[0] = v; while (v > 1 && n < MAX_ITER) { v = H(v); a[n++] = v; } return n; } // Counts a in [lo, hi] with a == 3*x + 2 for some integer x >= 1. static inline int64 countResidue(int64 lo, int64 hi) { if (lo > hi) return 0; int64 firstX = lo / 3; int64 lastX = (hi - 2) / 3; return max(0LL, lastX - firstX + 1); } static int64 solveQuery(int64 l1, int64 r1, int64 l2, int64 r2, int64 t) { int64 answer = 0; const int64 half = t / 2; const int64 cycleMax = (t + 1) / 3; const int64 S = t + 3; // Cows with x > floor(t/2) have not moved and are at position x. { int64 lo = max({l1, half + 1, l2}); int64 hi = min(r1, r2); if (lo <= hi) answer += hi - lo + 1; } // Cows with cycleMax < x <= floor(t/2) are at position 3*x - 1 - t. { int64 lo = max({ l1, cycleMax + 1, ceilDiv3(t + l2 + 1) }); int64 hi = min({ r1, half, (t + r2 + 1) / 3 }); if (lo <= hi) answer += hi - lo + 1; } const int64 cycleLo = max(l1, 1LL); const int64 cycleHi = min(r1, cycleMax); // Count positive positions of cows in the cyclic part. if (cycleLo <= cycleHi && r2 >= 1) { const int64 L = max(l2, 1LL); const int64 R = r2; array<int64, MAX_ITER> hZero, hLow, hHigh; int nZero = buildH(S - 1, hZero); int nLow = buildH(S + L - 1, hLow); int nHigh = buildH(S + R, hHigh); const int64 aLo = 3 * cycleLo + 2; const int64 aHi = 3 * cycleHi + 2; for (int k = 0; k + 1 < nZero && k + 1 < nLow && k + 1 < nHigh; ++k) { if (hZero[k] < 5) break; int64 lo = max(aLo, hLow[k + 1] + 1); int64 hi = min(aHi, hHigh[k + 1]); hi = min(hi, hZero[k]); answer += countResidue(lo, hi); } } // Count cows x >= 1 whose position is zero. // G has an exact inverse unless v == 2 (mod 3). if (l2 == 0 && cycleLo <= cycleHi) { int64 v = S; while (v >= 5) { int64 rem = v % 3; if (rem == 2) { int64 x = (v - 2) / 3; if (l1 <= x && x <= r1) ++answer; break; } int64 q = v / 3; v = (rem == 0 ? 2 * q : 2 * q + 1); } } // Cow 0 follows the same recurrence, but starts with z = 3. if (l1 == 0) { int64 z = 3; int64 next = G(z); while (next <= S) { z = next; next = G(z); } int64 position = (z == S ? 0 : next - S); if (l2 <= position && position <= r2) ++answer; } return answer; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; string output; output.reserve(static_cast<size_t>(Q) * 22); while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; output += to_string(solveQuery(l1, r1, l2, r2, t)); output.push_back('\n'); } cout << output; return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; // If F(v) = floor(3v / 2), then F(v) <= z exactly when v <= G(z). static inline int64 G(int64 z) { return (2 * z + 1) / 3; } // Maximum x >= 0 satisfying G^k(z) >= 3x + 2. // Returns -1 if no such x exists. static inline int64 maxId(int64 z) { if (z < 2) return -1; return (z - 2) / 3; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; while (Q--) { int64 l1, r1, l2, r2, t; cin >> l1 >> r1 >> l2 >> r2 >> t; int64 answer = 0; auto addRange = [&](int64 lo, int64 hi) { if (lo <= hi) answer += hi - lo + 1; }; /* * Cows whose first jump has not happened yet satisfy x > floor(t / 3). * * If t < 2x, cow x is still at position x. */ addRange( max(l1, max(l2, t / 2 + 1)), min(r1, r2) ); /* * If 2x <= t < 3x, cow x has position 3x - 1 - t. */ { int64 lo = max(l1, t / 3 + 1); int64 hi = min(r1, t / 2); int64 minX = (t + l2 + 3) / 3; // ceil((t + l2 + 1) / 3) int64 maxX = (t + r2 + 1) / 3; lo = max(lo, minX); hi = min(hi, maxX); addRange(lo, hi); } /* * For a cow x, define jump times: * s_0 = 3x * s_{k+1} = s_k + floor(s_k / 2) + 1. * * Let y_k = s_k + 2 and F(v) = floor(3v / 2). * Then y_{k+1} = F(y_k), y_0 = 3x + 2. * * At time t, phase k satisfies s_k <= t < s_{k+1}, * and the cow's position is y_{k+1} - t - 3. */ const int64 T = t + 2; const int64 L = t + l2 + 3; const int64 U = t + r2 + 3; // At iteration k: // hT = G^k(T), hU = G^k(U), hV = G^k(L - 1). int64 hT = T; int64 hU = U; int64 hV = L - 1; while (true) { int64 bT = maxId(hT); // The phase-ID intervals only shrink as k increases. if (bT < l1) break; int64 nextHT = G(hT); int64 nextHU = G(hU); int64 nextHV = G(hV); int64 nextBT = maxId(nextHT); // B_{k+1}(T) int64 nextBU = maxId(nextHU); // B_{k+1}(U) int64 nextBV = maxId(nextHV); // B_{k+1}(L - 1) int64 lo = max(l1, max(nextBT + 1, nextBV + 1)); int64 hi = min(r1, min(bT, nextBU)); addRange(lo, hi); hT = nextHT; hU = nextHU; hV = nextHV; } cout << answer << '\n'; } return 0; }