Submission #753720

#TimeUsernameProblemLanguageResultExecution timeMemory
753720nicksmsRectangles (IOI19_rect)C++17
100 / 100
2312 ms783520 KiB
#include "rect.h"
/**
 *      Author:  Nicholas Winschel
 *      Created: 05.15.2023 13:01:55
**/

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using db=long double;
template<class T> using V=vector<T>;
using vi = V<int>;
using vl = V<ll>;
using pi = pair<short,short>;
#define f first
#define s second
#define sz(x) (int)((x).size())



template<class T, size_t SZ> struct RMQ { // floor(log_2(x))
	static constexpr int level(int x) { return 31-__builtin_clz(x); }
	array<array<T,SZ>, level(SZ)+1> jmp;
	T cmb(T a, T b) { return min(a, b); }
	void init(const V<T>& v) { assert(sz(v) <= SZ);
		copy(v.begin(),v.end(), begin(jmp[0]));
		for (int j = 1; 1<<j <= sz(v); ++j) {
			for (int i=0; i < sz(v)-(1<<j)+1; i++) jmp[j][i] = cmb(jmp[j-1][i],
				jmp[j-1][i+(1<<(j-1))]);
		}
	}
	T query(int l, int r) {
		assert(l <= r); int d = level(r-l+1);
		return cmb(jmp[d][l],jmp[d][r-(1<<d)+1]); }
};

const size_t MAXN = 2505;
V<short> L[MAXN],R[MAXN],U[MAXN],D[MAXN];
RMQ<short,MAXN> rL[MAXN],rR[MAXN],rU[MAXN],rD[MAXN];
int n,m;

bool chk(int r, int c, int w, int h) {
	if (w == 0 || h == 0 || r+h >= n || c+w >= m) return false;
	// cout << "q " << rR[c-1].query(r,r+h-1) << " " << rL[c+w].query(r,r+h-1)
	// 	<< " " << rU[r+h].query(c,c+w-1) << " " << rD[r-1].query(c,c+w-1) << "\n";
	return rR[c-1].query(r,r+h-1) >= w
		&& rL[c+w].query(r,r+h-1) >= w
		&& rU[r+h].query(c,c+w-1) >= h
		&& rD[r-1].query(c,c+w-1) >= h;
}

long long count_rectangles(std::vector<std::vector<int> > a) {
	// cout << "hi" << endl;
	n = sz(a); m = sz(a[0]);
	for (int i = 0; i < m; i++) {
		L[i]=R[i]=V<short>(n);
	}
	for (int i = 0; i < n; i++) {
		U[i]=D[i]=V<short>(m);
	}
	for (int r = 0; r < n; r++) {
		stack<int> stk;
		for (int c = 0; c < m; c++) {
			while (sz(stk) && a[r][stk.top()] <= a[r][c]) R[stk.top()][r] = c-stk.top()-1, stk.pop();
			stk.push(c);
		}
		while (sz(stk)) R[stk.top()][r]=m-1-stk.top(),stk.pop();
		for (int c = m-1; c >= 0; c--) {
			while (sz(stk) && a[r][stk.top()] <= a[r][c]) L[stk.top()][r] = stk.top()-c-1, stk.pop();
			stk.push(c);
		}
		while (sz(stk)) L[stk.top()][r]=stk.top(), stk.pop();
	}
	for (int c = 0; c < m; c++) {
		stack<int> stk;
		for (int r = 0; r < n; r++) {
			while (sz(stk) && a[stk.top()][c] <= a[r][c]) D[stk.top()][c] = r-stk.top()-1, stk.pop();
			stk.push(r);
		}
		while (sz(stk)) D[stk.top()][c]=n-1-stk.top(),stk.pop();
		for (int r = n-1; r >= 0; r--) {
			while (sz(stk) && a[stk.top()][c] <= a[r][c]) U[stk.top()][c] = stk.top()-r-1, stk.pop();
			stk.push(r);
		}
		while (sz(stk)) U[stk.top()][c]=stk.top(),stk.pop();
	}
	// for (int r = 0; r < n; r++) {
	// 	for (int c = 0; c < m; c++) {
	// 		cout << r << " " << c << ": " << D[r][c] << " " << U[r][c] << "\n";
	// 	}
	// }
	for (int i = 0; i < m; i++) {
		rL[i].init(L[i]);
		rR[i].init(R[i]);
	}
	for (int i = 0; i < n; i++) {
		rU[i].init(U[i]);
		rD[i].init(D[i]);
	}

	V<pair<pi,pi>> pos;
	pos.reserve(MAXN*MAXN*6);
	auto pu = [&](pi &&a, pi &&b) {
		if (!chk(a.f,a.s,b.f,b.s)) return;
		pos.emplace_back(a,b);
	};
	for (int r = 1; r < n-1; r++) {
		for (int c = 1; c < m-1; c++) {
			pu(pi{r,c},pi{R[c-1][r], D[r-1][c]});
			if (L[c+1][r] && c-L[c+1][r]+1 > 0) pu(pi{r,c-L[c+1][r]+1},pi{L[c+1][r], D[r-1][c]});
			if (U[r+1][c] && r-U[r+1][c]+1 > 0) pu(pi{r-U[r+1][c]+1,c},pi{R[c-1][r], U[r+1][c]});
			if (L[c+1][r] && c-L[c+1][r]+1 > 0 && U[r+1][c] && r-U[r+1][c]+1 > 0)
				pu(pi{r-U[r+1][c]+1,c-L[c+1][r]+1},pi{L[c+1][r], U[r+1][c]});
			if (L[c+1][r] && c-L[c+1][r]+1 > 0) pu(pi{r,c-L[c+1][r]+1},pi{L[c+1][r], D[r-1][c-L[c+1][r]+1]});
			if (U[r+1][c] && r-U[r+1][c]+1 > 0) pu(pi{r-U[r+1][c]+1,c},pi{R[c-1][r-U[r+1][c]+1], U[r+1][c]});
			
		}
	}
	sort(pos.begin(),pos.end());
	return unique(pos.begin(),pos.end())-pos.begin();
}

Compilation message (stderr)

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from rect.cpp:7:
rect.cpp: In instantiation of 'void RMQ<T, SZ>::init(V<T>&) [with T = short int; long unsigned int SZ = 2505; V<T> = std::vector<short int, std::allocator<short int> >]':
rect.cpp:93:18:   required from here
rect.cpp:25:42: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
   25 |  void init(const V<T>& v) { assert(sz(v) <= SZ);
      |                                          ^
#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...