結果

問題 No.2065 Sum of Min
ユーザー shobonvipshobonvip
提出日時 2022-09-02 22:32:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,937 ms / 2,000 ms
コード長 994 bytes
コンパイル時間 4,359 ms
コンパイル使用メモリ 265,436 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-27 22:08:46
合計ジャッジ時間 23,782 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 517 ms
6,940 KB
testcase_05 AC 821 ms
6,940 KB
testcase_06 AC 989 ms
6,944 KB
testcase_07 AC 622 ms
6,944 KB
testcase_08 AC 565 ms
6,944 KB
testcase_09 AC 292 ms
6,944 KB
testcase_10 AC 1,937 ms
6,944 KB
testcase_11 AC 1,928 ms
6,940 KB
testcase_12 AC 916 ms
6,944 KB
testcase_13 AC 915 ms
6,940 KB
testcase_14 AC 926 ms
6,940 KB
testcase_15 AC 916 ms
6,940 KB
testcase_16 AC 916 ms
6,944 KB
testcase_17 AC 917 ms
6,940 KB
testcase_18 AC 927 ms
6,944 KB
testcase_19 AC 919 ms
6,940 KB
testcase_20 AC 924 ms
6,944 KB
testcase_21 AC 919 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;

typedef modint998244353 mint;
typedef long long ll;

int main(){
	int n,q;
	cin >> n >> q;
	int b = int(pow(n, 0.5) + 1);
	vector<ll> a(n), dat(n), pfix(n);
	for (int i=0; i<n; i++){
		cin >> a[i];
		dat[i] = a[i];
	}

	for (int i=0; i<b; i++){
		sort(dat.begin() + min(n, i*b), dat.begin() + min(n, (i+1)*b));
		ll tmp = 0;
		for (int j=i*b; j<min(n, (i+1)*b); j++){
			tmp += dat[j];
			pfix[j] = tmp;
		}
	}

	for (int cases=0; cases<q; cases++){
		int l,r;
		ll x;
		cin >> l >> r >> x;
		l--;
		int tl = l+b-(l%b), tr = r-(r%b);
		ll ans = 0;
		for (int i=l; i<min(r,tl); i++){
			ans += min(x, a[i]);
		}

		for (int i=tl/b; i<tr/b; i++){
			int targ = lower_bound(dat.begin() + i*b, dat.begin() + (i+1)*b, x) - dat.begin() - i*b;
			ans += (b-targ) * x;
			if (targ > 0) ans += pfix[i*b+targ-1];
		}

		for (int i=max(tl,tr); i<r; i++){
			ans += min(x, a[i]);
		}

		cout << ans << endl;
	}
}
0