Submission #295399

#TimeUsernameProblemLanguageResultExecution timeMemory
295399SamAndRectangles (IOI19_rect)C++17
72 / 100
5050 ms255048 KiB
#include "rect.h" #include <bits/stdc++.h> using namespace std; #define m_p make_pair #define fi first #define se second #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) typedef long long ll; const int N = 2505; const int xx[4] = {1, 0, -1, 0}; const int yy[4] = {0, 1, 0, -1}; int n, m; int a[N][N]; bool c[N][N]; int q; int minx, maxx, miny, maxy; void dfs(int x, int y) { c[x][y] = true; ++q; minx = min(minx, x); maxx = max(maxx, x); miny = min(miny, y); maxy = max(maxy, y); for (int i = 0; i < 4; ++i) { int hx = x + xx[i]; int hy = y + yy[i]; if (hx >= 1 && hx <= n && hy >= 1 && hy <= m && !c[hx][hy] && a[hx][hy] == 0) { dfs(hx, hy); } } } ll solv6() { ll ans = 0; for (int x = 1; x <= n; ++x) { for (int y = 1; y <= m; ++y) { if (a[x][y] == 0) { if (!c[x][y]) { q = 0; minx = maxx = x; miny = maxy = y; dfs(x, y); if (minx == 1 || maxx == n) continue; if (miny == 1 || maxy == m) continue; if ((maxx - minx + 1) * (maxy - miny + 1) == q) { ++ans; } } } } } return ans; } int l[N][N], r[N][N]; int maxu[N]; int ul[N], ur[N]; long long count_rectangles(std::vector<std::vector<int> > A) { n = sz(A); m = sz(A[0]); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { a[i][j] = A[i - 1][j - 1]; } } bool z = true; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (a[i][j] > 1) { z = false; } } } if (z) { return solv6(); } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { l[i][j] = j; while (l[i][j] > 1 && a[i][j] > a[i][l[i][j] - 1]) --l[i][j]; r[i][j] = j; while (r[i][j] < m && a[i][j] > a[i][r[i][j] + 1]) ++r[i][j]; } } ll ans = 0; for (int x1 = 2; x1 < n; ++x1) { for (int x2 = x1; x2 < n; ++x2) { for (int y = 1; y <= m; ++y) { if (x2 == x1) { maxu[y] = a[x2][y]; ul[y] = l[x2][y]; ur[y] = r[x2][y]; } else { maxu[y] = max(maxu[y], a[x2][y]); ul[y] = max(ul[y], l[x2][y]); ur[y] = min(ur[y], r[x2][y]); } } for (int y1_ = 2; y1_ < m; ++y1_) { for (int y2 = y1_; y2 < m; ++y2) { if (maxu[y2] >= a[x1 - 1][y2]) break; if (maxu[y2] >= a[x2 + 1][y2]) break; if (ur[y1_ - 1] >= y2 && ul[y2 + 1] <= y1_) ++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...