結果

問題 No.877 Range ReLU Query
ユーザー le_panda_noirle_panda_noir
提出日時 2020-03-28 09:06:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,048 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 120,172 KB
実行使用メモリ 19,488 KB
最終ジャッジ日時 2023-08-30 17:23:49
合計ジャッジ時間 9,741 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 4 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#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<int> T(N, 0, [](int a, int b){return a + b;});
  SegTree<int> T2(N, 0, [](int a, int b){return a + b;});

  vector<int> 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