답안 #626267

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
626267 2022-08-11T10:42:23 Z Eae02 메기 농장 (IOI22_fish) C++17
100 / 100
742 ms 76392 KB
#include "fish.h"

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define all(x) begin(x),end(x)

struct SegTree {
	using T = ll; // use pair of value and index to get index from queries
	T f(T a, T b) { return max(a, b); }
	static constexpr T UNIT = 0; // neutral value for f
	
	vector<T> s; ll n;
	SegTree(ll len) : s(2 * len, UNIT), n(len) {}
	void set(ll pos, T val) {
		for (s[pos += n] = val; pos /= 2;)
			s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
	}
	T query(ll lo, ll hi) { // query lo to hi (hi not included)
		if (lo >= hi) return UNIT;
		T ra = UNIT, rb = UNIT;
		for (lo += n, hi += n; lo < hi; lo /= 2, hi /= 2) {
			if (lo % 2) ra = f(ra, s[lo++]);
			if (hi % 2) rb = f(s[--hi], rb);
		}
		return f(ra, rb);
	}
};

vector<vector<pair<ll, ll>>> fishWeightSum;

ll weightSum(ll x, ll y) {
	if (x < 0 || x >= (ll)fishWeightSum.size())
		return 0;
	ll idx = lower_bound(all(fishWeightSum[x]), make_pair(y, (ll)-1)) - fishWeightSum[x].begin();
	if (fishWeightSum[x][idx].first > y)
		idx--;
	return fishWeightSum[x][idx].second;
}

long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
	if (M == 0) return 0;
	
	vector<pair<ll, ll>> piers; piers.reserve(M * 2);
	vector<tuple<ll, ll, ll>> fish; fish.reserve(M);
	for (ll i = 0; i < M; i++) {
		fish.emplace_back(X[i], Y[i], W[i]);
		if (X[i] != 0)
			piers.emplace_back(X[i] - 1, Y[i]);
		if (X[i] != N - 1)
			piers.emplace_back(X[i] + 1, Y[i]);
	}
	sort(all(piers));
	piers.erase(unique(all(piers)), piers.end());
	
	sort(all(fish));
	fishWeightSum.resize(N);
	for (ll i = 0; i < N; i++) {
		fishWeightSum[i].emplace_back(-1, 0);
	}
	for (auto [x, y, w] : fish) {
		fishWeightSum[x].emplace_back(y, fishWeightSum[x].back().second + w);
	}
	for (ll i = 0; i < N; i++) {
		fishWeightSum[i].emplace_back(N + 10, fishWeightSum[i].back().second);
	}
	
	SegTree st1(piers.size());
	SegTree st2(piers.size());
	SegTree st3(piers.size());
	
	vector<array<ll, 2>> dpv(piers.size() + 1);
	array<ll, 2>* dp = &dpv[1];
	for (ll lp = (ll)piers.size() - 1; lp >= -1; lp--) {
		ll cx = lp == -1 ? -5 : piers[lp].first;
		ll cy = lp == -1 ? -5 : piers[lp].second;
		
		ll x1ShorterFirstIdx = lower_bound(all(piers), make_pair(cx + 1, (ll)-1)) - piers.begin();
		ll x1LongerFirstIdx = lower_bound(all(piers), make_pair(cx + 1, cy)) - piers.begin();
		ll x2ShorterFirstIdx = lower_bound(all(piers), make_pair(cx + 2, (ll)-1)) - piers.begin();
		ll x2LongerFirstIdx = lower_bound(all(piers), make_pair(cx + 2, cy)) - piers.begin();
		ll c3FirstIdx = lower_bound(all(piers), make_pair(cx + 3, (ll)-1)) - piers.begin();
		
		ll ans = 0;
		ans = max(ans, st1.query(x1ShorterFirstIdx, x1LongerFirstIdx));
		ans = max(ans, st3.query(x2ShorterFirstIdx, x2LongerFirstIdx));
		ans = max(ans, max(st2.query(x2LongerFirstIdx, c3FirstIdx), 0LL) - weightSum(cx + 1, cy));
		ans = max(ans, st2.query(c3FirstIdx, (ll)piers.size()));
		dp[lp][0] = ans;
		ans = max(ans, max(st2.query(x1LongerFirstIdx, x2ShorterFirstIdx), 0LL) - weightSum(cx, cy) - weightSum(cx + 1, cy));
		dp[lp][1] = ans;
		
		/*
		for (ll allowNext = 0; allowNext < 2; allowNext++) {
			ll ans = 0;
			ans = max(ans, st1.query(x1ShorterFirstIdx, x1LongerFirstIdx));
			if (allowNext) {
				ans = max(ans, max(st2.query(x1LongerFirstIdx, x2ShorterFirstIdx), 0LL) - weightSum(cx, cy) - weightSum(cx + 1, cy));
			}
			
			ans = max(ans, st3.query(x2ShorterFirstIdx, x2LongerFirstIdx));
			
			ans = max(ans, max(st3.query(x2LongerFirstIdx, c3FirstIdx), 0LL) - weightSum(cx + 1, cy));
			
			ans = max(ans, st3.query(c3FirstIdx, (ll)piers.size()));
			
			dp[lp][allowNext] = ans;
		}*/
		
		if (lp != -1) {
			st1.set(lp, dp[lp][0] + weightSum(cx + 1, cy) - weightSum(cx, cy));
			st2.set(lp, dp[lp][1] + weightSum(cx + 1, cy) + weightSum(cx - 1, cy));
			st3.set(lp, dp[lp][1] + weightSum(cx + 1, cy));
		}
	}
	
	return dp[-1][1];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 18080 KB Output is correct
