답안 #626230

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
626230 2022-08-11T10:13:53 Z Eae02 메기 농장 (IOI22_fish) C++17
32 / 100
800 ms 94392 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)
		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)LLONG_MIN)) - 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;
	vector<tuple<ll, ll, ll>> fish;
	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(LLONG_MIN, 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(LLONG_MAX, fishWeightSum[i].back().second);
	}
	
	SegTree st1(piers.size());
	SegTree st2(piers.size());
	SegTree st3(piers.size());
	SegTree st4(piers.size());
	SegTree st5(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)LLONG_MIN)) - 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)LLONG_MIN)) - 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)LLONG_MIN)) - piers.begin();
		
		for (ll allowNext = 0; allowNext < 2; allowNext++) {
			ll ans = 0;
			if (allowNext) {
				ans = max(ans, st1.query(x1ShorterFirstIdx, x1LongerFirstIdx));
				//for (ll np = x1ShorterFirstIdx; np < x1LongerFirstIdx; np++) {
				//	auto [nx, ny] = piers[np];
				//	ans = max(ans, dp[np][0] + weightSum(nx + 1, ny) - weightSum(nx, ny));
				//}
				
				ans = max(ans, st2.query(x1LongerFirstIdx, x2ShorterFirstIdx) - weightSum(cx, cy) - weightSum(cx + 1, cy));
				//for (ll np = x1LongerFirstIdx; np < x2ShorterFirstIdx; np++) {
				//	auto [nx, ny] = piers[np];
				//	ans = max(ans, dp[np][1] + weightSum(nx + 1, ny) + weightSum(nx - 1, ny) - weightSum(cx, cy) - weightSum(cx + 1, cy));
				//}
			}
			
			ans = max(ans, st3.query(x2ShorterFirstIdx, x2LongerFirstIdx));
			//for (ll np = x2ShorterFirstIdx; np < x2LongerFirstIdx; np++) {
			//	auto [nx, ny] = piers[np];
			//	ans = max(ans, dp[np][1] + weightSum(nx + 1, ny));
			//}
			
			ans = max(ans, st4.query(x2LongerFirstIdx, c3FirstIdx) - weightSum(cx + 1, cy));
			//for (ll np = x2LongerFirstIdx; np < c3FirstIdx; np++) {
			//	auto [nx, ny] = piers[np];
			//	ans = max(ans, dp[np][1] + weightSum(nx + 1, ny) + weightSum(nx - 1, ny) - weightSum(cx + 1, cy));
			//}
			
			ans = max(ans, st5.query(c3FirstIdx, (ll)piers.size()));
			//for (ll np = c3FirstIdx; np < (ll)piers.size(); np++) {
			//	auto [nx, ny] = piers[np];
			//	ans = max(ans, dp[np][1] + weightSum(nx + 1, ny) + weightSum(nx - 1, ny));
			//}
			
			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));
			st4.set(lp, dp[lp][1] + weightSum(cx + 1, cy) + weightSum(cx - 1, cy));
			st5.set(lp, dp[lp][1] + weightSum(cx + 1, cy) + weightSum(cx - 1, cy));
		}
	}
	
	return dp[-1][1];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 21196 KB Output is correct
