Cod sursa(job #2658744)

Utilizator CyborgSquirrelJardan Andrei CyborgSquirrel Data 14 octombrie 2020 22:00:12
Problema Struti Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.19 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <deque>

using namespace std;

ifstream fin("struti.in");
ofstream fout("struti.out");

int p;
int w, h;
int ma[1041][1041];

int lo[1041][1041];
int hi[1041][1041];

void read(){
	fin >> h >> w;
	fin >> p;
	for(int y = 1; y <= h; ++y){
		for(int x = 1; x <= w; ++x){
			fin >> ma[x][y];
		}
	}
}

struct jojo{
	int v, p;
};

struct dequeparttwo{
	jojo v[5041];
	int lt=0, rt=0;
	void clear(){lt=rt=0;}
	void pop_front(){lt++;}
	void pop_back(){rt--;}
	bool empty(){return lt>=rt;}
	jojo &front(){return v[lt];}
	jojo &back(){return v[rt-1];}
	void push_back(jojo j){v[rt]=j;rt++;}
};
dequeparttwo lod, hid;

int thedif, thecone;

void yikes(int dw, int dh, bool nuke){
	
	for(int y = 1; y <= h; ++y){
		for(int x = 1; x <= w; ++x){
			int a = ma[x][y];
			
			while(!hid.empty() && x-hid.front().p >= dw)hid.pop_front();
			while(!hid.empty() && a>hid.back().v)hid.pop_back();
			hid.push_back({a, x});
			
			while(!lod.empty() && x-lod.front().p >= dw)lod.pop_front();
			while(!lod.empty() && a<lod.back().v)lod.pop_back();
			lod.push_back({a, x});
			
			hi[x][y] = hid.front().v;
			lo[x][y] = lod.front().v;
		}
		hid.clear();
		lod.clear();
	}
	
	if(nuke)thecone = 0;
	
	for(int x = 1; x <= w; ++x){
		for(int y = 1; y <= h; ++y){
			int a;
			
			a = hi[x][y];
			while(!hid.empty() && y-hid.front().p >= dh)hid.pop_front();
			while(!hid.empty() && a>hid.back().v)hid.pop_back();
			hid.push_back({a, y});
			
			a = lo[x][y];
			while(!lod.empty() && y-lod.front().p >= dh)lod.pop_front();
			while(!lod.empty() && a<lod.back().v)lod.pop_back();
			lod.push_back({a, y});
			
			if(x >= dw && y >= dh){
				int d = hid.front().v - lod.front().v;
				if(thecone==0 || d<thedif){
					thedif = d;
					thecone = 0;
				}
				if(d == thedif){
					thecone++;
				}
			}
		}
		hid.clear();
		lod.clear();
	}
	
}

int main(){
	// ios_base::sync_with_stdio(false);
	read();
	for(int i = 0; i < p; ++i){
		int dw, dh;
		fin >> dh >> dw;
		yikes(dw, dh, true);
		if(dw != dh)yikes(dh, dw, false);
		fout << thedif << " " << thecone << "\n";
	}
	return 0;
}