結果

問題 No.2065 Sum of Min
ユーザー kyo1kyo1
提出日時 2022-09-02 22:12:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 3,090 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 218,788 KB
実行使用メモリ 12,572 KB
最終ジャッジ日時 2024-04-27 21:47:04
合計ジャッジ時間 6,090 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 74 ms
12,376 KB
testcase_05 AC 67 ms
12,448 KB
testcase_06 AC 79 ms
12,572 KB
testcase_07 AC 59 ms
12,444 KB
testcase_08 AC 91 ms
12,448 KB
testcase_09 AC 83 ms
12,448 KB
testcase_10 AC 87 ms
12,448 KB
testcase_11 AC 88 ms
12,444 KB
testcase_12 AC 111 ms
12,444 KB
testcase_13 AC 107 ms
12,444 KB
testcase_14 AC 107 ms
12,448 KB
testcase_15 AC 108 ms
12,448 KB
testcase_16 AC 111 ms
12,316 KB
testcase_17 AC 108 ms
12,444 KB
testcase_18 AC 115 ms
12,440 KB
testcase_19 AC 109 ms
12,316 KB
testcase_20 AC 109 ms
12,568 KB
testcase_21 AC 108 ms
12,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename T, typename F>
class SegmentTree {
 public:
  SegmentTree(const std::size_t size, const T identity, const F operate)
      : identity(identity),
        operate(operate),
        nodes((1 << (32 - __builtin_clz(size))) << 1, identity) {}

  SegmentTree(const std::vector<T> &values, const T identity, const F operate)
      : identity(identity),
        operate(operate),
        nodes((1 << (32 - __builtin_clz(values.size()))) << 1, identity) {
    std::copy(values.begin(), values.end(), nodes.begin() + size());
    for (std::size_t i = size() - 1; i > 0; i--) {
      nodes[i] = operate(nodes[i << 1], nodes[(i << 1) | 1]);
    }
  }

  std::size_t size() const { return nodes.size() >> 1; }

  void update(const std::size_t index, const T value) {
    assert(index < size());
    nodes[index + size()] = value;
    for (std::size_t i = (index + size()) >> 1; i > 0; i >>= 1) {
      nodes[i] = operate(nodes[i << 1], nodes[(i << 1) | 1]);
    }
  }

  T fold(const std::size_t left, const std::size_t right) const {  // [left, right)
    assert(left < right && right <= size());
    T x = identity;
    T y = identity;
    for (std::size_t l = left + size(), r = right + size(); l < r; l >>= 1, r >>= 1) {
      if (l % 2 != 0) x = operate(x, nodes[l++]);
      if (r % 2 != 0) y = operate(nodes[--r], y);
    }
    return operate(x, y);
  }

 private:
  const T identity;
  const F operate;

  std::vector<T> nodes;
};

template <typename T, typename F>
SegmentTree<T, F> make_segment_tree(const std::size_t size, const T identity, const F operate) {
  return SegmentTree<T, F>(size, identity, operate);
}

template <typename T, typename F>
SegmentTree<T, F> make_segment_tree(const std::vector<T> &values, const T identity,
                                    const F operate) {
  return SegmentTree<T, F>(values, identity, operate);
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N, Q;
  cin >> N >> Q;
  vector<int64_t> A(N);
  for (auto &&e : A) {
    cin >> e;
  }
  vector<tuple<int64_t, int, int>> XLR(Q);
  for (auto &&[x, l, r] : XLR) {
    cin >> l >> r >> x;
    l--;
  }
  vector<int> indices(Q);
  iota(indices.begin(), indices.end(), 0);
  sort(indices.begin(), indices.end(), [&XLR](const auto a, const auto b) {
    const auto [ax, al, ar] = XLR[a];
    const auto [bx, bl, br] = XLR[b];
    return ax > bx;
  });
  priority_queue<pair<int64_t, int>> que;
  for (int i = 0; i < N; i++) {
    que.emplace(A[i], i);
  }
  auto st1 = make_segment_tree<int64_t>(A, 0, [](const auto a, const auto b) { return a + b; });
  auto st2 = make_segment_tree<int64_t>(N, 0, [](const auto a, const auto b) { return a + b; });
  vector<int64_t> res(Q);
  for (const int i : indices) {
    const auto [x, l, r] = XLR[i];
    while (!que.empty() && x < que.top().first) {
      st1.update(que.top().second, 0);
      st2.update(que.top().second, 1);
      que.pop();
    }
    res[i] = st1.fold(l, r) + x * st2.fold(l, r);
  }
  for (const auto &e : res) {
    cout << e << '\n';
  }
  return 0;
}
0