Cod sursa(job #3353030)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 3 mai 2026 19:31:25
Problema Struti Scor 100
Compilator cpp-64 Status done
Runda cerc-acs-02-05-26 Marime 2.98 kb
#include <bits/stdc++.h>  

using namespace std; 

const int max_size = 1e3 + 20, INF = 1e9 + 7;

int a[max_size][max_size], n, m, mnrow[max_size][max_size], mxrow[max_size][max_size], mn, tot;

void solve (int dx, int dy)
{
    for (int i = 1; i <= n; i++)
    {
        deque <int> mndq, mxdq;
        for (int j = 1; j <= m; j++)
        {
            while (!mndq.empty() && a[i][mndq.back()] >= a[i][j])
            {
                mndq.pop_back();
            }
            if (!mndq.empty() && mndq.front() <= j - dy)
            {
                mndq.pop_front();
            }
            mndq.push_back(j);
            while (!mxdq.empty() && a[i][mxdq.back()] <= a[i][j])
            {
                mxdq.pop_back();
            }
            if (!mxdq.empty() && mxdq.front() <= j - dy)
            {
                mxdq.pop_front();
            }
            mxdq.push_back(j);
            if (j >= dy)
            {
                mnrow[i][j] = a[i][mndq.front()];
                mxrow[i][j] = a[i][mxdq.front()];
            }
        }
    }
    for (int j = dy; j <= m; j++)
    {
        deque <int> mndq, mxdq;
        for (int i = 1; i <= n; i++)
        {
            while (!mndq.empty() && mnrow[mndq.back()][j] >= mnrow[i][j])
            {
                mndq.pop_back();
            }
            if (!mndq.empty() && mndq.front() <= i - dx)
            {
                mndq.pop_front();
            }
            mndq.push_back(i);
            while (!mxdq.empty() && mxrow[mxdq.back()][j] <= mxrow[i][j])
            {
                mxdq.pop_back();
            }
            if (!mxdq.empty() && mxdq.front() <= i - dx)
            {
                mxdq.pop_front();
            }
            mxdq.push_back(i);
            if (i >= dx)
            {
                int diff = mxrow[mxdq.front()][j] - mnrow[mndq.front()][j];
                if (diff < mn)
                {
                    mn = diff;
                    tot = 0;
                }
                if (diff == mn)
                {
                    tot++;
                }
            }
        }
    }
}

void solve()
{
    int q;
    cin >> n >> m >> q;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
    while (q--)
    {
        int dx, dy;
        cin >> dx >> dy;
        mn = INF;
        tot = 0;
        solve(dx, dy);
        if (dx != dy)
        {
            solve(dy, dx);
        }
        cout << mn << " " << tot << "\n";
    }
}

signed main() 
{ 
#ifdef LOCAL 
    freopen("test.in", "r", stdin); 
    freopen("test.out", "w", stdout);
#else
    freopen("struti.in", "r", stdin);
    freopen("struti.out", "w", stdout);
#endif // LOCAL 
    ios_base::sync_with_stdio(false); 
    cin.tie(0); 
    cout.tie(0); 
    long long tt;
    tt = 1;
    while (tt--)
    {
        solve();
    }
    return 0; 
}