#include <bits/stdc++.h> using namespace std; using ll = long long; // #ifdef DEBUG // #include "debug.h" // #else // #define debug(...) 42 // #endif #define debug(...) 42 #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define each(a,x) for(auto& a : (x)) #define sz(x) int((x).size()) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } const int P = 1e9 + 7, phiP = P - 1; struct modint { int x = 0; modint operator+(modint o) const {return {x + o.x >= P ? x + o.x - P : x + o.x}; } modint operator-(modint o) const {return {x < o.x ? x - o.x + P : x - o.x}; } modint operator+() const {return *this; } modint operator-() const {return {x ? P - x : 0}; } modint operator*(modint o) const {return {int(ll(x) * o.x % P)}; } modint operator/(modint o) const {return *this * o.inv(); } modint & operator+=(modint o) {return *this = *this + o; } modint & operator-=(modint o) {return *this = *this - o; } modint & operator*=(modint o) {return *this = *this * o; } modint & operator/=(modint o) {return *this = *this / o; } modint pow(ll e) const { modint ret{1}, b(*this); for (; e; e >>= 1) { if (e & 1) ret *= b; b *= b; } return ret; } modint inv() const {return pow(phiP - 1); } friend void __dbg(modint m) { cout << m.x - P * (2 * m.x >= P); } }; const modint inv2 = modint{2}.inv(); #ifdef DEBUG int stability(vi a) { int inc = 1, dec = 1, score = 1; repp(i, 1, sz(a)) { inc = a[i] > a[i-1] ? inc + 1 : 1; dec = a[i] < a[i-1] ? dec + 1 : 1; maxi(score, max(inc, dec)); } return score; } int func_f(vi a, vi b) { int ans = 1e9; rep(msk, 1 << (sz(a) + sz(b))) { if (__builtin_popcount(msk) != sz(a)) continue; vi c(sz(a) + sz(b)); int ia = 0, ib = 0; rep(id, sz(a) + sz(b)) if (msk & (1 << id)) c[id] = a[ia++]; else c[id] = b[ib++]; mini(ans, stability(c)); } return ans; } vector<modint> sol_brute(vi a, vi b) { int n = sz(a), m = sz(b); vector<modint> ans(n + m + 1); rep(ra, n) rep(la, ra + 1) rep(rb, m) rep(lb, rb + 1) { vi aa(a.begin() + la, a.begin() + ra + 1), bb(b.begin() + lb, b.begin() + rb + 1); int here = func_f(aa, bb); ++ans[here].x; } return ans; } #endif vector<pi> incdec(const vi &a) { const int n = sz(a); if (n <= 1) return {}; vector<pi> ret = {pi{0, 1}}; repp(i, 2, n) if ((a[i-2] < a[i-1] && a[i-1] < a[i]) || (a[i-2] > a[i-1] && a[i-1] > a[i])) ++ret.back().second; else ret.emplace_back(i-1, i); debug(a, ret); return ret; } modint choose2(ll x) { return modint{int(x * (x - 1) / 2 % P)}; } modint atmostx(int x, int n, int m, vector<pi> &ranges) { ranges.erase(remove_if(all(ranges), [&](pi lr) { auto [l, r] = lr; return r - l + 1 <= x; }), ranges.end()); if (sz(ranges)) debug(x, n, m, ranges); modint ans; auto add_empty = [&](int l, int r) { if (sz(ranges)) debug("add_empty()", l, r); int len = r - l + 1; assert(l <= r); if (sz(ranges)) debug(choose2(len + 1) * choose2(m + 1)); ans += choose2(len + 1) * choose2(m + 1); }; auto add_full = [&](int rangel, int ranger) { if (sz(ranges)) debug("add_full()", rangel, ranger); int len = ranger - rangel + 1; assert(rangel <= ranger); for (int need = 0; need <= m; ++need) { int l_here = (x - 1) * need + 2; int r_here = (x - 1) * (need + 1) + 1; if (need == 0) --l_here; mini(r_here, len); if (l_here > r_here) break; modint waysa = modint{(len + 1 - l_here) + (len + 1 - r_here)} * inv2 * modint{r_here - l_here + 1}; modint waysb = choose2(m+2 - need - !need); if (sz(ranges)) debug(waysa, waysb, waysa * waysb); ans += waysa * waysb; } }; static deque<modint> dq; dq.clear(); modint sumdq, sumprefdq, sumc2; auto push0 = [&]() { debug(dq, sumdq, sumprefdq, sumc2); sumc2 -= modint{m+1} * sumdq - sumprefdq; dq.push_front(modint{}); sumprefdq += sumdq; if (sz(dq) == m + 2) { sumdq -= dq[m+1]; sumprefdq -= modint{m+1} * dq[m+1]; dq.pop_back(); } }; push0(); auto getsum = [&]() { return sumc2 - dq[0] * choose2(m+2) + dq[0] * choose2(m+1); }; auto process_empty = [&](int l, int r) { if (sz(ranges)) debug("process_empty()", l, r); assert(l <= r); add_empty(l, r); modint len{r - l + 1}; debug(getsum(), len); ans += getsum() * len; dq[0] += len; sumdq += len; sumc2 += choose2(m + 2) * len; }; int nx = 0; for (auto [l, r] : ranges) { if (nx <= l-1) process_empty(nx, l-1); if (nx - 1 == l) { dq[0] -= modint{1}; sumdq -= modint{1}; sumc2 -= choose2(m + 2); ans -= choose2(m + 1); } add_full(l, r); for (int l_here_virtual = l + 1; l_here_virtual <= r; l_here_virtual += x - 1) { if (l_here_virtual != l + 1) push0(); int l_here = l_here_virtual - (l_here_virtual == l + 1); int r_here = min(r, l_here_virtual + x - 2); modint multip{r_here - l_here + 1 - bool(l_here == nx - 1)}; if (sz(ranges)) debug(getsum(), multip, dq, sumdq, sumprefdq, sumc2); ans += getsum() * multip; } for (int l_here_virtual = l + 1, i = 0; l_here_virtual <= r && i <= m; (l_here_virtual += x - 1), ++i) { int l_here = l_here_virtual - (l_here_virtual == l + 1); int r_here = min(r, l_here_virtual + x - 2); modint len{r_here - l_here + 1}; dq[i] += len; sumdq += len; sumprefdq += modint{i} * len; sumc2 += len * choose2(m+2-i); } debug(dq, sumdq, sumprefdq, sumc2); nx = r + 1; } if (nx != n) process_empty(nx, n-1); debug(ans); return ans; } vector<modint> oneside(const vi &a, int m) { debug("ONESIDE BEGINS", a, m); const int n = sz(a); vector<pi> ranges = incdec(a); vector<modint> ret(n + m + 1); repp(x, 2, n + m + 1) ret[x] = atmostx(x, n, m, ranges); // we only want to have |A| > |B| // we have to sub |B| >= |A| debug(ret); { modint badlens; repp(alen, 1, min(n, m) + 1) badlens += choose2(m + 2 - alen) * modint{n - alen + 1}; debug(badlens); repp(i, 2, n + m + 1) ret[i] -= badlens; } return ret; } void solve() { int n, m; cin >> n >> m; vi a(n), b(m); rep(i, n) cin >> a[i]; rep(i, m) cin >> b[i]; vector<modint> aleft = oneside(a, sz(b)); debug(aleft); vector<modint> bleft = oneside(b, sz(a)); debug(bleft); assert(sz(aleft) == n + m + 1 && sz(bleft) == n + m + 1); debug("POSTPROC"); vector<modint> ans(n + m + 1); repp(i, 1, n + m + 1) ans[i] = aleft[i] + bleft[i]; debug(ans); { modint equal; // we have to add |A| = |B| repp(i, 1, min(n, m) + 1) equal += modint{n + 1 - i} * modint{m + 1 - i}; repp(i, 2, n + m + 1) ans[i] += equal; debug(equal); } debug(ans); // go from pref to normal reppr(i, 1, n + m + 1) ans[i] -= ans[i-1]; debug(ans); // auto bruted = sol_brute(a, b); // debug(bruted); // repp(i, 1, n + m + 1) // assert(bruted[i].x == ans[i].x); repp(i, 1, n + m + 1) cout << ans[i].x << ' '; cout << '\n'; } int main() { #ifdef DEBUG const int MEMSIZE = 1024 * 1024 * 1024; static_assert(MEMSIZE % 16 == 0); static char stack[MEMSIZE]; asm volatile ( "mov %[newstack], %%rsp\n" "call *%[funcptr]" :: [funcptr] "r" (solve), [newstack] "r" (stack + MEMSIZE) ); exit(0); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); #endif }
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | #include <bits/stdc++.h> using namespace std; using ll = long long; // #ifdef DEBUG // #include "debug.h" // #else // #define debug(...) 42 // #endif #define debug(...) 42 #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define each(a,x) for(auto& a : (x)) #define sz(x) int((x).size()) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } const int P = 1e9 + 7, phiP = P - 1; struct modint { int x = 0; modint operator+(modint o) const {return {x + o.x >= P ? x + o.x - P : x + o.x}; } modint operator-(modint o) const {return {x < o.x ? x - o.x + P : x - o.x}; } modint operator+() const {return *this; } modint operator-() const {return {x ? P - x : 0}; } modint operator*(modint o) const {return {int(ll(x) * o.x % P)}; } modint operator/(modint o) const {return *this * o.inv(); } modint & operator+=(modint o) {return *this = *this + o; } modint & operator-=(modint o) {return *this = *this - o; } modint & operator*=(modint o) {return *this = *this * o; } modint & operator/=(modint o) {return *this = *this / o; } modint pow(ll e) const { modint ret{1}, b(*this); for (; e; e >>= 1) { if (e & 1) ret *= b; b *= b; } return ret; } modint inv() const {return pow(phiP - 1); } friend void __dbg(modint m) { cout << m.x - P * (2 * m.x >= P); } }; const modint inv2 = modint{2}.inv(); #ifdef DEBUG int stability(vi a) { int inc = 1, dec = 1, score = 1; repp(i, 1, sz(a)) { inc = a[i] > a[i-1] ? inc + 1 : 1; dec = a[i] < a[i-1] ? dec + 1 : 1; maxi(score, max(inc, dec)); } return score; } int func_f(vi a, vi b) { int ans = 1e9; rep(msk, 1 << (sz(a) + sz(b))) { if (__builtin_popcount(msk) != sz(a)) continue; vi c(sz(a) + sz(b)); int ia = 0, ib = 0; rep(id, sz(a) + sz(b)) if (msk & (1 << id)) c[id] = a[ia++]; else c[id] = b[ib++]; mini(ans, stability(c)); } return ans; } vector<modint> sol_brute(vi a, vi b) { int n = sz(a), m = sz(b); vector<modint> ans(n + m + 1); rep(ra, n) rep(la, ra + 1) rep(rb, m) rep(lb, rb + 1) { vi aa(a.begin() + la, a.begin() + ra + 1), bb(b.begin() + lb, b.begin() + rb + 1); int here = func_f(aa, bb); ++ans[here].x; } return ans; } #endif vector<pi> incdec(const vi &a) { const int n = sz(a); if (n <= 1) return {}; vector<pi> ret = {pi{0, 1}}; repp(i, 2, n) if ((a[i-2] < a[i-1] && a[i-1] < a[i]) || (a[i-2] > a[i-1] && a[i-1] > a[i])) ++ret.back().second; else ret.emplace_back(i-1, i); debug(a, ret); return ret; } modint choose2(ll x) { return modint{int(x * (x - 1) / 2 % P)}; } modint atmostx(int x, int n, int m, vector<pi> &ranges) { ranges.erase(remove_if(all(ranges), [&](pi lr) { auto [l, r] = lr; return r - l + 1 <= x; }), ranges.end()); if (sz(ranges)) debug(x, n, m, ranges); modint ans; auto add_empty = [&](int l, int r) { if (sz(ranges)) debug("add_empty()", l, r); int len = r - l + 1; assert(l <= r); if (sz(ranges)) debug(choose2(len + 1) * choose2(m + 1)); ans += choose2(len + 1) * choose2(m + 1); }; auto add_full = [&](int rangel, int ranger) { if (sz(ranges)) debug("add_full()", rangel, ranger); int len = ranger - rangel + 1; assert(rangel <= ranger); for (int need = 0; need <= m; ++need) { int l_here = (x - 1) * need + 2; int r_here = (x - 1) * (need + 1) + 1; if (need == 0) --l_here; mini(r_here, len); if (l_here > r_here) break; modint waysa = modint{(len + 1 - l_here) + (len + 1 - r_here)} * inv2 * modint{r_here - l_here + 1}; modint waysb = choose2(m+2 - need - !need); if (sz(ranges)) debug(waysa, waysb, waysa * waysb); ans += waysa * waysb; } }; static deque<modint> dq; dq.clear(); modint sumdq, sumprefdq, sumc2; auto push0 = [&]() { debug(dq, sumdq, sumprefdq, sumc2); sumc2 -= modint{m+1} * sumdq - sumprefdq; dq.push_front(modint{}); sumprefdq += sumdq; if (sz(dq) == m + 2) { sumdq -= dq[m+1]; sumprefdq -= modint{m+1} * dq[m+1]; dq.pop_back(); } }; push0(); auto getsum = [&]() { return sumc2 - dq[0] * choose2(m+2) + dq[0] * choose2(m+1); }; auto process_empty = [&](int l, int r) { if (sz(ranges)) debug("process_empty()", l, r); assert(l <= r); add_empty(l, r); modint len{r - l + 1}; debug(getsum(), len); ans += getsum() * len; dq[0] += len; sumdq += len; sumc2 += choose2(m + 2) * len; }; int nx = 0; for (auto [l, r] : ranges) { if (nx <= l-1) process_empty(nx, l-1); if (nx - 1 == l) { dq[0] -= modint{1}; sumdq -= modint{1}; sumc2 -= choose2(m + 2); ans -= choose2(m + 1); } add_full(l, r); for (int l_here_virtual = l + 1; l_here_virtual <= r; l_here_virtual += x - 1) { if (l_here_virtual != l + 1) push0(); int l_here = l_here_virtual - (l_here_virtual == l + 1); int r_here = min(r, l_here_virtual + x - 2); modint multip{r_here - l_here + 1 - bool(l_here == nx - 1)}; if (sz(ranges)) debug(getsum(), multip, dq, sumdq, sumprefdq, sumc2); ans += getsum() * multip; } for (int l_here_virtual = l + 1, i = 0; l_here_virtual <= r && i <= m; (l_here_virtual += x - 1), ++i) { int l_here = l_here_virtual - (l_here_virtual == l + 1); int r_here = min(r, l_here_virtual + x - 2); modint len{r_here - l_here + 1}; dq[i] += len; sumdq += len; sumprefdq += modint{i} * len; sumc2 += len * choose2(m+2-i); } debug(dq, sumdq, sumprefdq, sumc2); nx = r + 1; } if (nx != n) process_empty(nx, n-1); debug(ans); return ans; } vector<modint> oneside(const vi &a, int m) { debug("ONESIDE BEGINS", a, m); const int n = sz(a); vector<pi> ranges = incdec(a); vector<modint> ret(n + m + 1); repp(x, 2, n + m + 1) ret[x] = atmostx(x, n, m, ranges); // we only want to have |A| > |B| // we have to sub |B| >= |A| debug(ret); { modint badlens; repp(alen, 1, min(n, m) + 1) badlens += choose2(m + 2 - alen) * modint{n - alen + 1}; debug(badlens); repp(i, 2, n + m + 1) ret[i] -= badlens; } return ret; } void solve() { int n, m; cin >> n >> m; vi a(n), b(m); rep(i, n) cin >> a[i]; rep(i, m) cin >> b[i]; vector<modint> aleft = oneside(a, sz(b)); debug(aleft); vector<modint> bleft = oneside(b, sz(a)); debug(bleft); assert(sz(aleft) == n + m + 1 && sz(bleft) == n + m + 1); debug("POSTPROC"); vector<modint> ans(n + m + 1); repp(i, 1, n + m + 1) ans[i] = aleft[i] + bleft[i]; debug(ans); { modint equal; // we have to add |A| = |B| repp(i, 1, min(n, m) + 1) equal += modint{n + 1 - i} * modint{m + 1 - i}; repp(i, 2, n + m + 1) ans[i] += equal; debug(equal); } debug(ans); // go from pref to normal reppr(i, 1, n + m + 1) ans[i] -= ans[i-1]; debug(ans); // auto bruted = sol_brute(a, b); // debug(bruted); // repp(i, 1, n + m + 1) // assert(bruted[i].x == ans[i].x); repp(i, 1, n + m + 1) cout << ans[i].x << ' '; cout << '\n'; } int main() { #ifdef DEBUG const int MEMSIZE = 1024 * 1024 * 1024; static_assert(MEMSIZE % 16 == 0); static char stack[MEMSIZE]; asm volatile ( "mov %[newstack], %%rsp\n" "call *%[funcptr]" :: [funcptr] "r" (solve), [newstack] "r" (stack + MEMSIZE) ); exit(0); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); #endif } |