zoukankan      html  css  js  c++  java
  • 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:
    title

    Solution

    题意

    给定 (n imes m) 的矩阵,求所有 (a imes 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;
    }
    
  • 相关阅读:
    [Project Euler] Problem 58
    [Project Euler] Problem 59 Decrption
    [Project Euler] Problem 57
    VS2010 + WinDDK 搭建驱动开发环境
    利用C++模板特性计算各整数类型的最大最小值
    虚表的那些事儿
    ModuleNotFoundError: No module named 'pip._vendor.six'
    OpenCVPython系列之单应性查找对象理论篇
    OpenCVPython系列之背景分离
    OpenCVPython系列之Shi—tomasi拐角检测器
  • 原文地址:https://www.cnblogs.com/wulitaotao/p/11628796.html
Copyright © 2011-2022 走看看