Submission #205724

#TimeUsernameProblemLanguageResultExecution timeMemory
205724ADJARectangles (IOI19_rect)C++14
72 / 100
5070 ms303692 KiB
#include "rect.h" #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> #include <set> #include <map> #include <algorithm> #include <bitset> #include <cmath> #include <queue> #include <stack> #include <string> #define sz(x) (int) x.size() #define all(x) x.begin(), x.end() using namespace std; const int MAXN = 2550; class Fenwick { private: vector <long long> f; public: void init(int n) { f.assign(n, 0); } void update(int pos, int delta) { for (; pos < sz(f); pos = (pos | (pos + 1))) { f[pos] += delta; } } long long sum(int pos) { long long res = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) { res += f[pos]; } return res; } long long sum(int l, int r) { return sum(r) - sum(l - 1); } }; struct Segment { int r, from, to, len; }; bool cmpFromToR(Segment &a, Segment &b) { // pair<pair<int, int>, int> ap = {{a.from, a.to}, a.r}; // pair<pair<int, int>, int> bp = {{b.from, b.to}, b.r}; // return ap < bp; if (a.from != b.from) { return a.from < b.from; } if (a.to != b.to) { return a.to < b.to; } return a.r < b.r; } bool cmpRToLen(Segment &a, Segment &b) { // pair<pair<int, int>, int> ap = {{a.r, a.to}, a.len}; // pair<pair<int, int>, int> bp = {{b.r, b.to}, b.len}; // return ap < bp; if (a.r != b.r) { return a.r < b.r; } if (a.to != b.to) { return a.to < b.to; } return a.len < b.len; } bool cmpToRFrom(Segment &a, Segment &b) { // pair<pair<int, int>, int> ap = {{a.to, a.r}, -a.from}; // pair<pair<int, int>, int> bp = {{b.to, b.r}, -b.from}; // return ap < bp; if (a.to != b.to) { return a.to < b.to; } if (a.r != b.r) { return a.r < b.r; } return -a.from < -b.from; } int n, m; int a[MAXN][MAXN]; vector<Segment> rows, cols; Fenwick f; void initRowsAndCols() { for (int i = 1; i + 1 < n; i++) { stack <int> st; for (int j = 0; j < m; j++) { while (!st.empty() && a[i][st.top()] < a[i][j]) { st.pop(); } if (!st.empty() && st.top() + 1 < j) { rows.push_back({i, st.top(), j, 1}); } st.push(j); } while (!st.empty()) { st.pop(); } for (int j = m - 1; j >= 0; j--) { while (!st.empty() && a[i][st.top()] < a[i][j]) { st.pop(); } if (!st.empty() && a[i][st.top()] != a[i][j] && j + 1 < st.top()) { rows.push_back({i, j, st.top(), 1}); } st.push(j); } } for (int j = 1; j + 1 < m; j++) { stack <int> st; for (int i = 0; i < n; i++) { while (!st.empty() && a[st.top()][j] < a[i][j]) { st.pop(); } if (!st.empty() && st.top() + 1 < i) { cols.push_back({j, st.top(), i, 1}); } st.push(i); } while (!st.empty()) { st.pop(); } for (int i = n - 1; i >= 0; i--) { while (!st.empty() && a[st.top()][j] < a[i][j]) { st.pop(); } if (!st.empty() && a[st.top()][j] != a[i][j] && i + 1 < st.top()) { cols.push_back({j, i, st.top(), 1}); } st.push(i); } } } void calcLen() { sort(all(rows), cmpFromToR); for (int i = 0; i < sz(rows); i++) { if (i > 0 && rows[i - 1].from == rows[i].from && rows[i - 1].to == rows[i].to && rows[i - 1].r + 1 == rows[i].r) { rows[i].len = rows[i - 1].len + 1; } } sort(all(cols), cmpFromToR); for (int i = 0; i < sz(cols); i++) { if (i > 0 && cols[i - 1].from == cols[i].from && cols[i - 1].to == cols[i].to && cols[i - 1].r + 1 == cols[i].r) { cols[i].len = cols[i - 1].len + 1; } } } void printSegment(Segment &a) { cerr << a.r << " " << a.from << " " << a.to << " " << a.len << endl; } long long count_rectangles(std::vector<std::vector<int> > A) { n = sz(A); m = sz(A[0]); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { a[i][j] = A[i][j]; } } initRowsAndCols(); calcLen(); f.init(m); sort(all(rows), cmpRToLen); sort(all(cols), cmpToRFrom); /* for (int i = 0; i < sz(rows); i++) { printSegment(rows[i]); } cerr << "Cols:" << endl; for (int i = 0; i < sz(cols); i++) { printSegment(cols[i]); } cerr << endl; */ long long ans = 0; vector<int> curRows; vector<int> curCols; int pRows = 0, pCols = 0; for (int i = 1; i + 1 < n; i++) { for (int j = 1; j + 1 < m; j++) { while (pRows < sz(rows) && rows[pRows].r == i && rows[pRows].to == j + 1) { curRows.push_back(pRows); pRows++; } while (pCols < sz(cols) && cols[pCols].to == i + 1 && cols[pCols].r == j) { curCols.push_back(pCols); pCols++; } // cerr << i << " " << j << " " << sz(curRows) << " " << sz(curCols) << endl; int curColsP = 0; for (auto &row : curRows) { while (curColsP < sz(curCols) && cols[curCols[curColsP]].from >= i - rows[row].len) { f.update(j - cols[curCols[curColsP]].len + 1, 1); curColsP++; } ans += f.sum(0, rows[row].from + 1); } for (int k = 0; k < curColsP; k++) { f.update(j - cols[curCols[k]].len + 1, -1); } curRows.clear(); curCols.clear(); } } 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...