Submission #1068090

#TimeUsernameProblemLanguageResultExecution timeMemory
1068090Gromp15Rectangles (IOI19_rect)C++17
72 / 100
1626 ms242772 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx2") #include "rect.h" #define ll long long #define ar array #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using namespace std; template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define rint(l, r) uniform_int_distribution<int>(l, r)(rng) struct dsu { int N, M; vector<int> p, size, lx, rx, ly, ry; dsu(int n, int m) : N(n), M(m), p(n*m), size(n*m, 1), lx(n*m), rx(n*m), ly(n*m), ry(n*m) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { p[i*m+j] = i*m+j; lx[i*m+j] = i; rx[i*m+j] = i; ly[i*m+j] = j; ry[i*m+j] = j; } } int get(int v) { return v == p[v] ? v : p[v] = get(p[v]); } void merge(int a, int b) { a = get(a), b = get(b); if (a == b) return; if (size[a] < size[b]) swap(a, b); ckmin(lx[a], lx[b]); ckmin(ly[a], ly[b]); ckmax(rx[a], rx[b]); ckmax(ry[a], ry[b]); size[a] += size[b], p[b] = a; } int good(int v) { return v == get(v) && (rx[v] - lx[v] + 1) * (ry[v] - ly[v] + 1) == size[v] && lx[v] >= 1 && ly[v] >= 1 && rx[v] < N-1 && ry[v] < M-1; } }; long long count_rectangles(std::vector<std::vector<int>> a) { int n = sz(a), m = sz(a[0]); if ((n <= 700 && m <= 700) || n <= 3) { int n = sz(a), m = sz(a[0]); vector ok(n, vector(n, vector<bool>(m))); for (int i = 0; i < m; i++) { for (int j = 1; j < n-1; j++) { int mx = 0; for (int k = j; k < n-1; k++) { ckmax(mx, a[k][i]); ok[j][k][i] = mx >= min(a[j-1][i], a[k+1][i]); } } } vector<vector<int>> right(n, vector<int>(m, -1)), left(n, vector<int>(m, m-1)); for (int i = 0; i < n; i++) { for (int j = 1; j < m-1; j++) { int mx = 0; for (int k = j; k < m-1; k++) { ckmax(mx, a[i][k]); if (mx >= a[i][j-1]) break; right[i][j] = k; } } for (int j = m-2; j >= 1; j--) { int mx = 0; for (int k = j; k >= 1; k--) { ckmax(mx, a[i][k]); if (mx >= a[i][j+1]) break; left[i][j] = k; } } } long long ans = 0; for (int i = 1; i < n-1; i++) { vector<int> R(m, m-2), L(m, -1); for (int j = i; j < n-1; j++) { for (int k = 0; k < m; k++) { ckmin(R[k], right[j][k]); ckmax(L[k], left[j][k]); } vector<int> to(m, -1); for (int k = m-1; k >= 0; k--) if (!ok[i][j][k]) { int l = k; while (l && !ok[i][j][l-1]) l--; for (int z = l; z <= k; z++) to[z] = k; k = l; } for (int k = 1; k < m-1; k++) { int r = min(to[k], R[k]); for (int l = k; l <= r; l++) ans += L[l] <= k; } } } return ans; } dsu D(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!a[i][j]) { if (i+1 < n && !a[i+1][j]) D.merge(i*m+j, (i+1)*m+j); if (j+1 < m && !a[i][j+1]) D.merge(i*m+j, i*m+j+1); } int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ans += !a[i][j] && D.good(i*m+j); 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...