結果

問題 No.877 Range ReLU Query
ユーザー risujirohrisujiroh
提出日時 2019-09-06 21:39:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 1,721 bytes
コンパイル時間 2,143 ms
コンパイル使用メモリ 178,956 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-04-25 21:57:49
合計ジャッジ時間 4,669 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 97 ms
8,704 KB
testcase_12 AC 82 ms
8,112 KB
testcase_13 AC 68 ms
7,424 KB
testcase_14 AC 70 ms
7,168 KB
testcase_15 AC 104 ms
9,344 KB
testcase_16 AC 98 ms
9,088 KB
testcase_17 AC 100 ms
9,240 KB
testcase_18 AC 98 ms
9,324 KB
testcase_19 AC 80 ms
9,452 KB
testcase_20 AC 87 ms
9,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

struct SegmentTree {
  using T = pair<lint, lint>;
  static T op(const T& a, const T& b) { return {a.first + b.first, a.second + b.second}; }
  static constexpr T e() { return {0, 0}; }

  const int n;
  V<T> t;
  SegmentTree(int n) : n(n), t(2 * n, e()) {}
  T& operator[](int i) { return t[i + n]; }
  void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); }
  T acc(int l, int r) const {
    T resl = e(), resr = e();
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) resl = op(resl, t[l++]);
      if (r & 1) resr = op(t[--r], resr);
    }
    return op(resl, resr);
  }
  void set(int i, const T& a) {
    t[i += n] = a;
    while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, q; cin >> n >> q;
  V<> a(n); for (auto&& e : a) cin >> e;
  struct Q { int id, l, r, x; };
  V<Q> qs(q);
  for (int i = 0; i < q; ++i) {
    int tp; cin >> tp;
    int l, r, x; cin >> l >> r >> x, --l;
    qs[i] = {i, l, r, x};
  }
  sort(begin(qs), end(qs), [](const Q& l, const Q& r) { return l.x > r.x; });
  V<> idx(n);
  iota(begin(idx), end(idx), 0);
  sort(begin(idx), end(idx), [&](int i, int j) { return a[i] > a[j]; });
  V<lint> res(q);
  auto itr = begin(idx);
  SegmentTree st(n);
  for (const auto& e : qs) {
    while (itr != end(idx) and a[*itr] >= e.x) {
      st.set(*itr, {a[*itr], 1});
      ++itr;
    }
    auto p = st.acc(e.l, e.r);
    res[e.id] = p.first - p.second * e.x;
  }
  
  for (lint e : res) {
    cout << e << '\n';
  }
}
0