結果
問題 | No.59 鉄道の旅 |
ユーザー | Pachicobue |
提出日時 | 2017-07-14 19:51:14 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 95 ms / 5,000 ms |
コード長 | 5,546 bytes |
コンパイル時間 | 1,678 ms |
コンパイル使用メモリ | 172,396 KB |
実行使用メモリ | 36,108 KB |
最終ジャッジ日時 | 2024-10-07 21:52:14 |
合計ジャッジ時間 | 3,082 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 10 ms
36,020 KB |
testcase_01 | AC | 9 ms
35,996 KB |
testcase_02 | AC | 10 ms
36,000 KB |
testcase_03 | AC | 9 ms
36,024 KB |
testcase_04 | AC | 95 ms
36,028 KB |
testcase_05 | AC | 10 ms
36,068 KB |
testcase_06 | AC | 9 ms
35,976 KB |
testcase_07 | AC | 9 ms
35,976 KB |
testcase_08 | AC | 20 ms
36,000 KB |
testcase_09 | AC | 18 ms
35,972 KB |
testcase_10 | AC | 17 ms
35,936 KB |
testcase_11 | AC | 14 ms
35,996 KB |
testcase_12 | AC | 47 ms
36,108 KB |
testcase_13 | AC | 77 ms
36,000 KB |
testcase_14 | AC | 95 ms
36,036 KB |
testcase_15 | AC | 9 ms
35,960 KB |
ソースコード
#include <bits/stdc++.h> #define show(x) cout << #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; constexpr int MAX = 1000000; template <typename Base> struct SegmentTree { public: using BaseAlgebra = Base; using AccMonoid = typename BaseAlgebra::AccMonoid; using OpMonoid = typename BaseAlgebra::OpMonoid; using T = typename BaseAlgebra::T; using F = typename BaseAlgebra::OpMonoid::T; SegmentTree(const int n) : data_num(n), height(__lg(2 * data_num - 1)), size(1 << (1 + height)), half(size >> 1), value(size, AccMonoid::identity()), action(size, OpMonoid::identity()) { assert(n > 0); } 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), action(size, OpMonoid::identity()) { 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]); } } T get(const int a) const { assert(0 <= a and a < data_num); return accumulate(a, a + 1); } void set(const int a, const T& val) { assert(0 <= a and a < data_num); const int node = a + half; value[node] = val; for (int i = node / 2; i > 0; i /= 2) { value[i] = acc(value[2 * i], value[2 * i + 1]); } } T accumulate(const int a, const int b) const // Accumulate (a,b] { assert(0 <= a and a < b and b <= data_num); return accumulateRec(1, 0, half, a, b); } void modify(const int a, const int b, const F& f) // Apply f on (a,b] { assert(0 <= a and a < b and b <= data_num); if (f == OpMonoid::identity()) { return; } modifyRec(1, 0, half, a, b, f); } private: void modifyRec(const int int_index, const int int_left, const int int_right, const int mod_left, const int mod_right, const F& f) { if (mod_left <= int_left and int_right <= mod_right) { value[int_index] = act(f, value[int_index]); action[int_index] = compose(f, action[int_index]); } else if (int_right <= mod_left or mod_right <= int_left) { // Do nothing } else { modifyRec(2 * int_index, int_left, (int_left + int_right) / 2, 0, half, action[int_index]); modifyRec(2 * int_index, int_left, (int_left + int_right) / 2, mod_left, mod_right, f); modifyRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, 0, half, action[int_index]); modifyRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, mod_left, mod_right, f); value[int_index] = acc(value[2 * int_index], value[2 * int_index + 1]); action[int_index] = OpMonoid::identity(); } } T accumulateRec(const int int_index, const int int_left, const int int_right, const int mod_left, const int mod_right) const { if (mod_left <= int_left and int_right <= mod_right) { return value[int_index]; } else if (int_right <= mod_left or mod_right <= int_left) { return AccMonoid::identity(); } else { return act(action[int_index], acc(accumulateRec(2 * int_index, int_left, (int_left + int_right) / 2, mod_left, mod_right), accumulateRec(2 * int_index + 1, (int_left + int_right) / 2, int_right, mod_left, mod_right))); } } 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) vector<F> action; // Tree for action(length: half) bool has_lazy; const AccMonoid acc{}; const OpMonoid compose{}; const BaseAlgebra act{}; }; struct Sum_Plus { using T = ll; struct AccMonoid { T operator()(const T& a, const T& b) const { return a + b; } static constexpr T identity() { return 0; } }; struct OpMonoid { using T = ll; T operator()(const T& f1, const T& f2) const { return f1 + f2; } static constexpr T identity() { return 0; } }; T operator()(const OpMonoid::T& f, const T& x) const { return f + x; } }; int main() { int N, K; cin >> N >> K; SegmentTree<Sum_Plus> seg(MAX + 1); for (ll i = 0; i < N; i++) { int W; cin >> W; if (W > 0) { if (seg.accumulate(W, MAX + 1) < K) { seg.modify(W, W + 1, 1); } } else { W = -W; const int num = seg.get(W); if (num > 0) { seg.set(W, num - 1); } } } cout << seg.accumulate(0, MAX + 1) << endl; return 0; }