結果

問題 No.2139 K Consecutive Sushi
ユーザー SSRSSSRS
提出日時 2022-12-03 00:15:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 322 ms / 2,000 ms
コード長 1,580 bytes
コンパイル時間 2,077 ms
コンパイル使用メモリ 171,560 KB
実行使用メモリ 12,376 KB
最終ジャッジ日時 2024-04-18 09:24:48
合計ジャッジ時間 6,879 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 281 ms
12,176 KB
testcase_04 AC 322 ms
12,280 KB
testcase_05 AC 283 ms
12,288 KB
testcase_06 AC 258 ms
12,288 KB
testcase_07 AC 283 ms
12,160 KB
testcase_08 AC 300 ms
12,244 KB
testcase_09 AC 300 ms
12,288 KB
testcase_10 AC 307 ms
12,160 KB
testcase_11 AC 304 ms
12,376 KB
testcase_12 AC 294 ms
12,160 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 27 ms
5,376 KB
testcase_24 AC 91 ms
5,504 KB
testcase_25 AC 214 ms
11,904 KB
testcase_26 AC 25 ms
5,376 KB
testcase_27 AC 75 ms
5,760 KB
testcase_28 AC 74 ms
5,760 KB
testcase_29 AC 36 ms
5,376 KB
testcase_30 AC 100 ms
7,552 KB
testcase_31 AC 272 ms
12,296 KB
testcase_32 AC 37 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000;
template <typename T>
struct lazy_segment_tree{
	int N;
	vector<T> ST;
	vector<T> lazy;
	lazy_segment_tree(int n){
		N = 1;
		while (N < n){
			N *= 2;
		}
		ST = vector<T>(N * 2 - 1, 0);
		lazy = vector<T>(N * 2 - 1, 0);
	}
	void eval(int i){
		if (i < N - 1){
			lazy[i * 2 + 1] += lazy[i];
			lazy[i * 2 + 2] += lazy[i];
		}
		ST[i] += lazy[i];
		lazy[i] = 0;
	}
	void range_add(int L, int R, T x, int i, int l, int r){
		eval(i);
		if (R <= l || r <= L){
			return;
		} else if (L <= l && r <= R){
			lazy[i] += x;
			eval(i);
		} else {
			int m = (l + r) / 2;
			range_add(L, R, x, i * 2 + 1, l, m);
			range_add(L, R, x, i * 2 + 2, m, r);
			ST[i] = max(ST[i * 2 + 1], ST[i * 2 + 2]);
		}
	}
	void range_add(int L, int R, T x){
		range_add(L, R, x, 0, 0, N);
	}
	T range_max(int L, int R, int i, int l, int r){
		eval(i);
		if (R <= l || r <= L){
			return -INF;
		} else if (L <= l && r <= R){
			return ST[i];
		} else {
			int m = (l + r) / 2;
			return max(range_max(L, R, i * 2 + 1, l, m), range_max(L, R, i * 2 + 2, m, r));
		}
	}
	T range_max(int L, int R){
		return range_max(L, R, 0, 0, N);
	}
	T all(){
		eval(0);
		return ST[0];
	}
};
int main(){
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  lazy_segment_tree<long long> dp(N + 1);
  for (int i = 0; i < N; i++){
    dp.range_add(i + 1, i + 2, dp.range_max(max(i - K + 1, 0), i + 1));
    dp.range_add(max(i - K + 2, 0), i + 1, A[i]);
  }
  cout << dp.all() << endl;
}
0