답안 #676251

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676251 2022-12-29T22:32:51 Z dutinmeow Two Dishes (JOI19_dishes) C++17
100 / 100
7337 ms 311880 KB
#include <bits/stdc++.h>

template<class segment_tree_template>
class lazy_segment_tree : public segment_tree_template {
	using T = typename segment_tree_template::node_type;
	using L = typename segment_tree_template::lazy_type;
	using segment_tree_template::node_default_value;
	using segment_tree_template::lazy_default_value;
	using segment_tree_template::merge;
	using segment_tree_template::apply;

protected:
	int n;
	std::vector<T> tree;
	std::vector<L> lazy;

private:
	void pushdown(int t, int tl, int tr) {
		if (lazy[t] == lazy_default_value)
			return;
		int tm = (tl + tr) / 2;
		apply(tree[t * 2], lazy[t * 2], lazy[t], tl, tm);
		apply(tree[t * 2 + 1], lazy[t * 2 + 1], lazy[t], tm + 1, tr);
		lazy[t] = lazy_default_value;
	}

	void insert(int i, T v, int t, int tl, int tr) {
		if (tl == tr) {
			tree[t] = v;
			return;
		}
		pushdown(t, tl, tr);
		int tm = (tl + tr) / 2;
		if (i <= tm)
			insert(i, v, t * 2, tl, tm);
		else
			insert(i, v, t * 2 + 1, tm + 1, tr);
		tree[t] = merge(tree[t * 2], tree[t * 2 + 1]);
	}

	void update(int l, int r, L v, int t, int tl, int tr) {
		if (r < tl || tr < l)
			return;
		if (l <= tl && tr <= r) {
			apply(tree[t], lazy[t], v, tl, tr);
			return;
		}
		pushdown(t, tl, tr);
		int tm = (tl + tr) / 2;
		update(l, r, v, t * 2, tl, tm);
		update(l, r, v, t * 2 + 1, tm + 1, tr);
		tree[t] = merge(tree[t * 2], tree[t * 2 + 1]);
	}

	T query(int l, int r, int t, int tl, int tr) {
		if (r < tl || tr < l)
			return node_default_value;
		if (l <= tl && tr <= r)
			return tree[t];
		pushdown(t, tl, tr);
		int tm = (tl + tr) / 2;
		return merge(query(l, r, t * 2, tl, tm), query(l, r, t * 2 + 1, tm + 1, tr));
	}

public:
	lazy_segment_tree() = default;

	lazy_segment_tree(int _n) { init(_n); }

	void init(int _n) {
		n = _n;
		tree.assign(4 * n, node_default_value);
		lazy.assign(4 * n, lazy_default_value);
	}

	void insert(int i, T v) { insert(i, v, 1, 0, n - 1); }

	void update(int l, int r, L v) { update(l, r, v, 1, 0, n - 1); }

	T query(int l, int r) { return query(l, r, 1, 0, n - 1); }
};

const long long INF = 1e18;

struct range_add_range_max_segment_tree_template {
	using node_type = long long;
	using lazy_type = long long;
	const node_type node_default_value = -INF;
	const lazy_type lazy_default_value = 0;

	node_type merge(node_type a, node_type b) { return std::max(a, b); }

	void apply(node_type &a, lazy_type &b, lazy_type c, int tl, int tr) { a += c, b += c; }
};

using range_add_range_max_segment_tree = lazy_segment_tree<range_add_range_max_segment_tree_template>;

