結果

問題 No.877 Range ReLU Query
ユーザー leaf_1415leaf_1415
提出日時 2019-09-06 21:49:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,868 bytes
コンパイル時間 957 ms
コンパイル使用メモリ 76,520 KB
実行使用メモリ 18,212 KB
最終ジャッジ日時 2024-04-25 21:58:31
合計ジャッジ時間 3,755 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,168 KB
testcase_01 AC 4 ms
7,296 KB
testcase_02 AC 6 ms
9,460 KB
testcase_03 AC 5 ms
7,424 KB
testcase_04 AC 4 ms
7,296 KB
testcase_05 AC 5 ms
9,408 KB
testcase_06 AC 5 ms
9,280 KB
testcase_07 AC 5 ms
9,328 KB
testcase_08 AC 6 ms
9,328 KB
testcase_09 AC 5 ms
7,296 KB
testcase_10 AC 5 ms
9,456 KB
testcase_11 AC 151 ms
17,280 KB
testcase_12 AC 131 ms
16,668 KB
testcase_13 AC 96 ms
13,236 KB
testcase_14 AC 107 ms
16,492 KB
testcase_15 AC 153 ms
17,704 KB
testcase_16 AC 139 ms
17,952 KB
testcase_17 AC 146 ms
17,136 KB
testcase_18 AC 139 ms
18,212 KB
testcase_19 AC 111 ms
17,708 KB
testcase_20 AC 122 ms
16,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;
typedef pair<llint, P> T;

struct SegTree{
	int size;
	vector<llint> seg;
	
	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++) seg[i] = 0;
	}
	
	void update(int i, llint val)
	{
		i += (1 << size);
		seg[i] = val;
		while(i > 1){
			i /= 2;
			seg[i] = (seg[i*2] + seg[i*2+1]);
		}
	}

	llint query(int a, int b, int k, int l, int r)
	{
		if(b < l || r < a) return 0;
		if(a <= l && r <= b) return seg[k];
		llint lval = query(a, b, k*2, l, (l+r)/2);
		llint rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return lval + rval;
	}
	llint query(int a, int b)
	{
		if(a > b) return 0;
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

llint n, Q;
llint a[100005];
llint L[100005], R[100005], X[100005];
SegTree seg(17), seg2(17);
llint ans[100005];
vector<T> vec;

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> Q;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= Q; i++) cin >> L[i] >> L[i] >> R[i] >> X[i];
	
	for(int i = 1; i <= n; i++) vec.push_back(make_pair(a[i], make_pair(1, i)));
	for(int i = 1; i <= Q; i++) vec.push_back(make_pair(X[i], make_pair(2, i)));
	sort(vec.rbegin(), vec.rend());
	
	seg.init(), seg2.init();
	for(int i = 0; i < vec.size(); i++){
		if(vec[i].second.first == 1){
			seg.update(vec[i].second.second, vec[i].first);
			seg2.update(vec[i].second.second, 1);
		}
		else{
			llint x = vec[i].first, id = vec[i].second.second;
			llint l = L[id], r = R[id];
			llint tmp = seg.query(l, r) + (r-l+1-seg2.query(l, r))*x;
			tmp -= (r-l+1) * x;
			ans[id] = tmp;
		}
	}
	for(int i = 1; i <= Q; i++) cout << ans[i] << "\n";
	flush(cout);
	
	return 0;
}
0