답안 #522641

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522641 2022-02-05T09:59:16 Z blue Two Dishes (JOI19_dishes) C++17
100 / 100
2975 ms 369864 KB
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
using namespace std;

#define sz(x) int(x.size())
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;

const int mx = 1'000'000;
const ll INF = 1'000'000'000'000'000'000LL;

int N, M;
vll A(1+mx), S(1+mx), P(1+mx);
vll B(1+mx), T(1+mx), Q(1+mx);

vll Asum(1+mx, 0);
vll Bsum(1+mx, 0);


bool dbg = 0;




struct delta
{
	int p;
	ll v;
};

bool operator < (delta A, delta B)
{
	if(A.p == B.p) return A.v < B.v;
	return A.p < B.p;
}




int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int N, M;
	cin >> N >> M;

	for(int i = 1; i <= N; i++)
	{
		cin >> A[i] >> S[i] >> P[i];
		Asum[i] = Asum[i-1] + A[i];
	}

	for(int j = 1; j <= M; j++)
	{
		cin >> B[j] >> T[j] >> Q[j];
		Bsum[j] = Bsum[j-1] + B[j];
	}

















	multiset<delta> hi_points[1+M], lo_points[1+M];

	ll basicCost = 0;




	for(int i = 1; i <= N; i++)
	{
		if(Asum[i] > S[i]) continue;

		int lo = 0, hi = M;
		while(lo != hi)
		{
			int mid = (lo+hi)/2 + 1;

			if(Bsum[mid] + Asum[i] <= S[i])
				lo = mid;
			else
				hi = mid-1;
		}

		if(lo == M)
		{
			basicCost += P[i];

			if(dbg) cerr << i << " : " << "basicCost += " << P[i] << '\n';
		}
		else
		{
			delta cr{i, +P[i]};
			if(dbg) cerr << lo+1 << " : " << i << ' ' << P[i] << '\n';
			// cerr << lo+1 << ' ' << i << ' ' << +P[i] << '\n';

			if(cr.v >= 0) hi_points[lo+1].insert(cr);
			else 
			{
				cr.v *= -1;
				lo_points[lo+1].insert(cr);
			}
		}
	}

	if(dbg)cerr << "-----\n";









	for(int j = 1; j <= M; j++)
	{
		if(Bsum[j] > T[j]) continue;

		int lo = 0, hi = N;
		while(lo != hi)
		{
			int mid = (lo+hi)/2+1;

			if(Asum[mid] + Bsum[j] <= T[j])
				lo = mid;
			else
				hi = mid-1;
		}

		// cerr << j << " : " << lo << '\n';

		if(lo == N)
		{
			basicCost += Q[j];
			if(dbg) cerr << j << " : " << "basicCost += " << Q[j] << '\n';
		}
		else
		{
			basicCost += Q[j];
			delta cr{lo+1, -Q[j]};

			if(dbg) cerr << j << ' ' << lo+1 << ' ' << -T[j] << '\n';

			if(cr.v >= 0) hi_points[j].insert(cr);
			else 
			{
				cr.v *= -1;
				lo_points[j].insert(cr);
			}
		}
	}




	vi init_size(1+M, 0);
	for(int j = 1; j <= M; j++) init_size[j] = sz(hi_points[j]) + sz(lo_points[j]);







	multiset<delta>* hp[1+M];
	multiset<delta>* lp[1+M];
	for(int j = 1; j <= M; j++)
	{
		hp[j] = &hi_points[j];
		lp[j] = &lo_points[j];
	}

	for(int j = 1; j <= M; j++)
	{
		while(!lp[j]->empty())
		{
			auto lit = lp[j]->begin();

			delta lpv = *lit;


			auto hit = hp[j]->lower_bound({lpv.p, -INF});

			if(hit == hp[j]->end())
			{
				lp[j]->clear();
				break;
			}

			delta hpv = *hit;


			ll cng = min(lpv.v, hpv.v);


			lp[j]->erase(lit);
			if(lpv.v != cng)
				lp[j]->insert({lpv.p, lpv.v - cng});

			hp[j]->erase(hit);
			if(hpv.v != cng)
				hp[j]->insert({hpv.p, hpv.v - cng});
		}

		if(j < M)
		{
			if(init_size[j] > init_size[j+1])
			{
				swap(lp[j], lp[j+1]);
				swap(hp[j], hp[j+1]);
				swap(init_size[j], init_size[j+1]);
			}

			init_size[j+1] += init_size[j];

			for(delta x : *lp[j])
				lp[j+1]->insert(x);

			for(delta y : *hp[j])
				hp[j+1]->insert(y);

			lp[j]->clear();
			hp[j]->clear();
		}
	}

	// cerr << "basicCost = "<< basicCost << '\n';





	ll ans = basicCost;

	for(delta z : *hp[M])
		ans += z.v;

	cout << ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 299 ms 108208 KB Output is correct
