Submission #295193

#TimeUsernameProblemLanguageResultExecution timeMemory
295193SamAndRectangles (IOI19_rect)C++17
50 / 100
5064 ms196576 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; int n, m; int a[N][N]; int p[N][N]; int sum(int x1, int y1_, int x2, int y2) { return p[x2][y2] - p[x1 - 1][y2] - p[x2][y1_ - 1] + p[x1 - 1][y1_ - 1]; } int S(int x1, int y1_, int x2, int y2) { return (x2 - x1 + 1) * (y2 - y1_ + 1); } int u[N][N], u1[N][N]; int l[N][N], l1[N][N]; ll solv6() { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + a[i][j]; if (a[i][j - 1] == 1) { l[i][j] = l[i][j - 1] + 1; l1[i][j] = j - 1; } else { l[i][j] = 0; l1[i][j] = l1[i][j - 1]; } if (a[i - 1][j] == 1) { u[i][j] = u[i - 1][j] + 1; u1[i][j] = i - 1; } else { u[i][j] = 0; u1[i][j] = u1[i - 1][j]; } } } ll ans = 0; for (int x2 = 2; x2 <= n; ++x2) { for (int y2 = 2; y2 <= m; ++y2) { if (!l[x2][y2]) continue; if (!u[x2][y2]) continue; int x1 = u1[x2][y2 - 1]; int y1_ = l1[x2 - 1][y2]; if (!x1) continue; if (!y1_) continue; if (x1 + 1 == x2) continue; if (y1_ + 1 == y2) continue; if (sum(x1 + 1, y1_ + 1, x2 - 1, y2 - 1)) continue; if (sum(x1, y1_ + 1, x1, y2 - 1) != S(x1, y1_ + 1, x1, y2 - 1)) continue; if (sum(x2, y1_ + 1, x2, y2 - 1) != S(x2, y1_ + 1, x2, y2 - 1)) continue; if (sum(x1 + 1, y1_, x2 - 1, y1_) != S(x1 + 1, y1_, x2 - 1, y1_)) continue; if (sum(x1 + 1, y2, x2 - 1, y2) != S(x1 + 1, y2, x2 - 1, y2)) continue; ++ans; } } return ans; } 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(); } ll ans = 0; for (int x1 = 2; x1 < n; ++x1) { for (int x2 = x1; x2 < n; ++x2) { for (int y1_ = 2; y1_ < m; ++y1_) { for (int y2 = y1_; y2 < m; ++y2) { bool z = true; for (int i = x1; i <= x2; ++i) { for (int j = y1_; j <= y2; ++j) { if (a[i][j] >= a[x1 - 1][j]) { z = false; break; } if (a[i][j] >= a[x2 + 1][j]) { z = false; break; } if (a[i][j] >= a[i][y1_ - 1]) { z = false; break; } if (a[i][j] >= a[i][y2 + 1]) { z = false; break; } } if (!z) break; } if (z) ++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...