Cod sursa(job #2120700)

Utilizator stefan_creastaStefan Creasta stefan_creasta Data 2 februarie 2018 19:39:46
Problema Struti Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.29 kb
#include <cstdio>
#include <deque>
#include <cstring>
using namespace std;
const int NMAX = 1005;
int v[NMAX][NMAX];
int amax[NMAX][NMAX];
int amin[NMAX][NMAX];
int bmax[NMAX][NMAX];
int bmin[NMAX][NMAX];
int n, m;
struct ABC {
  int x, y;
};
deque <ABC> dqmax;
deque <ABC> dqmin;
void remove_a(ABC poz) {
  while(!dqmax.empty() && v[poz.x][poz.y] > v[dqmax.back().x][dqmax.back().y]) {
    dqmax.pop_back();
  }
  dqmax.push_back(poz);
  while(!dqmin.empty() && v[poz.x][poz.y] < v[dqmin.back().x][dqmin.back().y]) {
    dqmin.pop_back();
  }
  dqmin.push_back(poz);
}
void init_a(int k) {
  memset(amax, 0, sizeof(amax));
  memset(amin, 0, sizeof(amin));
  for(int i = 1; i <= n; ++i) {
    int j = 1;
    dqmax.clear();
    dqmin.clear();
    while(j < k) {
      remove_a({i, j});
      amax[i][j] = v[dqmax.front().x][dqmax.front().y];
      amin[i][j++] = v[dqmin.front().x][dqmin.front().y];
    }
    while(j <= m) {
      remove_a({i, j});
      while(!dqmax.empty() && j - dqmax.front().y >= k) {
        dqmax.pop_front();
      }
      while(!dqmin.empty() && j - dqmin.front().y >= k) {
        dqmin.pop_front();
      }
      amax[i][j] = v[dqmax.front().x][dqmax.front().y];
      amin[i][j++] = v[dqmin.front().x][dqmin.front().y];
    }
  }
}
void r_a(ABC poz) {
  while(!dqmax.empty() && amax[poz.x][poz.y] > amax[dqmax.back().x][dqmax.back().y]) {
    dqmax.pop_back();
  }
  dqmax.push_back(poz);
  while(!dqmin.empty() && amin[poz.x][poz.y] < amin[dqmin.back().x][dqmin.back().y]) {
    dqmin.pop_back();
  }
  dqmin.push_back(poz);
}
void update_a(int k) {
  memset(bmax, 0, sizeof(bmax));
  for(int j = 1; j <= m; ++j) {
    int i = 1;
    dqmax.clear();
    dqmin.clear();
    while(i < k) {
      r_a({i, j});
      bmax[i][j] = amax[dqmax.front().x][dqmax.front().y];
      bmin[i++][j] = amin[dqmin.front().x][dqmin.front().y];
    }
    while(i <= n) {
      r_a({i, j});
      while(!dqmax.empty() && i - dqmax.front().x >= k) {
        dqmax.pop_front();
      }
      while(!dqmin.empty() && i - dqmin.front().x >= k) {
        dqmin.pop_front();
      }
      bmax[i][j] = amax[dqmax.front().x][dqmax.front().y];
      bmin[i++][j] = amin[dqmin.front().x][dqmin.front().y];
    }
  }
}

int main() {
  int p;
  freopen("struti.in", "r", stdin);
  freopen("struti.out", "w", stdout);
  scanf("%d%d%d", &n, &m, &p);
  for(int i = 1; i <= n; ++i) {
    for(int j = 1; j <= m; ++j) {
      scanf("%d", &v[i][j]);
    }
  }
  while(p--) {
    int x, y;
    scanf("%d%d", &x, &y);
    init_a(y);
    update_a(x);
    int sol = 0x7fffffff, apsol = 0;
    for(int i = 1; i <= n; ++i) {
      for(int j = 1; j <= m; ++j) {
        if(i >= x && j >= y) {
          if(sol > bmax[i][j] - bmin[i][j]) {
            sol = bmax[i][j] - bmin[i][j];
            apsol = 0;
          }
          if(sol == bmax[i][j] - bmin[i][j]) {
            ++apsol;
          }
        }
      }
    }
    if(x != y) {
      init_a(x);
      update_a(y);
      for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
          if(i >= y && j >= x) {
            if(sol > bmax[i][j] - bmin[i][j]) {
              sol = bmax[i][j] - bmin[i][j];
              apsol = 0;
            }
            if(sol == bmax[i][j] - bmin[i][j]) {
              ++apsol;
            }
          }
        }
      }
    }
    printf("%d %d\n", sol, apsol);
  }
  return 0;
}