結果

問題 No.877 Range ReLU Query
ユーザー ngtkanangtkana
提出日時 2019-09-09 11:56:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 238 ms / 2,000 ms
コード長 4,420 bytes
コンパイル時間 3,064 ms
コンパイル使用メモリ 226,920 KB
実行使用メモリ 13,052 KB
最終ジャッジ日時 2024-04-25 22:14:44
合計ジャッジ時間 6,115 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 3 ms
6,816 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 232 ms
12,868 KB
testcase_12 AC 206 ms
12,468 KB
testcase_13 AC 168 ms
9,940 KB
testcase_14 AC 179 ms
10,268 KB
testcase_15 AC 238 ms
12,776 KB
testcase_16 AC 223 ms
12,748 KB
testcase_17 AC 228 ms
12,900 KB
testcase_18 AC 225 ms
12,776 KB
testcase_19 AC 216 ms
12,920 KB
testcase_20 AC 233 ms
13,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
  std::cerr << " " << head;
  debug_impl(tail...);
}
#define debug(...)\
  std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
  debug_impl(__VA_ARGS__);\
  std::cerr << std::noboolalpha;

template<typename Value, typename BinaryOp>
class segment_tree {
    int                 sz, n, N;
    BinaryOp            op;
    Value               id;
    std::vector<Value>  table;

    auto& op_eq(Value& x, Value y) const { return x = op(x, y); }

    void merge(int u)
      { table.at(u) = op(table.at(2 * u), table.at(2 * u + 1)); }

    Value query_impl(int l, int r, int k, int L, int R) const {
      return l <= L && R <= r
        ? table.at(k)
        : R <= l || r <= L
        ? id
        : op(
          query_impl(l, r, 2 * k, L, (L + R) / 2),
          query_impl(l, r, 2 * k + 1, (L + R) / 2, R)
        );
    }

  public:
    segment_tree(int sz, BinaryOp op, Value id):
      sz    (sz),
      n     (std::pow(2, int(std::log2(2 * sz - 1)))),
      N     (n * 2),
      op    (op),
      id    (id),
      table (N, id)
      {}

    auto& at(int i)       { return table.at(n + i); }

    auto& at(int i) const { return table.at(n + i); }

    auto collect() const {
      auto ret = std::vector<Value>(sz);
      for (auto i = 0; i < sz; i++)
        { ret.at(i) = at(i); }
      return ret;
    }

    auto query(int l, int r) const
      { return query_impl(l, r, 1, 0, n); }

    void build_oneline(int i)
      { for (i += n, i /= 2; i > 0; i /= 2) merge(i); }

    void build()
      { for (auto i = 1; i < n; i++) merge(i); }

    void update (int u, Value val) {
      at(u) = val;
      build_oneline(u);
    }

    void add (int u, Value val) {
      at(u) += val;
      build_oneline(u);
    }
};

template<typename Value, typename BinaryOp>
auto make_segment_tree(int sz, BinaryOp op, Value id)
  { return segment_tree<Value, BinaryOp>(sz, std::move(op), id); }

template <typename T>
std::istream& operator>> (std::istream& is, std::vector<T>& v) {
  for (auto & x : v) is >> x;
  return is;
}

template <typename T>
std::ostream& operator<< (std::ostream& os, const std::vector<T>& v) {
  auto n = v.size();
  os << "{";
  for (size_t i = 0; i < n; i++)
    {os << (i > 0 ? "," : "") << v.at(i);}
  return os << "}";
}

template <typename T, typename U>
std::ostream& operator<< (std::ostream& os, const std::pair<T, U>& pair)
  { return os << "(" << pair.first << "," << pair.second << ")"; }

template <typename T, typename U>
std::istream& operator>> (std::iostream& is, std::pair<T, U>& pair)
  { return is >> pair.first >> pair.second; }

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n, q; std::cin >> n >> q;
  auto tree = make_segment_tree(
    n,
    [](auto x, auto y){ return std::make_pair(x.first + y.first, x.second + y.second); },
    std::make_pair(0LL, 0)
  );
  std::vector<std::pair<int, int>> pairs;
  rep(i, 0, n) {
    int x; std::cin >> x;
    pairs.emplace_back(std::make_pair(x, i));
  }
  std::sort(all(pairs));
  std::reverse(all(pairs));
  std::vector<std::tuple<int, int, int, int>> queries;
  rep(i, 0, q) {
    int c, l, r, x;
    std::cin >> c >> l >> r >> x;
    queries.emplace_back(std::make_tuple(x, l, r, i));
  }
  std::sort(all(queries));
  std::reverse(all(queries));
  std::vector<std::pair<int, lint>> answers;
  auto it = pairs.begin();
  for (auto query : queries) {
    int x, l, r, i; std::tie(x, l, r, i) = query;
    l--;
    // debug(x, l, r, i);
    for (; it != pairs.end(); it++) {
      int y, j; std::tie(y, j) = *it;
      if (y <= x) break;
      tree.update(j, std::make_pair(y, 1));
    }
    // debug(tree.collect());
    lint sum, len; std::tie(sum, len) = tree.query(l, r);
    auto ret = sum - len * x;
    answers.emplace_back(i, ret);
  }
  std::sort(all(answers));
  for (auto pair : answers)
    { std::cout << pair.second << std::endl; }
  return 0;
}
0