#include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int maxN = 10 * 1000 + 10; int A[maxN]; int B[maxN]; ll TOT[2 * maxN]; int n, m; #define ARR vector<pii> ARR difs[maxN]; void prep() { difs[2].push_back({2, 0}); fwd(len, 3, max(n, m) + 1) { difs[len].push_back({len, 0}); int posib = len; int req = 0; fwd(ops, 1, len + 1) { int got = (len + 2 * ops) / (ops + 1); if (got != posib) { difs[len].push_back({posib - 1, ops - req}); posib = got; req = ops; } } } } void combine(ARR &a, ARR &b, ARR &c) { c.clear(); int bptr = 0; rep(i, sz(a)) { auto [got, ops] = a[i]; while (bptr < sz(b) && b[bptr].st >= got) c.push_back(b[bptr ++]); if (sz(c) && c.back().st == got) { c.back().nd += ops; } else { c.push_back({got, ops}); } } while (bptr < sz(b)) c.push_back(b[bptr ++]); } void update(ARR &a, int len) { int cost = 0; rep(i, sz(a)) { cost += a[i].nd; if (cost >= len) return; int best = 2; int l = cost; int r = len; if (i + 1 < sz(a)) { best = a[i + 1].st + 1; r = min(len, l + a[i + 1].nd); } l = max(l, 1); TOT[best] += (2 * m + 3 - l - r) * (r - l) / 2; } } void solve() { ARR tmp, a; rep(l, n - 1) { a.clear(); a.push_back({2, 0}); int up = (A[l] < A[l + 1]); int p = l; while (p != n - 1) { int pv = p; while (p < n - 1 && (A[p] < A[p + 1]) == up) { p += 1; } update(a, min(pv + 1 - l + 1, m + 1)); fwd(r, pv + 2, p + 1) { combine(a, difs[r - pv + 1], tmp); update(tmp, min(r - l + 1, m + 1)); } if (p - pv + 1 > 2) swap(tmp, a); up ^= 1; } } } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> m; prep(); rep(i, n) cin >> A[i]; rep(i, m) cin >> B[i]; solve(); swap(n, m); swap(A, B); solve(); ll all_pairs = 1ll * n * (n + 1) * m * (m + 1) / 4; ll found = accumulate(TOT + 1, TOT + n + m + 1, 0ll); TOT[2] += all_pairs - found; const int mod = 1e9 + 7; fwd(i, 1, n + m + 1) cout << TOT[i] % mod << ' '; cout << '\n'; return 0; }
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 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i ++) #define rep(i, n) fwd(i, 0, n) #define all(X) begin(X), end(X) #define sz(X) ((int)X.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define ll long long #ifdef LOC auto &operator << (auto &out, pair<auto, auto> a) { return out << "(" << a.st << ", " << a.nd << ")"; } auto &operator << (auto &out, auto a) { out << "{"; for (auto b : a) out << b << ", "; return out << "}"; } void dump(auto... x) { ((cerr << x << ", "), ...) << '\n'; } #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int maxN = 10 * 1000 + 10; int A[maxN]; int B[maxN]; ll TOT[2 * maxN]; int n, m; #define ARR vector<pii> ARR difs[maxN]; void prep() { difs[2].push_back({2, 0}); fwd(len, 3, max(n, m) + 1) { difs[len].push_back({len, 0}); int posib = len; int req = 0; fwd(ops, 1, len + 1) { int got = (len + 2 * ops) / (ops + 1); if (got != posib) { difs[len].push_back({posib - 1, ops - req}); posib = got; req = ops; } } } } void combine(ARR &a, ARR &b, ARR &c) { c.clear(); int bptr = 0; rep(i, sz(a)) { auto [got, ops] = a[i]; while (bptr < sz(b) && b[bptr].st >= got) c.push_back(b[bptr ++]); if (sz(c) && c.back().st == got) { c.back().nd += ops; } else { c.push_back({got, ops}); } } while (bptr < sz(b)) c.push_back(b[bptr ++]); } void update(ARR &a, int len) { int cost = 0; rep(i, sz(a)) { cost += a[i].nd; if (cost >= len) return; int best = 2; int l = cost; int r = len; if (i + 1 < sz(a)) { best = a[i + 1].st + 1; r = min(len, l + a[i + 1].nd); } l = max(l, 1); TOT[best] += (2 * m + 3 - l - r) * (r - l) / 2; } } void solve() { ARR tmp, a; rep(l, n - 1) { a.clear(); a.push_back({2, 0}); int up = (A[l] < A[l + 1]); int p = l; while (p != n - 1) { int pv = p; while (p < n - 1 && (A[p] < A[p + 1]) == up) { p += 1; } update(a, min(pv + 1 - l + 1, m + 1)); fwd(r, pv + 2, p + 1) { combine(a, difs[r - pv + 1], tmp); update(tmp, min(r - l + 1, m + 1)); } if (p - pv + 1 > 2) swap(tmp, a); up ^= 1; } } } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> m; prep(); rep(i, n) cin >> A[i]; rep(i, m) cin >> B[i]; solve(); swap(n, m); swap(A, B); solve(); ll all_pairs = 1ll * n * (n + 1) * m * (m + 1) / 4; ll found = accumulate(TOT + 1, TOT + n + m + 1, 0ll); TOT[2] += all_pairs - found; const int mod = 1e9 + 7; fwd(i, 1, n + m + 1) cout << TOT[i] % mod << ' '; cout << '\n'; return 0; } |