結果

問題 No.877 Range ReLU Query
ユーザー le_panda_noirle_panda_noir
提出日時 2020-03-28 09:08:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 611 ms / 2,000 ms
コード長 2,061 bytes
コンパイル時間 1,728 ms
コンパイル使用メモリ 121,528 KB
実行使用メモリ 24,944 KB
最終ジャッジ日時 2024-04-25 22:21:08
合計ジャッジ時間 8,301 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,948 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 4 ms
6,944 KB
testcase_11 AC 586 ms
24,944 KB
testcase_12 AC 515 ms
22,352 KB
testcase_13 AC 393 ms
16,376 KB
testcase_14 AC 427 ms
19,480 KB
testcase_15 AC 611 ms
24,068 KB
testcase_16 AC 588 ms
22,896 KB
testcase_17 AC 589 ms
23,224 KB
testcase_18 AC 579 ms
22,788 KB
testcase_19 AC 519 ms
23,788 KB
testcase_20 AC 556 ms
24,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;

#define rep(i,n) for(int i=0;i<(n);++i)

#include <functional>
template<class T> struct SegTree {
  private:
    int size;
    vector<T> v;
    using F = function<T(T,T)>;
    F f; T default_value;
  public:
    SegTree(int n, T default_value, F f) : default_value(default_value), f(f) {
      size = 1;
      while (size < n) size *= 2;
      v.resize(2 * size - 1, default_value);
    }
    void update(int index, T val) {
      index = index + size - 1;
      v[index] = val;
      while (index > 0) {
        index = (index - 1) / 2;
        v[index] = f(v[2 * index + 1], v[2 * index + 2]);
      }
    }
    T query(int l, int r) { return query(l, r, 0, size, 0); }
    T query(int l, int r, int cur_l, int cur_r, int index) {
      // [l, r)に対するクエリ
      if (cur_r <= l || r <= cur_l) return default_value;
      if (l <= cur_l && cur_r <= r) return v[index];

      int mid = (cur_l + cur_r) / 2;
      return f(query(l, r, cur_l, mid, 2 * index + 1), query(l, r, mid, cur_r, 2 * index + 2));
    }
};



int main() {
  int N; cin >> N;
  int Q; cin >> Q;

  priority_queue<pair<pii, int>> q;
  rep(i, N) {
    int a; cin >> a;
    q.emplace(pii(a, 0), i);
  }

  map<int, vector<pii>> m;
  rep(i, Q) {
    int t; cin >> t;
    int l, r, x;
    cin >> l >> r >> x;
    --l;
    q.emplace(pii(x, 1), i);
    m[x].emplace_back(l, r);
  }

  SegTree<ll> T(N, 0, [](ll a, ll b){return a + b;});
  SegTree<ll> T2(N, 0, [](ll a, ll b){return a + b;});

  vector<ll> ans(Q);
  while (!q.empty()) {
    auto p = q.top(); q.pop();
    if (p.first.second == 0) {
      T.update(p.second, p.first.first);
      T2.update(p.second, 1);
    } else {
      int l = m[p.first.first].back().first,
          r = m[p.first.first].back().second;
      m[p.first.first].pop_back();
      ans[p.second] = T.query(l, r) - T2.query(l, r) * p.first.first;
    }
  }

  rep(i, Q)
    cout << ans[i] << endl;

  return 0;
}
0