結果

問題 No.1079 まお
ユーザー kimiyukikimiyuki
提出日時 2020-08-06 03:20:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 545 ms / 2,000 ms
コード長 2,556 bytes
コンパイル時間 3,215 ms
コンパイル使用メモリ 219,768 KB
実行使用メモリ 26,084 KB
最終ジャッジ日時 2023-10-19 03:40:02
合計ジャッジ時間 12,835 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 7 ms
4,348 KB
testcase_08 AC 6 ms
4,348 KB
testcase_09 AC 6 ms
4,348 KB
testcase_10 AC 6 ms
4,348 KB
testcase_11 AC 6 ms
4,348 KB
testcase_12 AC 225 ms
14,740 KB
testcase_13 AC 267 ms
16,900 KB
testcase_14 AC 343 ms
19,220 KB
testcase_15 AC 389 ms
21,332 KB
testcase_16 AC 436 ms
23,708 KB
testcase_17 AC 504 ms
23,708 KB
testcase_18 AC 481 ms
23,708 KB
testcase_19 AC 529 ms
23,972 KB
testcase_20 AC 508 ms
23,972 KB
testcase_21 AC 500 ms
23,972 KB
testcase_22 AC 520 ms
26,084 KB
testcase_23 AC 519 ms
26,084 KB
testcase_24 AC 525 ms
26,084 KB
testcase_25 AC 534 ms
26,084 KB
testcase_26 AC 545 ms
26,084 KB
testcase_27 AC 99 ms
9,948 KB
testcase_28 AC 228 ms
17,944 KB
testcase_29 AC 294 ms
26,084 KB
testcase_30 AC 292 ms
26,084 KB
testcase_31 AC 112 ms
9,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0