Submission #291168

#TimeUsernameProblemLanguageResultExecution timeMemory
291168IgorIRectangles (IOI19_rect)C++17
13 / 100
2680 ms343512 KiB
#include <bits/stdc++.h> using namespace std; const int INF = 88888888; const int N = 2502; typedef vector<vector<int> > vvi; typedef vector<int> vi; #define all(x) (x).begin(), (x).end() int n, m; int le[N][N], ri[N][N], up[N][N], down[N][N]; int go_le[2][N][N], go_ri[2][N][N], go_up[2][N][N], go_down[2][N][N]; int check_rect(int x1, int y1, int x2, int y2, vector<vector<int> > &a) { if (x1 <= 0 || x2 >= n - 1 || y1 <= 0 || y2 >= m - 1) return 0; if (x1 > x2 || y1 > y2) return 0; if (!(down[x1 - 1][y1] == x2 + 1 || up[x2 + 1][y1] == x1 - 1)) return 0; if (!(down[x1 - 1][y2] == x2 + 1 || up[x2 + 1][y2] == x1 - 1)) return 0; if (!(ri[x1][y1 - 1] == y2 + 1 || le[x1][y2 + 1] == y1 - 1)) return 0; if (!(ri[x2][y1 - 1] == y2 + 1 || le[x2][y2 + 1] == y1 - 1)) return 0; int D = 0; if (a[x1][y1 - 1] <= a[x1][y2 + 1]) { D = go_down[1][x1][y1 - 1]; } else { D = go_down[0][x1][y2 + 1]; } int U = 0; if (a[x2][y1 - 1] <= a[x2][y2 + 1]) { U = go_up[1][x2][y1 - 1]; } else { U = go_up[0][x2][y2 + 1]; } int R = 0; if (a[x1 - 1][y1] <= a[x2 + 1][y1]) { R = go_ri[1][x1 - 1][y1]; } else { R = go_ri[0][x2 + 1][y1]; } int L = 0; if (a[x1 - 1][y2] <= a[x2 + 1][y2]) { L = go_le[1][x1 - 1][y2]; } else { L = go_le[0][x2 + 1][y2]; } int t = (x2 < D && U < x1 && y2 < R && L < y1); if (!t) return 0; if (a[x1 - 1][y1] == a[x2 + 1][y1] || a[x1 - 1][y2] == a[x2 + 1][y2]) return 1; return 2; } long long count_rectangles(vector<vector<int> > a) { n = a.size(), m = a[0].size(); for (int i = 0; i < n; i++) { vector<pair<int, int> > st; st.push_back({-1, INF}); for (int j = 0; j < m; j++) { while (st.back().second < a[i][j]) st.pop_back(); le[i][j] = st.back().first; st.push_back({j, a[i][j]}); } } for (int i = 0; i < n; i++) { vector<pair<int, int> > st; st.push_back({m, INF}); for (int j = m - 1; j >= 0; j--) { while (st.back().second < a[i][j]) st.pop_back(); ri[i][j] = st.back().first; st.push_back({j, a[i][j]}); } } for (int j = 0; j < m; j++) { vector<pair<int, int> > st; st.push_back({-1, INF}); for (int i = 0; i < n; i++) { while (st.back().second < a[i][j]) st.pop_back(); up[i][j] = st.back().first; st.push_back({i, a[i][j]}); } } for (int j = 0; j < m; j++) { vector<pair<int, int> > st; st.push_back({n, INF}); for (int i = n - 1; i >= 0; i--) { while (st.back().second < a[i][j]) st.pop_back(); down[i][j] = st.back().first; st.push_back({i, a[i][j]}); } } for (int i = 1; i < n; i++) { vector<pair<int, int> > st; st.push_back({-1, INF}); vector<int> pack; pack.push_back(n); for (int j = 0; j < m; j++) { int unpack = n; while (st.back().second < a[i][j]) { unpack = min(unpack, down[i - 1][st.back().first]); st.pop_back(); unpack = min(unpack, pack.back()); pack.pop_back(); } st.push_back({j, a[i][j]}); pack.push_back(unpack); go_down[0][i][j] = unpack; } } for (int i = 1; i < n; i++) { vector<pair<int, int> > st; st.push_back({m, INF}); vector<int> pack; pack.push_back(n); for (int j = m - 1; j >= 0; j--) { int unpack = n; while (st.back().second < a[i][j]) { unpack = min(unpack, down[i - 1][st.back().first]); st.pop_back(); unpack = min(unpack, pack.back()); pack.pop_back(); } st.push_back({j, a[i][j]}); pack.push_back(unpack); go_down[1][i][j] = unpack; } } for (int i = 0; i + 1 < n; i++) { vector<pair<int, int> > st; st.push_back({-1, INF}); vector<int> pack; pack.push_back(-1); for (int j = 0; j < m; j++) { int unpack = -1; while (st.back().second < a[i][j]) { unpack = max(unpack, up[i + 1][st.back().first]); st.pop_back(); unpack = max(unpack, pack.back()); pack.pop_back(); } st.push_back({j, a[i][j]}); pack.push_back(unpack); go_up[0][i][j] = unpack; } } for (int i = 0; i + 1 < n; i++) { vector<pair<int, int> > st; st.push_back({m, INF}); vector<int> pack; pack.push_back(-1); for (int j = m - 1; j >= 0; j--) { int unpack = -1; while (st.back().second < a[i][j]) { unpack = max(unpack, up[i + 1][st.back().first]); st.pop_back(); unpack = max(unpack, pack.back()); pack.pop_back(); } st.push_back({j, a[i][j]}); pack.push_back(unpack); go_up[1][i][j] = unpack; } } for (int j = 1; j < m; j++) { vector<pair<int, int> > st; st.push_back({-1, INF}); vector<int> pack; pack.push_back(m); for (int i = 0; i < n; i++) { int unpack = m; while (st.back().second < a[i][j]) { unpack = min(unpack, ri[st.back().first][j - 1]); st.pop_back(); unpack = min(unpack, pack.back()); pack.pop_back(); } st.push_back({i, a[i][j]}); pack.push_back(unpack); go_ri[0][i][j] = unpack; } } for (int j = 1; j < m; j++) { vector<pair<int, int> > st; st.push_back({n, INF}); vector<int> pack; pack.push_back(m); for (int i = n - 1; i >= 0; i--) { int unpack = m; while (st.back().second < a[i][j]) { unpack = min(unpack, ri[st.back().first][j - 1]); st.pop_back(); unpack = min(unpack, pack.back()); pack.pop_back(); } st.push_back({i, a[i][j]}); pack.push_back(unpack); go_ri[1][i][j] = unpack; } } for (int j = 0; j + 1 < m; j++) { vector<pair<int, int> > st; st.push_back({-1, INF}); vector<int> pack; pack.push_back(-1); for (int i = 0; i < n; i++) { int unpack = -1; while (st.back().second < a[i][j]) { unpack = max(unpack, le[st.back().first][j + 1]); st.pop_back(); unpack = max(unpack, pack.back()); pack.pop_back(); } st.push_back({i, a[i][j]}); pack.push_back(unpack); go_le[0][i][j] = unpack; } } for (int j = 0; j + 1 < m; j++) { vector<pair<int, int> > st; st.push_back({n, INF}); vector<int> pack; pack.push_back(-1); for (int i = n - 1; i >= 0; i--) { int unpack = -1; while (st.back().second < a[i][j]) { unpack = max(unpack, le[st.back().first][j + 1]); st.pop_back(); unpack = max(unpack, pack.back()); pack.pop_back(); } st.push_back({i, a[i][j]}); pack.push_back(unpack); go_le[1][i][j] = unpack; } } vector<vector<int> > rect; int ans = 0; for (int i = 1; i + 1 < n; i++) { for (int j = 1; j + 1 < m; j++) { { int r = ri[i][j - 1] - 1; int u0 = up[i + 1][j] + 1; int u1 = up[i + 1][r] + 1; int d0 = down[i - 1][j] - 1; int d1 = down[i - 1][r] - 1; ans += check_rect(u0, j, i, r, a); if (u0 != u1) ans += check_rect(u1, j, i, r, a); ans += check_rect(i, j, d0, r, a); if (d0 != d1) ans += check_rect(i, j, d1, r, a); } { int l = le[i][j + 1] + 1; int u0 = up[i + 1][l] + 1; int u1 = up[i + 1][j] + 1; int d0 = down[i - 1][l] - 1; int d1 = down[i - 1][j] - 1; if (a[i][j + 1] != a[i][l - 1]) ans += check_rect(u0, l, i, j, a); if (a[i][j + 1] != a[i][l - 1] && u0 != u1) ans += check_rect(u1, l, i, j, a); if (a[i][j + 1] != a[i][l - 1]) ans += check_rect(i, l, d0, j, a); if (a[i][j + 1] != a[i][l - 1] && d0 != d1) ans += check_rect(i, l, d1, j, a); } } } return ans / 2; }
#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...