Submission #364437

#TimeUsernameProblemLanguageResultExecution timeMemory
364437saarang123Rectangles (IOI19_rect)C++17
37 / 100
5111 ms539136 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2502; template<bool returnMax, bool returnIndex> struct SparseTable { int n, k; vector<int> a; vector<vector<int>> st; void init(vector<int> a) { this->a = a; n = (int) a.size(); k = lg(n); st.assign(n + 1, vector<int>(k + 1)); build(); } int lg(int x) { return 31 - __builtin_clz(x); } int merge(int x, int y) { if (returnMax) return a[x] >= a[y] ? x : y; return a[x] < a[y] ? x : y; } void build() { for (int i = 0; i < n; i++) st[i][0] = i; for (int j = 1; j <= k; j++) for (int i = 0; i + (1 << j) - 1 < n; i++) st[i][j] = merge(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } int query(int l, int r) { l--; r--; int j = lg(r - l + 1); int idx = merge(st[l][j], st[r - (1 << j) + 1][j]); return (returnIndex ? idx : a[idx]); } }; long long count_rectangles(vector<vector<int>> a) { int n = a.size(), m = a[0].size(); vector<SparseTable<1, 0>> rows(n); for(int i = 0; i < n; i++) { rows[i].init(a[i]); } vector<SparseTable<1, 0>> columns(m); for(int j = 0; j < m; j++) { vector<int> t; for(int i = 0; i < n; i++) t.push_back(a[i][j]); columns[j].init(t); } long long ans = 0; for(int r1 = 1; r1 < n - 1; r1++) { for(int c1 = 1; c1 < m - 1; c1++) { for(int r2 = r1; r2 < n - 1; r2++) { for(int c2 = c1; c2 < m - 1; c2++) { //if(r1 == r2 && c1 == c2) continue; bool flag = 1; for(int i = r1; i <= r2; i++) { flag &= (rows[i].query(c1 + 1, c2 + 1) < min(a[i][c1 - 1], a[i][c2 + 1])); if(!flag) break; } for(int i = c1; i <= c2; i++) { if(!flag) break; flag &= (columns[i].query(r1 + 1, r2 + 1) < min(a[r1 - 1][i], a[r2 + 1][i])); } ans += flag; } } } } 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...