Submission #145066

#TimeUsernameProblemLanguageResultExecution timeMemory
145066jwvg0425Rectangles (IOI19_rect)C++17
59 / 100
5083 ms47188 KiB
#include "rect.h" #include <stdio.h> #include <vector> #include <queue> #include <algorithm> #include <iostream> #include <string> #include <bitset> #include <map> #include <set> #include <tuple> #include <string.h> #include <math.h> #include <random> #include <functional> #include <assert.h> #include <math.h> #include <stack> #define all(x) (x).begin(), (x).end() #define xx first #define yy second using namespace std; using i64 = long long int; using ii = pair<int, int>; using ii64 = pair<i64, i64>; int up[2505][2505]; int down[2505][2505]; long long count_rectangles(vector<vector<int>> a) { int n = a.size(); int m = a[0].size(); i64 ans = 0; for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { int yu = y - 1; int yd = y + 1; while (yd < m && a[x][yd] < a[x][y]) yd++; while (yu >= 0 && a[x][yu] < a[x][y]) yu--; down[x][y] = yd - y - 1; up[x][y] = y - yu - 1; } } for (int l = 0; l < n - 2; l++) { vector<int> dnow(m, 987654321); vector<int> unow(m, 987654321); vector<int> maxv(m, 0); // 좌우에서 y별 최댓값값 for (int r = l + 2; r < n; r++) { vector<int> newup(m + 1, 0); vector<int> newdown(m + 1, 0); vector<int> delup(m + 1, 0); vector<int> deldown(m + 1, 0); vector<int> lrup(m + 1, 0); vector<int> lrdown(m + 1, 0); vector<bool> lr(m, 0); // l - r은 가능한지 vector<int> depth(m + 1, 0); for (int y = 0; y < m; y++) { dnow[y] = min(dnow[y], down[r - 1][y]); unow[y] = min(unow[y], up[r - 1][y]); maxv[y] = max(maxv[y], a[r - 1][y]); lr[y] = maxv[y] < a[l][y] && maxv[y] < a[r][y]; } int lru = 0, lrd = 0; vector<int> dval(m + 1, 0); vector<int> uval(m + 1, 0); for (int y = 0; y < m; y++) { uval[y] = min(unow[y], lru); if (lr[y]) lru++; else lru = 0; } for (int y = m - 1; y >= 0; y--) { dval[y] = min(dnow[y], lrd); if (lr[y]) lrd++; else lrd = 0; } for (int y = 0; y < m; y++) { if (uval[y] >= 1) { newup[y - uval[y]]++; delup[y - 1]++; } if (dval[y] >= 1) { newdown[y + 1]++; deldown[y + dval[y]]++; } } int d = 0; for (int y = 0; y < m; y++) { d += newup[y]; d -= delup[y]; depth[y] = d; } // (ypos, minvalue) stack<ii> s; for (int y = 1; y < m; y++) { for (int j = 0; j < newdown[y]; j++) s.emplace(y, 987654321); if (!s.empty()) { s.top().yy = min(s.top().yy, depth[y]); } for (int j = 0; j < deldown[y]; j++) { ii top = s.top(); int f = depth[top.xx - 1] + newup[top.xx]; ans += f - top.yy; s.pop(); if (!s.empty()) s.top().yy = min(s.top().yy, top.yy); } } } } 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...