結果

問題 No.877 Range ReLU Query
ユーザー tomatoma
提出日時 2019-09-06 22:42:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 504 ms / 2,000 ms
コード長 2,426 bytes
コンパイル時間 2,269 ms
コンパイル使用メモリ 193,676 KB
実行使用メモリ 23,496 KB
最終ジャッジ日時 2023-08-08 04:22:10
合計ジャッジ時間 8,310 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 497 ms
22,688 KB
testcase_12 AC 422 ms
20,804 KB
testcase_13 AC 337 ms
15,640 KB
testcase_14 AC 360 ms
16,352 KB
testcase_15 AC 504 ms
22,968 KB
testcase_16 AC 490 ms
22,044 KB
testcase_17 AC 495 ms
22,180 KB
testcase_18 AC 488 ms
22,128 KB
testcase_19 AC 432 ms
23,496 KB
testcase_20 AC 483 ms
23,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0