天天看點

Codeforces 1195E OpenStreetMap (單調隊列)

Codeforces Round #574 (Div. 2)

題目連結:E. OpenStreetMap

Seryozha conducts a course dedicated to building a map of heights of Stepanovo recreation center. He laid a rectangle grid of size \(n×m\) cells on a map (rows of grid are numbered from \(1\) to \(n\) from north to south, and columns are numbered from \(1\) to \(m\) from west to east). After that he measured the average height of each cell above Rybinsk sea level and obtained a matrix of heights of size \(n×m\). The cell \((i,j)\) lies on the intersection of the \(i\)-th row and the \(j\)-th column and has height \(h_{i,j}\).

Seryozha is going to look at the result of his work in the browser. The screen of Seryozha's laptop can fit a subrectangle of size \(a×b\) of matrix of heights \((1≤a≤n, 1≤b≤m)\). Seryozha tries to decide how the weather can affect the recreation center — for example, if it rains, where all the rainwater will gather. To do so, he is going to find the cell having minimum height among all cells that are shown on the screen of his laptop.

Help Seryozha to calculate the sum of heights of such cells for all possible subrectangles he can see on his screen. In other words, you have to calculate the sum of minimum heights in submatrices of size \(a×b\) with top left corners in \((i,j)\) over all \(1≤i≤n−a+1\) and \(1≤j≤m−b+1\).

Consider the sequence \(g_i=(g_{i−1}⋅x+y)\ mod\ z\). You are given integers \(g0\), \(x\), \(y\) and \(z\). By miraculous coincidence, \(h_{i,j}=g_{(i−1)⋅m+j−1}\) (\((i−1)⋅m+j−1\) is the index).

Input

The first line of the input contains four integers \(n\), \(m\), \(a\) and \(b\) \((1≤n,m≤3000, 1≤a≤n, 1≤b≤m)\) — the number of rows and columns in the matrix Seryozha has, and the number of rows and columns that can be shown on the screen of the laptop, respectively.

The second line of the input contains four integers \(g0\), \(x\), \(y\) and \(z\) \((0≤g_0,x,y<z≤10^9)\).

Output

Print a single integer — the answer to the problem.

Examples

input
3 4 2 1 
1 2 3 59
           
output
111
           

Note

The matrix from the first example:

Solution

題意

給定 \(n \times m\) 的矩陣,求所有 \(a \times b\) 的子矩陣的最小值的和。

題解

單調隊列

與 洛谷 P2216 類似,先用單調隊列維護每一行的最小值,再用單調隊列維護每一列的最小值。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 3010;

ll mt[maxn][maxn];

ll minr[maxn][maxn], minc[maxn][maxn];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll a, b, m, n;
    ll g, x, y, z;
    cin >> n >> m >> a >> b;
    cin >> g >> x >> y >> z;
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            mt[i][j] = g;
            g = (g * x + y) % z;
        }
    }

    for(int i = 1; i <= n; ++i) {
        ll l = 1, r = 1;
        ll q[maxn] = {0, l};
        for(int j = 1; j <= m; ++j) {
            while(r >= l && j - q[l] >= b) ++l;
            while(r >= l && mt[i][j] <= mt[i][q[r]]) --r;
            q[++r] = j;
            if(j >= b) {
                minr[i][j - b + 1] = mt[i][q[l]];
            }
        }
    }

    for(int i = 1; i <= m - b + 1; ++i) {
        ll l = 1, r = 1;
        ll q[maxn] = {0, l};
        for(int j = 1; j <= n; ++j) {
            while(r >= l && j - q[l] >= a) ++l;
            while(r >= l && minr[j][i] <= minr[q[r]][i]) --r;
            q[++r] = j;
            if(j >= a) {
                minc[j - a + 1][i] = minr[q[l]][i];
            }
        }
    }
    ll ans = 0;
    for(int i = 1; i <= n - a + 1; ++i) {
        for(int j = 1; j <= m - b + 1; ++j) {
            ans += minc[i][j];
        }
    }
    cout << ans << endl;
    return 0;
}