結果
| 問題 |
No.877 Range ReLU Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-06 22:42:03 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 521 ms / 2,000 ms |
| コード長 | 2,426 bytes |
| コンパイル時間 | 2,577 ms |
| コンパイル使用メモリ | 195,748 KB |
| 実行使用メモリ | 23,396 KB |
| 最終ジャッジ日時 | 2024-11-08 10:08:32 |
| 合計ジャッジ時間 | 8,512 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 20 |
ソースコード
#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using P = pair<ll, ll>;
template<typename T>
class SegmentTree {
private:
using F = function<T(T, T)>; // モノイド型
int n; // 横幅
F f; // モノイド
T e; // モノイド単位元
vector<T> data;
public:
// init忘れに注意
SegmentTree() {}
SegmentTree(F f, T e) :f(f), e(e) {}
void init(int n_) {
n = 1;
while (n < n_)n <<= 1;
data.assign(n << 1, e);
}
void build(const vector<T>& v) {
int n_ = v.size();
init(n_);
rep(i, n_)data[n + i] = v[i];
for (int i = n - 1; i >= 0; i--) {
data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
}
}
void set_val(int idx, T val) {
idx += n;
data[idx] = val;
while (idx >>= 1) {
data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
}
}
T query(int a, int b) {
// [a,b)
T vl = e, vr = e;
for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
if (l & 1)vl = f(vl, data[l++]); // unknown
if (r & 1)vr = f(data[--r], vr); // unknown
}
return f(vl, vr);
}
};
int main()
{
// 入出力の準備
int N, Q;
cin >> N >> Q;
vector<ll> a(N);
rep(i, N)cin >> a[i];
vector<vector<int>> querys;
rep(q, Q) {
int com, l, r, x;
cin >> com >> l >> r >> x;
l--; r--;
querys.push_back({ x,l,r,q });
}
sort(querys.begin(), querys.end());
priority_queue<P, vector<P>, greater<P>> pq;
rep(i, N)pq.push({ a[i],i });
// セグ木の準備
constexpr ll e = 0;
function<ll(ll, ll)> f = [](ll a, ll b) {
return a + b;
};
SegmentTree<ll> lower_sum(f, e);
SegmentTree<ll> lower_num(f, e);
SegmentTree<ll> all_a(f, e);
lower_sum.init(N);
lower_num.init(N);
all_a.build(a);
// クエリ昇順処理
map<int, ll> mp;
for (auto query : querys) {
ll x = query[0];
ll l = query[1];
ll r = query[2];
ll qnum = query[3];
// 低い方を探して埋め込む
while (!pq.empty() && pq.top().first <= x) {
ll val, idx;
tie(val, idx) = pq.top(); pq.pop();
lower_sum.set_val(idx, val);
lower_num.set_val(idx, 1);
}
// 計算
ll range_sum = lower_sum.query(l, r + 1);
ll range_count = lower_num.query(l, r + 1);
range_count = (r - l + 1) - range_count;
ll res = all_a.query(l, r + 1);
res -= range_sum;
res -= range_count * x;
mp[qnum] = res;
}
for (auto itr : mp)cout << itr.second << endl;
return 0;
}