Submission #1264811

#TimeUsernameProblemLanguageResultExecution timeMemory
1264811kustov_vadim_533Sandcastle 2 (JOI22_ho_t5)C++20
10 / 100
5092 ms3712 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double ld;
typedef unsigned long long ull;

#define len(v) (int)((v).size())


map<int, pair<int, int>> mp;

bool check(const pair<int, int> &p1, const pair<int, int> &p2){
	return (abs(p1.first - p2.first) + abs(p1.second - p2.second)) == 1;
}

bool check(int i){
	{
		auto it = mp.find(i);
		if (it != mp.begin() && !(check((--it)->second, mp[i]))){
			return false;
		}
	}
	{
		auto it = mp.find(i);
		++it;
		if (it != mp.end() && !(check(it->second, mp[i]))){
			return false;
		}
	}
	return true;
}

void solve() {
	int h, w;
	cin >> h >> w;
	vector<vector<int>> a(h, vector<int>(w));
	for (int i = 0; i < h; ++i){
		for (int j = 0; j < w; ++j){
			cin >> a[i][j];
		}
	}

	ll ans = 0;
	for (int li = 0; li < h; ++li){
		for (int ri = li; ri < h; ++ri){
			for (int lj = 0; lj < w; ++lj){
				int count = 0;
				mp.clear();

				for (int rj = lj; rj < w; ++rj){
					for (int i = li; i <= ri; ++i){
						mp[a[i][rj]] = {i, rj};

						int l = -1, r = -1;
						{
							auto it = mp.find(a[i][rj]);
							if (it != mp.begin()){
								--it;
								l = it->first;
							}
						}
						{
							auto it = mp.find(a[i][rj]);
							++it;
							if (it != mp.end()){
								r = it->first;
							}
						}

						mp.erase(a[i][rj]);
						if (l != -1){
							count -= check(l);
						}
						if (r != -1){
							count -= check(r);
						}

						mp[a[i][rj]] = {i, rj};
						if (l != -1){
							count += check(l);
						}
						if (r != -1){
							count += check(r);
						}
						count += check(a[i][rj]);
						count--;
					}

					ans += count == 0;
				}
			}
		}
	}
	cout << ans << '\n';
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	cout.precision(60);

	int t = 1;
//	cin >> t;

	while (t--) {
		solve();
	}
}
#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...