Submission #825913

#TimeUsernameProblemLanguageResultExecution timeMemory
825913pawnedRectangles (IOI19_rect)C++17
50 / 100
1108 ms80120 KiB
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

#include "rect.h"

int dirX[4] = {-1, 1, 0, 0};
int dirY[4] = {0, 0, -1, 1};

int N, M;
int grid[2505][2505];
bool vis[2505][2505];

int tpt, bpt, lpt, rpt;
// tpt = top, bpt = bottom, lpt = left, rpt = right
int compsize = 0;

void bfs(int x, int y) {
	queue<ii> q;
	q.push({x, y});
	vis[x][y] = true;
	tpt = max(tpt, x);
	bpt = min(bpt, x);
	rpt = max(rpt, y);
	lpt = min(lpt, y);
	compsize++;
	while (!q.empty()) {
		ii k = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int newX = k.fi + dirX[i];
			int newY = k.se + dirY[i];
			if (newX < 0 || newX >= N)
				continue;
			if (newY < 0 || newY >= M)
				continue;
			if (grid[newX][newY] == 0 && !vis[newX][newY]) {
				vis[newX][newY] = true;
				tpt = max(tpt, newX);
				bpt = min(bpt, newX);
				rpt = max(rpt, newY);
				lpt = min(lpt, newY);
				compsize++;
				q.push({newX, newY});
			}
		}
	}
}

void reset() {
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			grid[i][j] = 0;
			vis[i][j] = false;
		}
	}
}

bool check(int x0, int x1, int y0, int y1) {
	for (int i = x0; i <= x1; i++) {
		for (int j = y0; j <= y1; j++) {
			int h0 = grid[i][j];
			int h1 = grid[i][y0 - 1];
			int h2 = grid[i][y1 + 1];
			int h3 = grid[x0 - 1][j];
			int h4 = grid[x1 + 1][j];
			if (h0 >= h1 || h0 >= h2 || h0 >= h3 || h0 >= h4)
				return false;
		}
	}
	return true;
}

ll count_rectangles(vector<vi> a) {
	N = a.size();
	M = a[0].size();
	reset();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			grid[i][j] = a[i][j];
		}
	}
// SUBTASKS 1, 2, 3, 5 BEGIN
	if (N * M <= 40000) {
		int total = 0;
		for (int x0 = 1; x0 <= N - 2; x0++) {
			for (int x1 = x0; x1 <= N - 2; x1++) {
				for (int y0 = 1; y0 <= M - 2; y0++) {
					for (int y1 = y0; y1 <= M - 2; y1++) {
						if (check(x0, x1, y0, y1))
							total++;
					}
				}
			}
		}
		return total;
	}
// SUBTASKS 1, 2, 3, 5 END
	ll total = 0;
	for (int i = 0; i <= N - 1; i++) {
		for (int j = 0; j <= M - 1; j++) {
			if (grid[i][j] == 0 && !vis[i][j]) {
				tpt = -1;
				bpt = 10000;
				rpt = -1;
				lpt = 10000;
				compsize = 0;
				bfs(i, j);
				if (bpt == 0 || tpt == N - 1)
					continue;
				if (lpt == 0 || rpt == M - 1)
					continue;
				if (compsize == (tpt - bpt + 1) * (rpt - lpt + 1)) {
//					cout<<"good "<<i<<" "<<j<<endl;
					total++;
				}
			}
		}
	}
	return total;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...