답안 #626415

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
626415 2022-08-11T12:26:50 Z TheLostCookie 메기 농장 (IOI22_fish) C++17
100 / 100
257 ms 31784 KB
#include <bits/stdc++.h>
#include "fish.h"
using namespace std;

typedef long long int lli;
typedef vector<lli> vi;
typedef pair<int,int> ii;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ROF(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define pb push_back
#define fi first
#define se second
#define all(v) begin(v),end(v)

const int INF = 2e9;

long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
	vi inc(M+N,0), dec(M+N,0);

	vector<pair<ii,lli>> ptw;
	FOR(i,0,M) ptw.pb({{X[i],Y[i]},W[i]});
	FOR(i,0,N) ptw.pb({{i,INF},0});
	sort(all(ptw));

	vector<ii> pts;
	vector<lli> wts{0};
	FOR(i,0,M+N) {
		pts.pb(ptw[i].fi);
		wts.pb(wts.back()+ptw[i].se);
	}

	//counts fish in [{x,y1},{x,y2}], inclusive of endpoints
	//uses two pointers for const op
	function<lli(lli,lli,lli,int&,int&)> countFish = [&] (lli x, lli y1, lli y2, int &l, int &r) {
		if(x<0||x>=N||y1>y2) return 0LL;
		else {
			while(l<int(pts.size())&&pts[l] < ii{x,y1}) l++;
			while(l>0 && pts[l-1] >= ii{x,y1}) l--;
			while(r<int(pts.size())&&pts[r] <= ii{x,y2}) r++;
			while(r>0 && pts[r-1] > ii{x,y2}) r--;
			return wts[r]-wts[l];
		}
	};

	int lPtr[2], rPtr[2];
	FOR(i,0,N) {
		int l = lower_bound(all(pts), ii{i,0}) - begin(pts);
		int r = lower_bound(all(pts), ii{i+1,0}) - begin(pts);
		
		vector<vi> colInc(3,vi(r-l,0));
		vi colDec(r-l,0);
		
		int p = lower_bound(all(pts), ii{i-2,0}) - begin(pts);
		lPtr[0] = rPtr[0] = lPtr[1] = rPtr[1] = lower_bound(all(pts), ii{i-1,0}) - begin(pts);
		
		FOR(j,l,r) {
			if(j>l) colInc[0][j-l]=max(colInc[0][j-l],colInc[0][j-1-l]);
			while(p<int(pts.size())&&pts[p].fi==i-2&&pts[p].se<=pts[j].se) {
				colInc[0][j-l]=max(colInc[0][j-l],dec[p]);
				p++;
			}
		}
		FOR(j,l,r) colInc[0][j-l] += countFish(i-1,0,pts[j].se-1,lPtr[0],rPtr[0]);
		
		p = lower_bound(all(pts), ii{i-2,0}) - begin(pts);
		while(p<int(pts.size())&&pts[p].fi==i-2) {
			colInc[1][0] = max(colInc[1][0],dec[p]+countFish(i-1,0,pts[p].se-1,lPtr[0],rPtr[0])),p++;
		}
		FOR(j,l+1,r) colInc[1][j-l] = max(colInc[1][j-l],colInc[1][j-1-l]);
		
		p = lower_bound(all(pts), ii{i-1,0}) - begin(pts);
		FOR(j,l,r) {
			if(j>l) colInc[2][j-l]=max(colInc[2][j-l],colInc[2][j-1-l]+countFish(i-1,pts[j-1].se,pts[j].se-1,lPtr[0],rPtr[0]));
			while(p<int(pts.size())&&pts[p].fi==i-1&&pts[p].se<=pts[j].se) {
				colInc[2][j-l]=max(colInc[2][j-l],inc[p]+countFish(i-1,pts[p].se,pts[j].se-1,lPtr[1],rPtr[1]));
				p++;
			}
		}
		
		p = lower_bound(all(pts), ii{i-1,INF}) - begin(pts);
		lPtr[0] = rPtr[0] = lower_bound(all(pts), ii{i+1,0}) - begin(pts);
		ROF(j,l,r) {
			if(i>0&&j<r-1) colDec[j-l]=max(colDec[j-l],colDec[j+1-l]+ptw[j].se);
			while(p>=0&&pts[p].fi==i-1&&pts[p].se>=pts[j].se) {
				colDec[j-l]=max(colDec[j-l],dec[p]+countFish(i,pts[j].se,pts[p].se-1,lPtr[0],rPtr[0]));
				p--;
			}
		}
		
		FOR(j,l,r) {
			FOR(k,0,3) {
				inc[j] = max(inc[j],colInc[k][j-l]);
			}
			dec[j] = max(dec[j],colDec[j-l]);
		}
		dec[r-1] = max(inc[r-1],dec[r-1]);
	}

	lli ans = max(*max_element(all(inc)),*max_element(all(dec)));

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 13496 KB Output is correct
2 Correct 117 ms 16264 KB Output is correct
3 Correct 56 ms 6384 KB Output is correct
4 Correct 56 ms 6340 KB Output is correct
5 Correct 212 ms 31784 KB Output is correct
6 Correct 233 ms 31560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 127 ms 18572 KB Output is correct
3 Correct 154 ms 24628 KB Output is correct
4 Correct 94 ms 13576 KB Output is correct
5 Correct 101 ms 16232 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 0 ms 212 KB Output is correct
10 Correct 55 ms 6372 KB Output is correct
11 Correct 56 ms 6340 KB Output is correct
12 Correct 90 ms 13628 KB Output is correct
13 Correct 107 ms 16308 KB Output is correct
14 Correct 89 ms 12424 KB Output is correct
15 Correct 100 ms 13644 KB Output is correct
16 Correct 87 ms 12344 KB Output is correct
17 Correct 101 ms 13648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 6380 KB Output is correct
2 Correct 55 ms 6340 KB Output is correct
3 Correct 83 ms 10560 KB Output is correct
4 Correct 80 ms 9792 KB Output is correct
5 Correct 110 ms 13868 KB Output is correct
6 Correct 106 ms 13876 KB Output is correct
7 Correct 117 ms 13788 KB Output is correct
8 Correct 124 ms 13876 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 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 1 ms 340 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 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 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 1 ms 340 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 26 ms 4036 KB Output is correct
18 Correct 27 ms 3912 KB Output is correct
19 Correct 22 ms 3936 KB Output is correct
20 Correct 21 ms 3912 KB Output is correct
21 Correct 22 ms 3944 KB Output is correct
22 Correct 47 ms 7948 KB Output is correct
23 Correct 5 ms 980 KB Output is correct
24 Correct 19 ms 2780 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 5 ms 980 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 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 1 ms 340 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 26 ms 4036 KB Output is correct
18 Correct 27 ms 3912 KB Output is correct
19 Correct 22 ms 3936 KB Output is correct
20 Correct 21 ms 3912 KB Output is correct
21 Correct 22 ms 3944 KB Output is correct
22 Correct 47 ms 7948 KB Output is correct
23 Correct 5 ms 980 KB Output is correct
24 Correct 19 ms 2780 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 5 ms 980 KB Output is correct
27 Correct 4 ms 728 KB Output is correct
28 Correct 109 ms 17208 KB Output is correct
29 Correct 161 ms 27064 KB Output is correct
30 Correct 174 ms 26956 KB Output is correct
31 Correct 168 ms 26952 KB Output is correct
32 Correct 148 ms 26812 KB Output is correct
33 Correct 169 ms 26940 KB Output is correct
34 Correct 172 ms 26980 KB Output is correct
35 Correct 65 ms 9972 KB Output is correct
36 Correct 165 ms 26940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 6380 KB Output is correct
2 Correct 55 ms 6340 KB Output is correct
3 Correct 83 ms 10560 KB Output is correct
4 Correct 80 ms 9792 KB Output is correct
5 Correct 110 ms 13868 KB Output is correct
6 Correct 106 ms 13876 KB Output is correct
7 Correct 117 ms 13788 KB Output is correct
8 Correct 124 ms 13876 KB Output is correct
9 Correct 116 ms 13876 KB Output is correct
10 Correct 93 ms 11836 KB Output is correct
11 Correct 167 ms 24624 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 55 ms 6380 KB Output is correct
19 Correct 59 ms 6388 KB Output is correct
20 Correct 55 ms 6340 KB Output is correct
21 Correct 56 ms 6340 KB Output is correct
22 Correct 136 ms 13872 KB Output is correct
23 Correct 174 ms 24588 KB Output is correct
24 Correct 173 ms 24492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 13496 KB Output is correct
2 Correct 117 ms 16264 KB Output is correct
3 Correct 56 ms 6384 KB Output is correct
4 Correct 56 ms 6340 KB Output is correct
5 Correct 212 ms 31784 KB Output is correct
6 Correct 233 ms 31560 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 127 ms 18572 KB Output is correct
9 Correct 154 ms 24628 KB Output is correct
10 Correct 94 ms 13576 KB Output is correct
11 Correct 101 ms 16232 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 55 ms 6372 KB Output is correct
17 Correct 56 ms 6340 KB Output is correct
18 Correct 90 ms 13628 KB Output is correct
19 Correct 107 ms 16308 KB Output is correct
20 Correct 89 ms 12424 KB Output is correct
21 Correct 100 ms 13644 KB Output is correct
22 Correct 87 ms 12344 KB Output is correct
23 Correct 101 ms 13648 KB Output is correct
24 Correct 63 ms 6380 KB Output is correct
25 Correct 55 ms 6340 KB Output is correct
26 Correct 83 ms 10560 KB Output is correct
27 Correct 80 ms 9792 KB Output is correct
28 Correct 110 ms 13868 KB Output is correct
29 Correct 106 ms 13876 KB Output is correct
30 Correct 117 ms 13788 KB Output is correct
31 Correct 124 ms 13876 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 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 1 ms 340 KB Output is correct
41 Correct 2 ms 468 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 26 ms 4036 KB Output is correct
49 Correct 27 ms 3912 KB Output is correct
50 Correct 22 ms 3936 KB Output is correct
51 Correct 21 ms 3912 KB Output is correct
52 Correct 22 ms 3944 KB Output is correct
53 Correct 47 ms 7948 KB Output is correct
54 Correct 5 ms 980 KB Output is correct
55 Correct 19 ms 2780 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 5 ms 980 KB Output is correct
58 Correct 4 ms 728 KB Output is correct
59 Correct 109 ms 17208 KB Output is correct
60 Correct 161 ms 27064 KB Output is correct
61 Correct 174 ms 26956 KB Output is correct
62 Correct 168 ms 26952 KB Output is correct
63 Correct 148 ms 26812 KB Output is correct
64 Correct 169 ms 26940 KB Output is correct
65 Correct 172 ms 26980 KB Output is correct
66 Correct 65 ms 9972 KB Output is correct
67 Correct 165 ms 26940 KB Output is correct
68 Correct 116 ms 13876 KB Output is correct
69 Correct 93 ms 11836 KB Output is correct
70 Correct 167 ms 24624 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 55 ms 6380 KB Output is correct
78 Correct 59 ms 6388 KB Output is correct
79 Correct 55 ms 6340 KB Output is correct
80 Correct 56 ms 6340 KB Output is correct
81 Correct 136 ms 13872 KB Output is correct
82 Correct 174 ms 24588 KB Output is correct
83 Correct 173 ms 24492 KB Output is correct
84 Correct 252 ms 31040 KB Output is correct
85 Correct 217 ms 31560 KB Output is correct
86 Correct 242 ms 30884 KB Output is correct
87 Correct 255 ms 31696 KB Output is correct
88 Correct 1 ms 212 KB Output is correct
89 Correct 257 ms 31620 KB Output is correct
90 Correct 242 ms 31624 KB Output is correct
91 Correct 220 ms 31548 KB Output is correct