結果

問題 No.877 Range ReLU Query
ユーザー tkmst201tkmst201
提出日時 2019-09-06 23:12:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 258 ms / 2,000 ms
コード長 2,594 bytes
コンパイル時間 1,842 ms
コンパイル使用メモリ 178,824 KB
実行使用メモリ 15,964 KB
最終ジャッジ日時 2024-04-25 22:06:56
合計ジャッジ時間 5,695 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 248 ms
14,808 KB
testcase_12 AC 216 ms
14,808 KB
testcase_13 AC 168 ms
11,480 KB
testcase_14 AC 169 ms
10,712 KB
testcase_15 AC 258 ms
15,964 KB
testcase_16 AC 244 ms
15,324 KB
testcase_17 AC 245 ms
15,188 KB
testcase_18 AC 246 ms
14,804 KB
testcase_19 AC 194 ms
14,808 KB
testcase_20 AC 230 ms
14,804 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:87:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   87 |                 scanf("%d", &a);
      |                 ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, pii> P;
const ll INF = 1ll<<60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

struct SegTree {
	int n;
	vector<pll> dat, sum;
	
	SegTree(int _n) {
		n = 1;
		while (n < _n) n *= 2;
		dat.resize(n * 2 - 1, pll(INF, 0));
		sum.resize(n * 2 - 1, pll(0, 0));
	}
	
	pll add(pll a, pll b) {
		return pll(a.fi + b.fi, a.se + b.se);
	}
	
	void update(int pos, pll x) {
		int i = pos + n - 1;
		dat[i] = min(dat[i], x);
		while (i > 0) {
			i = (i - 1) / 2;
			dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
		}
		
		i = pos + n - 1;
		if (dat[i].se != -1) sum[i] = pll(x.fi, 1);
		else sum[i] = pll(0, 0);
		
		while (i > 0) {
			i = (i - 1) / 2;
			sum[i] = add(sum[i * 2 + 1], sum[i * 2 + 2]);
		}
	}
	
	void set(int i, pll x) {
		dat[i + n - 1] = x;
		update(i, x);
	}
	
	pll sumquery(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0) r = n;
		
		if (r <= a || b <= l) return pll(0, 0);
		
		if (a <= l && r <= b) return sum[k];
		return add(sumquery(a, b, k * 2 + 1, l, (l + r) / 2), sumquery(a, b, k * 2 + 2, (l + r) / 2, r));
	}
	
	// 探索範囲[a,b), 現在見ているkノード[l,r)
	pll minquery(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0) r = n;
		
		if (r <= a || b <= l) return pll(INF, 0);
		if (a <= l && r <= b) return dat[k];
		return min( minquery(a, b, k * 2 + 1, l, (l + r) / 2), minquery(a, b, k * 2 + 2, (l + r) / 2, r) );
		
	}
};

int N, Q;
ll ans[112345];

int main() {
	cin >> N >> Q;
	
	SegTree seg(N);
	REP(i, N) {
		int a;
		scanf("%d", &a);
		seg.set(i, pll(a, i));
	}
	
	vector<P> v;
	REP(i, Q) {
		int q, l, r;
		cin >> q >> l >> r;
		l--; r--;
		if (q == 1) {
			int x; cin >> x;
			v.push_back(P(pii(x, i), pii(l, r)));
		}
	}
	sort(ALL(v));
	
	REP(i, Q) {
		int x = v[i].fi.fi;
		int qidx = v[i].fi.se;
		int l = v[i].se.fi;
		int r = v[i].se.se + 1;
		
		pll cur;
		while ((cur = seg.minquery(0, N)).fi <= x) {
			seg.set(cur.se, pll(INF, -1));
		}
		
		cur = seg.sumquery(l, r);
		ans[qidx] = cur.fi - cur.se * x;
	}
	
	REP(i, Q) printf("%lld\n", ans[i]);
	
	
	return 0;
}
0