Pagini recente » Cod sursa (job #3148394) | Cod sursa (job #774542) | Cod sursa (job #904220) | Cod sursa (job #655258) | Cod sursa (job #2658738)
#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;
};
deque<jojo> 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;
}