Submission #1084465

#TimeUsernameProblemLanguageResultExecution timeMemory
1084465steveonalexRectangles (IOI19_rect)C++17
72 / 100
1334 ms1048576 KiB
#include "rect.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1LL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() #define block_of_code if(true) ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll gcd(ll a, ll b){return __gcd(a, b);} ll lcm(ll a, ll b){return a / gcd(a, b) * b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ull mask){return __builtin_ctzll(mask);} int logOf(ull mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} double rngesus_d(double l, double r){ double cur = rngesus(0, MASK(60) - 1); cur /= MASK(60) - 1; return l + cur * (r - l); } template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } namespace Sub7{ vector<vector<pair<int, int>>> generate_pair(vector<vector<int>> a){ int n = a.size(), m = a[0].size(); vector<vector<pair<int, int>>> what(n); for(int i = 1; i+1<n; ++i){ vector<int> st; st.reserve(m); for(int j = m-1; j>=0; --j){ int best = -1; while(st.size() && a[i][st.back()] <= a[i][j]){ int r = st.back(); maximize(best, a[i][r]); if (r != j + 1) what[i].push_back({j, r}); st.pop_back(); } if (st.size() && best < a[i][j] && st.back() != j + 1){ what[i].push_back({j, st.back()}); } st.push_back(j); } // sort(ALL(what[i])); } return what; } ll solve(vector<vector<int>> a){ int n, m; n = a.size(), m = a[0].size(); ll ans = 0; vector<vector<int>> b(m, vector<int> (n)); for(int i = 0; i<n; ++i) for(int j = 0; j < m; ++j) b[j][i] = a[i][j]; vector<vector<pair<int, int>>> pair_row = generate_pair(a), pair_col = generate_pair(b); vector<int> candidate_row[n][m], candidate_col[n][m]; vector<int> idx_col[n][n], idx_row[m][m]; for(int i = 1; i+1 < n; ++i){ for(pair<int, int> j: pair_row[i]){ candidate_row[i][j.first + 1].push_back(j.second - 1); idx_row[j.first][j.second].push_back(i); } } for(int j = 1; j+1 < m; ++j){ for(pair<int, int> i: pair_col[j]){ candidate_col[i.first + 1][j].push_back(i.second - 1); idx_col[i.first][i.second].push_back(j); } } for(int i = 1; i+1<n; ++i) for(int j = 1; j+1<m; ++j) if (candidate_row[i][j].size() && candidate_col[i][j].size()){ for(int y: candidate_row[i][j]) for(int x: candidate_col[i][j]){ // check the rectangle (i, j, x, y) // printArr(idx_row[j-1][y+1]); int cnt1 = upper_bound(ALL(idx_row[j-1][y+1]), x) - lower_bound(ALL(idx_row[j-1][y+1]), i); if (cnt1 != x - i + 1) continue; int cnt2 = upper_bound(ALL(idx_col[i-1][x+1]), y) - lower_bound(ALL(idx_col[i-1][x+1]), j); if (cnt2 != y - j + 1) continue; ans++; } } return ans; } } ll count_rectangles(vector<vector<int>> a) { return Sub7::solve(a); }
#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...