其他分享
首页 > 其他分享> > Codeforces - 1195E - OpenStreetMap - 单调队列

Codeforces - 1195E - OpenStreetMap - 单调队列

作者:互联网

https://codeforc.es/contest/1195/problem/E

一个能运行但是会T的版本,因为本质上还是\(O(nmab)\)的算法。每次\(O(ab)\)初始化矩阵中的可能有用的点,然后\(O(n-a)\)往下推。

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

#define ERR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {cerr << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << "=" << a << ", ";
    err(++it, args...);
}

#define ERR1(arg,n) { cerr<<""<<#arg<<"=\n  "; for(int i=1;i<=n;i++) cerr<<arg[i]<<" "; cerr<<"\n"; }
#define ERR2(arg,n,m) { cerr<<""<<#arg<<"=\n"; for(int i=1;i<=n;i++) { cerr<<"  "; for(int j=1;j<=m;j++)cerr<<arg[i][j]<<" "; cerr<<"\n"; } }

int n, m, a, b;
int x, y, z;

int g[3000 * 3000 + 5];
int h[3005][3005];

struct node {
    int v, x;
    node(int vv, int xx) {
        v = vv;
        x = xx;
    }
};

int curx, cury;

deque<node> dq;

void calc(int x, int y) {
    curx = x, cury = y;
    while(!dq.empty())
        dq.pop_back();
    for(int i = 1; i <= a; i++) {
        int minline = h[x + i - 1][y];
        for(int j = 2; j <= b; j++) {
            minline = min(minline, h[x + i - 1][y + j - 1]);
        }
        while(!dq.empty() && minline <= dq.back().v) {
            dq.pop_back();
        }
        if(dq.empty() || minline > dq.back().v) {
            dq.push_back(node(minline, x + i - 1));
        }
    }
}

void move_to_nextline() {
    curx++;
    if(dq.front().x < curx)
        dq.pop_front();
    int minline = h[curx + a - 1][cury];
    for(int j = 2; j <= b; j++) {
        minline = min(minline, h[curx + a - 1][cury + j - 1]);
    }
    while(!dq.empty() && minline <= dq.back().v) {
        dq.pop_back();
    }
    if(dq.empty() || minline > dq.back().v) {
        dq.push_back(node(minline, curx + a - 1));
    }
}

ll ans = 0;

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
    //freopen("Yinku.out", "w", stdout);
#endif // Yinku
    while(~scanf("%d%d%d%d", &n, &m, &a, &b)) {
        scanf("%d%d%d%d", &g[1], &x, &y, &z);
        for(int i = 2; i <= n * m; i++)
            g[i] = (1ll * g[i - 1] * x % z + y) % z;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                h[i][j] = g[(i - 1) * m + j];
        //ERR2(h, n, m);
        ans = 0;
        for(int i = 1; i + a - 1 <= n; i++) {
            for(int j = 1; j + b - 1 <= m; j++) {
                calc(i, j);
                ans += dq.front().v;
                for(int di = 1; di <= a; di++) {
                    move_to_nextline();
                }
                //ERR(ans);
            }
        }
        printf("%lld\n", ans);
    }
}

标签:OpenStreetMap,args,int,d%,Codeforces,curx,back,1195E,dq
来源: https://www.cnblogs.com/Yinku/p/11205792.html