Submission #871578

# Submission time Handle Problem Language Result Execution time Memory
871578 2023-11-11T06:39:52 Z qthang2k11 Two Dishes (JOI19_dishes) C++17
100 / 100
4130 ms 281608 KB
// [+]
#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
 
const int N = 1e6 + 5;
 
int n, m, a[N], b[N], p[N], q[N];
ll s[N], t[N];
 
ll sum_a[N], sum_b[N];
 
vector<pair<int, ll>> node[N];
 
int M;
 
const ll INF = 1e18;
 
struct Node {
	ll mx, pls, val;
	Node() {
		mx = val = -INF;
		pls = 0;
	}
};
 
struct LazySegmentTree {
	Node IT[N << 2];
	
	inline void apply_add(int id, ll w) {
		IT[id].mx += w;
		IT[id].val += w;
		IT[id].pls += w;
 	}
	
	inline void apply_max(int id, ll w) {
		IT[id].mx = max(IT[id].mx, w);
		IT[id].val = max(IT[id].val, w);
	}
	
	void push(int id) {
		if (ll &lz = IT[id].pls) {
			apply_add(id << 1, lz);
			apply_add(id << 1 | 1, lz);
			lz = 0;
		}
		ll &lz = IT[id].mx;
		if (lz != -INF) {
			apply_max(id << 1, lz);
			apply_max(id << 1 | 1, lz);
			lz = -INF;
		}
	}
	
	void update_add(int x, int y, ll w, int id, int l, int r) {
		if (l > y || r < x) return;
		if (x <= l && r <= y) 
			return apply_add(id, w);
		push(id);
		int mid = (l + r) / 2;
		update_add(x, y, w, id << 1, l, mid);
		update_add(x, y, w, id << 1 | 1, mid + 1, r);
	}
	
	inline void update_add(int x, int y, ll w) {
		update_add(x, y, w, 1, 1, M);
	}
	
	void update_max(int x, int y, ll w, int id, int l, int r) {
		if (l > y || r < x) return;
		if (x <= l && r <= y) 
			return apply_max(id, w);
		push(id);
		int mid = (l + r) / 2;
		update_max(x, y, w, id << 1, l, mid);
		update_max(x, y, w, id << 1 | 1, mid + 1, r);
	}
	
	inline void update_max(int x, int y, ll w) {
		update_max(x, y, w, 1, 1, M);
	}
	
	ll get(int x, int id, int l, int r) {
		if (l == r) return IT[id].val;
		push(id);
		int mid = (l + r) / 2;
		if (x <= mid) return get(x, id << 1, l, mid);
		else return get(x, id << 1 | 1, mid + 1, r);
	}
	
	inline ll get(int x) {
		return get(x, 1, 1, M);
	}
} seg;
 
