Submission #614059

#TimeUsernameProblemLanguageResultExecution timeMemory
614059valerikkSandcastle 2 (JOI22_ho_t5)C++17
71 / 100
63 ms1208 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 7001;
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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...