int main() {
	using namespace std;

	int N, M;
	cin >> N >> M;
	vector<long long> A(N + 1), S(N + 1), P(N + 1);
	for (int i = 1; i <= N; i++)
		cin >> A[i] >> S[i] >> P[i];
	vector<long long> B(M + 1), T(M + 1), Q(M + 1);
	for (int i = 1; i <= M; i++)
		cin >> B[i] >> T[i] >> Q[i];
	vector<long long> C(N + 1), D(M + 1);
	C[0] = D[0] = 0;
	for (int i = 1; i <= N; i++)
		C[i] = C[i - 1] + A[i];
	for (int i = 1; i <= M; i++)
		D[i] = D[i - 1] + B[i];

	long long ans = 0;
	map<pair<int, int>, long long> Z;
	for (int i = 1; i <= N; i++) {
		if (C[i] > S[i])
			continue;
		if (C[i] + D[M] <= S[i]) {
			ans += P[i];
			continue;
		}
		int ind = prev(upper_bound(D.begin(), D.end(), S[i] - C[i])) - D.begin();
		Z[{i - 1, -ind - 1}] += P[i];
	}
	for (int i = 1; i <= M; i++) {
		if (D[i] > T[i])
			continue;
		if (D[i] + C[N] <= T[i]) {
			ans += Q[i];
			continue;
		}
		int ind = prev(upper_bound(C.begin(), C.end(), T[i] - D[i])) - C.begin();
		Z[{ind, -i}] -= Q[i], ans += Q[i];
	}

	range_add_range_max_segment_tree sgt(M + 2);
	for (int i = 0; i <= M + 1; i++)
		sgt.insert(i, 0);
	for (auto [k, v] : Z) {
		int x = k.first, y = -k.second;
		auto tem = sgt.query(0, y);
		sgt.insert(y, tem);
		sgt.update(0, y - 1, v);
	}

	cout << sgt.query(0, M + 1) + ans << '\n';
}

Compilation message

dishes.cpp: In function 'int main()':
dishes.cpp:143:7: warning: unused variable 'x' [-Wunused-variable]
  143 |   int x = k.first, y = -k.second;
      |       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 685 ms 53924 KB Output is correct
