結果
問題 | No.728 ギブ and テイク |
ユーザー | Pachicobue |
提出日時 | 2017-11-12 00:17:19 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 546 ms / 3,000 ms |
コード長 | 3,430 bytes |
コンパイル時間 | 1,594 ms |
コンパイル使用メモリ | 176,824 KB |
実行使用メモリ | 109,872 KB |
最終ジャッジ日時 | 2024-05-03 16:50:42 |
合計ジャッジ時間 | 6,914 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 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 | 23 ms
8,960 KB |
testcase_14 | AC | 37 ms
11,776 KB |
testcase_15 | AC | 15 ms
6,784 KB |
testcase_16 | AC | 32 ms
11,008 KB |
testcase_17 | AC | 29 ms
10,496 KB |
testcase_18 | AC | 370 ms
71,916 KB |
testcase_19 | AC | 395 ms
75,624 KB |
testcase_20 | AC | 473 ms
99,968 KB |
testcase_21 | AC | 410 ms
78,928 KB |
testcase_22 | AC | 146 ms
33,904 KB |
testcase_23 | AC | 89 ms
21,504 KB |
testcase_24 | AC | 304 ms
62,592 KB |
testcase_25 | AC | 297 ms
62,324 KB |
testcase_26 | AC | 142 ms
32,812 KB |
testcase_27 | AC | 546 ms
109,872 KB |
testcase_28 | AC | 330 ms
109,804 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() { 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; }