結果

問題 No.728 ギブ and テイク
ユーザー PachicobuePachicobue
提出日時 2018-08-24 11:19:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 528 ms / 3,000 ms
コード長 3,013 bytes
コンパイル時間 2,293 ms
コンパイル使用メモリ 208,816 KB
実行使用メモリ 107,492 KB
最終ジャッジ日時 2023-09-02 10:45:25
合計ジャッジ時間 7,992 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 22 ms
8,808 KB
testcase_14 AC 37 ms
11,640 KB
testcase_15 AC 16 ms
6,800 KB
testcase_16 AC 33 ms
10,660 KB
testcase_17 AC 30 ms
10,248 KB
testcase_18 AC 360 ms
70,116 KB
testcase_19 AC 378 ms
73,816 KB
testcase_20 AC 449 ms
97,936 KB
testcase_21 AC 395 ms
76,720 KB
testcase_22 AC 142 ms
33,252 KB
testcase_23 AC 88 ms
20,992 KB
testcase_24 AC 294 ms
61,060 KB
testcase_25 AC 288 ms
60,856 KB
testcase_26 AC 136 ms
31,952 KB
testcase_27 AC 528 ms
107,492 KB
testcase_28 AC 314 ms
107,436 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
constexpr std::size_t PC(const ull v)
{
    ull count = (v & 0x5555555555555555ULL) + ((v >> 1) & 0x5555555555555555ULL);
    count = (count & 0x3333333333333333ULL) + ((count >> 2) & 0x3333333333333333ULL);
    count = (count & 0x0f0f0f0f0f0f0f0fULL) + ((count >> 4) & 0x0f0f0f0f0f0f0f0fULL);
    count = (count & 0x00ff00ff00ff00ffULL) + ((count >> 8) & 0x00ff00ff00ff00ffULL);
    count = (count & 0x0000ffff0000ffffULL) + ((count >> 16) & 0x0000ffff0000ffffULL);
    return static_cast<std::size_t>((count & 0x00000000ffffffffULL) + ((count >> 32) & 0x00000000ffffffffULL));
}
constexpr std::size_t LG(ull v) { return v == 0 ? 0 : (v--, v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), PC(v)); }
constexpr ull SZ(const ull v) { return 1ULL << LG(v); }
template <typename Monoid>
class SegmentTree
{
public:
    using BaseMonoid = Monoid;
    using T = typename Monoid::T;
    template <typename InIt>
    SegmentTree(const InIt first, const InIt last) : data_num(distance(first, last)), half(SZ(data_num)), value(half << 1, Monoid::id())
    {
        std::copy(first, last, value.begin() + half);
        for (std::size_t i = half - 1; i >= 1; i--) { up(i); }
    }
    template <typename F, typename QueryOp, typename AccOp>
    F query(std::size_t L, std::size_t R, const QueryOp& query_op, const AccOp& acc_op) const
    {
        F accl{}, accr{};
        for (L += half, R += half; L < R; L >>= 1, R >>= 1) {
            if (L & 1) { accl = acc_op(accl, query_op(value[L++])); }
            if (R & 1) { accr = acc_op(query_op(value[--R]), accr); }
        }
        return acc_op(accl, accr);
    }

private:
    void up(const std::size_t i) { value[i] = acc(value[i << 1], value[i << 1 | 1]); }
    const std::size_t data_num, half;
    std::vector<T> value;
    const Monoid acc{};
};
struct MergedSequence
{
    using F = ll;
    using T = std::vector<F>;
    T operator()(const T& a, const T& b) const
    {
        T c(a.size() + b.size());
        std::merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());
        return c;
    }
    static T id() { return T{}; }
};
int main()
{
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    std::size_t N;
    std::cin >> N;
    std::vector<ll> A(N), R(N);
    std::vector<std::vector<ll>> data(N);
    for (std::size_t i = 0; i < N; i++) { std::cin >> A[i]; }
    for (std::size_t i = 0, L; i < N; i++) { std::cin >> L >> R[i], data[i] = {A[i] - (ll)L}; }
    SegmentTree<MergedSequence> seg(data.begin(), data.end());
    ll ans = 0;
    for (std::size_t i = 0; i < N; i++) {
        const std::size_t k = std::upper_bound(A.begin(), A.end(), A[i] + R[i]) - A.begin();
        auto query = [&](const std::vector<ll>& v) -> ll { return std::upper_bound(v.begin(), v.end(), A[i]) - v.begin(); };
        ans += seg.query<ll>(i + 1, k, query, std::plus<ll>{});
    }
    std::cout << ans << std::endl;
    return 0;
}
0