結果

問題 No.728 ギブ and テイク
ユーザー PachicobuePachicobue
提出日時 2018-08-24 11:05:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 451 ms / 3,000 ms
コード長 2,823 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 210,340 KB
実行使用メモリ 33,368 KB
最終ジャッジ日時 2023-09-02 10:24:48
合計ジャッジ時間 8,812 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,388 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 23 ms
5,096 KB
testcase_14 AC 37 ms
5,164 KB
testcase_15 AC 16 ms
4,376 KB
testcase_16 AC 33 ms
5,144 KB
testcase_17 AC 30 ms
5,036 KB
testcase_18 AC 319 ms
19,320 KB
testcase_19 AC 337 ms
19,256 KB
testcase_20 AC 401 ms
32,724 KB
testcase_21 AC 356 ms
20,080 KB
testcase_22 AC 140 ms
10,800 KB
testcase_23 AC 91 ms
7,528 KB
testcase_24 AC 267 ms
19,856 KB
testcase_25 AC 262 ms
18,280 KB
testcase_26 AC 134 ms
11,908 KB
testcase_27 AC 451 ms
32,548 KB
testcase_28 AC 401 ms
33,368 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:56:87: 警告: narrowing conversion of ‘(((long long unsigned int)A.std::vector<long long int>::operator[](i)) - ((long long unsigned int)L))’ from ‘long long unsigned int’ to ‘long long int’ [-Wnarrowing]
   56 |     for (std::size_t i = 0, L; i < N; i++) { std::cin >> L >> R[i], X.push_back({A[i] - L, {false, i}}); }

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
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); }
struct Sum
{
    using T = ll;
    T operator()(const T& a, const T& b) const { return a + b; }
    static T inv(const T& a) { return -a; }
    static constexpr T id() { return 0; }
};
template <typename Base>
class BinaryIndexedTree
{
public:
    using T = typename Base::T;
    using AbelGroup = Base;
    BinaryIndexedTree(const std::size_t n) : data_num(n), size(SZ(n)), value(size + 1, Base::id()) {}
    T accumulate(const std::size_t a) const  // [0,a]
    {
        T sum = Base::id();
        for (std::size_t ind = a + 1; ind != 0; ind &= ind - 1) { sum = op(sum, value[ind]); }
        return sum;
    }
    T accumulate(const std::size_t l, const std::size_t r) const { return op(accumulate(r - 1), Base::inv(l == 0 ? Base::id() : accumulate(l - 1))); }
    void add(const std::size_t a, const T& val)
    {
        for (std::size_t ind = a + 1; ind <= size; ind += ind & (-ind)) { value[ind] = op(value[ind], val); }
    }

private:
    const std::size_t data_num, size;
    const Base op{};
    std::vector<T> value;
};

int main()
{
    std::size_t N;
    std::cin >> N;
    std::vector<ll> A(N), R(N);
    using P = std::pair<ll, std::pair<bool, std::size_t>>;  // (座標,(Aiの値か,添字))
    std::vector<P> X;
    for (std::size_t i = 0; i < N; i++) { std::cin >> A[i], X.push_back({A[i], {true, i}}); }
    for (std::size_t i = 0, L; i < N; i++) { std::cin >> L >> R[i], X.push_back({A[i] - L, {false, i}}); }
    std::sort(X.begin(), X.end());
    BinaryIndexedTree<Sum> bit(N);
    ll ans = 0;
    for (const auto p : X) {
        const std::size_t i = p.second.second;
        if (p.second.first) {
            const std::size_t k = std::lower_bound(A.begin(), A.end(), p.first + R[i] + 1) - A.begin();
            ans += bit.accumulate(i + 1, k);
        } else {
            bit.add(i, 1);
        }
    }
    std::cout << ans << std::endl;
    return 0;
}
0