답안 #374132

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
374132 2021-03-06T17:51:46 Z MetB 웜뱃 (IOI13_wombats) C++14
100 / 100
8283 ms 109124 KB
#include "wombats.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace __gnu_pbds;
 
using namespace std;
 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
 
const ll N = 2000001;
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
const int bl = 40;

int R, C, hor[5000][200], ver[5000][200], dp[bl][200];
int Rbl;

struct cut {
	vector<vector<ll>> d;

	cut() {
		for (int i = 0; i < C; i++)
			d.push_back(vector<ll>(C, INF));
	}

	cut(int pos) {

		for (int i = 0; i < C; i++) {
			d.push_back(vector<ll>(C));
		}

		int offset = bl * pos;

		for (int k = 0; k < C; k++) {
			dp[0][k] = 0;

			for (int i = k + 1; i < C; i++)
				dp[0][i] = dp[0][i-1] + hor[offset][i-1];

			for (int i = k - 1; i >= 0; i--)
				dp[0][i] = dp[0][i+1] + hor[offset][i];

			for (int i = 1; i < min(R - offset, bl); i++) {
				dp[i][0] = dp[i-1][0] + ver[offset+i-1][0];

				for (int j = 1; j < C; j++) {
					dp[i][j] = min(dp[i-1][j] + ver[offset+i-1][j], 
								   dp[i][j-1] + hor[offset+i][j-1]);
				}

				for (int j = C - 2; j >= 0; j--) {
					dp[i][j] = min(dp[i][j], dp[i][j+1] + hor[offset+i][j]);
				}
			}

			for (int i = 0; i < C; i++) {
				d[k][i] = dp[min(R - offset, bl)-1][i];
			}
		}

		/*cout << "Create on " << pos << endl;

		for (int i = 0; i < C; i++) {
			for (int j = 0; j < C; j++) {
				cout << d[i][j] << ' ';
			}
			cout << endl;
		}*/
	}

	vector<ll>& operator[](int x) {
		return d[x];
	}
};

cut merge(cut dl, cut dr, int pos) {
	cut d;

	pos = bl * (pos + 1) - 1;

	vector<int> opt(C, -1), optr(C, -1);

	for (int i = 0; i < C; i++) {
		for (int j = 0; j < C; j++) {
			if (d[i][i] > dl[i][j] + ver[pos][j] + dr[j][i]) {
				optr[i] = opt[i] = j; 
				d[i][i] = dl[i][j] + ver[pos][j] + dr[j][i];
			}
		}
	}

	for (int k = 1; k < C; k++) {

		for (int i = 0; i + k < C; i++) {
			for (int j = opt[i]; j <= opt[i+1]; j++) {
				if (d[i][i+k] > dl[i][j] + ver[pos][j] + dr[j][i+k]) {
					opt[i] = j; 
					d[i][i+k] = dl[i][j] + ver[pos][j] + dr[j][i+k];
				}
			}
		}

		for (int i = C - k - 1; i >= 0; i--) {
			int r = optr[i+k];
			for (int j = optr[i+k-1]; j <= r; j++) {
				if (d[i+k][i] > dl[i+k][j] + ver[pos][j] + dr[j][i]) {
					optr[i+k] = j; 
					d[i+k][i] = dl[i+k][j] + ver[pos][j] + dr[j][i];
				}
			}
		}
	}

	/*for (int i = 0; i < C; i++) {
		for (int j = 0; j < C; j++) {
			for (int k = 0; k < C; k++) {
				d[i][j] = min(d[i][j], dl[i][k] + ver[pos][k] + dr[k][j]);
			}
		}
	}*/

	/*cout << "Merge on " << pos << endl;

	for (int i = 0; i < C; i++) {
		cout << ver[pos][i] << endl;
	}

	for (int i = 0; i < C; i++) {
		for (int j = 0; j < C; j++) {
			cout << d[i][j] << ' ';
		}
		cout << endl;
	}*/

	return d;
}

struct DynamicGrid {
	int start;
	vector<cut> t;

	void build() {
		start = 1;
		while (start < Rbl) start <<= 1;
		t.resize(2 * start);
		build(1, 0, start - 1, Rbl);
	}

	void build(int node, int tl, int tr, int sz) {
		if (sz <= tl || tl == tr) {
			t[node] = cut(tl);
			return;
		}

		int tm = (tl + tr) / 2;

		build(2 * node, tl, tm, sz);
		build(2 * node + 1, tm + 1, tr, sz);
		if (sz <= tm + 1) t[node] = t[2 * node];
		else t[node] = merge(t[2 * node], t[2 * node + 1], tm);
	}

