[Solution]
I thought of size-balanced tree at first, the got only 60 at the exams. In fact, we can just modify two arrays ml[i] and mr[i] meaning the left and the right pos that the number can be that row. Because there are many queries but few changes, it can get AC. What a pity that I didn't come up with that while examing.
[Code]
#define PROC "random"
#include <cstdio>
#include <cctype>
#include <memory.h>
#include <algorithm>
using namespace std;
int nextInt() {
int d, s = 0;
do
d = getchar();
while (!isdigit(d));
do
s = s * 10 + d - 48, d = getchar();
while (isdigit(d));
return s;
}
#define _l (long long int)
#define mkword(a,b) (((a)<<16)|(b))
#define hiword(a) ((a)>>16)
#define loword(a) ((a)&65535)
const int maxn = 5009;
const int inf = 0x3f3f3f3f;
int n, m, t, a[maxn][maxn], r[maxn * maxn], ans[maxn * 2], ml[maxn], mr[maxn];
void init() {
int x, a, b, c, d, q;
x = nextInt();
a = nextInt();
b = nextInt();
c = nextInt();
d = nextInt();
n = nextInt();
m = nextInt();
q = nextInt();
t = n * m;
for (int i = 1; i <= t; i ++)
r[i] = i;
for (int i = 1; i <= t; i ++) {
x = (_l a * (_l x * x % d) % d + _l b * x % d + c) % d;
swap(r[i], r[x % i + 1]);
}
for (int i = 0; i < q; i ++) {
int u = nextInt();
int v = nextInt();
swap(r[u], r[v]);
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
::a[i][j] = r[i * m + j + 1];
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
r[::a[i][j]] = mkword(i + 1, j + 1);
}
void solve() {
for (int i = 1; i <= n; i ++)
ml[i] = 1, mr[i] = m;
for (int i = 0, p = 1; i < m + n - 1; i ++) {
while (ml[hiword(r[p])] > loword(r[p]) || mr[hiword(r[p])] < loword(r[p]))
p ++;
for (int j = 1; j < hiword(r[p]); j ++)
mr[j] = min(mr[j], loword(r[p]));
for (int j = hiword(r[p]) + 1; j <= n; j ++)
ml[j] = max(ml[j], loword(r[p]));
ans[i] = p ++;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
init();
// debugPrint();
solve();
for (int i = 0; i < (m + n - 1); i ++)
printf("%d%c", ans[i], ((i < (n + m - 2)) ? 32 : 10));
// putchar(10);
return 0;
}