2 Correct 697 ms 53304 KB Output is correct
3 Correct 516 ms 38744 KB Output is correct
4 Correct 618 ms 46816 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 649 ms 49468 KB Output is correct
7 Correct 279 ms 25900 KB Output is correct
8 Correct 254 ms 13564 KB Output is correct
9 Correct 552 ms 39636 KB Output is correct
10 Correct 520 ms 45276 KB Output is correct
11 Correct 351 ms 33104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 300 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 300 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 5 ms 724 KB Output is correct
18 Correct 5 ms 696 KB Output is correct
19 Correct 7 ms 852 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 6 ms 844 KB Output is correct
22 Correct 5 ms 724 KB Output is correct
23 Correct 5 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 300 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 5 ms 724 KB Output is correct
18 Correct 5 ms 696 KB Output is correct
19 Correct 7 ms 852 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 6 ms 844 KB Output is correct
22 Correct 5 ms 724 KB Output is correct
23 Correct 5 ms 724 KB Output is correct
24 Correct 528 ms 48556 KB Output is correct
25 Correct 560 ms 48448 KB Output is correct
26 Correct 573 ms 48548 KB Output is correct
27 Correct 584 ms 48488 KB Output is correct
28 Correct 549 ms 42452 KB Output is correct
29 Correct 442 ms 36728 KB Output is correct
30 Correct 1047 ms 60132 KB Output is correct
31 Correct 315 ms 30576 KB Output is correct
32 Correct 229 ms 18044 KB Output is correct
33 Correct 689 ms 47852 KB Output is correct
34 Correct 859 ms 54592 KB Output is correct
35 Correct 858 ms 54260 KB Output is correct
36 Correct 817 ms 53392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 300 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 5 ms 724 KB Output is correct
18 Correct 5 ms 696 KB Output is correct
19 Correct 7 ms 852 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 6 ms 844 KB Output is correct
22 Correct 5 ms 724 KB Output is correct
23 Correct 5 ms 724 KB Output is correct
24 Correct 528 ms 48556 KB Output is correct
25 Correct 560 ms 48448 KB Output is correct
26 Correct 573 ms 48548 KB Output is correct
27 Correct 584 ms 48488 KB Output is correct
28 Correct 549 ms 42452 KB Output is correct
29 Correct 442 ms 36728 KB Output is correct
30 Correct 1047 ms 60132 KB Output is correct
31 Correct 315 ms 30576 KB Output is correct
32 Correct 229 ms 18044 KB Output is correct
33 Correct 689 ms 47852 KB Output is correct
34 Correct 859 ms 54592 KB Output is correct
35 Correct 858 ms 54260 KB Output is correct
36 Correct 817 ms 53392 KB Output is correct
37 Correct 672 ms 51656 KB Output is correct
38 Correct 665 ms 51532 KB Output is correct
39 Correct 631 ms 48884 KB Output is correct
40 Correct 620 ms 48884 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1140 ms 63236 KB Output is correct
43 Correct 755 ms 50536 KB Output is correct
44 Correct 921 ms 57156 KB Output is correct
45 Correct 938 ms 57380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 300 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 5 ms 724 KB Output is correct
18 Correct 5 ms 696 KB Output is correct
19 Correct 7 ms 852 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 6 ms 844 KB Output is correct
22 Correct 5 ms 724 KB Output is correct
23 Correct 5 ms 724 KB Output is correct
24 Correct 528 ms 48556 KB Output is correct
25 Correct 560 ms 48448 KB Output is correct
26 Correct 573 ms 48548 KB Output is correct
27 Correct 584 ms 48488 KB Output is correct
28 Correct 549 ms 42452 KB Output is correct
29 Correct 442 ms 36728 KB Output is correct
30 Correct 1047 ms 60132 KB Output is correct
31 Correct 315 ms 30576 KB Output is correct
32 Correct 229 ms 18044 KB Output is correct
33 Correct 689 ms 47852 KB Output is correct
34 Correct 859 ms 54592 KB Output is correct
35 Correct 858 ms 54260 KB Output is correct
36 Correct 817 ms 53392 KB Output is correct
37 Correct 672 ms 51656 KB Output is correct
38 Correct 665 ms 51532 KB Output is correct
39 Correct 631 ms 48884 KB Output is correct
40 Correct 620 ms 48884 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1140 ms 63236 KB Output is correct
43 Correct 755 ms 50536 KB Output is correct
44 Correct 921 ms 57156 KB Output is correct
45 Correct 938 ms 57380 KB Output is correct
46 Correct 3417 ms 210416 KB Output is correct
47 Correct 3464 ms 210288 KB Output is correct
48 Correct 3234 ms 208332 KB Output is correct
49 Correct 3206 ms 208968 KB Output is correct
50 Correct 6810 ms 270384 KB Output is correct
51 Correct 4342 ms 203104 KB Output is correct
52 Correct 5105 ms 221952 KB Output is correct
53 Correct 5770 ms 256552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 685 ms 53924 KB Output is correct
2 Correct 697 ms 53304 KB Output is correct
3 Correct 516 ms 38744 KB Output is correct
4 Correct 618 ms 46816 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 649 ms 49468 KB Output is correct
7 Correct 279 ms 25900 KB Output is correct
8 Correct 254 ms 13564 KB Output is correct
9 Correct 552 ms 39636 KB Output is correct
10 Correct 520 ms 45276 KB Output is correct
11 Correct 351 ms 33104 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 300 KB Output is correct
23 Correct 0 ms 300 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 296 KB Output is correct
28 Correct 5 ms 724 KB Output is correct
29 Correct 5 ms 696 KB Output is correct
30 Correct 7 ms 852 KB Output is correct
31 Correct 6 ms 724 KB Output is correct
32 Correct 6 ms 844 KB Output is correct
33 Correct 5 ms 724 KB Output is correct
34 Correct 5 ms 724 KB Output is correct
35 Correct 528 ms 48556 KB Output is correct
36 Correct 560 ms 48448 KB Output is correct
37 Correct 573 ms 48548 KB Output is correct
38 Correct 584 ms 48488 KB Output is correct
39 Correct 549 ms 42452 KB Output is correct
40 Correct 442 ms 36728 KB Output is correct
41 Correct 1047 ms 60132 KB Output is correct
42 Correct 315 ms 30576 KB Output is correct
43 Correct 229 ms 18044 KB Output is correct
44 Correct 689 ms 47852 KB Output is correct
45 Correct 859 ms 54592 KB Output is correct
46 Correct 858 ms 54260 KB Output is correct
47 Correct 817 ms 53392 KB Output is correct
48 Correct 672 ms 51656 KB Output is correct
49 Correct 665 ms 51532 KB Output is correct
50 Correct 631 ms 48884 KB Output is correct
51 Correct 620 ms 48884 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1140 ms 63236 KB Output is correct
54 Correct 755 ms 50536 KB Output is correct
55 Correct 921 ms 57156 KB Output is correct
56 Correct 938 ms 57380 KB Output is correct
57 Correct 692 ms 52008 KB Output is correct
58 Correct 836 ms 51956 KB Output is correct
59 Correct 636 ms 49784 KB Output is correct
60 Correct 632 ms 49748 KB Output is correct
61 Correct 1054 ms 61112 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1091 ms 63360 KB Output is correct
64 Correct 794 ms 49976 KB Output is correct
65 Correct 930 ms 57888 KB Output is correct
66 Correct 921 ms 56516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 685 ms 53924 KB Output is correct
2 Correct 697 ms 53304 KB Output is correct
3 Correct 516 ms 38744 KB Output is correct
4 Correct 618 ms 46816 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 649 ms 49468 KB Output is correct
7 Correct 279 ms 25900 KB Output is correct
8 Correct 254 ms 13564 KB Output is correct
9 Correct 552 ms 39636 KB Output is correct
10 Correct 520 ms 45276 KB Output is correct
11 Correct 351 ms 33104 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 300 KB Output is correct
23 Correct 0 ms 300 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 0 ms 296 KB Output is correct
28 Correct 5 ms 724 KB Output is correct
29 Correct 5 ms 696 KB Output is correct
30 Correct 7 ms 852 KB Output is correct
31 Correct 6 ms 724 KB Output is correct
32 Correct 6 ms 844 KB Output is correct
33 Correct 5 ms 724 KB Output is correct
34 Correct 5 ms 724 KB Output is correct
35 Correct 528 ms 48556 KB Output is correct
36 Correct 560 ms 48448 KB Output is correct
37 Correct 573 ms 48548 KB Output is correct
38 Correct 584 ms 48488 KB Output is correct
39 Correct 549 ms 42452 KB Output is correct
40 Correct 442 ms 36728 KB Output is correct
41 Correct 1047 ms 60132 KB Output is correct
42 Correct 315 ms 30576 KB Output is correct
43 Correct 229 ms 18044 KB Output is correct
44 Correct 689 ms 47852 KB Output is correct
45 Correct 859 ms 54592 KB Output is correct
46 Correct 858 ms 54260 KB Output is correct
47 Correct 817 ms 53392 KB Output is correct
48 Correct 672 ms 51656 KB Output is correct
49 Correct 665 ms 51532 KB Output is correct
50 Correct 631 ms 48884 KB Output is correct
51 Correct 620 ms 48884 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1140 ms 63236 KB Output is correct
54 Correct 755 ms 50536 KB Output is correct
55 Correct 921 ms 57156 KB Output is correct
56 Correct 938 ms 57380 KB Output is correct
57 Correct 3417 ms 210416 KB Output is correct
58 Correct 3464 ms 210288 KB Output is correct
59 Correct 3234 ms 208332 KB Output is correct
60 Correct 3206 ms 208968 KB Output is correct
61 Correct 6810 ms 270384 KB Output is correct
62 Correct 4342 ms 203104 KB Output is correct
63 Correct 5105 ms 221952 KB Output is correct
64 Correct 5770 ms 256552 KB Output is correct
65 Correct 692 ms 52008 KB Output is correct
66 Correct 836 ms 51956 KB Output is correct
67 Correct 636 ms 49784 KB Output is correct
68 Correct 632 ms 49748 KB Output is correct
69 Correct 1054 ms 61112 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1091 ms 63360 KB Output is correct
72 Correct 794 ms 49976 KB Output is correct
73 Correct 930 ms 57888 KB Output is correct
74 Correct 921 ms 56516 KB Output is correct
75 Correct 3555 ms 210692 KB Output is correct
76 Correct 3548 ms 258992 KB Output is correct
77 Correct 3493 ms 243700 KB Output is correct
78 Correct 3455 ms 242852 KB Output is correct
79 Correct 7337 ms 311880 KB Output is correct
80 Correct 4364 ms 245188 KB Output is correct
81 Correct 5334 ms 273340 KB Output is correct
82 Correct 5897 ms 285072 KB Output is correct
83 Correct 6044 ms 298008 KB Output is correct