Submission #242756

#TimeUsernameProblemLanguageResultExecution timeMemory
242756LawlietRectangles (IOI19_rect)C++17
0 / 100
7 ms1408 KiB
#include <bits/stdc++.h>
#include "rect.h"
 
using namespace std;
typedef pair<int,int> pii;
 
const int LOG = 12;
const int MAXN = 2510;
const int INF = 1000000010;
const int MAXS = 2500*2500 + 10;

struct query
{
	int L, R;
	int ind, k;

	query(int l, int r, int i, int k)
		: L(l), R(r), ind(i), k(k) {}
};

class RMQ
{
	public:

		int cmp(int a, int b, int t)
		{
			if( t == 0 ) return max( a , b );
			return min( a,  b );
		}

		void build(vector<int>& v, int t)
		{
			int n = (int)v.size();

			for(int i = 1 ; i <= n ; i++)
				tab[0][i] = v[i - 1];

			for(int k = 0 ; k < LOG - 1 ; k++)
				for(int L = 1 ; L + (1 << (k + 1)) <= n + 1 ; L++)
					tab[k + 1][L] = cmp( tab[k][L] , tab[k][L + (1 << k)] , t );
		}

		int query(int L, int R, int t)
		{
			int sz = R - L + 1;
			int k = 31 - __builtin_clz(sz);

			return cmp( tab[k][L] , tab[k][R + 1 - (1 << k)] , t );
		}

	private:

		int tab[LOG][MAXN];
};
 
int n, m;
 
int v[MAXN][MAXN], qtd[MAXS];
int L[MAXN][MAXN], U[MAXN][MAXN];
int R[MAXN][MAXN][2], D[MAXN][MAXN][2];

vector<query> lineMin[MAXN], columnMin[MAXN];
vector<query> lineMax[MAXN], columnMax[MAXN];

RMQ curRMQ;
 
void makeHistogram()
{
	for(int i = 1 ; i <= n ; i++)
	{
		for(int j = 1 ; j <= m ; j++)
		{
			int& ansL = L[i][j]; ansL = j - 1;
			int& ansU = U[i][j]; ansU = i - 1;

			while( v[i][ansL] < v[i][j] ) ansL = L[i][ansL];
			while( v[ansU][j] < v[i][j] ) ansU = U[ansU][j];
		}
	}

	for(int j = m ; j > 0 ; j--)
	{
		for(int i = n ; i > 0 ; i--)
		{
			int ansR[2] = { j + 1 , j + 1 };
			int ansD[2] = { i + 1 , i + 1 };
 
 			while( v[i][ ansR[1] ] < v[i][j] ) ansR[1] = R[i][ ansR[1] ][1];
			while( v[i][ ansR[0] ] <= v[i][j] ) ansR[0] = R[i][ ansR[0] ][0];

			while( v[ ansD[1] ][j] < v[i][j] ) ansD[1] = D[ ansD[1] ][j][1];
			while( v[ ansD[0] ][j] <= v[i][j] ) ansD[0] = D[ ansD[0] ][j][0];

			R[i][j][0] = ansR[0]; R[i][j][1] = ansR[1];
			D[i][j][0] = ansD[0]; D[i][j][1] = ansD[1]; 
		}
	}
}
 
long long count_rectangles(vector< vector<int> > a)
{
	n = a.size();
	m = a[0].size();

	for(int i = 0 ; i <= n + 1 ; i++)
		for(int j = 0 ; j <= m + 1 ; j++)
			v[i][j] = INF;
 
	for(int i = 0 ; i < n ; i++)
		for(int j = 0 ; j < m ; j++)
			v[i + 1][j + 1] = a[i][j];
 
	makeHistogram();
 
	vector< pair<pii,pii> > rect;
 
	for(int i = 2 ; i < n ; i++)
	{
		for(int j = 2 ; j < m ; j++)
		{
			if( L[i][j] == 0 || U[i][j] == 0 ) continue;
			if( R[i][j][0] == m + 1 || D[i][j][0] == n + 1 ) continue;
 
			pii dY = { L[i][j] + 1 , R[i][j][0] - 1 };
			pii dX = { U[i][j] + 1 , D[i][j][0] - 1 };
 
			rect.push_back( { dX , dY } );
		}
	}

	sort( rect.begin() , rect.end() );

	auto it = unique( rect.begin() , rect.end() );
	rect.resize( it - rect.begin() );
  
	for(int i = 0 ; i < (int)rect.size() ; i++)
	{
		int x1 = rect[i].first.first;
		int x2 = rect[i].first.second;
		int y1 = rect[i].second.first;
		int y2 = rect[i].second.second;

		lineMin[y1 - 1].push_back( query( x1 , x2 , i , y2 ) );
		lineMax[y2 + 1].push_back( query( x1 , x2 , i , y1 ) );

		columnMin[x1 - 1].push_back( query( y1 , y2 , i , x2 ) );
		columnMax[x2 + 1].push_back( query( y1 , y2 , i , y1 ) );
	}

	for(int i = 1 ; i <= n ; i++)
	{
		vector<int> curMx, curMn;

		for(int j = 1 ; j <= m ; j++)
		{
			curMn.push_back( U[i][j] );
			curMx.push_back( D[i][j][1] );
		}

		curRMQ.build( curMx , 0 );

		while( !lineMax[i].empty() )
		{
			int k = lineMax[i].back().k;
			int curL = lineMax[i].back().L;
			int curR = lineMax[i].back().R;
			int ind = lineMax[i].back().ind;
			lineMax[i].pop_back();

			if( curRMQ.query( curL , curR , 0 ) <= k )
				qtd[ind]++;
		}

		curRMQ.build( curMn , 1 );

		while( !lineMin[i].empty() )
		{
			int k = lineMin[i].back().k;
			int curL = lineMin[i].back().L;
			int curR = lineMin[i].back().R;
			int ind = lineMin[i].back().ind;
			lineMin[i].pop_back();

			if( curRMQ.query( curL , curR , 1 ) >= k )
				qtd[ind]++;
		}
	}

	for(int j = 1 ; j <= m ; j++)
	{
		vector<int> curMx, curMn;

		for(int i = 1 ; i <= n ; i++)
		{
			curMn.push_back( L[i][j] );
			curMx.push_back( R[i][j][1] );
		}

		curRMQ.build( curMx , 0 );

		while( !columnMax[j].empty() )
		{
			int k = columnMax[j].back().k;
			int curL = columnMax[j].back().L;
			int curR = columnMax[j].back().R;
			int ind = columnMax[j].back().ind;
			columnMax[j].pop_back();

			if( curRMQ.query( curL , curR , 0 ) <= k )
				qtd[ind]++;
		}

		curRMQ.build( curMn , 1 );

		while( !columnMin[j].empty() )
		{
			int k = columnMin[j].back().k;
			int curL = columnMin[j].back().L;
			int curR = columnMin[j].back().R;
			int ind = columnMin[j].back().ind;
			columnMin[j].pop_back();

			if( curRMQ.query( curL , curR , 1 ) >= k )
				qtd[ind]++;
		}
	}

	int ans = 0;

	for(int i = 0 ; i < (int)rect.size() ; i++)
		if( qtd[i] == 0 ) ans++;
 
	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...