답안 #768103

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
768103 2023-06-27T13:09:21 Z qwerasdfzxcl Two Dishes (JOI19_dishes) C++17
100 / 100
3704 ms 259708 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr ll INF = 4e18;

struct Foo{
	ll a, b;
	bool id;

	Foo(){}

	ll calc(ll x){return max(a, x) + b;}

	void operator += (const Foo &F){
		if (!id) {*this = F; return;}

		a = (a+b > F.a)?a:(F.a - b);
		b += F.b;
	}
};

struct Seg{
	ll ans[1101000];
	Foo lazy[2202000];
	
	void propagate(int i, int l, int r){
		if (!lazy[i].id) return;

		if (l==r) ans[l] = lazy[i].calc(ans[l]);
		else{
			lazy[i<<1] += lazy[i];
			lazy[i<<1|1] += lazy[i]; 
		}

		lazy[i].id = 0;
	}

	void add(int i, int l, int r, int s, int e, int x){
		propagate(i, l, r);
		if (r<s || e<l) return;
		if (s<=l && r<=e){
			lazy[i].a = -INF, lazy[i].b = x, lazy[i].id = 1;
			propagate(i, l, r);
			return;
		}

		int m = (l+r)>>1;
		add(i<<1, l, m, s, e, x); add(i<<1|1, m+1, r, s, e, x);
	}

	void max(int i, int l, int r, int s, int e, ll x){
		propagate(i, l, r);
		if (r<s || e<l) return;
		if (s<=l && r<=e){
			lazy[i].a = x, lazy[i].b = 0, lazy[i].id = 1;
			propagate(i, l, r);
			return;
		}

		int m = (l+r)>>1;
		max(i<<1, l, m, s, e, x); max(i<<1|1, m+1, r, s, e, x);
	}

	ll query(int i, int l, int r, int p){
		propagate(i, l, r);
		if (l==r) return ans[l];

		int m = (l+r)>>1;
		if (p<=m) return query(i<<1, l, m, p);
		return query(i<<1|1, m+1, r, p);
	}
}tree;

int n, m;
int A[1001000], B[1001000], P[1001000], Q[1001000], mxA[1001000], mxB[1001000];
ll S[1001000], T[1001000], sumA[1001000], sumB[1001000], ans;
vector<pair<int, int>> lowerQ[1001000], upperQ[1001000];