	void update(int x) {
		update(1, 0, start - 1, x);
	}

	void update(int node, int tl, int tr, int x) {
		if (tl == tr) {
			t[node] = cut(x);
			return;
		} 

		int tm = (tl + tr) / 2;

		if (x <= tm) update(2 * node, tl, tm, x);
		else update(2 * node + 1, tm + 1, tr, x);

		if (Rbl <= tm + 1) t[node] = t[2 * node];
		else t[node] = merge(t[2 * node], t[2 * node + 1], tm);
	}

	int get(int a, int b) {
		//cout << "Get " << t[1][a][b] << endl;
		return t[1][a][b];
	}
} t;

void init(int r, int c, int H[5000][200], int V[5000][200]) {
	R = r, C = c;

	Rbl = (R + bl - 1) / bl;

	for (int i = 0; i < R; i++)
		for (int j = 0; j < C; j++) {
			hor[i][j] = H[i][j];
			ver[i][j] = V[i][j];
		}

	t.build();
}

void changeH(int P, int Q, int W) {
	hor[P][Q] = W;

	t.update(P / bl);
}

void changeV(int P, int Q, int W) {
	ver[P][Q] = W;
	t.update(P / bl);
}

int escape(int V1, int V2) {
	return t.get(V1, V2);
}

Compilation message

grader.c: In function 'int main()':
grader.c:15:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
   15 |  int res;
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
3 Correct 88 ms 13676 KB Output is correct
4 Correct 9 ms 12140 KB Output is correct
5 Correct 9 ms 12140 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 79 ms 1516 KB Output is correct
12 Correct 1 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 191 ms 1800 KB Output is correct
2 Correct 257 ms 1644 KB Output is correct
3 Correct 249 ms 1736 KB Output is correct
4 Correct 249 ms 1644 KB Output is correct
5 Correct 268 ms 1644 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1301 ms 1772 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 16108 KB Output is correct
2 Correct 13 ms 16108 KB Output is correct
3 Correct 13 ms 16108 KB Output is correct
4 Correct 58 ms 17024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 181 ms 1644 KB Output is correct
2 Correct 260 ms 1772 KB Output is correct
3 Correct 249 ms 1676 KB Output is correct
4 Correct 253 ms 1744 KB Output is correct
5 Correct 282 ms 1772 KB Output is correct
6 Correct 13 ms 16108 KB Output is correct
7 Correct 13 ms 16108 KB Output is correct
8 Correct 13 ms 16108 KB Output is correct
9 Correct 53 ms 16876 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12268 KB Output is correct
12 Correct 92 ms 13676 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 79 ms 1644 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1293 ms 1644 KB Output is correct
28 Correct 2033 ms 38280 KB Output is correct
29 Correct 2007 ms 35564 KB Output is correct
30 Correct 2029 ms 35356 KB Output is correct
31 Correct 2126 ms 39276 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 1804 KB Output is correct
2 Correct 273 ms 1900 KB Output is correct
3 Correct 247 ms 1736 KB Output is correct
4 Correct 248 ms 1644 KB Output is correct
5 Correct 267 ms 1784 KB Output is correct
6 Correct 13 ms 16108 KB Output is correct
7 Correct 13 ms 16108 KB Output is correct
8 Correct 13 ms 16108 KB Output is correct
9 Correct 53 ms 16876 KB Output is correct
10 Correct 9 ms 12140 KB Output is correct
11 Correct 9 ms 12140 KB Output is correct
12 Correct 86 ms 13676 KB Output is correct
13 Correct 9 ms 12140 KB Output is correct
14 Correct 9 ms 12140 KB Output is correct
15 Correct 1535 ms 99948 KB Output is correct
16 Correct 8283 ms 103316 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 79 ms 2796 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1291 ms 1900 KB Output is correct
30 Correct 2028 ms 42192 KB Output is correct
31 Correct 7846 ms 108728 KB Output is correct
32 Correct 7856 ms 108792 KB Output is correct
33 Correct 2019 ms 38996 KB Output is correct
34 Correct 7910 ms 105472 KB Output is correct
35 Correct 2005 ms 38784 KB Output is correct
36 Correct 7919 ms 105060 KB Output is correct
37 Correct 2114 ms 43624 KB Output is correct
38 Correct 7875 ms 109124 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 8154 ms 105496 KB Output is correct