結果
問題 | No.728 ギブ and テイク |
ユーザー | Pachicobue |
提出日時 | 2017-11-12 00:15:39 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,427 bytes |
コンパイル時間 | 1,891 ms |
コンパイル使用メモリ | 176,400 KB |
実行使用メモリ | 110,080 KB |
最終ジャッジ日時 | 2024-05-03 16:50:30 |
合計ジャッジ時間 | 8,617 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 | 1 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 1 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 | 24 ms
8,832 KB |
testcase_14 | AC | 41 ms
11,904 KB |
testcase_15 | AC | 18 ms
6,912 KB |
testcase_16 | AC | 37 ms
11,136 KB |
testcase_17 | AC | 32 ms
10,496 KB |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | AC | 101 ms
21,632 KB |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 153 ms
32,896 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 1 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); int 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 += seg.query(i + 1, upper, A[i]); } } cout << sum << endl; return 0; }