結果

問題 No.1117 数列分割
ユーザー Y17Y17
提出日時 2020-07-17 23:22:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,417 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 178,116 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 04:17:14
合計ジャッジ時間 37,844 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 250 ms
4,380 KB
testcase_04 AC 253 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 263 ms
4,376 KB
testcase_09 AC 75 ms
4,376 KB
testcase_10 AC 272 ms
4,380 KB
testcase_11 AC 1,194 ms
4,376 KB
testcase_12 AC 1,297 ms
4,376 KB
testcase_13 AC 1,138 ms
4,380 KB
testcase_14 AC 1,793 ms
4,380 KB
testcase_15 AC 1,787 ms
4,376 KB
testcase_16 AC 2,679 ms
4,376 KB
testcase_17 AC 325 ms
4,376 KB
testcase_18 AC 2,626 ms
4,376 KB
testcase_19 AC 2,974 ms
4,380 KB
testcase_20 AC 2,348 ms
4,376 KB
testcase_21 AC 2,989 ms
4,380 KB
testcase_22 TLE -
testcase_23 TLE -
testcase_24 AC 2,990 ms
4,384 KB
testcase_25 TLE -
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 192 ms
4,376 KB
testcase_28 AC 191 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;
  using G = function< Monoid(Monoid, OperatorMonoid) >;
  using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;

  int sz, height;
  vector< Monoid > data;
  vector< OperatorMonoid > lazy;
  const F f;
  const G g;
  const H h;
  const Monoid M1;
  const OperatorMonoid OM0;


  LazySegmentTree(int n, const F f, const G g, const H h,
                  const Monoid &M1, const OperatorMonoid OM0)
      : f(f), g(g), h(h), M1(M1), OM0(OM0) {
    sz = 1;
    height = 0;
    while(sz < n) sz <<= 1, height++;
    data.assign(2 * sz, M1);
    lazy.assign(2 * sz, OM0);
  }

  void set(int k, const Monoid &x) {
    data[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      data[k] = f(data[2 * k + 0], data[2 * k + 1]);
    }
  }

  inline void propagate(int k) {
    if(lazy[k] != OM0) {
      lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      data[k] = reflect(k);
      lazy[k] = OM0;
    }
  }

  inline Monoid reflect(int k) {
    return lazy[k] == OM0 ? data[k] : g(data[k], lazy[k]);
  }

  inline void recalc(int k) {
    while(k >>= 1) data[k] = f(reflect(2 * k + 0), reflect(2 * k + 1));
  }

  inline void thrust(int k) {
    for(int i = height; i > 0; i--) propagate(k >> i);
  }

  void update(int a, int b, const OperatorMonoid &x) {
    thrust(a += sz);
    thrust(b += sz - 1);
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) lazy[l] = h(lazy[l], x), ++l;
      if(r & 1) --r, lazy[r] = h(lazy[r], x);
    }
    recalc(a);
    recalc(b);
  }

  Monoid query(int a, int b) {
    thrust(a += sz);
    thrust(b += sz - 1);
    Monoid L = M1, R = M1;
    for(int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if(l & 1) L = f(L, reflect(l++));
      if(r & 1) R = f(reflect(--r), R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) {
    return query(k, k + 1);
  }

  template< typename C >
  int find_subtree(int a, const C &check, Monoid &M, bool type) {
    while(a < sz) {
      propagate(a);
      Monoid nxt = type ? f(reflect(2 * a + type), M) : f(M, reflect(2 * a + type));
      if(check(nxt)) a = 2 * a + type;
      else M = nxt, a = 2 * a + 1 - type;
    }
    return a - sz;
  }

  template< typename C >
  int find_first(int a, const C &check) {
    Monoid L = M1;
    if(a <= 0) {
      if(check(f(L, reflect(1)))) return find_subtree(1, check, L, false);
      return -1;
    }
    thrust(a + sz);
    int b = sz;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) {
        Monoid nxt = f(L, reflect(a));
        if(check(nxt)) return find_subtree(a, check, L, false);
        L = nxt;
        ++a;
      }
    }
    return -1;
  }


  template< typename C >
  int find_last(int b, const C &check) {
    Monoid R = M1;
    if(b >= sz) {
      if(check(f(reflect(1), R))) return find_subtree(1, check, R, true);
      return -1;
    }
    thrust(b + sz - 1);
    int a = sz;
    for(b += sz; a < b; a >>= 1, b >>= 1) {
      if(b & 1) {
        Monoid nxt = f(reflect(--b), R);
        if(check(nxt)) return find_subtree(b, check, R, true);
        R = nxt;
      }
    }
    return -1;
  }
};


signed main(){
	int n, k, m;
	cin >> n >> k >> m;

	int a[3010];
	for(int i = 0;i < n;i++) cin >> a[i];


	int tmp[100010];
	for(int j = 1;j <= k;j++){
		LazySegmentTree<pair<int, int>, int> segA(n+1, 
		[](auto a, auto b){ return make_pair(max(a.first, b.first), max(a.second, b.second)); },
		[](auto a, int b){ 
			a.first += b;
			a.second -= b;
			return a; },
		[](int a, int b){ return a + b; },
		make_pair(INT_MIN*10000ll, INT_MIN*10000ll), 0);

		if(j == 1){
			segA.set(0, {0, 0});
		}else{
			for(int i = 0;i <= n;i++){
				segA.set(i, {tmp[i], tmp[i]});
			}
		}
		segA.build();
		for(int i = 0;i <= n;i++){
			if(i < j){
				tmp[i] = INT_MIN*10000ll;
			}else{
				segA.update(0, i, a[i-1]);
				pair<int, int> dp = segA.query(max(0ll, i-m), i);
				tmp[i] = max(dp.first, dp.second);
			}
		}
	}

	cout << tmp[n] << endl;

	return 0;
}
0