From 0f4f1a4fb9add9f735bf4e91be4866e4f1350a2d Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 26 May 2022 09:29:22 +0800 Subject: [PATCH] =?UTF-8?q?P2717=20=E5=AF=92=E5=81=87=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/76400157 --- Luogu/P2717/P2717.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Luogu/P2717/P2717.cpp diff --git a/Luogu/P2717/P2717.cpp b/Luogu/P2717/P2717.cpp new file mode 100644 index 00000000..cc21ef7b --- /dev/null +++ b/Luogu/P2717/P2717.cpp @@ -0,0 +1,56 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n, k, a[N], b[N]; +long long ans; + +void solve(int l, int r) { + if (l == r) { + if (a[l] >= 0) ans++; + return; + } + + int mid = l + r >> 1; + + solve(l, mid); + solve(mid + 1, r); + + b[mid] = a[mid]; + for (int i = mid - 1; i >= l; i--) b[i] = b[i + 1] + a[i]; + std::sort(b + l, b + mid + 1); + + b[mid + 1] = a[mid + 1]; + for (int i = mid + 2; i <= r; i++) b[i] = b[i - 1] + a[i]; + std::sort(b + mid + 1, b + r + 1); + + int i = l, j = r; + while (i <= mid) { + while (j > mid && b[i] + b[j] >= 0) j--; + ans += r - j; + i++; + } +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n >> k; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + + a[i] -= k; + } + + solve(1, n); + + cout << ans << endl; + + return 0; +}