結果

問題 No.728 ギブ and テイク
ユーザー PachicobuePachicobue
提出日時 2019-03-07 22:41:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 846 ms / 3,000 ms
コード長 5,545 bytes
コンパイル時間 973 ms
コンパイル使用メモリ 88,692 KB
実行使用メモリ 24,564 KB
最終ジャッジ日時 2023-09-05 19:28:53
合計ジャッジ時間 9,996 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 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 38 ms
4,472 KB
testcase_14 AC 63 ms
4,988 KB
testcase_15 AC 27 ms
4,380 KB
testcase_16 AC 56 ms
4,704 KB
testcase_17 AC 50 ms
4,548 KB
testcase_18 AC 582 ms
17,480 KB
testcase_19 AC 618 ms
17,608 KB
testcase_20 AC 732 ms
22,528 KB
testcase_21 AC 648 ms
18,484 KB
testcase_22 AC 241 ms
9,600 KB
testcase_23 AC 154 ms
6,872 KB
testcase_24 AC 481 ms
15,352 KB
testcase_25 AC 481 ms
15,268 KB
testcase_26 AC 233 ms
9,592 KB
testcase_27 AC 836 ms
24,564 KB
testcase_28 AC 846 ms
24,424 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
// https://yukicoder.me/problems/no/728
using ll = long long;
using ull = unsigned long long;
template <typename T>
constexpr T PopCount(T v) { return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL), v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL), v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL, static_cast<T>(v * 0x0101010101010101ULL >> 56 & 0x7f); }
template <typename T>
constexpr T log2p1(T v) { return v |= (v >> 1), v |= (v >> 2), v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32), PopCount(v); }
template <typename T>
constexpr bool ispow2(const T v) { return (v << 1) == (T(1) << (log2p1(v))); }
template <typename T>
constexpr T ceil2(const T v) { return ispow2(v) ? v : T(1) << log2p1(v); }
template <typename T>
constexpr T floor2(const T v) { return v == 0 ? T(0) : ispow2(v) ? v : T(1) << (log2p1(v) - 1); }
//!==============================================================================================!//
//!  dP   dP   dP                            dP            dP      8888ba.88ba             dP    !//
//!  88   88   88                            88            88      88  '8b  '8b            88    !//
//!  88  .8P  .8P .d8888b. dP   .dP .d8888b. 88 .d8888b. d8888P    88   88   88 .d8888b. d8888P  !//
//!  88  d8'  d8' 88'  '88 88   d8' 88ooood8 88 88ooood8   88      88   88   88 88'  '88   88    !//
//!  88.d8P8.d8P  88.  .88 88 .88'  88.  ... 88 88.  ...   88      88   88   88 88.  .88   88    !//
//!  8888' Y88'   '88888P8 8888P'   '88888P' dP '88888P'   dP      dP   dP   dP '88888P8   dP    !//
//!==============================================================================================!//
template <std::size_t L>
class WaveletMatrix
{
public:
    using T = unsigned long long;
    template <typename InIt>
    WaveletMatrix(const InIt first, const InIt last) : sz(std::distance(first, last))  // [0,2^L)の整数列
    {
        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(FID{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  // x[l,r) \in [v,2^L)
    {
        std::size_t ans = 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), ans += (b ? 0 : rr - rl);
        }
        return r - l + ans;
    }
    std::size_t lessThan(const std::size_t l, const std::size_t r, const T v) const { return r - l - noLessThan(l, r, v); }                                        // x[l,r) \in [0,v)
    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); }  // x[l,r) \in [vinf,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); }                                              // x[l,r) \in {v}
    T quantile(std::size_t l, std::size_t r, std::size_t k) const                                                                                                  // x[l,r)  k-th
    {
        std::size_t ans = 0;
        for (std::size_t i = 0, j = L - 1; i < L; i++, j--) {
            const std::size_t rl = table[i].rank(l), rr = table[i].rank(r);
            const bool b = rr - rl > k;
            l = (b ? rl + zero[i] : l - rl), r = (b ? rr + zero[i] : r - rr), k += (b ? 0 : k -= rr - rl), ans |= ((std::size_t)b << j);
        }
        return ans;
    }

private:
    class FID
    {
    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:
        FID(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 : PopCount(data[n / BS] & ((1ULL << (n % BS)) - 1))); }
    };
    const std::size_t sz;
    std::vector<std::size_t> zero;
    std::vector<FID> table;
};
int main()
{
    std::size_t N;
    std::cin >> N;
    std::vector<ull> A(N), A_L(N), L(N), R(N);
    for (std::size_t i = 0; i < N; i++) { std::cin >> A[i], A[i] += 1000000000ULL; }
    for (std::size_t i = 0; i < N; i++) { std::cin >> L[i] >> R[i], A_L[i] = A[i] - L[i]; }
    WaveletMatrix<40> wm(A_L.begin(), A_L.end());
    ull ans = 0;
    for (std::size_t i = 0; i < N; i++) {
        const ull Obj = A[i] + R[i];
        const std::size_t ri = std::size_t(std::upper_bound(A.begin(), A.end(), Obj) - A.begin());
        ans += wm.rangeFreq(i + 1, ri, 0, A[i] + 1);
    }
    std::cout << ans << std::endl;
    return 0;
}
0