結果
問題 | No.1079 まお |
ユーザー |
|
提出日時 | 2020-08-06 03:20:32 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 664 ms / 2,000 ms |
コード長 | 2,556 bytes |
コンパイル時間 | 2,883 ms |
コンパイル使用メモリ | 210,604 KB |
最終ジャッジ日時 | 2025-01-12 15:13:00 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:81:28: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 81 | int n; int64_t k; scanf("%d%" SCNd64, &n, &k); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~ main.cpp:84:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 84 | scanf("%" SCNd64, &a[i]); | ~~~~~^~~~~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) std::begin(x), std::end(x) using namespace std; struct item_t { int length; int64_t min; bool duplicated; }; int64_t solve2(int64_t k, vector<item_t> l, vector<item_t> r) { sort(ALL(l), [&](item_t x1, item_t x2) { return x1.min > x2.min; }); sort(ALL(r), [&](item_t y1, item_t y2) { return y1.min > y2.min; }); int64_t ans = 0; int64_t y_count = 0; int64_t y_sum_length = 0; auto y = r.begin(); for (auto x : l) { for (; y != r.end() and x.min < y->min; ++ y) { y_count += 1; y_sum_length += y->length; } if (not x.duplicated) { ans += x.length * y_count + y_sum_length; } } return ans; } template <class InputIterator> map<int64_t, vector<item_t> > solve1(InputIterator first, InputIterator last) { map<int64_t, vector<item_t> > f; int length = 0; int64_t x = INT64_MAX; bool duplicated = false; for (; first != last; ++ first) { length += 1; if (*first == x) { duplicated = true; } else if (*first < x) { x = *first; duplicated = false; } f[*first].push_back(item_t{length, x, duplicated}); } return f; } int64_t solve(int n, int64_t k, const vector<int64_t> & a) { int64_t ans = 0; auto go = [&](auto && go, int l, int r) -> void { if (r - l == 0) return; if (r - l == 1) { if (2 * a[l] == k) { ans += 1; } return; } int m = (r + l) / 2; auto f = solve1(a.rbegin() + (n - m), a.rbegin() + (n - l)); auto g = solve1(a.begin() + m, a.begin() + r); for (auto && it1 : f) { auto it2 = g.find(k - it1.first); if (it2 != g.end()) { ans += solve2(k, it1.second, it2->second); ans += solve2(k, it2->second, it1.second); } } go(go, l, m); go(go, m, r); }; go(go, 0, n); return ans; } int main() { int n; int64_t k; scanf("%d%" SCNd64, &n, &k); vector<int64_t> a(n); REP (i, n) { scanf("%" SCNd64, &a[i]); } int64_t ans = solve(n, k, a); printf("%" PRId64 "\n", ans); return 0; }