제출 #390341

#제출 시각아이디문제언어결과실행 시간메모리
390341SuhaibSawalha1Rectangles (IOI19_rect)C++17
50 / 100
5083 ms994704 KiB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
 
vector<int> lg{-1};
 
struct RMQ {
	vector<vector<int>> sp;
	void init (vector<int> &a) {
		int n = a.size();
		sp.resize(lg[n]+1, vector<int>(n));
		sp[0] = a;
		for (int i = 1; i < lg[n]+1; ++i) {
			for (int j = 0; j < n - (1 << i) + 1; ++j) {
				sp[i][j] = max(sp[i - 1][j], sp[i - 1][j + (1 << (i - 1))]);
			}
		}
	}
	int query (int l, int r) {
		assert(r >= l);
		int k = lg[r-l+1];
		return max(sp[k][l], sp[k][r - (1 << k) + 1]);
	}
};

struct RMQ2 {
	vector<vector<int>> sp;
	void init (vector<int> &a) {
		int n = a.size();
		sp.resize(lg[n]+1, vector<int>(n));
		sp[0] = a;
		for (int i = 1; i < lg[n]+1; ++i) {
			for (int j = 0; j < n - (1 << i) + 1; ++j) {
				sp[i][j] = min(sp[i - 1][j], sp[i - 1][j + (1 << (i - 1))]);
			}
		}
	}
	int query (int l, int r) {
		assert(r >= l);
		int k = lg[r-l+1];
		return min(sp[k][l], sp[k][r - (1 << k) + 1]);
	}
};
 
long long count_rectangles(vector<vector<int>> a) {
	int n = a.size(), m = a[0].size();
	int left[n][m], right[n][m];
	for (int i = 0; i < n; ++i) {
		stack<int> st;
		for (int j = 0; j < m; ++j) {
			while (st.size() && a[i][st.top()] < a[i][j]) {
				st.pop();
			}
			left[i][j] = st.size() ? st.top() + 1 : 0;
			st.push(j);
		}
	}
	for (int i = 0; i < n; ++i) {
		stack<int> st;
		for (int j = m - 1; ~j; --j) {
			while (st.size() && a[i][st.top()] < a[i][j]) {
				st.pop();
			}
			right[i][j] = st.size() ? st.top() - 1 : m - 1;
			st.push(j);
		}
	}
	for (int i = 1; i <= n; ++i) {
		lg.push_back(lg.back()+!(i&(i-1)));
	}
	RMQ col[m];
	for (int j = 1; j < m - 1; ++j) {
		vector<int> p(n);
		for (int i = 0; i < n; ++i) {
			p[i] = a[i][j];
		}
		col[j].init(p);
	}
	RMQ2 R[m];
	RMQ L[m];
	for (int j = 0; j < m; ++j) {
		vector<int> p(n);
		for (int i = 0; i < n; ++i) {
			p[i] = left[i][j];
		}
		L[j].init(p);
		for (int i = 0; i < n; ++i) {
			p[i] = right[i][j];
		}
		R[j].init(p);
	}
	long long ans = 0;
	for (int x = 1; x < n - 1; ++x) {
		for (int y = 1; y < m - 1; ++y) {
			for (int i = x; i < n  - 1; ++i) {
				if (a[i][y] >= a[i][y - 1] || a[i][y] >= a[x - 1][y]) {
					break;
				}
				bool bad = 0;
				int limit = min(m - 1, R[y - 1].query(x, i) + 1);
				for (int j = y; j < limit && !bad; ++j) {
					bool good = L[j + 1].query(x, i) <= y;
					int mx = col[j].query(x, i);
					good &= mx < a[i + 1][j] && mx < a[x - 1][j];
					bad |= mx >= a[x - 1][j] || mx >= a[i + 1][j];
					ans += good;
				}
			}
		}
	}
	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...