結果

問題 No.728 ギブ and テイク
ユーザー PachicobuePachicobue
提出日時 2017-11-12 00:17:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 618 ms / 3,000 ms
コード長 3,430 bytes
コンパイル時間 1,767 ms
コンパイル使用メモリ 175,712 KB
実行使用メモリ 109,820 KB
最終ジャッジ日時 2023-08-16 07:52:07
合計ジャッジ時間 7,728 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 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,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 26 ms
8,864 KB
testcase_14 AC 45 ms
11,796 KB
testcase_15 AC 19 ms
6,948 KB
testcase_16 AC 40 ms
10,976 KB
testcase_17 AC 36 ms
10,384 KB
testcase_18 AC 414 ms
72,100 KB
testcase_19 AC 444 ms
75,480 KB
testcase_20 AC 534 ms
99,800 KB
testcase_21 AC 465 ms
78,648 KB
testcase_22 AC 166 ms
33,756 KB
testcase_23 AC 101 ms
21,572 KB
testcase_24 AC 339 ms
62,264 KB
testcase_25 AC 334 ms
62,352 KB
testcase_26 AC 159 ms
32,748 KB
testcase_27 AC 618 ms
109,820 KB
testcase_28 AC 358 ms
109,804 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

struct Sorted {
    using T = vector<ll>;

    struct AccMonoid {
        T operator()(const T& a, const T& b) const
        {
            T c(a.size() + b.size());
            merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());
            return c;
        }
        static T identity() { return vector<ll>{}; }
    };
};


template <typename Base>
class SegmentTree
{
public:
    using BaseAlgebra = Base;
    using AccMonoid = typename BaseAlgebra::AccMonoid;
    using T = typename BaseAlgebra::T;

    SegmentTree(const std::vector<T>& val) : data_num(val.size()), height(__lg(2 * data_num - 1)), size(1 << (1 + height)), half(size >> 1), value(size)
    {
        for (int data = 0; data < half; data++) {
            if (data < data_num) {
                value[data + half] = val[data];
            } else {
                value[data + half] = AccMonoid::identity();
            }
        }
        for (int node = half - 1; node >= 1; node--) {
            value[node] = acc(value[2 * node], value[2 * node + 1]);
        }
    }
    int query(const int a, const int b, const ll v) const
    {
        assert(0 <= a and a < b and b <= data_num);
        return queryRec(1, 0, half, a, b, v);
    }

private:
    int queryRec(const int int_index, const int int_left, const int int_right, const int mod_left, const int mod_right, const ll v) const
    {
        if (mod_left <= int_left and int_right <= mod_right) {
            return upper_bound(value[int_index].begin(), value[int_index].end(), v) - value[int_index].begin();
        } else if (int_right <= mod_left or mod_right <= int_left) {
            return 0;
        } else {
            return queryRec(2 * int_index, int_left, (int_left + int_right) / 2, mod_left, mod_right, v) + queryRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, mod_left, mod_right, v);
        }
    }
    const int data_num;  // Num of valid data on leaves.
    const int height;
    const int size;
    const int half;
    vector<T> value;  // Tree for value(length: size)
    const AccMonoid acc{};
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    vector<ll> L(N);
    vector<ll> A_R(N);
    vector<vector<ll>> A_L(N, vector<ll>(1, 0));
    for (int i = 0; i < N; i++) {
        ll R;
        cin >> L[i] >> R;
        A_R[i] = A[i] + R;
        A_L[i][0] = A[i] - L[i];
    }
    SegmentTree<Sorted> seg(A_L);
    ll sum = 0;
    for (int i = 0; i < N; i++) {
        const int upper = upper_bound(A.begin(), A.end(), A_R[i]) - A.begin();
        if (i + 1 < upper) {
            sum += (ll)seg.query(i + 1, upper, A[i]);
        }
    }
    cout << sum << endl;


    return 0;
}
0