2 Correct 121 ms 25196 KB Output is correct
3 Correct 11 ms 7316 KB Output is correct
4 Correct 13 ms 7260 KB Output is correct
5 Correct 800 ms 93068 KB Output is correct
6 Correct 790 ms 94392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 350 ms 44156 KB Output is correct
3 Correct 419 ms 53368 KB Output is correct
4 Correct 100 ms 21212 KB Output is correct
5 Correct 120 ms 25080 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 14 ms 7336 KB Output is correct
11 Correct 12 ms 7444 KB Output is correct
12 Correct 199 ms 30232 KB Output is correct
13 Correct 252 ms 36048 KB Output is correct
14 Correct 150 ms 25184 KB Output is correct
15 Correct 204 ms 27964 KB Output is correct
16 Correct 156 ms 25116 KB Output is correct
17 Correct 182 ms 27900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 7308 KB Output is correct
2 Correct 11 ms 7252 KB Output is correct
3 Correct 84 ms 18064 KB Output is correct
4 Correct 62 ms 15192 KB Output is correct
5 Correct 154 ms 27704 KB Output is correct
6 Correct 149 ms 27732 KB Output is correct
7 Correct 140 ms 27844 KB Output is correct
8 Correct 138 ms 27688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 260 KB Output is correct
4 Correct 1 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 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 3 ms 852 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 260 KB Output is correct
4 Correct 1 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 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 3 ms 852 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 111 ms 10144 KB Output is correct
18 Correct 89 ms 9928 KB Output is correct
19 Correct 84 ms 10056 KB Output is correct
20 Correct 80 ms 9272 KB Output is correct
21 Correct 82 ms 9260 KB Output is correct
22 Correct 159 ms 18112 KB Output is correct
23 Correct 23 ms 2880 KB Output is correct
24 Correct 76 ms 7948 KB Output is correct
25 Incorrect 3 ms 596 KB 1st lines differ - on the 1st token, expected: '443843838929', found: '443838673362'
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 260 KB Output is correct
4 Correct 1 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 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 3 ms 852 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 111 ms 10144 KB Output is correct
18 Correct 89 ms 9928 KB Output is correct
19 Correct 84 ms 10056 KB Output is correct
20 Correct 80 ms 9272 KB Output is correct
21 Correct 82 ms 9260 KB Output is correct
22 Correct 159 ms 18112 KB Output is correct
23 Correct 23 ms 2880 KB Output is correct
24 Correct 76 ms 7948 KB Output is correct
25 Incorrect 3 ms 596 KB 1st lines differ - on the 1st token, expected: '443843838929', found: '443838673362'
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 7308 KB Output is correct
2 Correct 11 ms 7252 KB Output is correct
3 Correct 84 ms 18064 KB Output is correct
4 Correct 62 ms 15192 KB Output is correct
5 Correct 154 ms 27704 KB Output is correct
6 Correct 149 ms 27732 KB Output is correct
7 Correct 140 ms 27844 KB Output is correct
8 Correct 138 ms 27688 KB Output is correct
9 Correct 228 ms 37044 KB Output is correct
10 Correct 138 ms 22668 KB Output is correct
11 Correct 327 ms 44848 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 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 12 ms 7252 KB Output is correct
19 Correct 12 ms 7332 KB Output is correct
20 Correct 11 ms 7276 KB Output is correct
21 Correct 14 ms 7308 KB Output is correct
22 Incorrect 254 ms 36288 KB 1st lines differ - on the 1st token, expected: '45561826463480', found: '45449488300152'
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 21196 KB Output is correct
2 Correct 121 ms 25196 KB Output is correct
3 Correct 11 ms 7316 KB Output is correct
4 Correct 13 ms 7260 KB Output is correct
5 Correct 800 ms 93068 KB Output is correct
6 Correct 790 ms 94392 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 350 ms 44156 KB Output is correct
9 Correct 419 ms 53368 KB Output is correct
10 Correct 100 ms 21212 KB Output is correct
11 Correct 120 ms 25080 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 1 ms 212 KB Output is correct
16 Correct 14 ms 7336 KB Output is correct
17 Correct 12 ms 7444 KB Output is correct
18 Correct 199 ms 30232 KB Output is correct
19 Correct 252 ms 36048 KB Output is correct
20 Correct 150 ms 25184 KB Output is correct
21 Correct 204 ms 27964 KB Output is correct
22 Correct 156 ms 25116 KB Output is correct
23 Correct 182 ms 27900 KB Output is correct
24 Correct 11 ms 7308 KB Output is correct
25 Correct 11 ms 7252 KB Output is correct
26 Correct 84 ms 18064 KB Output is correct
27 Correct 62 ms 15192 KB Output is correct
28 Correct 154 ms 27704 KB Output is correct
29 Correct 149 ms 27732 KB Output is correct
30 Correct 140 ms 27844 KB Output is correct
31 Correct 138 ms 27688 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 260 KB Output is correct
35 Correct 1 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 0 ms 212 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 3 ms 852 KB Output is correct
42 Correct 2 ms 468 KB Output is correct
43 Correct 3 ms 596 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 2 ms 512 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 5 ms 852 KB Output is correct
48 Correct 111 ms 10144 KB Output is correct
49 Correct 89 ms 9928 KB Output is correct
50 Correct 84 ms 10056 KB Output is correct
51 Correct 80 ms 9272 KB Output is correct
52 Correct 82 ms 9260 KB Output is correct
53 Correct 159 ms 18112 KB Output is correct
54 Correct 23 ms 2880 KB Output is correct
55 Correct 76 ms 7948 KB Output is correct
56 Incorrect 3 ms 596 KB 1st lines differ - on the 1st token, expected: '443843838929', found: '443838673362'
57 Halted 0 ms 0 KB -