1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
const int MAXN = 3e3 + 3;
int h[MAXN][MAXN], mi[MAXN][MAXN];
int que[MAXN], head, tail;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, a, b, x, y, z, g;
cin >> n >> m >> a >> b;
cin >> g >> x >> y >> z;
FOR2(i, 0, n, j, 0, m) {
h[i][j] = g;
g = (1LL * x * g + y) % z;
}
FOR(i, 0, n) {
head = tail = 0;
FOR(j, 0, b) {
while (head < tail && h[i][j] <= h[i][que[tail - 1]]) {
--tail;
}
que[tail++] = j;
}
mi[i][b - 1] = h[i][que[head]];
FOR(j, b, m) {
if (j - que[head] >= b) {
++head;
}
while (head < tail && h[i][j] <= h[i][que[tail - 1]]) {
--tail;
}
que[tail++] = j;
mi[i][j] = h[i][que[head]];
}
}
ll ans = 0;
FOR(j, b - 1, m) {
head = tail = 0;
FOR(i, 0, a) {
while (head < tail && mi[i][j] <= mi[que[tail - 1]][j]) {
--tail;
}
que[tail++] = i;
}
ans += mi[que[head]][j];
FOR(i, a, n) {
if (i - que[head] >= a) {
++head;
}
while (head < tail && mi[i][j] <= mi[que[tail - 1]][j]) {
--tail;
}
que[tail++] = i;
ans += mi[que[head]][j];
}
}
cout << ans << "\n";
return 0;
}
|