Submission #298821

#TimeUsernameProblemLanguageResultExecution timeMemory
298821square1001Rectangles (IOI19_rect)C++14
37 / 100
5086 ms720540 KiB
#include "rect.h"
#include <vector>
#include <algorithm>
using namespace std;
long long count_rectangles(std::vector<std::vector<int> > A) {
	const int inf = 1012345678;
	int H = A.size(), W = A[0].size();
	vector<vector<int> > TA(W, vector<int>(H));
	for(int i = 0; i < H; ++i) {
		for(int j = 0; j < W; ++j) {
			TA[j][i] = A[i][j];
		}
	}
	vector<vector<bool> > flag_row(H, vector<bool>((W - 1) * (W - 2) / 2));
	vector<vector<bool> > flag_col(W, vector<bool>((H - 1) * (H - 2) / 2));
	for(int i = 0; i < H; ++i) {
		for(int j = 1; j < W; ++j) {
			int d = (j - 1) * (j - 2) / 2 - 1;
			int mx = -inf;
			for(int k = j - 1; k >= 1; --k) {
				mx = max(mx, A[i][k]);
				flag_row[i][d + k] = (A[i][k - 1] > mx && A[i][j] > mx);
			}
		}
	}
	for(int i = 0; i < W; ++i) {
		for(int j = 1; j < H; ++j) {
			int d = (j - 1) * (j - 2) / 2 - 1;
			int mx = -inf;
			for(int k = j - 1; k >= 1; --k) {
				mx = max(mx, TA[i][k]);
				flag_col[i][d + k] = (TA[i][k - 1] > mx && TA[i][j] > mx);
			}
		}
	}
	vector<vector<short> > sum_row(H + 1, vector<short>((W - 1) * (W - 2) / 2));
	vector<vector<short> > sum_col(W + 1, vector<short>((H - 1) * (H - 2) / 2));
	for(int i = 0; i < H; ++i) {
		int d = (W - 1) * (W - 2) / 2;
		for(int j = 0; j < d; ++j) {
			sum_row[i + 1][j] = sum_row[i][j] + flag_row[i][j];
		}
	}
	for(int i = 0; i < W; ++i) {
		int d = (H - 1) * (H - 2) / 2;
		for(int j = 0; j < d; ++j) {
			sum_col[i + 1][j] = sum_col[i][j] + flag_col[i][j];
		}
	}
	long long ans = 0;
	for(int xa = 1; xa < H; ++xa) {
		for(int ya = 1; ya < W; ++ya) {
			for(int yb = ya + 1; yb < W; ++yb) {
				int yid = (yb - 1) * (yb - 2) / 2 + ya - 1;
				if(!flag_row[xa][yid]) continue;
				// ----- At most N patterns for each xa ----- //
				for(int xb = xa + 1; xb < H; ++xb) {
					int xid = (xb - 1) * (xb - 2) / 2 + xa - 1;
					int sr = sum_row[xb][yid] - sum_row[xa][yid];
					int sc = sum_col[yb][xid] - sum_col[ya][xid];
					if(sr == xb - xa && sc == yb - ya) {
						++ans;
					}
				}	
			}
		}
	}
	return ans;
}
#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...