結果

問題 No.728 ギブ and テイク
ユーザー PachicobuePachicobue
提出日時 2018-10-07 20:41:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 823 ms / 3,000 ms
コード長 3,379 bytes
コンパイル時間 2,582 ms
コンパイル使用メモリ 209,656 KB
実行使用メモリ 24,116 KB
最終ジャッジ日時 2024-04-20 18:18:26
合計ジャッジ時間 10,188 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 33 ms
6,944 KB
testcase_14 AC 55 ms
6,944 KB
testcase_15 AC 24 ms
6,944 KB
testcase_16 AC 50 ms
6,944 KB
testcase_17 AC 46 ms
6,940 KB
testcase_18 AC 556 ms
17,044 KB
testcase_19 AC 596 ms
16,972 KB
testcase_20 AC 689 ms
21,208 KB
testcase_21 AC 627 ms
17,832 KB
testcase_22 AC 237 ms
9,220 KB
testcase_23 AC 151 ms
7,068 KB
testcase_24 AC 458 ms
14,844 KB
testcase_25 AC 459 ms
14,576 KB
testcase_26 AC 229 ms
9,264 KB
testcase_27 AC 823 ms
23,340 KB
testcase_28 AC 783 ms
24,116 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ull = unsigned long long;
constexpr std::size_t PC(ull v) { return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast<std::size_t>(v * 0x0101010101010101ULL >> 56 & 0x7f); }
class WaveletMatrix
{
public:
    static constexpr std::size_t L = 31;
    using T = ull;
    template <typename InIt>
    WaveletMatrix(const InIt first, const InIt last) : sz(std::distance(first, last))
    {
        std::vector<bool> b(sz);
        std::vector<T> v(first, last);
        for (std::size_t i = 0, j = L - 1; i < L; i++, j--) {
            std::vector<T> z, o;
            for (std::size_t i = 0; i < sz; i++) { b[i] = (v[i] >> j) & 1, (b[i] ? o : z).push_back(v[i]); }
            table.push_back(FullyIndexableDictionary{b}), v = z, zero.push_back(z.size());
            for (const T e : o) { v.push_back(e); }
        }
    }
    std::size_t noLessThan(std::size_t l, std::size_t r, const T v) const
    {
        std::size_t off = 0;
        for (std::size_t i = 0, j = L - 1; i < L; i++, j--) {
            const bool b = (v >> j) & 1;
            const std::size_t rl = table[i].rank(l), rr = table[i].rank(r);
            l = (b ? rl + zero[i] : l - rl), r = (b ? rr + zero[i] : r - rr), off += (b ? 0 : rr - rl);
        }
        return r - l + off;
    }
    std::size_t rangeFreq(const std::size_t l, const std::size_t r, const T vinf, const T vsup) const { return noLessThan(l, r, vinf) - noLessThan(l, r, vsup); }
    std::size_t rank(const std::size_t l, const std::size_t r, const T v) const { return rangeFreq(l, r, v, v + 1); }

private:
    class FullyIndexableDictionary
    {
    private:
        using B = unsigned long long;
        static constexpr std::size_t BS = sizeof(B) * 8;
        const std::size_t sz;
        std::vector<B> data;
        std::vector<std::size_t> large;

    public:
        FullyIndexableDictionary(const std::vector<bool>& b) : sz(b.size()), data((sz + BS) / BS, 0), large((sz + BS) / BS, 0)
        {
            std::size_t one = 0;
            for (std::size_t i = 0; i < sz; i++) {
                data[i / BS] |= ((B)b[i] << (i % BS)), one += b[i];
                if (i % BS == BS - 1) { large[(i + 1) / BS] = one; }
            }
        }
        bool operator[](const std::size_t n) const { return (data[n / BS] >> (n % BS)) & 1; }
        std::size_t rank(const std::size_t n) const { return large[n / BS] + (n % BS == 0 ? 0 : PC(data[n / BS] & ((1ULL << (n % BS)) - 1))); }
    };
    const std::size_t sz;
    std::vector<std::size_t> zero;
    std::vector<FullyIndexableDictionary> table;
};

int main()
{
    constexpr ull OFFSET = 1000000000ULL;
    std::size_t N;
    std::cin >> N;
    std::vector<ull> A(N), L(N), R(N), B(N);
    for (auto& e : A) { std::cin >> e; }
    for (std::size_t i = 0; i < N; i++) { std::cin >> L[i] >> R[i], B[i] = A[i] - L[i] + OFFSET; }
    WaveletMatrix wm{B.begin(), B.end()};
    ull ans = 0;
    for (std::size_t i = 0; i < N - 1; i++) {
        const std::size_t isup = std::upper_bound(A.begin(), A.end(), A[i] + R[i]) - A.begin();
        ans += wm.rangeFreq(i + 1, isup, 0, A[i] + OFFSET + 1);
    }
    std::cout << ans << std::endl;
    return 0;
}
0