void init(){
	for (int i=1;i<=n;i++) sumA[i] = sumA[i-1] + A[i];
	for (int i=1;i<=m;i++) sumB[i] = sumB[i-1] + B[i];
 
	for (int i=1;i<=n;i++){
		mxB[i] = upper_bound(sumB, sumB+m+1, S[i] - sumA[i]) - sumB - 1;

		if (P[i] > 0){
			if (mxB[i] >= 0) lowerQ[i-1].emplace_back(mxB[i], P[i]);
		}

		else if (P[i] < 0){
			ans += P[i];
			if (mxB[i] < m) upperQ[i-1].emplace_back(mxB[i]+1, -P[i]);
		}
		
	}
	for (int i=1;i<=m;i++){
		mxA[i] = upper_bound(sumA, sumA+n+1, T[i] - sumB[i]) - sumA - 1;

		if (Q[i] > 0){
			if (mxA[i]==n) ans += Q[i];
			else if (mxA[i] >= 0) upperQ[mxA[i]].emplace_back(i, Q[i]);
		}

		else if (Q[i] < 0){
			ans += Q[i];
			if (mxA[i]==-1) ans -= Q[i];
			else if (mxA[i] < n) lowerQ[mxA[i]].emplace_back(i-1, -Q[i]);
		}
		
	}

	for (int i=0;i<n;i++){
		sort(lowerQ[i].begin(), lowerQ[i].end(), greater<pair<int, int>>());
		sort(upperQ[i].begin(), upperQ[i].end());
	}

}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(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];
 
	init();
 
	for (int x=0;x<n;x++){
		for (auto &[y, val]:upperQ[x]) tree.add(1, 0, m, y, m, val);
		for (auto &[y, val]:lowerQ[x]){
			tree.add(1, 0, m, 0, y, val);
			tree.max(1, 0, m, y+1, m, tree.query(1, 0, m, y));
		}
	}
 
	printf("%lld\n", tree.query(1, 0, m, m) + ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 269 ms 80432 KB Output is correct
2 Correct 269 ms 80128 KB Output is correct
3 Correct 161 ms 64640 KB Output is correct
4 Correct 236 ms 74168 KB Output is correct
5 Correct 23 ms 47416 KB Output is correct
6 Correct 240 ms 77484 KB Output is correct
7 Correct 86 ms 52892 KB Output is correct
8 Correct 89 ms 59048 KB Output is correct
9 Correct 167 ms 64712 KB Output is correct
10 Correct 250 ms 84800 KB Output is correct
11 Correct 134 ms 64632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47420 KB Output is correct
2 Correct 25 ms 47316 KB Output is correct
3 Correct 26 ms 47432 KB Output is correct
4 Correct 26 ms 47424 KB Output is correct
5 Correct 25 ms 47316 KB Output is correct
6 Correct 27 ms 47372 KB Output is correct
7 Correct 26 ms 47412 KB Output is correct
8 Correct 25 ms 47368 KB Output is correct
9 Correct 24 ms 47316 KB Output is correct
10 Correct 24 ms 47376 KB Output is correct
11 Correct 24 ms 47352 KB Output is correct
12 Correct 23 ms 47320 KB Output is correct
13 Correct 25 ms 47320 KB Output is correct
14 Correct 23 ms 47316 KB Output is correct
15 Correct 24 ms 47316 KB Output is correct
16 Correct 22 ms 47432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47420 KB Output is correct
2 Correct 25 ms 47316 KB Output is correct
3 Correct 26 ms 47432 KB Output is correct
4 Correct 26 ms 47424 KB Output is correct
5 Correct 25 ms 47316 KB Output is correct
6 Correct 27 ms 47372 KB Output is correct
7 Correct 26 ms 47412 KB Output is correct
8 Correct 25 ms 47368 KB Output is correct
9 Correct 24 ms 47316 KB Output is correct
10 Correct 24 ms 47376 KB Output is correct
11 Correct 24 ms 47352 KB Output is correct
12 Correct 23 ms 47320 KB Output is correct
13 Correct 25 ms 47320 KB Output is correct
14 Correct 23 ms 47316 KB Output is correct
15 Correct 24 ms 47316 KB Output is correct
16 Correct 22 ms 47432 KB Output is correct
17 Correct 24 ms 47572 KB Output is correct
18 Correct 25 ms 47596 KB Output is correct
19 Correct 26 ms 47688 KB Output is correct
20 Correct 27 ms 47708 KB Output is correct
21 Correct 26 ms 47728 KB Output is correct
22 Correct 26 ms 47700 KB Output is correct
23 Correct 27 ms 47700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47420 KB Output is correct
2 Correct 25 ms 47316 KB Output is correct
3 Correct 26 ms 47432 KB Output is correct
4 Correct 26 ms 47424 KB Output is correct
5 Correct 25 ms 47316 KB Output is correct
6 Correct 27 ms 47372 KB Output is correct
7 Correct 26 ms 47412 KB Output is correct
8 Correct 25 ms 47368 KB Output is correct
9 Correct 24 ms 47316 KB Output is correct
10 Correct 24 ms 47376 KB Output is correct
11 Correct 24 ms 47352 KB Output is correct
12 Correct 23 ms 47320 KB Output is correct
13 Correct 25 ms 47320 KB Output is correct
14 Correct 23 ms 47316 KB Output is correct
15 Correct 24 ms 47316 KB Output is correct
16 Correct 22 ms 47432 KB Output is correct
17 Correct 24 ms 47572 KB Output is correct
18 Correct 25 ms 47596 KB Output is correct
19 Correct 26 ms 47688 KB Output is correct
20 Correct 27 ms 47708 KB Output is correct
21 Correct 26 ms 47728 KB Output is correct
22 Correct 26 ms 47700 KB Output is correct
23 Correct 27 ms 47700 KB Output is correct
24 Correct 191 ms 64708 KB Output is correct
25 Correct 183 ms 77004 KB Output is correct
26 Correct 225 ms 78564 KB Output is correct
27 Correct 210 ms 81644 KB Output is correct
28 Correct 252 ms 79092 KB Output is correct
29 Correct 160 ms 64720 KB Output is correct
30 Correct 508 ms 82544 KB Output is correct
31 Correct 97 ms 67588 KB Output is correct
32 Correct 86 ms 59164 KB Output is correct
33 Correct 335 ms 77632 KB Output is correct
34 Correct 416 ms 81100 KB Output is correct
35 Correct 478 ms 82636 KB Output is correct
36 Correct 455 ms 82512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47420 KB Output is correct
2 Correct 25 ms 47316 KB Output is correct
3 Correct 26 ms 47432 KB Output is correct
4 Correct 26 ms 47424 KB Output is correct
5 Correct 25 ms 47316 KB Output is correct
6 Correct 27 ms 47372 KB Output is correct
7 Correct 26 ms 47412 KB Output is correct
8 Correct 25 ms 47368 KB Output is correct
9 Correct 24 ms 47316 KB Output is correct
10 Correct 24 ms 47376 KB Output is correct
11 Correct 24 ms 47352 KB Output is correct
12 Correct 23 ms 47320 KB Output is correct
13 Correct 25 ms 47320 KB Output is correct
14 Correct 23 ms 47316 KB Output is correct
15 Correct 24 ms 47316 KB Output is correct
16 Correct 22 ms 47432 KB Output is correct
17 Correct 24 ms 47572 KB Output is correct
18 Correct 25 ms 47596 KB Output is correct
19 Correct 26 ms 47688 KB Output is correct
20 Correct 27 ms 47708 KB Output is correct
21 Correct 26 ms 47728 KB Output is correct
22 Correct 26 ms 47700 KB Output is correct
23 Correct 27 ms 47700 KB Output is correct
24 Correct 191 ms 64708 KB Output is correct
25 Correct 183 ms 77004 KB Output is correct
26 Correct 225 ms 78564 KB Output is correct
27 Correct 210 ms 81644 KB Output is correct
28 Correct 252 ms 79092 KB Output is correct
29 Correct 160 ms 64720 KB Output is correct
30 Correct 508 ms 82544 KB Output is correct
31 Correct 97 ms 67588 KB Output is correct
32 Correct 86 ms 59164 KB Output is correct
33 Correct 335 ms 77632 KB Output is correct
34 Correct 416 ms 81100 KB Output is correct
35 Correct 478 ms 82636 KB Output is correct
36 Correct 455 ms 82512 KB Output is correct
37 Correct 235 ms 78556 KB Output is correct
38 Correct 222 ms 81628 KB Output is correct
39 Correct 271 ms 84788 KB Output is correct
40 Correct 262 ms 84772 KB Output is correct
41 Correct 30 ms 47316 KB Output is correct
42 Correct 533 ms 82520 KB Output is correct
43 Correct 380 ms 77568 KB Output is correct
44 Correct 445 ms 81048 KB Output is correct
45 Correct 480 ms 82560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47420 KB Output is correct
2 Correct 25 ms 47316 KB Output is correct
3 Correct 26 ms 47432 KB Output is correct
4 Correct 26 ms 47424 KB Output is correct
5 Correct 25 ms 47316 KB Output is correct
6 Correct 27 ms 47372 KB Output is correct
7 Correct 26 ms 47412 KB Output is correct
8 Correct 25 ms 47368 KB Output is correct
9 Correct 24 ms 47316 KB Output is correct
10 Correct 24 ms 47376 KB Output is correct
11 Correct 24 ms 47352 KB Output is correct
12 Correct 23 ms 47320 KB Output is correct
13 Correct 25 ms 47320 KB Output is correct
14 Correct 23 ms 47316 KB Output is correct
15 Correct 24 ms 47316 KB Output is correct
16 Correct 22 ms 47432 KB Output is correct
17 Correct 24 ms 47572 KB Output is correct
18 Correct 25 ms 47596 KB Output is correct
19 Correct 26 ms 47688 KB Output is correct
20 Correct 27 ms 47708 KB Output is correct
21 Correct 26 ms 47728 KB Output is correct
22 Correct 26 ms 47700 KB Output is correct
23 Correct 27 ms 47700 KB Output is correct
24 Correct 191 ms 64708 KB Output is correct
25 Correct 183 ms 77004 KB Output is correct
26 Correct 225 ms 78564 KB Output is correct
27 Correct 210 ms 81644 KB Output is correct
28 Correct 252 ms 79092 KB Output is correct
29 Correct 160 ms 64720 KB Output is correct
30 Correct 508 ms 82544 KB Output is correct
31 Correct 97 ms 67588 KB Output is correct
32 Correct 86 ms 59164 KB Output is correct
33 Correct 335 ms 77632 KB Output is correct
34 Correct 416 ms 81100 KB Output is correct
35 Correct 478 ms 82636 KB Output is correct
36 Correct 455 ms 82512 KB Output is correct
37 Correct 235 ms 78556 KB Output is correct
38 Correct 222 ms 81628 KB Output is correct
39 Correct 271 ms 84788 KB Output is correct
40 Correct 262 ms 84772 KB Output is correct
41 Correct 30 ms 47316 KB Output is correct
42 Correct 533 ms 82520 KB Output is correct
43 Correct 380 ms 77568 KB Output is correct
44 Correct 445 ms 81048 KB Output is correct
45 Correct 480 ms 82560 KB Output is correct
46 Correct 1157 ms 190440 KB Output is correct
47 Correct 1052 ms 206224 KB Output is correct
48 Correct 1274 ms 221868 KB Output is correct
49 Correct 1461 ms 221812 KB Output is correct
50 Correct 3490 ms 211104 KB Output is correct
51 Correct 1999 ms 183692 KB Output is correct
52 Correct 2447 ms 200284 KB Output is correct
53 Correct 3086 ms 209972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 269 ms 80432 KB Output is correct
2 Correct 269 ms 80128 KB Output is correct
3 Correct 161 ms 64640 KB Output is correct
4 Correct 236 ms 74168 KB Output is correct
5 Correct 23 ms 47416 KB Output is correct
6 Correct 240 ms 77484 KB Output is correct
7 Correct 86 ms 52892 KB Output is correct
8 Correct 89 ms 59048 KB Output is correct
9 Correct 167 ms 64712 KB Output is correct
10 Correct 250 ms 84800 KB Output is correct
11 Correct 134 ms 64632 KB Output is correct
12 Correct 24 ms 47420 KB Output is correct
13 Correct 25 ms 47316 KB Output is correct
14 Correct 26 ms 47432 KB Output is correct
15 Correct 26 ms 47424 KB Output is correct
16 Correct 25 ms 47316 KB Output is correct
17 Correct 27 ms 47372 KB Output is correct
18 Correct 26 ms 47412 KB Output is correct
19 Correct 25 ms 47368 KB Output is correct
20 Correct 24 ms 47316 KB Output is correct
21 Correct 24 ms 47376 KB Output is correct
22 Correct 24 ms 47352 KB Output is correct
23 Correct 23 ms 47320 KB Output is correct
24 Correct 25 ms 47320 KB Output is correct
25 Correct 23 ms 47316 KB Output is correct
26 Correct 24 ms 47316 KB Output is correct
27 Correct 22 ms 47432 KB Output is correct
28 Correct 24 ms 47572 KB Output is correct
29 Correct 25 ms 47596 KB Output is correct
30 Correct 26 ms 47688 KB Output is correct
31 Correct 27 ms 47708 KB Output is correct
32 Correct 26 ms 47728 KB Output is correct
33 Correct 26 ms 47700 KB Output is correct
34 Correct 27 ms 47700 KB Output is correct
35 Correct 191 ms 64708 KB Output is correct
36 Correct 183 ms 77004 KB Output is correct
37 Correct 225 ms 78564 KB Output is correct
38 Correct 210 ms 81644 KB Output is correct
39 Correct 252 ms 79092 KB Output is correct
40 Correct 160 ms 64720 KB Output is correct
41 Correct 508 ms 82544 KB Output is correct
42 Correct 97 ms 67588 KB Output is correct
43 Correct 86 ms 59164 KB Output is correct
44 Correct 335 ms 77632 KB Output is correct
45 Correct 416 ms 81100 KB Output is correct
46 Correct 478 ms 82636 KB Output is correct
47 Correct 455 ms 82512 KB Output is correct
48 Correct 235 ms 78556 KB Output is correct
49 Correct 222 ms 81628 KB Output is correct
50 Correct 271 ms 84788 KB Output is correct
51 Correct 262 ms 84772 KB Output is correct
52 Correct 30 ms 47316 KB Output is correct
53 Correct 533 ms 82520 KB Output is correct
54 Correct 380 ms 77568 KB Output is correct
55 Correct 445 ms 81048 KB Output is correct
56 Correct 480 ms 82560 KB Output is correct
57 Correct 200 ms 78436 KB Output is correct
58 Correct 259 ms 81604 KB Output is correct
59 Correct 353 ms 86008 KB Output is correct
60 Correct 248 ms 85888 KB Output is correct
61 Correct 176 ms 66252 KB Output is correct
62 Correct 25 ms 47332 KB Output is correct
63 Correct 544 ms 88580 KB Output is correct
64 Correct 364 ms 83848 KB Output is correct
65 Correct 488 ms 85648 KB Output is correct
66 Correct 508 ms 87464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 269 ms 80432 KB Output is correct
2 Correct 269 ms 80128 KB Output is correct
3 Correct 161 ms 64640 KB Output is correct
4 Correct 236 ms 74168 KB Output is correct
5 Correct 23 ms 47416 KB Output is correct
6 Correct 240 ms 77484 KB Output is correct
7 Correct 86 ms 52892 KB Output is correct
8 Correct 89 ms 59048 KB Output is correct
9 Correct 167 ms 64712 KB Output is correct
10 Correct 250 ms 84800 KB Output is correct
11 Correct 134 ms 64632 KB Output is correct
12 Correct 24 ms 47420 KB Output is correct
13 Correct 25 ms 47316 KB Output is correct
14 Correct 26 ms 47432 KB Output is correct
15 Correct 26 ms 47424 KB Output is correct
16 Correct 25 ms 47316 KB Output is correct
17 Correct 27 ms 47372 KB Output is correct
18 Correct 26 ms 47412 KB Output is correct
19 Correct 25 ms 47368 KB Output is correct
20 Correct 24 ms 47316 KB Output is correct
21 Correct 24 ms 47376 KB Output is correct
22 Correct 24 ms 47352 KB Output is correct
23 Correct 23 ms 47320 KB Output is correct
24 Correct 25 ms 47320 KB Output is correct
25 Correct 23 ms 47316 KB Output is correct
26 Correct 24 ms 47316 KB Output is correct
27 Correct 22 ms 47432 KB Output is correct
28 Correct 24 ms 47572 KB Output is correct
29 Correct 25 ms 47596 KB Output is correct
30 Correct 26 ms 47688 KB Output is correct
31 Correct 27 ms 47708 KB Output is correct
32 Correct 26 ms 47728 KB Output is correct
33 Correct 26 ms 47700 KB Output is correct
34 Correct 27 ms 47700 KB Output is correct
35 Correct 191 ms 64708 KB Output is correct
36 Correct 183 ms 77004 KB Output is correct
37 Correct 225 ms 78564 KB Output is correct
38 Correct 210 ms 81644 KB Output is correct
39 Correct 252 ms 79092 KB Output is correct
40 Correct 160 ms 64720 KB Output is correct
41 Correct 508 ms 82544 KB Output is correct
42 Correct 97 ms 67588 KB Output is correct
43 Correct 86 ms 59164 KB Output is correct
44 Correct 335 ms 77632 KB Output is correct
45 Correct 416 ms 81100 KB Output is correct
46 Correct 478 ms 82636 KB Output is correct
47 Correct 455 ms 82512 KB Output is correct
48 Correct 235 ms 78556 KB Output is correct
49 Correct 222 ms 81628 KB Output is correct
50 Correct 271 ms 84788 KB Output is correct
51 Correct 262 ms 84772 KB Output is correct
52 Correct 30 ms 47316 KB Output is correct
53 Correct 533 ms 82520 KB Output is correct
54 Correct 380 ms 77568 KB Output is correct
55 Correct 445 ms 81048 KB Output is correct
56 Correct 480 ms 82560 KB Output is correct
57 Correct 1157 ms 190440 KB Output is correct
58 Correct 1052 ms 206224 KB Output is correct
59 Correct 1274 ms 221868 KB Output is correct
60 Correct 1461 ms 221812 KB Output is correct
61 Correct 3490 ms 211104 KB Output is correct
62 Correct 1999 ms 183692 KB Output is correct
63 Correct 2447 ms 200284 KB Output is correct
64 Correct 3086 ms 209972 KB Output is correct
65 Correct 200 ms 78436 KB Output is correct
66 Correct 259 ms 81604 KB Output is correct
67 Correct 353 ms 86008 KB Output is correct
68 Correct 248 ms 85888 KB Output is correct
69 Correct 176 ms 66252 KB Output is correct
70 Correct 25 ms 47332 KB Output is correct
71 Correct 544 ms 88580 KB Output is correct
72 Correct 364 ms 83848 KB Output is correct
73 Correct 488 ms 85648 KB Output is correct
74 Correct 508 ms 87464 KB Output is correct
75 Correct 906 ms 235396 KB Output is correct
76 Correct 1269 ms 251100 KB Output is correct
77 Correct 1557 ms 242636 KB Output is correct
78 Correct 1069 ms 242648 KB Output is correct
79 Correct 3704 ms 249620 KB Output is correct
80 Correct 2153 ms 247004 KB Output is correct
81 Correct 2656 ms 247548 KB Output is correct
82 Correct 3357 ms 243868 KB Output is correct
83 Correct 3240 ms 259708 KB Output is correct