結果
問題 | No.728 ギブ and テイク |
ユーザー | Pachicobue |
提出日時 | 2017-11-12 01:33:29 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 447 ms / 3,000 ms |
コード長 | 3,090 bytes |
コンパイル時間 | 1,783 ms |
コンパイル使用メモリ | 175,688 KB |
実行使用メモリ | 34,432 KB |
最終ジャッジ日時 | 2024-05-03 16:52:46 |
合計ジャッジ時間 | 6,110 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1 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,944 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 1 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,940 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 20 ms
6,944 KB |
testcase_14 | AC | 34 ms
6,940 KB |
testcase_15 | AC | 15 ms
6,944 KB |
testcase_16 | AC | 31 ms
6,944 KB |
testcase_17 | AC | 29 ms
6,940 KB |
testcase_18 | AC | 325 ms
19,444 KB |
testcase_19 | AC | 330 ms
19,516 KB |
testcase_20 | AC | 379 ms
34,168 KB |
testcase_21 | AC | 338 ms
19,608 KB |
testcase_22 | AC | 133 ms
11,380 KB |
testcase_23 | AC | 84 ms
7,360 KB |
testcase_24 | AC | 259 ms
19,132 KB |
testcase_25 | AC | 252 ms
19,120 KB |
testcase_26 | AC | 129 ms
11,300 KB |
testcase_27 | AC | 447 ms
34,428 KB |
testcase_28 | AC | 406 ms
34,432 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 1 ms
5,376 KB |
testcase_31 | AC | 1 ms
5,376 KB |
testcase_32 | AC | 1 ms
5,376 KB |
ソースコード
#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 Sum { using T = ll; T operator()(const T& a, const T& b) const { return a + b; } T inv(const T& a) const { return -a; } static constexpr T identity() { return 0; } }; template <typename Base> class BinaryIndexedTree { public: using T = typename Base::T; BinaryIndexedTree(const int n) : data_num(n), size(1 << (__lg(2 * data_num - 1))), value(size + 1, Base::identity()) { assert(n > 0); } BinaryIndexedTree(const vector<T>& val) : data_num(val.size()), size(1 << (__lg(2 * data_num - 1))), value(size + 1, Base::identity()) { for (int i = 1; i <= size; i++) { value[i] = val[i - 1]; } for (int x = 1; x < size; x++) { value[x + (x & -x)] += value[x]; } } T accumulate(const int a) const { assert(0 <= a and a < data_num); int ind = a + 1; T sum = Base::identity(); while (ind > 0) { sum = op(sum, value[ind]); ind &= ind - 1; } return sum; } void add(const int a, const T& val) { assert(0 <= a and a < data_num); int ind = a + 1; while (ind <= size) { value[ind] = op(value[ind], val); ind += ind & (-ind); } } private: const int data_num; const int size; const Base op{}; vector<T> value; }; struct Data { ll value; bool from_a; int index; ll A_R; bool operator<(const Data& d) const { return (value != d.value) ? (value < d.value) : (from_a < d.from_a); } }; int main() { int N; cin >> N; BinaryIndexedTree<Sum> bit(N); vector<ll> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } vector<Data> datas; for (int i = 0; i < N; i++) { ll L, R; cin >> L >> R; datas.push_back(Data{A[i], true, i, A[i] + R}); datas.push_back(Data{A[i] - L, false, i, 0}); } sort(datas.begin(), datas.end()); ll sum = 0; for (const auto& d : datas) { const int ind = d.index; if (d.from_a) { const int upper = upper_bound(A.begin(), A.end(), d.A_R) - A.begin() - 1; if (ind < upper) { sum += bit.accumulate(upper) - bit.accumulate(ind); } } else { bit.add(ind, 1); } } cout << sum << endl; return 0; }