Submission #291124

#TimeUsernameProblemLanguageResultExecution timeMemory
291124IgorIRectangles (IOI19_rect)C++17
37 / 100
5060 ms168488 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 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]; bool check_rect(int x1, int y1, int x2, int y2, vector<vector<int> > &a) { 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]; } return (x2 < D && U < x1 && y2 < R && L < y1); } long long count_rectangles(vector<vector<int> > a) { int 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; } } int ans = 0; for (int x1 = 1; x1 <= n - 2; x1++) { for (int x2 = x1; x2 <= n - 2; x2++) { for (int y1 = 1; y1 <= m - 2; y1++) { for (int y2 = y1; y2 <= m - 2; y2++) { ans += (int)check_rect(x1, y1, x2, y2, a); } } } } 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...