2 Correct 116 ms 21440 KB Output is correct
3 Correct 17 ms 7252 KB Output is correct
4 Correct 11 ms 7252 KB Output is correct
5 Correct 635 ms 73684 KB Output is correct
6 Correct 667 ms 75488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 340 ms 35872 KB Output is correct
3 Correct 452 ms 43392 KB Output is correct
4 Correct 94 ms 18012 KB Output is correct
5 Correct 106 ms 21468 KB Output is correct
6 Correct 1 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 0 ms 212 KB Output is correct
10 Correct 20 ms 7320 KB Output is correct
11 Correct 12 ms 7380 KB Output is correct
12 Correct 190 ms 24488 KB Output is correct
13 Correct 216 ms 29320 KB Output is correct
14 Correct 157 ms 21188 KB Output is correct
15 Correct 215 ms 23640 KB Output is correct
16 Correct 123 ms 21328 KB Output is correct
17 Correct 160 ms 23556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 7300 KB Output is correct
2 Correct 16 ms 7320 KB Output is correct
3 Correct 102 ms 16224 KB Output is correct
4 Correct 73 ms 13892 KB Output is correct
5 Correct 135 ms 24604 KB Output is correct
6 Correct 138 ms 24508 KB Output is correct
7 Correct 144 ms 24568 KB Output is correct
8 Correct 143 ms 24600 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 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 3 ms 608 KB Output is correct
11 Correct 2 ms 500 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 488 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 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 3 ms 608 KB Output is correct
11 Correct 2 ms 500 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 488 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 92 ms 8316 KB Output is correct
18 Correct 101 ms 8344 KB Output is correct
19 Correct 77 ms 8412 KB Output is correct
20 Correct 80 ms 7912 KB Output is correct
21 Correct 64 ms 7788 KB Output is correct
22 Correct 139 ms 15416 KB Output is correct
23 Correct 21 ms 2332 KB Output is correct
24 Correct 69 ms 6392 KB Output is correct
25 Correct 3 ms 480 KB Output is correct
26 Correct 20 ms 2124 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 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 3 ms 608 KB Output is correct
11 Correct 2 ms 500 KB Output is correct
12 Correct 3 ms 468 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 488 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 92 ms 8316 KB Output is correct
18 Correct 101 ms 8344 KB Output is correct
19 Correct 77 ms 8412 KB Output is correct
20 Correct 80 ms 7912 KB Output is correct
21 Correct 64 ms 7788 KB Output is correct
22 Correct 139 ms 15416 KB Output is correct
23 Correct 21 ms 2332 KB Output is correct
24 Correct 69 ms 6392 KB Output is correct
25 Correct 3 ms 480 KB Output is correct
26 Correct 20 ms 2124 KB Output is correct
27 Correct 6 ms 1236 KB Output is correct
28 Correct 468 ms 38480 KB Output is correct
29 Correct 669 ms 57936 KB Output is correct
30 Correct 694 ms 66396 KB Output is correct
31 Correct 679 ms 66148 KB Output is correct
32 Correct 440 ms 49252 KB Output is correct
33 Correct 714 ms 66880 KB Output is correct
34 Correct 681 ms 66896 KB Output is correct
35 Correct 260 ms 24692 KB Output is correct
36 Correct 666 ms 65444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 7300 KB Output is correct
2 Correct 16 ms 7320 KB Output is correct
3 Correct 102 ms 16224 KB Output is correct
4 Correct 73 ms 13892 KB Output is correct
5 Correct 135 ms 24604 KB Output is correct
6 Correct 138 ms 24508 KB Output is correct
7 Correct 144 ms 24568 KB Output is correct
8 Correct 143 ms 24600 KB Output is correct
9 Correct 173 ms 30868 KB Output is correct
10 Correct 122 ms 19388 KB Output is correct
11 Correct 235 ms 38620 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 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 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 11 ms 7308 KB Output is correct
19 Correct 12 ms 7336 KB Output is correct
20 Correct 14 ms 7340 KB Output is correct
21 Correct 12 ms 7304 KB Output is correct
22 Correct 191 ms 30040 KB Output is correct
23 Correct 332 ms 47712 KB Output is correct
24 Correct 351 ms 49792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 18080 KB Output is correct
2 Correct 116 ms 21440 KB Output is correct
3 Correct 17 ms 7252 KB Output is correct
4 Correct 11 ms 7252 KB Output is correct
5 Correct 635 ms 73684 KB Output is correct
6 Correct 667 ms 75488 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 340 ms 35872 KB Output is correct
9 Correct 452 ms 43392 KB Output is correct
10 Correct 94 ms 18012 KB Output is correct
11 Correct 106 ms 21468 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 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 20 ms 7320 KB Output is correct
17 Correct 12 ms 7380 KB Output is correct
18 Correct 190 ms 24488 KB Output is correct
19 Correct 216 ms 29320 KB Output is correct
20 Correct 157 ms 21188 KB Output is correct
21 Correct 215 ms 23640 KB Output is correct
22 Correct 123 ms 21328 KB Output is correct
23 Correct 160 ms 23556 KB Output is correct
24 Correct 12 ms 7300 KB Output is correct
25 Correct 16 ms 7320 KB Output is correct
26 Correct 102 ms 16224 KB Output is correct
27 Correct 73 ms 13892 KB Output is correct
28 Correct 135 ms 24604 KB Output is correct
29 Correct 138 ms 24508 KB Output is correct
30 Correct 144 ms 24568 KB Output is correct
31 Correct 143 ms 24600 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 2 ms 340 KB Output is correct
41 Correct 3 ms 608 KB Output is correct
42 Correct 2 ms 500 KB Output is correct
43 Correct 3 ms 468 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 2 ms 488 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 4 ms 596 KB Output is correct
48 Correct 92 ms 8316 KB Output is correct
49 Correct 101 ms 8344 KB Output is correct
50 Correct 77 ms 8412 KB Output is correct
51 Correct 80 ms 7912 KB Output is correct
52 Correct 64 ms 7788 KB Output is correct
53 Correct 139 ms 15416 KB Output is correct
54 Correct 21 ms 2332 KB Output is correct
55 Correct 69 ms 6392 KB Output is correct
56 Correct 3 ms 480 KB Output is correct
57 Correct 20 ms 2124 KB Output is correct
58 Correct 6 ms 1236 KB Output is correct
59 Correct 468 ms 38480 KB Output is correct
60 Correct 669 ms 57936 KB Output is correct
61 Correct 694 ms 66396 KB Output is correct
62 Correct 679 ms 66148 KB Output is correct
63 Correct 440 ms 49252 KB Output is correct
64 Correct 714 ms 66880 KB Output is correct
65 Correct 681 ms 66896 KB Output is correct
66 Correct 260 ms 24692 KB Output is correct
67 Correct 666 ms 65444 KB Output is correct
68 Correct 173 ms 30868 KB Output is correct
69 Correct 122 ms 19388 KB Output is correct
70 Correct 235 ms 38620 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 0 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 11 ms 7308 KB Output is correct
78 Correct 12 ms 7336 KB Output is correct
79 Correct 14 ms 7340 KB Output is correct
80 Correct 12 ms 7304 KB Output is correct
81 Correct 191 ms 30040 KB Output is correct
82 Correct 332 ms 47712 KB Output is correct
83 Correct 351 ms 49792 KB Output is correct
84 Correct 742 ms 70432 KB Output is correct
85 Correct 712 ms 70908 KB Output is correct
86 Correct 619 ms 74984 KB Output is correct
87 Correct 622 ms 76392 KB Output is correct
88 Correct 0 ms 212 KB Output is correct
89 Correct 624 ms 76364 KB Output is correct
90 Correct 586 ms 69852 KB Output is correct
91 Correct 495 ms 61484 KB Output is correct