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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| #include <bits/stdc++.h>
using i64 = int64_t;
namespace FASTIO { char ibuf[1 << 21], *p1 = ibuf, *p2 = ibuf; char getc() { return p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++; } template<class T> bool read(T &x) { x = 0; int f = 0; char ch = getc(); while (ch < '0' || ch > '9') f |= ch == '-', ch = getc(); while (ch >= '0' && ch <= '9') x = (x * 10) + (ch ^ 48), ch = getc(); x = (f ? -x : x); return 1; } template<typename A, typename ...B> bool read(A &x, B &...y) { return read(x) && read(y...); } } using FASTIO::read;
const int kMaxN = 2e3 + 5, kMod = 1e9 + 7;
int n, m, q; int f[kMaxN][kMaxN], g[kMaxN][kMaxN], lx[kMaxN][kMaxN], ly[kMaxN][kMaxN]; int qx[kMaxN][kMaxN], qy[kMaxN][kMaxN], hx[kMaxN], tx[kMaxN], hy[kMaxN], ty[kMaxN]; int cntx[kMaxN][kMaxN], cnty[kMaxN][kMaxN];
struct SGT { int mxx, mx[kMaxN << 2], se[kMaxN << 2], tag[kMaxN << 2], idx[kMaxN]; bool have[kMaxN << 2];
void pushup(int x) { if (mx[x << 1] == mx[x << 1 | 1]) { mx[x] = mx[x << 1], se[x] = std::max(se[x << 1], se[x << 1 | 1]); } else if (mx[x << 1] > mx[x << 1 | 1]) { mx[x] = mx[x << 1], se[x] = std::max(se[x << 1], mx[x << 1 | 1]); } else { mx[x] = mx[x << 1 | 1], se[x] = std::max(mx[x << 1], se[x << 1 | 1]); } }
void addtag(int x, int v) { if (mx[x] <= v) return; mx[x] = tag[x] = v; }
void pushdown(int x) { if (!~tag[x]) return; addtag(x << 1, tag[x]), addtag(x << 1 | 1, tag[x]); tag[x] = -1; }
void build(int x, int l, int r) { mx[x] = 1e9, se[x] = 0, tag[x] = -1, mxx = std::max(mxx, x); have[x] = (l != r); if (l == r) { idx[l] = x; return; } int mid = (l + r) >> 1; build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r); pushup(x); }
void update(int x, int l, int r, int ql, int qr, int v) { if (l > qr || r < ql) { return; } else if (l >= ql && r <= qr) { if (v >= mx[x]) return; else if (v > se[x]) return addtag(x, v); } pushdown(x); int mid = (l + r) >> 1; update(x << 1, l, mid, ql, qr, v), update(x << 1 | 1, mid + 1, r, ql, qr, v); pushup(x); }
void pushall() { for (int i = 1; i <= mxx; ++i) if (have[i]) pushdown(i); for (int i = mxx; i; --i) if (have[i]) pushup(i); }
int query(int x) { return mx[idx[x]]; } } sx[kMaxN], sy[kMaxN];
void inc(int &x, int y) { (x += y) >= kMod ? (x -= kMod) : x; } void dec(int &x, int y) { (x -= y) < 0 ? (x += kMod) : x; }
void solve() { for (int i = 0; i <= n; ++i) { hx[i] = 1, tx[i] = 0; std::fill_n(cntx[i], std::max(n, m) + 1, 0); } for (int i = 0; i <= m; ++i) { hy[i] = 1, ty[i] = 0; std::fill_n(cnty[i], std::max(n, m) + 1, 0); } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (i > 1) { if (j > 1) { for (; hx[i - 1] <= tx[i - 1] && f[i - 1][qx[i - 1][tx[i - 1]]] < f[i - 1][j - 1]; --tx[i - 1]) { dec(cntx[i - 1][f[i - 1][qx[i - 1][tx[i - 1]]]], g[i - 1][qx[i - 1][tx[i - 1]]]); } qx[i - 1][++tx[i - 1]] = j - 1; inc(cntx[i - 1][f[i - 1][j - 1]], g[i - 1][j - 1]); } for (; hx[i - 1] <= tx[i - 1] && qx[i - 1][hx[i - 1]] < lx[i][j]; ++hx[i - 1]) { dec(cntx[i - 1][f[i - 1][qx[i - 1][hx[i - 1]]]], g[i - 1][qx[i - 1][hx[i - 1]]]); } if (hx[i - 1] <= tx[i - 1]) { int mx = f[i - 1][qx[i - 1][hx[i - 1]]] + 1, cnt = cntx[i - 1][mx - 1]; if (mx > f[i][j]) { f[i][j] = mx, g[i][j] = cnt; } else if (mx == f[i][j]) { inc(g[i][j], cnt); } } } if (j > 1) { if (i > 2) { for (; hy[j - 1] <= ty[j - 1] && f[qy[j - 1][ty[j - 1]]][j - 1] < f[i - 2][j - 1]; --ty[j - 1]) { dec(cnty[j - 1][f[qy[j - 1][ty[j - 1]]][j - 1]], g[qy[j - 1][ty[j - 1]]][j - 1]); } qy[j - 1][++ty[j - 1]] = i - 2; inc(cnty[j - 1][f[i - 2][j - 1]], g[i - 2][j - 1]); } for (; hy[j - 1] <= ty[j - 1] && qy[j - 1][hy[j - 1]] < ly[i][j]; ++hy[j - 1]) { dec(cnty[j - 1][f[qy[j - 1][hy[j - 1]]][j - 1]], g[qy[j - 1][hy[j - 1]]][j - 1]); } if (hy[j - 1] <= ty[j - 1]) { int mx = f[qy[j - 1][hy[j - 1]]][j - 1] + 1, cnt = cnty[j - 1][mx - 1]; if (mx > f[i][j]) { f[i][j] = mx, g[i][j] = cnt; } else if (mx == f[i][j]) { inc(g[i][j], cnt); } } } } } }
void dickdreamer() { read(n, m, q); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { f[i][j] = g[i][j] = 1; lx[i][j] = ly[i][j] = 1e9; } } for (int i = 1; i <= m; ++i) sx[i].mxx = 0, sx[i].build(1, 1, n); for (int i = 1; i <= n; ++i) sy[i].mxx = 0, sy[i].build(1, 1, m); for (int cs = 1; cs <= q; ++cs) { int _x1, _y1, _x2, _y2; read(_x1, _y1, _x2, _y2); if (_x1 < _x2) sx[_y2].update(1, 1, n, _x1 + 1, _x2, _y1); if (_y1 < _y2) sy[_x2].update(1, 1, m, _y1 + 1, _y2, _x1); } for (int i = m; i; --i) { sx[i].pushall(); for (int j = 1; j <= n; ++j) { lx[j][i] = sx[i].query(j); if (i != m) lx[j][i] = std::min(lx[j][i], lx[j][i + 1]); } } for (int i = n; i; --i) { sy[i].pushall(); for (int j = 1; j <= m; ++j) { ly[i][j] = sy[i].query(j); if (i != n) ly[i][j] = std::min(ly[i][j], ly[i + 1][j]); } } solve(); int mx = 0, cnt = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (f[i][j] > mx) { mx = f[i][j], cnt = g[i][j]; } else if (f[i][j] == mx) { cnt = (cnt + g[i][j]) % kMod; } } } std::cout << mx << ' ' << cnt << '\n'; }
int32_t main() { #ifdef ORZXKR freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif std::cout.tie(0); int T = 1; read(T); while (T--) dickdreamer(); return 0; }
|