int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> a[i] >> s[i] >> p[i];
	for (int i = 1; i <= m; i++)
		cin >> b[i] >> t[i] >> q[i];
	
	for (int i = 1; i <= n; i++)
		sum_a[i] = sum_a[i - 1] + a[i];
	for (int i = 1; i <= m; i++)
		sum_b[i] = sum_b[i - 1] + b[i];
		
	for (int i = 1; i <= n; i++) {
		if (sum_a[i] > s[i]) continue;
		
		int pos = ([&] () -> int {
			int l = 1, r = m, ans = 0;
			while (l <= r) {
				int mid = (l + r) / 2;
				if (sum_a[i] + sum_b[mid] <= s[i]) {
					ans = mid;
					l = mid + 1;
				} else r = mid - 1;
			}
			return ans;
		}());
		
		node[i].emplace_back(pos, p[i]);
	}
	
	ll siu = 0;
	for (int i = 1; i <= m; i++) {
		if (sum_b[i] > t[i]) continue;
		
		int pos = ([&] () -> int {
			int l = 1, r = n, ans = 0;
			while (l <= r) {
				int mid = (l + r) / 2;
				if (sum_b[i] + sum_a[mid] <= t[i]) {
					ans = mid;
					l = mid + 1;
				} else r = mid - 1;
			}
			return ans;
		}());
		
		if (pos != n) node[pos + 1].emplace_back(i - 1, -q[i]);
		siu += q[i];
	}
	
	M = m + 1;
	
	seg.update_max(1, M, 0);
	
	for (int i = 1; i <= n; i++) {
		auto &cur = node[i];
		sort(cur.begin(), cur.end());
		cur.emplace_back(m, 0);
		vector<pair<int, ll>> tmp;
		
		for (const auto &elem: cur) {
			if (tmp.empty() || tmp.back().first != elem.first)
				tmp.emplace_back(elem);
			else tmp.back().second += elem.second;
		}
		
		ll sum = 0;
		for (auto &elem: tmp) {
			elem.first++;
			sum += elem.second;
		}
				
		ll pre_max = -INF;
		for (int i = 0; i < (int)tmp.size(); i++) {
			int L = 1, R = tmp[i].first;
			if (i - 1 >= 0) L = tmp[i - 1].first + 1;
			
			seg.update_add(L, R, sum);
			if (L - 1 > 0) pre_max = max(pre_max, seg.get(L - 1));
			seg.update_max(L, R, pre_max);
			
			sum -= tmp[i].second;
		}
	}
	
	cout << seg.get(M) + siu;
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 291 ms 159060 KB Output is correct
2 Correct 289 ms 159828 KB Output is correct
3 Correct 152 ms 154740 KB Output is correct
4 Correct 227 ms 157288 KB Output is correct
5 Correct 20 ms 131932 KB Output is correct
6 Correct 263 ms 158560 KB Output is correct
7 Correct 68 ms 137832 KB Output is correct
8 Correct 95 ms 149272 KB Output is correct
9 Correct 153 ms 155232 KB Output is correct
10 Correct 265 ms 157760 KB Output is correct
11 Correct 123 ms 151892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 131808 KB Output is correct
2 Correct 20 ms 131900 KB Output is correct
3 Correct 21 ms 131932 KB Output is correct
4 Correct 21 ms 131932 KB Output is correct
5 Correct 20 ms 131780 KB Output is correct
6 Correct 20 ms 131752 KB Output is correct
7 Correct 21 ms 131968 KB Output is correct
8 Correct 20 ms 131928 KB Output is correct
9 Correct 20 ms 131932 KB Output is correct
10 Correct 19 ms 131952 KB Output is correct
11 Correct 21 ms 131932 KB Output is correct
12 Correct 19 ms 131932 KB Output is correct
13 Correct 20 ms 131932 KB Output is correct
14 Correct 20 ms 131928 KB Output is correct
15 Correct 19 ms 131912 KB Output is correct
16 Correct 19 ms 131932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 131808 KB Output is correct
2 Correct 20 ms 131900 KB Output is correct
3 Correct 21 ms 131932 KB Output is correct
4 Correct 21 ms 131932 KB Output is correct
5 Correct 20 ms 131780 KB Output is correct
6 Correct 20 ms 131752 KB Output is correct
7 Correct 21 ms 131968 KB Output is correct
8 Correct 20 ms 131928 KB Output is correct
9 Correct 20 ms 131932 KB Output is correct
10 Correct 19 ms 131952 KB Output is correct
11 Correct 21 ms 131932 KB Output is correct
12 Correct 19 ms 131932 KB Output is correct
13 Correct 20 ms 131932 KB Output is correct
14 Correct 20 ms 131928 KB Output is correct
15 Correct 19 ms 131912 KB Output is correct
16 Correct 19 ms 131932 KB Output is correct
17 Correct 21 ms 131928 KB Output is correct
18 Correct 21 ms 132188 KB Output is correct
19 Correct 23 ms 132220 KB Output is correct
20 Correct 22 ms 131928 KB Output is correct
21 Correct 23 ms 132176 KB Output is correct
22 Correct 24 ms 132188 KB Output is correct
23 Correct 23 ms 132192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 131808 KB Output is correct
2 Correct 20 ms 131900 KB Output is correct
3 Correct 21 ms 131932 KB Output is correct
4 Correct 21 ms 131932 KB Output is correct
5 Correct 20 ms 131780 KB Output is correct
6 Correct 20 ms 131752 KB Output is correct
7 Correct 21 ms 131968 KB Output is correct
8 Correct 20 ms 131928 KB Output is correct
9 Correct 20 ms 131932 KB Output is correct
10 Correct 19 ms 131952 KB Output is correct
11 Correct 21 ms 131932 KB Output is correct
12 Correct 19 ms 131932 KB Output is correct
13 Correct 20 ms 131932 KB Output is correct
14 Correct 20 ms 131928 KB Output is correct
15 Correct 19 ms 131912 KB Output is correct
16 Correct 19 ms 131932 KB Output is correct
17 Correct 21 ms 131928 KB Output is correct
18 Correct 21 ms 132188 KB Output is correct
19 Correct 23 ms 132220 KB Output is correct
20 Correct 22 ms 131928 KB Output is correct
21 Correct 23 ms 132176 KB Output is correct
22 Correct 24 ms 132188 KB Output is correct
23 Correct 23 ms 132192 KB Output is correct
24 Correct 235 ms 153588 KB Output is correct
25 Correct 200 ms 158908 KB Output is correct
26 Correct 258 ms 153244 KB Output is correct
27 Correct 267 ms 156268 KB Output is correct
28 Correct 277 ms 154468 KB Output is correct
29 Correct 139 ms 153424 KB Output is correct
30 Correct 625 ms 158800 KB Output is correct
31 Correct 101 ms 141416 KB Output is correct
32 Correct 99 ms 148316 KB Output is correct
33 Correct 367 ms 153936 KB Output is correct
34 Correct 506 ms 158324 KB Output is correct
35 Correct 648 ms 155724 KB Output is correct
36 Correct 601 ms 155600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 131808 KB Output is correct
2 Correct 20 ms 131900 KB Output is correct
3 Correct 21 ms 131932 KB Output is correct
4 Correct 21 ms 131932 KB Output is correct
5 Correct 20 ms 131780 KB Output is correct
6 Correct 20 ms 131752 KB Output is correct
7 Correct 21 ms 131968 KB Output is correct
8 Correct 20 ms 131928 KB Output is correct
9 Correct 20 ms 131932 KB Output is correct
10 Correct 19 ms 131952 KB Output is correct
11 Correct 21 ms 131932 KB Output is correct
12 Correct 19 ms 131932 KB Output is correct
13 Correct 20 ms 131932 KB Output is correct
14 Correct 20 ms 131928 KB Output is correct
15 Correct 19 ms 131912 KB Output is correct
16 Correct 19 ms 131932 KB Output is correct
17 Correct 21 ms 131928 KB Output is correct
18 Correct 21 ms 132188 KB Output is correct
19 Correct 23 ms 132220 KB Output is correct
20 Correct 22 ms 131928 KB Output is correct
21 Correct 23 ms 132176 KB Output is correct
22 Correct 24 ms 132188 KB Output is correct
23 Correct 23 ms 132192 KB Output is correct
24 Correct 235 ms 153588 KB Output is correct
25 Correct 200 ms 158908 KB Output is correct
26 Correct 258 ms 153244 KB Output is correct
27 Correct 267 ms 156268 KB Output is correct
28 Correct 277 ms 154468 KB Output is correct
29 Correct 139 ms 153424 KB Output is correct
30 Correct 625 ms 158800 KB Output is correct
31 Correct 101 ms 141416 KB Output is correct
32 Correct 99 ms 148316 KB Output is correct
33 Correct 367 ms 153936 KB Output is correct
34 Correct 506 ms 158324 KB Output is correct
35 Correct 648 ms 155724 KB Output is correct
36 Correct 601 ms 155600 KB Output is correct
37 Correct 277 ms 154844 KB Output is correct
38 Correct 275 ms 157772 KB Output is correct
39 Correct 273 ms 159456 KB Output is correct
40 Correct 272 ms 159772 KB Output is correct
41 Correct 22 ms 131928 KB Output is correct
42 Correct 629 ms 160336 KB Output is correct
43 Correct 394 ms 155476 KB Output is correct
44 Correct 509 ms 159060 KB Output is correct
45 Correct 614 ms 157084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 131808 KB Output is correct
2 Correct 20 ms 131900 KB Output is correct
3 Correct 21 ms 131932 KB Output is correct
4 Correct 21 ms 131932 KB Output is correct
5 Correct 20 ms 131780 KB Output is correct
6 Correct 20 ms 131752 KB Output is correct
7 Correct 21 ms 131968 KB Output is correct
8 Correct 20 ms 131928 KB Output is correct
9 Correct 20 ms 131932 KB Output is correct
10 Correct 19 ms 131952 KB Output is correct
11 Correct 21 ms 131932 KB Output is correct
12 Correct 19 ms 131932 KB Output is correct
13 Correct 20 ms 131932 KB Output is correct
14 Correct 20 ms 131928 KB Output is correct
15 Correct 19 ms 131912 KB Output is correct
16 Correct 19 ms 131932 KB Output is correct
17 Correct 21 ms 131928 KB Output is correct
18 Correct 21 ms 132188 KB Output is correct
19 Correct 23 ms 132220 KB Output is correct
20 Correct 22 ms 131928 KB Output is correct
21 Correct 23 ms 132176 KB Output is correct
22 Correct 24 ms 132188 KB Output is correct
23 Correct 23 ms 132192 KB Output is correct
24 Correct 235 ms 153588 KB Output is correct
25 Correct 200 ms 158908 KB Output is correct
26 Correct 258 ms 153244 KB Output is correct
27 Correct 267 ms 156268 KB Output is correct
28 Correct 277 ms 154468 KB Output is correct
29 Correct 139 ms 153424 KB Output is correct
30 Correct 625 ms 158800 KB Output is correct
31 Correct 101 ms 141416 KB Output is correct
32 Correct 99 ms 148316 KB Output is correct
33 Correct 367 ms 153936 KB Output is correct
34 Correct 506 ms 158324 KB Output is correct
35 Correct 648 ms 155724 KB Output is correct
36 Correct 601 ms 155600 KB Output is correct
37 Correct 277 ms 154844 KB Output is correct
38 Correct 275 ms 157772 KB Output is correct
39 Correct 273 ms 159456 KB Output is correct
40 Correct 272 ms 159772 KB Output is correct
41 Correct 22 ms 131928 KB Output is correct
42 Correct 629 ms 160336 KB Output is correct
43 Correct 394 ms 155476 KB Output is correct
44 Correct 509 ms 159060 KB Output is correct
45 Correct 614 ms 157084 KB Output is correct
46 Correct 1333 ms 247064 KB Output is correct
47 Correct 1363 ms 262608 KB Output is correct
48 Correct 1338 ms 271560 KB Output is correct
49 Correct 1339 ms 271960 KB Output is correct
50 Correct 4094 ms 276100 KB Output is correct
51 Correct 2158 ms 248204 KB Output is correct
52 Correct 2833 ms 265984 KB Output is correct
53 Correct 3901 ms 258872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 291 ms 159060 KB Output is correct
2 Correct 289 ms 159828 KB Output is correct
3 Correct 152 ms 154740 KB Output is correct
4 Correct 227 ms 157288 KB Output is correct
5 Correct 20 ms 131932 KB Output is correct
6 Correct 263 ms 158560 KB Output is correct
7 Correct 68 ms 137832 KB Output is correct
8 Correct 95 ms 149272 KB Output is correct
9 Correct 153 ms 155232 KB Output is correct
10 Correct 265 ms 157760 KB Output is correct
11 Correct 123 ms 151892 KB Output is correct
12 Correct 19 ms 131808 KB Output is correct
13 Correct 20 ms 131900 KB Output is correct
14 Correct 21 ms 131932 KB Output is correct
15 Correct 21 ms 131932 KB Output is correct
16 Correct 20 ms 131780 KB Output is correct
17 Correct 20 ms 131752 KB Output is correct
18 Correct 21 ms 131968 KB Output is correct
19 Correct 20 ms 131928 KB Output is correct
20 Correct 20 ms 131932 KB Output is correct
21 Correct 19 ms 131952 KB Output is correct
22 Correct 21 ms 131932 KB Output is correct
23 Correct 19 ms 131932 KB Output is correct
24 Correct 20 ms 131932 KB Output is correct
25 Correct 20 ms 131928 KB Output is correct
26 Correct 19 ms 131912 KB Output is correct
27 Correct 19 ms 131932 KB Output is correct
28 Correct 21 ms 131928 KB Output is correct
29 Correct 21 ms 132188 KB Output is correct
30 Correct 23 ms 132220 KB Output is correct
31 Correct 22 ms 131928 KB Output is correct
32 Correct 23 ms 132176 KB Output is correct
33 Correct 24 ms 132188 KB Output is correct
34 Correct 23 ms 132192 KB Output is correct
35 Correct 235 ms 153588 KB Output is correct
36 Correct 200 ms 158908 KB Output is correct
37 Correct 258 ms 153244 KB Output is correct
38 Correct 267 ms 156268 KB Output is correct
39 Correct 277 ms 154468 KB Output is correct
40 Correct 139 ms 153424 KB Output is correct
41 Correct 625 ms 158800 KB Output is correct
42 Correct 101 ms 141416 KB Output is correct
43 Correct 99 ms 148316 KB Output is correct
44 Correct 367 ms 153936 KB Output is correct
45 Correct 506 ms 158324 KB Output is correct
46 Correct 648 ms 155724 KB Output is correct
47 Correct 601 ms 155600 KB Output is correct
48 Correct 277 ms 154844 KB Output is correct
49 Correct 275 ms 157772 KB Output is correct
50 Correct 273 ms 159456 KB Output is correct
51 Correct 272 ms 159772 KB Output is correct
52 Correct 22 ms 131928 KB Output is correct
53 Correct 629 ms 160336 KB Output is correct
54 Correct 394 ms 155476 KB Output is correct
55 Correct 509 ms 159060 KB Output is correct
56 Correct 614 ms 157084 KB Output is correct
57 Correct 276 ms 158580 KB Output is correct
58 Correct 287 ms 161616 KB Output is correct
59 Correct 283 ms 162888 KB Output is correct
60 Correct 282 ms 162896 KB Output is correct
61 Correct 649 ms 162056 KB Output is correct
62 Correct 20 ms 131932 KB Output is correct
63 Correct 667 ms 164220 KB Output is correct
64 Correct 391 ms 159192 KB Output is correct
65 Correct 519 ms 162376 KB Output is correct
66 Correct 635 ms 159272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 291 ms 159060 KB Output is correct
2 Correct 289 ms 159828 KB Output is correct
3 Correct 152 ms 154740 KB Output is correct
4 Correct 227 ms 157288 KB Output is correct
5 Correct 20 ms 131932 KB Output is correct
6 Correct 263 ms 158560 KB Output is correct
7 Correct 68 ms 137832 KB Output is correct
8 Correct 95 ms 149272 KB Output is correct
9 Correct 153 ms 155232 KB Output is correct
10 Correct 265 ms 157760 KB Output is correct
11 Correct 123 ms 151892 KB Output is correct
12 Correct 19 ms 131808 KB Output is correct
13 Correct 20 ms 131900 KB Output is correct
14 Correct 21 ms 131932 KB Output is correct
15 Correct 21 ms 131932 KB Output is correct
16 Correct 20 ms 131780 KB Output is correct
17 Correct 20 ms 131752 KB Output is correct
18 Correct 21 ms 131968 KB Output is correct
19 Correct 20 ms 131928 KB Output is correct
20 Correct 20 ms 131932 KB Output is correct
21 Correct 19 ms 131952 KB Output is correct
22 Correct 21 ms 131932 KB Output is correct
23 Correct 19 ms 131932 KB Output is correct
24 Correct 20 ms 131932 KB Output is correct
25 Correct 20 ms 131928 KB Output is correct
26 Correct 19 ms 131912 KB Output is correct
27 Correct 19 ms 131932 KB Output is correct
28 Correct 21 ms 131928 KB Output is correct
29 Correct 21 ms 132188 KB Output is correct
30 Correct 23 ms 132220 KB Output is correct
31 Correct 22 ms 131928 KB Output is correct
32 Correct 23 ms 132176 KB Output is correct
33 Correct 24 ms 132188 KB Output is correct
34 Correct 23 ms 132192 KB Output is correct
35 Correct 235 ms 153588 KB Output is correct
36 Correct 200 ms 158908 KB Output is correct
37 Correct 258 ms 153244 KB Output is correct
38 Correct 267 ms 156268 KB Output is correct
39 Correct 277 ms 154468 KB Output is correct
40 Correct 139 ms 153424 KB Output is correct
41 Correct 625 ms 158800 KB Output is correct
42 Correct 101 ms 141416 KB Output is correct
43 Correct 99 ms 148316 KB Output is correct
44 Correct 367 ms 153936 KB Output is correct
45 Correct 506 ms 158324 KB Output is correct
46 Correct 648 ms 155724 KB Output is correct
47 Correct 601 ms 155600 KB Output is correct
48 Correct 277 ms 154844 KB Output is correct
49 Correct 275 ms 157772 KB Output is correct
50 Correct 273 ms 159456 KB Output is correct
51 Correct 272 ms 159772 KB Output is correct
52 Correct 22 ms 131928 KB Output is correct
53 Correct 629 ms 160336 KB Output is correct
54 Correct 394 ms 155476 KB Output is correct
55 Correct 509 ms 159060 KB Output is correct
56 Correct 614 ms 157084 KB Output is correct
57 Correct 1333 ms 247064 KB Output is correct
58 Correct 1363 ms 262608 KB Output is correct
59 Correct 1338 ms 271560 KB Output is correct
60 Correct 1339 ms 271960 KB Output is correct
61 Correct 4094 ms 276100 KB Output is correct
62 Correct 2158 ms 248204 KB Output is correct
63 Correct 2833 ms 265984 KB Output is correct
64 Correct 3901 ms 258872 KB Output is correct
65 Correct 276 ms 158580 KB Output is correct
66 Correct 287 ms 161616 KB Output is correct
67 Correct 283 ms 162888 KB Output is correct
68 Correct 282 ms 162896 KB Output is correct
69 Correct 649 ms 162056 KB Output is correct
70 Correct 20 ms 131932 KB Output is correct
71 Correct 667 ms 164220 KB Output is correct
72 Correct 391 ms 159192 KB Output is correct
73 Correct 519 ms 162376 KB Output is correct
74 Correct 635 ms 159272 KB Output is correct
75 Correct 1374 ms 265648 KB Output is correct
76 Correct 1411 ms 281608 KB Output is correct
77 Correct 1385 ms 272744 KB Output is correct
78 Correct 1380 ms 272720 KB Output is correct
79 Correct 4130 ms 276968 KB Output is correct
80 Correct 2166 ms 250480 KB Output is correct
81 Correct 2865 ms 259696 KB Output is correct
82 Correct 3967 ms 259936 KB Output is correct
83 Correct 3808 ms 267816 KB Output is correct