Submission #823545

#TimeUsernameProblemLanguageResultExecution timeMemory
823545vjudge1Rectangles (IOI19_rect)C++17
100 / 100
2023 ms577320 KiB
#include<bits/stdc++.h> #include "rect.h" using namespace std; using ll = long long; const int M = (1 << 13); int t[2 * M]; void update(int pos, int val) { for(t[pos += M] += val; pos > 1; pos >>= 1) t[pos >> 1] = t[pos] + t[(pos ^ 1)]; } int get(int ql, int qr) { int sum = 0; for(ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) { if(ql & 1) sum += t[ql++]; if(qr & 1) sum += t[--qr]; } return sum; } // O(nlogn) vector<pair<int, int>> get_segments(vector<int> a) { int n = (int)a.size(); vector<pair<int, int>> res; vector<int> st; for(int i = 0; i < n; i++) { while(!st.empty() && a[st.back()] < a[i]) { if(i - st.back() > 1) res.push_back({st.back() + 1, i - 1}); st.pop_back(); } if(!st.empty()) { if(i - st.back() > 1) res.push_back({st.back() + 1, i - 1}); if(a[st.back()] == a[i]) st.pop_back(); } st.push_back(i); } return res; } const int N = 2505; struct segment { int l, d; }; vector<segment> h[N][N], g[N][N]; ll count_rectangles(vector<vector<int>> a) { int n = (int)a.size(), m = (int)a[0].size(); if(n < 3 || m < 3) return 0; for(int i = 0; i < n; i++) { vector<pair<int, int>> s = get_segments(a[i]); for(auto [l, r] : s) g[i][r].push_back({l, 0}); } for(int i = 0; i < m; i++) { vector<int> b; for(int j = 0; j < n; j++) b.push_back(a[j][i]); vector<pair<int, int>> s = get_segments(b); for(auto [l, r] : s) h[r][i].push_back({l, 0}); } for(int i = 0; i < n; i++) { int cnt[n] = {}; bool us[n] = {}; for(int j = 0; j < m; j++) { if(!j) { for(auto c : h[i][j]) cnt[c.l]++; } else { for(auto c : h[i][j]) us[c.l] = 1; for(auto c : h[i][j - 1]) if(!us[c.l]) cnt[c.l] = 0; for(auto c : h[i][j]) { cnt[c.l]++; us[c.l] = 0; } } for(auto &c : h[i][j]) { c.d = cnt[c.l]; } } } for(int i = 0; i < m; i++) { int cnt[m] = {}; bool us[m] = {}; for(int j = 0; j < n; j++) { if(!j) { for(auto c : g[j][i]) cnt[c.l]++; } else { for(auto c : g[j][i]) us[c.l] = 1; for(auto c : g[j - 1][i]) if(!us[c.l]) cnt[c.l] = 0; for(auto c : g[j][i]) { cnt[c.l]++; us[c.l] = 0; } } for(auto &c : g[j][i]) c.d = cnt[c.l]; } } ll ans = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { sort(g[i][j].begin(), g[i][j].end(), [](segment x, segment y) {return x.l < y.l;}); sort(h[i][j].begin(), h[i][j].end(), [](segment x, segment y) {return x.d > y.d;}); int h_l = 0; for(segment cur : g[i][j]) { while(h_l < (int)h[i][j].size() && j - h[i][j][h_l].d < cur.l) { update(h[i][j][h_l++].l, +1); } ans += get(i - cur.d + 1, i); } while(h_l > 0) update(h[i][j][--h_l].l, -1); } } 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...