Submission #528821

# Submission time Handle Problem Language Result Execution time Memory
528821 2022-02-21T14:07:08 Z fhvirus Two Dishes (JOI19_dishes) C++17
100 / 100
2063 ms 242652 KB
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

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

	vector<int64_t> preA(N + 1, 0);
	for (int i = 1; i <= N; ++i)
		preA[i] = preA[i - 1] + A[i];

	vector<int64_t> preB(M + 1, 0);
	for (int i = 1; i <= M; ++i)
		preB[i] = preB[i - 1] + B[i];

	vector<int64_t> boundP(N + 1, 0);
	for (int i = 1; i <= N; ++i)
		boundP[i] = (upper_bound(AI(preB), S[i] - preA[i]) - begin(preB)) - 1;

	vector<int64_t> boundQ(M + 1, 0);
	for (int i = 1; i <= M; ++i)
		boundQ[i] = (upper_bound(AI(preA), T[i] - preB[i]) - begin(preA)) - 1;

	int64_t ans = 0;
	vector<int64_t> dp_diff(M + 1, 0);
	vector<vector<int64_t>> out(N + 1);
	for (int j = 0; j <= M; ++j)
		if (boundQ[j] >= N)
			ans += Q[j];
		else if (boundQ[j] >= 0)
			out[boundQ[j] + 1].emplace_back(j);

	set<int> not_zero;
	vector<int> modified;
	for (int i = 1; i <= N; ++i) {
		if (boundP[i] >= 0) {
			ans += P[i];
			if (boundP[i] < M) {
				dp_diff[boundP[i] + 1] -= P[i];
				not_zero.emplace(boundP[i] + 1);
				modified.emplace_back(boundP[i] + 1);
			}
		}

		for (const int& j: out[i]) {
			dp_diff[j] += Q[j];
			not_zero.emplace(j);
			modified.emplace_back(j);
		}

		sort(AI(modified));
		for (const int& j: modified) {
			auto it = not_zero.lower_bound(j);
			auto ti = it;
			int64_t lst = 0;
			while (it != end(not_zero)) {
				dp_diff[*it] += lst;
				if (dp_diff[*it] >= 0) break;
				lst = dp_diff[*it];
				dp_diff[*it] = 0;
				++it;
			}
			not_zero.erase(ti, it);
		}
		modified.clear();
	}

	cout << accumulate(AI(dp_diff), ans) << '\n';

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 225 ms 31296 KB Output is correct
2 Correct 189 ms 36304 KB Output is correct
3 Correct 150 ms 32472 KB Output is correct
4 Correct 217 ms 42044 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 197 ms 34876 KB Output is correct
7 Correct 73 ms 14912 KB Output is correct
8 Correct 84 ms 18300 KB Output is correct
9 Correct 163 ms 33476 KB Output is correct
10 Correct 156 ms 32712 KB Output is correct
11 Correct 107 ms 26868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 3 ms 588 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 3 ms 588 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 131 ms 19012 KB Output is correct
25 Correct 194 ms 41396 KB Output is correct
26 Correct 144 ms 29768 KB Output is correct
27 Correct 210 ms 45424 KB Output is correct
28 Correct 172 ms 31344 KB Output is correct
29 Correct 134 ms 30408 KB Output is correct
30 Correct 290 ms 34024 KB Output is correct
31 Correct 93 ms 19176 KB Output is correct
32 Correct 71 ms 16612 KB Output is correct
33 Correct 210 ms 31788 KB Output is correct
34 Correct 261 ms 36420 KB Output is correct
35 Correct 242 ms 27592 KB Output is correct
36 Correct 242 ms 27592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 3 ms 588 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 131 ms 19012 KB Output is correct
25 Correct 194 ms 41396 KB Output is correct
26 Correct 144 ms 29768 KB Output is correct
27 Correct 210 ms 45424 KB Output is correct
28 Correct 172 ms 31344 KB Output is correct
29 Correct 134 ms 30408 KB Output is correct
30 Correct 290 ms 34024 KB Output is correct
31 Correct 93 ms 19176 KB Output is correct
32 Correct 71 ms 16612 KB Output is correct
33 Correct 210 ms 31788 KB Output is correct
34 Correct 261 ms 36420 KB Output is correct
35 Correct 242 ms 27592 KB Output is correct
36 Correct 242 ms 27592 KB Output is correct
37 Correct 172 ms 32704 KB Output is correct
38 Correct 247 ms 48464 KB Output is correct
39 Correct 295 ms 45808 KB Output is correct
40 Correct 170 ms 36420 KB Output is correct
41 Correct 0 ms 208 KB Output is correct
42 Correct 316 ms 37144 KB Output is correct
43 Correct 229 ms 34596 KB Output is correct
44 Correct 269 ms 39204 KB Output is correct
45 Correct 259 ms 30704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 2 ms 460 KB Output is correct
18 Correct 3 ms 588 KB Output is correct
19 Correct 3 ms 460 KB Output is correct
20 Correct 2 ms 460 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 460 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 131 ms 19012 KB Output is correct
25 Correct 194 ms 41396 KB Output is correct
26 Correct 144 ms 29768 KB Output is correct
27 Correct 210 ms 45424 KB Output is correct
28 Correct 172 ms 31344 KB Output is correct
29 Correct 134 ms 30408 KB Output is correct
30 Correct 290 ms 34024 KB Output is correct
31 Correct 93 ms 19176 KB Output is correct
32 Correct 71 ms 16612 KB Output is correct
33 Correct 210 ms 31788 KB Output is correct
34 Correct 261 ms 36420 KB Output is correct
35 Correct 242 ms 27592 KB Output is correct
36 Correct 242 ms 27592 KB Output is correct
37 Correct 172 ms 32704 KB Output is correct
38 Correct 247 ms 48464 KB Output is correct
39 Correct 295 ms 45808 KB Output is correct
40 Correct 170 ms 36420 KB Output is correct
41 Correct 0 ms 208 KB Output is correct
42 Correct 316 ms 37144 KB Output is correct
43 Correct 229 ms 34596 KB Output is correct
44 Correct 269 ms 39204 KB Output is correct
45 Correct 259 ms 30704 KB Output is correct
46 Correct 865 ms 164452 KB Output is correct
47 Correct 1229 ms 242652 KB Output is correct
48 Correct 1648 ms 228924 KB Output is correct
49 Correct 859 ms 182160 KB Output is correct
50 Correct 1981 ms 185468 KB Output is correct
51 Correct 1344 ms 169628 KB Output is correct
52 Correct 1746 ms 189292 KB Output is correct
53 Correct 1738 ms 153816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 31296 KB Output is correct
2 Correct 189 ms 36304 KB Output is correct
3 Correct 150 ms 32472 KB Output is correct
4 Correct 217 ms 42044 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 197 ms 34876 KB Output is correct
7 Correct 73 ms 14912 KB Output is correct
8 Correct 84 ms 18300 KB Output is correct
9 Correct 163 ms 33476 KB Output is correct
10 Correct 156 ms 32712 KB Output is correct
11 Correct 107 ms 26868 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 3 ms 588 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 131 ms 19012 KB Output is correct
36 Correct 194 ms 41396 KB Output is correct
37 Correct 144 ms 29768 KB Output is correct
38 Correct 210 ms 45424 KB Output is correct
39 Correct 172 ms 31344 KB Output is correct
40 Correct 134 ms 30408 KB Output is correct
41 Correct 290 ms 34024 KB Output is correct
42 Correct 93 ms 19176 KB Output is correct
43 Correct 71 ms 16612 KB Output is correct
44 Correct 210 ms 31788 KB Output is correct
45 Correct 261 ms 36420 KB Output is correct
46 Correct 242 ms 27592 KB Output is correct
47 Correct 242 ms 27592 KB Output is correct
48 Correct 172 ms 32704 KB Output is correct
49 Correct 247 ms 48464 KB Output is correct
50 Correct 295 ms 45808 KB Output is correct
51 Correct 170 ms 36420 KB Output is correct
52 Correct 0 ms 208 KB Output is correct
53 Correct 316 ms 37144 KB Output is correct
54 Correct 229 ms 34596 KB Output is correct
55 Correct 269 ms 39204 KB Output is correct
56 Correct 259 ms 30704 KB Output is correct
57 Correct 246 ms 42628 KB Output is correct
58 Correct 187 ms 39492 KB Output is correct
59 Correct 199 ms 37448 KB Output is correct
60 Correct 286 ms 46704 KB Output is correct
61 Correct 446 ms 43508 KB Output is correct
62 Correct 1 ms 320 KB Output is correct
63 Correct 349 ms 37236 KB Output is correct
64 Correct 255 ms 34780 KB Output is correct
65 Correct 321 ms 35708 KB Output is correct
66 Correct 287 ms 30784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 31296 KB Output is correct
2 Correct 189 ms 36304 KB Output is correct
3 Correct 150 ms 32472 KB Output is correct
4 Correct 217 ms 42044 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 197 ms 34876 KB Output is correct
7 Correct 73 ms 14912 KB Output is correct
8 Correct 84 ms 18300 KB Output is correct
9 Correct 163 ms 33476 KB Output is correct
10 Correct 156 ms 32712 KB Output is correct
11 Correct 107 ms 26868 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 0 ms 204 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 3 ms 588 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 2 ms 460 KB Output is correct
33 Correct 2 ms 460 KB Output is correct
34 Correct 2 ms 460 KB Output is correct
35 Correct 131 ms 19012 KB Output is correct
36 Correct 194 ms 41396 KB Output is correct
37 Correct 144 ms 29768 KB Output is correct
38 Correct 210 ms 45424 KB Output is correct
39 Correct 172 ms 31344 KB Output is correct
40 Correct 134 ms 30408 KB Output is correct
41 Correct 290 ms 34024 KB Output is correct
42 Correct 93 ms 19176 KB Output is correct
43 Correct 71 ms 16612 KB Output is correct
44 Correct 210 ms 31788 KB Output is correct
45 Correct 261 ms 36420 KB Output is correct
46 Correct 242 ms 27592 KB Output is correct
47 Correct 242 ms 27592 KB Output is correct
48 Correct 172 ms 32704 KB Output is correct
49 Correct 247 ms 48464 KB Output is correct
50 Correct 295 ms 45808 KB Output is correct
51 Correct 170 ms 36420 KB Output is correct
52 Correct 0 ms 208 KB Output is correct
53 Correct 316 ms 37144 KB Output is correct
54 Correct 229 ms 34596 KB Output is correct
55 Correct 269 ms 39204 KB Output is correct
56 Correct 259 ms 30704 KB Output is correct
57 Correct 865 ms 164452 KB Output is correct
58 Correct 1229 ms 242652 KB Output is correct
59 Correct 1648 ms 228924 KB Output is correct
60 Correct 859 ms 182160 KB Output is correct
61 Correct 1981 ms 185468 KB Output is correct
62 Correct 1344 ms 169628 KB Output is correct
63 Correct 1746 ms 189292 KB Output is correct
64 Correct 1738 ms 153816 KB Output is correct
65 Correct 246 ms 42628 KB Output is correct
66 Correct 187 ms 39492 KB Output is correct
67 Correct 199 ms 37448 KB Output is correct
68 Correct 286 ms 46704 KB Output is correct
69 Correct 446 ms 43508 KB Output is correct
70 Correct 1 ms 320 KB Output is correct
71 Correct 349 ms 37236 KB Output is correct
72 Correct 255 ms 34780 KB Output is correct
73 Correct 321 ms 35708 KB Output is correct
74 Correct 287 ms 30784 KB Output is correct
75 Correct 1351 ms 212944 KB Output is correct
76 Correct 958 ms 197468 KB Output is correct
77 Correct 883 ms 184384 KB Output is correct
78 Correct 1532 ms 231228 KB Output is correct
79 Correct 2063 ms 186096 KB Output is correct
80 Correct 1454 ms 171244 KB Output is correct
81 Correct 1780 ms 172284 KB Output is correct
82 Correct 1812 ms 153320 KB Output is correct
83 Correct 1872 ms 173228 KB Output is correct