2 Correct 311 ms 107388 KB Output is correct
3 Correct 217 ms 85604 KB Output is correct
4 Correct 228 ms 101008 KB Output is correct
5 Correct 31 ms 62912 KB Output is correct
6 Correct 267 ms 102256 KB Output is correct
7 Correct 113 ms 85572 KB Output is correct
8 Correct 92 ms 62968 KB Output is correct
9 Correct 209 ms 85616 KB Output is correct
10 Correct 289 ms 110664 KB Output is correct
11 Correct 161 ms 93416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 62896 KB Output is correct
2 Correct 24 ms 62924 KB Output is correct
3 Correct 25 ms 62960 KB Output is correct
4 Correct 29 ms 62924 KB Output is correct
5 Correct 25 ms 62896 KB Output is correct
6 Correct 25 ms 62868 KB Output is correct
7 Correct 32 ms 62876 KB Output is correct
8 Correct 25 ms 62924 KB Output is correct
9 Correct 25 ms 62924 KB Output is correct
10 Correct 24 ms 62892 KB Output is correct
11 Correct 27 ms 62868 KB Output is correct
12 Correct 23 ms 62872 KB Output is correct
13 Correct 23 ms 62860 KB Output is correct
14 Correct 25 ms 62940 KB Output is correct
15 Correct 22 ms 62924 KB Output is correct
16 Correct 25 ms 62828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 62896 KB Output is correct
2 Correct 24 ms 62924 KB Output is correct
3 Correct 25 ms 62960 KB Output is correct
4 Correct 29 ms 62924 KB Output is correct
5 Correct 25 ms 62896 KB Output is correct
6 Correct 25 ms 62868 KB Output is correct
7 Correct 32 ms 62876 KB Output is correct
8 Correct 25 ms 62924 KB Output is correct
9 Correct 25 ms 62924 KB Output is correct
10 Correct 24 ms 62892 KB Output is correct
11 Correct 27 ms 62868 KB Output is correct
12 Correct 23 ms 62872 KB Output is correct
13 Correct 23 ms 62860 KB Output is correct
14 Correct 25 ms 62940 KB Output is correct
15 Correct 22 ms 62924 KB Output is correct
16 Correct 25 ms 62828 KB Output is correct
17 Correct 27 ms 63172 KB Output is correct
18 Correct 27 ms 63284 KB Output is correct
19 Correct 26 ms 63312 KB Output is correct
20 Correct 25 ms 63180 KB Output is correct
21 Correct 25 ms 63268 KB Output is correct
22 Correct 25 ms 63436 KB Output is correct
23 Correct 28 ms 63404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 62896 KB Output is correct
2 Correct 24 ms 62924 KB Output is correct
3 Correct 25 ms 62960 KB Output is correct
4 Correct 29 ms 62924 KB Output is correct
5 Correct 25 ms 62896 KB Output is correct
6 Correct 25 ms 62868 KB Output is correct
7 Correct 32 ms 62876 KB Output is correct
8 Correct 25 ms 62924 KB Output is correct
9 Correct 25 ms 62924 KB Output is correct
10 Correct 24 ms 62892 KB Output is correct
11 Correct 27 ms 62868 KB Output is correct
12 Correct 23 ms 62872 KB Output is correct
13 Correct 23 ms 62860 KB Output is correct
14 Correct 25 ms 62940 KB Output is correct
15 Correct 22 ms 62924 KB Output is correct
16 Correct 25 ms 62828 KB Output is correct
17 Correct 27 ms 63172 KB Output is correct
18 Correct 27 ms 63284 KB Output is correct
19 Correct 26 ms 63312 KB Output is correct
20 Correct 25 ms 63180 KB Output is correct
21 Correct 25 ms 63268 KB Output is correct
22 Correct 25 ms 63436 KB Output is correct
23 Correct 28 ms 63404 KB Output is correct
24 Correct 251 ms 108644 KB Output is correct
25 Correct 237 ms 108820 KB Output is correct
26 Correct 272 ms 108848 KB Output is correct
27 Correct 235 ms 108800 KB Output is correct
28 Correct 245 ms 102744 KB Output is correct
29 Correct 199 ms 96892 KB Output is correct
30 Correct 392 ms 120444 KB Output is correct
31 Correct 109 ms 97136 KB Output is correct
32 Correct 128 ms 74328 KB Output is correct
33 Correct 293 ms 108144 KB Output is correct
34 Correct 357 ms 114800 KB Output is correct
35 Correct 330 ms 114396 KB Output is correct
36 Correct 338 ms 113600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 62896 KB Output is correct
2 Correct 24 ms 62924 KB Output is correct
3 Correct 25 ms 62960 KB Output is correct
4 Correct 29 ms 62924 KB Output is correct
5 Correct 25 ms 62896 KB Output is correct
6 Correct 25 ms 62868 KB Output is correct
7 Correct 32 ms 62876 KB Output is correct
8 Correct 25 ms 62924 KB Output is correct
9 Correct 25 ms 62924 KB Output is correct
10 Correct 24 ms 62892 KB Output is correct
11 Correct 27 ms 62868 KB Output is correct
12 Correct 23 ms 62872 KB Output is correct
13 Correct 23 ms 62860 KB Output is correct
14 Correct 25 ms 62940 KB Output is correct
15 Correct 22 ms 62924 KB Output is correct
16 Correct 25 ms 62828 KB Output is correct
17 Correct 27 ms 63172 KB Output is correct
18 Correct 27 ms 63284 KB Output is correct
19 Correct 26 ms 63312 KB Output is correct
20 Correct 25 ms 63180 KB Output is correct
21 Correct 25 ms 63268 KB Output is correct
22 Correct 25 ms 63436 KB Output is correct
23 Correct 28 ms 63404 KB Output is correct
24 Correct 251 ms 108644 KB Output is correct
25 Correct 237 ms 108820 KB Output is correct
26 Correct 272 ms 108848 KB Output is correct
27 Correct 235 ms 108800 KB Output is correct
28 Correct 245 ms 102744 KB Output is correct
29 Correct 199 ms 96892 KB Output is correct
30 Correct 392 ms 120444 KB Output is correct
31 Correct 109 ms 97136 KB Output is correct
32 Correct 128 ms 74328 KB Output is correct
33 Correct 293 ms 108144 KB Output is correct
34 Correct 357 ms 114800 KB Output is correct
35 Correct 330 ms 114396 KB Output is correct
36 Correct 338 ms 113600 KB Output is correct
37 Correct 311 ms 111872 KB Output is correct
38 Correct 238 ms 111840 KB Output is correct
39 Correct 272 ms 121668 KB Output is correct
40 Correct 366 ms 121696 KB Output is correct
41 Correct 32 ms 62948 KB Output is correct
42 Correct 506 ms 123548 KB Output is correct
43 Correct 319 ms 111096 KB Output is correct
44 Correct 446 ms 117248 KB Output is correct
45 Correct 407 ms 117548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 62896 KB Output is correct
2 Correct 24 ms 62924 KB Output is correct
3 Correct 25 ms 62960 KB Output is correct
4 Correct 29 ms 62924 KB Output is correct
5 Correct 25 ms 62896 KB Output is correct
6 Correct 25 ms 62868 KB Output is correct
7 Correct 32 ms 62876 KB Output is correct
8 Correct 25 ms 62924 KB Output is correct
9 Correct 25 ms 62924 KB Output is correct
10 Correct 24 ms 62892 KB Output is correct
11 Correct 27 ms 62868 KB Output is correct
12 Correct 23 ms 62872 KB Output is correct
13 Correct 23 ms 62860 KB Output is correct
14 Correct 25 ms 62940 KB Output is correct
15 Correct 22 ms 62924 KB Output is correct
16 Correct 25 ms 62828 KB Output is correct
17 Correct 27 ms 63172 KB Output is correct
18 Correct 27 ms 63284 KB Output is correct
19 Correct 26 ms 63312 KB Output is correct
20 Correct 25 ms 63180 KB Output is correct
21 Correct 25 ms 63268 KB Output is correct
22 Correct 25 ms 63436 KB Output is correct
23 Correct 28 ms 63404 KB Output is correct
24 Correct 251 ms 108644 KB Output is correct
25 Correct 237 ms 108820 KB Output is correct
26 Correct 272 ms 108848 KB Output is correct
27 Correct 235 ms 108800 KB Output is correct
28 Correct 245 ms 102744 KB Output is correct
29 Correct 199 ms 96892 KB Output is correct
30 Correct 392 ms 120444 KB Output is correct
31 Correct 109 ms 97136 KB Output is correct
32 Correct 128 ms 74328 KB Output is correct
33 Correct 293 ms 108144 KB Output is correct
34 Correct 357 ms 114800 KB Output is correct
35 Correct 330 ms 114396 KB Output is correct
36 Correct 338 ms 113600 KB Output is correct
37 Correct 311 ms 111872 KB Output is correct
38 Correct 238 ms 111840 KB Output is correct
39 Correct 272 ms 121668 KB Output is correct
40 Correct 366 ms 121696 KB Output is correct
41 Correct 32 ms 62948 KB Output is correct
42 Correct 506 ms 123548 KB Output is correct
43 Correct 319 ms 111096 KB Output is correct
44 Correct 446 ms 117248 KB Output is correct
45 Correct 407 ms 117548 KB Output is correct
46 Correct 1516 ms 308932 KB Output is correct
47 Correct 1127 ms 308988 KB Output is correct
48 Correct 1246 ms 358044 KB Output is correct
49 Correct 1741 ms 358148 KB Output is correct
50 Correct 2664 ms 369288 KB Output is correct
51 Correct 1840 ms 301824 KB Output is correct
52 Correct 2435 ms 315456 KB Output is correct
53 Correct 2589 ms 333416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 299 ms 108208 KB Output is correct
2 Correct 311 ms 107388 KB Output is correct
3 Correct 217 ms 85604 KB Output is correct
4 Correct 228 ms 101008 KB Output is correct
5 Correct 31 ms 62912 KB Output is correct
6 Correct 267 ms 102256 KB Output is correct
7 Correct 113 ms 85572 KB Output is correct
8 Correct 92 ms 62968 KB Output is correct
9 Correct 209 ms 85616 KB Output is correct
10 Correct 289 ms 110664 KB Output is correct
11 Correct 161 ms 93416 KB Output is correct
12 Correct 31 ms 62896 KB Output is correct
13 Correct 24 ms 62924 KB Output is correct
14 Correct 25 ms 62960 KB Output is correct
15 Correct 29 ms 62924 KB Output is correct
16 Correct 25 ms 62896 KB Output is correct
17 Correct 25 ms 62868 KB Output is correct
18 Correct 32 ms 62876 KB Output is correct
19 Correct 25 ms 62924 KB Output is correct
20 Correct 25 ms 62924 KB Output is correct
21 Correct 24 ms 62892 KB Output is correct
22 Correct 27 ms 62868 KB Output is correct
23 Correct 23 ms 62872 KB Output is correct
24 Correct 23 ms 62860 KB Output is correct
25 Correct 25 ms 62940 KB Output is correct
26 Correct 22 ms 62924 KB Output is correct
27 Correct 25 ms 62828 KB Output is correct
28 Correct 27 ms 63172 KB Output is correct
29 Correct 27 ms 63284 KB Output is correct
30 Correct 26 ms 63312 KB Output is correct
31 Correct 25 ms 63180 KB Output is correct
32 Correct 25 ms 63268 KB Output is correct
33 Correct 25 ms 63436 KB Output is correct
34 Correct 28 ms 63404 KB Output is correct
35 Correct 251 ms 108644 KB Output is correct
36 Correct 237 ms 108820 KB Output is correct
37 Correct 272 ms 108848 KB Output is correct
38 Correct 235 ms 108800 KB Output is correct
39 Correct 245 ms 102744 KB Output is correct
40 Correct 199 ms 96892 KB Output is correct
41 Correct 392 ms 120444 KB Output is correct
42 Correct 109 ms 97136 KB Output is correct
43 Correct 128 ms 74328 KB Output is correct
44 Correct 293 ms 108144 KB Output is correct
45 Correct 357 ms 114800 KB Output is correct
46 Correct 330 ms 114396 KB Output is correct
47 Correct 338 ms 113600 KB Output is correct
48 Correct 311 ms 111872 KB Output is correct
49 Correct 238 ms 111840 KB Output is correct
50 Correct 272 ms 121668 KB Output is correct
51 Correct 366 ms 121696 KB Output is correct
52 Correct 32 ms 62948 KB Output is correct
53 Correct 506 ms 123548 KB Output is correct
54 Correct 319 ms 111096 KB Output is correct
55 Correct 446 ms 117248 KB Output is correct
56 Correct 407 ms 117548 KB Output is correct
57 Correct 281 ms 112340 KB Output is correct
58 Correct 341 ms 112340 KB Output is correct
59 Correct 408 ms 122640 KB Output is correct
60 Correct 295 ms 122608 KB Output is correct
61 Correct 745 ms 121320 KB Output is correct
62 Correct 29 ms 62924 KB Output is correct
63 Correct 475 ms 123664 KB Output is correct
64 Correct 341 ms 110172 KB Output is correct
65 Correct 433 ms 118548 KB Output is correct
66 Correct 407 ms 116820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 299 ms 108208 KB Output is correct
2 Correct 311 ms 107388 KB Output is correct
3 Correct 217 ms 85604 KB Output is correct
4 Correct 228 ms 101008 KB Output is correct
5 Correct 31 ms 62912 KB Output is correct
6 Correct 267 ms 102256 KB Output is correct
7 Correct 113 ms 85572 KB Output is correct
8 Correct 92 ms 62968 KB Output is correct
9 Correct 209 ms 85616 KB Output is correct
10 Correct 289 ms 110664 KB Output is correct
11 Correct 161 ms 93416 KB Output is correct
12 Correct 31 ms 62896 KB Output is correct
13 Correct 24 ms 62924 KB Output is correct
14 Correct 25 ms 62960 KB Output is correct
15 Correct 29 ms 62924 KB Output is correct
16 Correct 25 ms 62896 KB Output is correct
17 Correct 25 ms 62868 KB Output is correct
18 Correct 32 ms 62876 KB Output is correct
19 Correct 25 ms 62924 KB Output is correct
20 Correct 25 ms 62924 KB Output is correct
21 Correct 24 ms 62892 KB Output is correct
22 Correct 27 ms 62868 KB Output is correct
23 Correct 23 ms 62872 KB Output is correct
24 Correct 23 ms 62860 KB Output is correct
25 Correct 25 ms 62940 KB Output is correct
26 Correct 22 ms 62924 KB Output is correct
27 Correct 25 ms 62828 KB Output is correct
28 Correct 27 ms 63172 KB Output is correct
29 Correct 27 ms 63284 KB Output is correct
30 Correct 26 ms 63312 KB Output is correct
31 Correct 25 ms 63180 KB Output is correct
32 Correct 25 ms 63268 KB Output is correct
33 Correct 25 ms 63436 KB Output is correct
34 Correct 28 ms 63404 KB Output is correct
35 Correct 251 ms 108644 KB Output is correct
36 Correct 237 ms 108820 KB Output is correct
37 Correct 272 ms 108848 KB Output is correct
38 Correct 235 ms 108800 KB Output is correct
39 Correct 245 ms 102744 KB Output is correct
40 Correct 199 ms 96892 KB Output is correct
41 Correct 392 ms 120444 KB Output is correct
42 Correct 109 ms 97136 KB Output is correct
43 Correct 128 ms 74328 KB Output is correct
44 Correct 293 ms 108144 KB Output is correct
45 Correct 357 ms 114800 KB Output is correct
46 Correct 330 ms 114396 KB Output is correct
47 Correct 338 ms 113600 KB Output is correct
48 Correct 311 ms 111872 KB Output is correct
49 Correct 238 ms 111840 KB Output is correct
50 Correct 272 ms 121668 KB Output is correct
51 Correct 366 ms 121696 KB Output is correct
52 Correct 32 ms 62948 KB Output is correct
53 Correct 506 ms 123548 KB Output is correct
54 Correct 319 ms 111096 KB Output is correct
55 Correct 446 ms 117248 KB Output is correct
56 Correct 407 ms 117548 KB Output is correct
57 Correct 1516 ms 308932 KB Output is correct
58 Correct 1127 ms 308988 KB Output is correct
59 Correct 1246 ms 358044 KB Output is correct
60 Correct 1741 ms 358148 KB Output is correct
61 Correct 2664 ms 369288 KB Output is correct
62 Correct 1840 ms 301824 KB Output is correct
63 Correct 2435 ms 315456 KB Output is correct
64 Correct 2589 ms 333416 KB Output is correct
65 Correct 281 ms 112340 KB Output is correct
66 Correct 341 ms 112340 KB Output is correct
67 Correct 408 ms 122640 KB Output is correct
68 Correct 295 ms 122608 KB Output is correct
69 Correct 745 ms 121320 KB Output is correct
70 Correct 29 ms 62924 KB Output is correct
71 Correct 475 ms 123664 KB Output is correct
72 Correct 341 ms 110172 KB Output is correct
73 Correct 433 ms 118548 KB Output is correct
74 Correct 407 ms 116820 KB Output is correct
75 Correct 1152 ms 310784 KB Output is correct
76 Correct 1568 ms 310900 KB Output is correct
77 Correct 2094 ms 360508 KB Output is correct
78 Correct 1322 ms 360528 KB Output is correct
79 Correct 2869 ms 369864 KB Output is correct
80 Correct 1649 ms 294868 KB Output is correct
81 Correct 2254 ms 330984 KB Output is correct
82 Correct 2975 ms 336136 KB Output is correct
83 Correct 2654 ms 349252 KB Output is correct