Submission #614060

# Submission time Handle Problem Language Result Execution time Memory
614060 2022-07-30T17:42:06 Z valerikk Sandcastle 2 (JOI22_ho_t5) C++17
71 / 100
5000 ms 4460 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 50001;
const int M = 10000001;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};

int h, w;

struct Table {
	int a[N];

	int *operator[](int i) {
		return a + i * w;
	}

	Table() { 
		memset(a, 0, sizeof a); 
	}
} a, b, c[4], d[4];

int rmin[N], rmax[N];
long long rsum[N], rsumup[N], rsumdown[N];
int rcnt[N], rcntup[N], rcntdown[N];

int get_delta(int x, int y, int z) {
	int mn = M;
	for (int i = 0; i < 4; ++i) {
		if ((z >> i) & 1) continue;
		int xx = x + dx[i], yy = y + dy[i];
		if (xx >= 0 && xx < h && yy >= 0 && yy < w && a[xx][yy] > a[x][y] && a[xx][yy] < mn) mn = a[xx][yy];
	}
	return mn == M ? 0 : mn - a[x][y];
}

int main() {
	scanf("%d%d", &h, &w);
	for (int x = 0; x < h; ++x) {
		for (int y = 0; y < w; ++y) {
			scanf("%d", &a[x][y]);
		}
	}

	for (int x = 0; x < h; ++x) {
		for (int y = 0; y < w; ++y) {
			b[x][y] = get_delta(x, y, 0);
		}
	}
	for (int i = 0; i < 4; ++i) {
		for (int x = 0; x < h; ++x) {
			for (int y = 0; y < w; ++y) {
				c[i][x][y] = get_delta(x, y, 1 << i);
			}
		}
	}
	for (int i = 0; i < 4; ++i) {
		for (int x = 0; x < h; ++x) {
			for (int y = 0; y < w; ++y) {
				d[i][x][y] = get_delta(x, y, (1 << i) | (1 << ((i + 1) % 4)));
			}
		}
	}

	long long ans = h * w;

	for (int x = 0; x < h; ++x) {
		int len = 0;
		for (int y = 1; y < w; ++y) {
			++len;
			if (y == w - 1 || (a[x][y] < a[x][y + 1]) != (a[x][y - 1] < a[x][y])) {
				ans += 1ll * len * (len + 1) / 2;
				len = 0;
			}
		}
	}	
	for (int y = 0; y < w; ++y) {
		int len = 0;
		for (int x = 1; x < h; ++x) {
			++len;
			if (x == h - 1 || (a[x][y] < a[x + 1][y]) != (a[x - 1][y] < a[x][y])) {
				ans += 1ll * len * (len + 1) / 2;
				len = 0;
			}
		}
	}

	for (int y1 = 0; y1 < w; ++y1) {
		for (int x = 0; x < h; ++x) {
			rmin[x] = rmax[x] = a[x][y1];
			rsumup[x] = rsumdown[x] = rsum[x] = 0;
			rcntup[x] = rcntdown[x] = rcnt[x] = 0;
		}
		for (int y2 = y1 + 1; y2 < w; ++y2) {
			for (int x = 0; x < h; ++x) {
				rmin[x] = min(rmin[x], a[x][y2]);
				rmax[x] = max(rmax[x], a[x][y2]);
				if (y2 - 1 != y1) {
					rcntup[x] += c[0][x][y2 - 1] == 0;
					rsumup[x] += c[0][x][y2 - 1];
					rcntdown[x] += c[2][x][y2 - 1] == 0;
					rsumdown[x] += c[2][x][y2 - 1];	
					rcnt[x] += b[x][y2 - 1] == 0;
					rsum[x] += b[x][y2 - 1];
				}
			}
			for (int x = 0; x < h; ++x) {
				rcntup[x] += (d[3][x][y1] == 0) + (int)(d[0][x][y2] == 0);
				rsumup[x] += d[3][x][y1] + d[0][x][y2];
				rcntdown[x] += (d[2][x][y1] == 0) + (int)(d[1][x][y2] == 0);
				rsumdown[x] += d[2][x][y1] + d[1][x][y2];
				rcnt[x] += (c[3][x][y1] == 0) + (int)(c[1][x][y2] == 0);
				rsum[x] += c[3][x][y1] + c[1][x][y2];
			}
			for (int x1 = 0; x1 < h; ++x1) {
				long long sum = rsumup[x1];
				int cnt = rcntup[x1];
				int cmin = rmin[x1], cmax = rmax[x1];
				for (int x2 = x1 + 1; x2 < h; ++x2) {
					cmin = min(cmin, rmin[x2]);
					cmax = max(cmax, rmax[x2]);
					// if (x1 == 0 && x2 == 2 && y1 == 2 && y2 == 4) {
					// 	cerr << sum << " " << rsumdown[x2] << " " << cmax << " " << cmin << " " << cnt << " " << rcntdown[x2] << "\n";
					// }
					ans += sum + rsumdown[x2] == cmax - cmin && cnt + rcntdown[x2] == 1;
					cnt += rcnt[x2];
					sum += rsum[x2];
				}
			}
			for (int x = 0; x < h; ++x) {
				rcntup[x] -= (d[3][x][y1] == 0) + (int)(d[0][x][y2] == 0);
				rsumup[x] -= d[3][x][y1] + d[0][x][y2];
				rcntdown[x] -= (d[2][x][y1] == 0) + (int)(d[1][x][y2] == 0);
				rsumdown[x] -= d[2][x][y1] + d[1][x][y2];
				rcnt[x] -= (c[3][x][y1] == 0) + (int)(c[1][x][y2] == 0);
				rsum[x] -= c[3][x][y1] + c[1][x][y2];
			}
		}
	}
	
	printf("%lld\n", ans);

	return 0;	
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  scanf("%d%d", &h, &w);
      |  ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:42:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |    scanf("%d", &a[x][y]);
      |    ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Execution timed out 5012 ms 2388 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 2 ms 2260 KB Output is correct
5 Correct 1 ms 2260 KB Output is correct
6 Correct 2 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 2 ms 2260 KB Output is correct
5 Correct 1 ms 2260 KB Output is correct
6 Correct 2 ms 2260 KB Output is correct
7 Correct 1 ms 2260 KB Output is correct
8 Correct 2 ms 2260 KB Output is correct
9 Correct 3 ms 2260 KB Output is correct
10 Correct 3 ms 2260 KB Output is correct
11 Correct 10 ms 2260 KB Output is correct
12 Correct 2 ms 2260 KB Output is correct
13 Correct 3 ms 2260 KB Output is correct
14 Correct 2 ms 2260 KB Output is correct
15 Correct 3 ms 2260 KB Output is correct
16 Correct 3 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 2 ms 2260 KB Output is correct
5 Correct 1 ms 2260 KB Output is correct
6 Correct 2 ms 2260 KB Output is correct
7 Correct 1 ms 2260 KB Output is correct
8 Correct 2 ms 2260 KB Output is correct
9 Correct 3 ms 2260 KB Output is correct
10 Correct 3 ms 2260 KB Output is correct
11 Correct 10 ms 2260 KB Output is correct
12 Correct 2 ms 2260 KB Output is correct
13 Correct 3 ms 2260 KB Output is correct
14 Correct 2 ms 2260 KB Output is correct
15 Correct 3 ms 2260 KB Output is correct
16 Correct 3 ms 2260 KB Output is correct
17 Correct 3 ms 2516 KB Output is correct
18 Correct 32 ms 2280 KB Output is correct
19 Correct 68 ms 2260 KB Output is correct
20 Correct 37 ms 2260 KB Output is correct
21 Correct 27 ms 2260 KB Output is correct
22 Correct 26 ms 2260 KB Output is correct
23 Correct 25 ms 2260 KB Output is correct
24 Correct 21 ms 2260 KB Output is correct
25 Correct 27 ms 2260 KB Output is correct
26 Correct 28 ms 2260 KB Output is correct
27 Correct 26 ms 2296 KB Output is correct
28 Correct 31 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2260 KB Output is correct
2 Correct 1 ms 2260 KB Output is correct
3 Correct 2 ms 2260 KB Output is correct
4 Correct 2 ms 2260 KB Output is correct
5 Correct 1 ms 2260 KB Output is correct
6 Correct 2 ms 2260 KB Output is correct
7 Correct 1 ms 2260 KB Output is correct
8 Correct 2 ms 2260 KB Output is correct
9 Correct 3 ms 2260 KB Output is correct
10 Correct 3 ms 2260 KB Output is correct
11 Correct 10 ms 2260 KB Output is correct
12 Correct 2 ms 2260 KB Output is correct
13 Correct 3 ms 2260 KB Output is correct
14 Correct 2 ms 2260 KB Output is correct
15 Correct 3 ms 2260 KB Output is correct
16 Correct 3 ms 2260 KB Output is correct
17 Correct 3 ms 2516 KB Output is correct
18 Correct 32 ms 2280 KB Output is correct
19 Correct 68 ms 2260 KB Output is correct
20 Correct 37 ms 2260 KB Output is correct
21 Correct 27 ms 2260 KB Output is correct
22 Correct 26 ms 2260 KB Output is correct
23 Correct 25 ms 2260 KB Output is correct
24 Correct 21 ms 2260 KB Output is correct
25 Correct 27 ms 2260 KB Output is correct
26 Correct 28 ms 2260 KB Output is correct
27 Correct 26 ms 2296 KB Output is correct
28 Correct 31 ms 2260 KB Output is correct
29 Correct 11 ms 4460 KB Output is correct
30 Correct 1049 ms 2708 KB Output is correct
31 Correct 1138 ms 2672 KB Output is correct
32 Execution timed out 5010 ms 2644 KB Time limit exceeded
33 Halted 0 ms 0 KB -