結果
問題 | No.728 ギブ and テイク |
ユーザー | Pachicobue |
提出日時 | 2017-11-12 01:34:08 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 844 ms / 3,000 ms |
コード長 | 3,381 bytes |
コンパイル時間 | 1,914 ms |
コンパイル使用メモリ | 176,560 KB |
実行使用メモリ | 109,952 KB |
最終ジャッジ日時 | 2024-05-03 16:53:25 |
合計ジャッジ時間 | 9,641 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 2 ms
5,376 KB |
testcase_13 | AC | 35 ms
9,088 KB |
testcase_14 | AC | 60 ms
11,776 KB |
testcase_15 | AC | 25 ms
7,040 KB |
testcase_16 | AC | 54 ms
11,008 KB |
testcase_17 | AC | 47 ms
10,496 KB |
testcase_18 | AC | 572 ms
72,064 KB |
testcase_19 | AC | 631 ms
75,752 KB |
testcase_20 | AC | 725 ms
99,968 KB |
testcase_21 | AC | 647 ms
79,104 KB |
testcase_22 | AC | 239 ms
33,920 KB |
testcase_23 | AC | 147 ms
21,632 KB |
testcase_24 | AC | 480 ms
62,592 KB |
testcase_25 | AC | 472 ms
62,464 KB |
testcase_26 | AC | 231 ms
32,880 KB |
testcase_27 | AC | 844 ms
109,952 KB |
testcase_28 | AC | 630 ms
109,952 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 2 ms
5,376 KB |
testcase_32 | AC | 2 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 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() { 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; }