Submission #197909

#TimeUsernameProblemLanguageResultExecution timeMemory
197909arnold518Rectangles (IOI19_rect)C++14
100 / 100
4726 ms611216 KiB
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int MAXN = 2500;
 
int N, M, A[MAXN+10][MAXN+10];
int L[MAXN+10][MAXN+10], R[MAXN+10][MAXN+10], U[MAXN+10][MAXN+10], D[MAXN+10][MAXN+10];
 
struct Line
{
	int y, x;
	Line() {}
	Line(int y, int x1, int x2) : y(y), x((x1<<12)|x2) {}
	bool operator < (const Line &p) const
	{
		if(y!=p.y) return y<p.y;
		return x<p.x;
	}
 
	bool operator == (const Line &p) { return y==p.y && x==p.x; }
	bool operator != (const Line &p) { return !(y==p.y && x==p.x); }
};

struct Data
{
	Line first; int second;
	Data() {}
	Data(Line a, int b) : first(a), second(b) {}
	bool operator < (const Data &p) const { return first<p.first; };
};
 
Line H[MAXN*MAXN+10], V[MAXN*MAXN+10];
int HS, VS;
int lowH[MAXN*MAXN+10], lowV[MAXN*MAXN+10];
ll ans[MAXN*MAXN+10], anss;
int posH[MAXN+10][MAXN+10], posV[MAXN+10][MAXN+10];
Data HT[MAXN*MAXN+10], VT[MAXN*MAXN+10];
int HTS, VTS;

ll count_rectangles(vector<vector<int>> _A)
{
	int i, j;
 
	N=_A.size(); M=_A[0].size();
	for(i=1; i<=N; i++) for(j=1; j<=M; j++) A[i][j]=_A[i-1][j-1];
 
	for(i=1; i<=N; i++)
	{
		vector<int> S;
		S.clear();
		for(j=1; j<=M; j++) L[i][j]=0;
		for(j=1; j<=M; j++)
		{
			while(!S.empty() && A[i][S.back()]<=A[i][j]) S.pop_back();
			if(!S.empty()) L[i][j]=S.back();
			S.push_back(j);
		}
 
		S.clear();
		for(j=1; j<=M; j++) R[i][j]=M+1;
		for(j=M; j>=1; j--)
		{
			while(!S.empty() && A[i][S.back()]<=A[i][j]) S.pop_back();
			if(!S.empty()) R[i][j]=S.back();
			S.push_back(j);
		}
	}
	
	for(i=1; i<=M; i++)
	{
		vector<int> S;
		S.clear();
		for(j=1; j<=N; j++) U[j][i]=0;
		for(j=1; j<=N; j++)
		{
			while(!S.empty() && A[S.back()][i]<=A[j][i]) S.pop_back();
			if(!S.empty()) U[j][i]=S.back();
			S.push_back(j);
		}
 
		S.clear();
		for(j=1; j<=N; j++) D[j][i]=N+1;
		for(j=N; j>=1; j--)
		{
			while(!S.empty() && A[S.back()][i]<=A[j][i]) S.pop_back();
			if(!S.empty()) D[j][i]=S.back();
			S.push_back(j);
		}
	}
 
	for(i=1; i<=N; i++)
	{
		for(j=1; j<=M; j++)
		{
			if(L[i][j]==0) continue;
			if(R[i][j]==M+1) continue;
			H[HS++]=Line(i, L[i][j]+1, R[i][j]-1);
		}
		for(j=1; j<=M; j++)
		{
			if(U[i][j]==0) continue;
			if(D[i][j]==N+1) continue;
			V[VS++]=Line(j, U[i][j]+1, D[i][j]-1);
		}
	}
 
	sort(H, H+HS);
	HS=unique(H, H+HS)-H;
 
	sort(V, V+VS);
	VS=unique(V, V+VS)-V;
 
	for(i=HS-1, j=HS-1; i>=0; i--)
	{
		lowH[i]=H[i].y;
		Line t=Line(H[i].y+1, H[i].x>>12, H[i].x&4095);
		for(; j>=0 && t<H[j]; j--);
		if(j>=0 && t==H[j]) lowH[i]=lowH[j];
	}
 
	for(i=VS-1, j=VS-1; i>=0; i--)
	{
		lowV[i]=V[i].y;
		Line t=Line(V[i].y+1, V[i].x>>12, V[i].x&4095);
		for(; j>=0 && t<V[j]; j--);
		if(j>=0 && t==V[j]) lowV[i]=lowV[j];
	}
 	
	for(i=2; i<=N-1; i++) for(j=2; j<=M-1; j++)
	{
		if(L[i][j]==0) continue;
		if(R[i][j]==M+1) continue;
		if(U[i][j]==0) continue;
		if(D[i][j]==N+1) continue;
 	
		Line tu={U[i][j]+1, L[i][j]+1, R[i][j]-1};
		Line tl={L[i][j]+1, U[i][j]+1, D[i][j]-1};
 		
 		HT[HTS++]=Data(tu, (i<<12)|j);
 		VT[VTS++]=Data(tl, (i<<12)|j);
 	}

 	sort(HT, HT+HTS);
 	for(i=0, j=0; i<HTS; i++)
 	{
 		for(; j<HS && H[j]<HT[i].first; j++);
 		if(j<HS && HT[i].first==H[j]) posH[HT[i].second>>12][HT[i].second&4095]=j;
 		else posH[HT[i].second>>12][HT[i].second&4095]=-1;
 	}

 	sort(VT, VT+VTS);
 	for(i=0, j=0; i<VTS; i++)
 	{
 		for(; j<VS && V[j]<VT[i].first; j++);
 		if(j<VS && VT[i].first==V[j]) posV[VT[i].second>>12][VT[i].second&4095]=j;
 		else posV[VT[i].second>>12][VT[i].second&4095]=-1;
 	}

 	for(i=2; i<=N-1; i++) for(j=2; j<=M-1; j++)
	{
		if(L[i][j]==0) continue;
		if(R[i][j]==M+1) continue;
		if(U[i][j]==0) continue;
		if(D[i][j]==N+1) continue;

		int it=posH[i][j];
		int jt=posV[i][j];
 		
 		if(it==-1 || jt==-1) continue;
 
		if(lowH[it]<D[i][j]-1) continue;
		if(lowV[jt]<R[i][j]-1) continue;
 
		ans[anss++]=(((L[i][j]<<12)|R[i][j])*1000000000ll+((U[i][j]<<12)|D[i][j]));
	}
	sort(ans, ans+anss);
	return unique(ans, ans+anss)-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...