From d97fb5c2067f50e1febff447399e33e9523ead72 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Wed, 23 Jul 2025 15:13:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20T637678=20=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E7=9A=84=E5=AE=8C=E6=95=B4=E8=A7=A3=E5=86=B3=E6=96=B9?= =?UTF-8?q?=E6=A1=88=EF=BC=8C=E5=8C=85=E6=8B=AC=E5=AD=90=E9=9B=86=E5=92=8C?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E5=92=8C=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/7/23/T637678.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/7/23/T637678.cpp diff --git a/src/7/23/T637678.cpp b/src/7/23/T637678.cpp new file mode 100644 index 0000000..78f999f --- /dev/null +++ b/src/7/23/T637678.cpp @@ -0,0 +1,73 @@ +#include +#include +using namespace std; +using ll = int64_t; + +vector sub(vector& nums) { + int n = nums.size(); + if (n == 0) { + return {0}; + } + int tot = 1 << n; + vector res(tot, 0); + for (int i = 0; i < tot; i++) { + ll sum = 0; + for (int j = 0; j < n; j++) { + if (i & (1 << j)) { + sum += nums[j]; + } + } + res[i] = sum; + } + return res; +} + +int main() { + int n, q; + scanf("%d %d", &n, &q); + vector v(n); + for (int i = 0; i < n; i++) { + scanf("%d", &v[i]); + } + + int n1 = n / 2; + int n2 = n - n1; + vector A, B; + for (int i = 0; i < n1; i++) + A.push_back(v[i]); + for (int i = n1; i < n; i++) + B.push_back(v[i]); + + vector sumA = sub(A); + vector sumB = sub(B); + + sort(sumA.begin(), sumA.end()); + sort(sumB.begin(), sumB.end()); + + int lenA = sumA.size(); + int lenB = sumB.size(); + ll maxB = lenB > 0 ? sumB[lenB - 1] : 0; + + while (q--) { + ll l, r; + scanf("%lld %lld", &l, &r); + ll ans = 0; + int p = 0, q_idx = 0; + for (int i = lenA - 1; i >= 0; i--) { + ll a = sumA[i]; + if (a < l - maxB) { + break; + } + ll L = l - a; + ll R = r - a; + + while (p < lenB && sumB[p] < L) + p++; + while (q_idx < lenB && sumB[q_idx] <= R) + q_idx++; + ans += (q_idx - p); + } + printf("%lld\n", ans); + } + return 0; +} \ No newline at end of file