結果

問題 No.2332 Make a Sequence
ユーザー kwm_tkwm_t
提出日時 2023-06-01 22:59:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 3,947 bytes
コンパイル時間 4,644 ms
コンパイル使用メモリ 263,680 KB
実行使用メモリ 28,436 KB
最終ジャッジ日時 2023-08-28 02:11:29
合計ジャッジ時間 18,362 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 138 ms
23,396 KB
testcase_08 AC 25 ms
5,676 KB
testcase_09 AC 103 ms
18,024 KB
testcase_10 AC 126 ms
22,400 KB
testcase_11 AC 85 ms
15,796 KB
testcase_12 AC 164 ms
27,108 KB
testcase_13 AC 166 ms
28,000 KB
testcase_14 AC 164 ms
27,208 KB
testcase_15 AC 166 ms
27,164 KB
testcase_16 AC 166 ms
27,160 KB
testcase_17 AC 166 ms
28,436 KB
testcase_18 AC 164 ms
28,364 KB
testcase_19 AC 166 ms
28,020 KB
testcase_20 AC 165 ms
27,624 KB
testcase_21 AC 167 ms
27,180 KB
testcase_22 AC 167 ms
27,124 KB
testcase_23 AC 167 ms
27,132 KB
testcase_24 AC 165 ms
27,172 KB
testcase_25 AC 166 ms
27,168 KB
testcase_26 AC 165 ms
27,132 KB
testcase_27 AC 165 ms
27,104 KB
testcase_28 AC 166 ms
26,936 KB
testcase_29 AC 165 ms
27,176 KB
testcase_30 AC 165 ms
26,948 KB
testcase_31 AC 166 ms
26,900 KB
testcase_32 AC 165 ms
25,680 KB
testcase_33 AC 167 ms
25,688 KB
testcase_34 AC 166 ms
25,520 KB
testcase_35 AC 165 ms
26,684 KB
testcase_36 AC 165 ms
25,532 KB
testcase_37 AC 167 ms
26,832 KB
testcase_38 AC 166 ms
27,936 KB
testcase_39 AC 165 ms
26,396 KB
testcase_40 AC 165 ms
27,804 KB
testcase_41 AC 166 ms
26,628 KB
testcase_42 AC 170 ms
26,460 KB
testcase_43 AC 169 ms
25,312 KB
testcase_44 AC 169 ms
25,572 KB
testcase_45 AC 171 ms
25,540 KB
testcase_46 AC 170 ms
25,280 KB
testcase_47 AC 223 ms
23,968 KB
testcase_48 AC 224 ms
25,200 KB
testcase_49 AC 224 ms
24,276 KB
testcase_50 AC 224 ms
24,280 KB
testcase_51 AC 224 ms
23,952 KB
testcase_52 AC 165 ms
26,640 KB
testcase_53 AC 166 ms
26,936 KB
testcase_54 AC 165 ms
27,112 KB
testcase_55 AC 165 ms
26,892 KB
testcase_56 AC 165 ms
26,640 KB
testcase_57 AC 81 ms
10,104 KB
testcase_58 AC 73 ms
9,800 KB
testcase_59 AC 76 ms
10,040 KB
testcase_60 AC 77 ms
9,672 KB
testcase_61 AC 74 ms
8,732 KB
testcase_62 AC 191 ms
25,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
// https://ei1333.github.io/library/structure/convex-hull-trick/dynamic-li-chao-tree.hpp


/**
 * @brief Dynamic-Li-Chao-Tree
 * @docs docs/dynamic-li-chao-tree.md
*/
template< typename T, T x_low, T x_high, T id >
struct DynamicLiChaoTree {

	struct Line {
		T a, b;

		Line(T a, T b) : a(a), b(b) {}

		inline T get(T x) const { return a * x + b; }
	};

	struct Node {
		Line x;
		Node *l, *r;

		Node(const Line &x) : x{ x }, l{ nullptr }, r{ nullptr } {}
	};

	Node *root;

	DynamicLiChaoTree() : root{ nullptr } {}

	Node *add_line(Node *t, Line &x, const T &l, const T &r, const T &x_l, const T &x_r) {
		if (!t) return new Node(x);

		T t_l = t->x.get(l), t_r = t->x.get(r);

		if (t_l <= x_l && t_r <= x_r) {
			return t;
		}
		else if (t_l >= x_l && t_r >= x_r) {
			t->x = x;
			return t;
		}
		else {
			T m = (l + r) / 2;
			if (m == r) --m;
			T t_m = t->x.get(m), x_m = x.get(m);
			if (t_m > x_m) {
				swap(t->x, x);
				if (x_l >= t_l) t->l = add_line(t->l, x, l, m, t_l, t_m);
				else t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r);
			}
			else {
				if (t_l >= x_l) t->l = add_line(t->l, x, l, m, x_l, x_m);
				else t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r);
			}
			return t;
		}
	}

	void add_line(const T &a, const T &b) {
		Line x(a, b);
		root = add_line(root, x, x_low, x_high, x.get(x_low), x.get(x_high));
	}

	Node *add_segment(Node *t, Line &x, const T &a, const T &b, const T &l, const T &r, const T &x_l, const T &x_r) {
		if (r < a || b < l) return t;
		if (a <= l && r <= b) {
			Line y{ x };
			return add_line(t, y, l, r, x_l, x_r);
		}
		if (t) {
			T t_l = t->x.get(l), t_r = t->x.get(r);
			if (t_l <= x_l && t_r <= x_r) return t;
		}
		else {
			t = new Node(Line(0, id));
		}
		T m = (l + r) / 2;
		if (m == r) --m;
		T x_m = x.get(m);
		t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m);
		t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r);
		return t;
	}

	void add_segment(const T &l, const T &r, const T &a, const T &b) {
		Line x(a, b);
		root = add_segment(root, x, l, r - 1, x_low, x_high, x.get(x_low), x.get(x_high));
	}

	T query(const Node *t, const T &l, const T &r, const T &x) const {
		if (!t) return id;
		if (l == r) return t->x.get(x);
		T m = (l + r) / 2;
		if (m == r) --m;
		if (x <= m) return min(t->x.get(x), query(t->l, l, m, x));
		else return min(t->x.get(x), query(t->r, m + 1, r, x));
	}

	T query(const T &x) const {
		return query(root, x_low, x_high, x);
	}
};
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m; cin >> n >> m;
	vector<int>a(n), b(m), c(m);
	rep(i, n)cin >> a[i];
	rep(i, m)cin >> b[i];
	rep(i, m)cin >> c[i];
	vector<int>ab;
	ab = a;
	ab.push_back(-1);
	rep(i, m)ab.push_back(b[i]);
	auto get = z_algorithm(ab);
	get.erase(get.begin(), get.begin() + n + 1);
	DynamicLiChaoTree<long long, 0, 200020, LINF>dp;
	dp.add_segment(0, 1, 0, 0);
	long long ans = -1;
	rep(i, m + 1) {
		long long val = dp.query(i);
		if (LINF == val)break;
		if (m == i) {
			ans = val;
			break;
		}
		dp.add_segment(i, i + get[i] + 1, c[i], val - (long long)c[i] * i);
	}
	cout << ans << endl;
	return 0;
}
0