// [3A] Splatanie ciagow, Kamil Debowski #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } vector<long long> total; struct Q { long long a, b, c; // ax^2 + bx + c long long value(int x) const { return c + x * (b + x * a); } Q operator * (long long x) const { return Q{a * x, b * x, c * x}; } void operator *= (long long x) { a *= x; b *= x; c *= x; } void operator += (const Q& he) { a += he.a; b += he.b; c += he.c; } }; struct F { long long a, b; // a * x + b long long value(int answer) const { return a * answer + b; } void operator += (const F& he) { a += he.a; b += he.b; } void operator *= (long long x) { a *= x; b *= x; } F operator * (long long x) const { return F{a * x, b * x}; } Q operator * (const F& he) const { return Q{a * he.a, a * he.b + b * he.a, b * he.b}; } }; struct Block { int len; bool isReal; bool isFake() const { return !isReal; } vector<F> prefs; bool shouldCompute(int answer) const { int max_val = (len - 1) / (answer - 1); return (int) prefs.size() != max_val + 1; } bool compute(int answer) { int max_val = (len - 1) / (answer - 1); if((int) prefs.size() == max_val + 1) { return false; } prefs = vector<F>(max_val + 1, F{1, -1}); prefs[0] = F{1, 0}; if (max_val > 0) { prefs.back() = F{-max_val, len + max_val - 1}; } return true; } vector<int> getPrefCount(int answer) const { int max_val = (len - 1) / (answer - 1); vector<int> cnt(max_val + 1, answer - 1); cnt[0] = answer; // min(len, answer); if (max_val > 0) { cnt.back() = len - (answer) - (long long) (max_val - 1) * (answer - 1); } return cnt; } }; void solve(vector<int> a, vector<int> b) { vector<int> block_lens{1}; for (int i = 0; i + 1 < (int) a.size(); i++) { int block = 2; while (i + 2 < (int) a.size() && bool(a[i] < a[i+1]) == bool(a[i+1] < a[i+2])) { i++; block++; } block_lens.push_back(block - 1); } block_lens.push_back(1); vector<Block> blocks; for (int len : block_lens) { blocks.push_back(Block{len, true}); } Q prevSumQ{0,0,0}; for (int answer = 2; answer <= int(a.size() + b.size()); answer++) { // cerr << answer << " "; bool anyChange = false; for (const Block& block : blocks) { if (block.shouldCompute(answer)) { anyChange = true; } } Q sumQ{0, 0, 0}; auto markQ = [&](int need, Q q) { int B = b.size(); need = max(need, 1); if (need <= B) { q *= (long long) (B - need + 1) * (B - need + 2) / 2; sumQ += q; } }; auto markF = [&](int need, F f) { int B = b.size(); need = max(need, 1); if (need <= B) { f *= (long long) (B - need + 1) * (B - need + 2) / 2; // sumQ += f; sumQ.b += f.a; sumQ.c += f.b; } }; auto mark = [&](int need, int copies) { int B = b.size(); need = max(need, 1); if (need <= B) { total[answer] += (long long) copies * (B - need + 1) * (B - need + 2) / 2; } // for (int k = need; k <= B; k++) { // total[answer] += (long long) copies * (B - k + 1); // } }; if (anyChange) { vector<Block> new_blocks; // debug dd = debug(); // dd << imie(answer); for (Block block : blocks) { // dd << "(" << block.len << ", " << block.isReal << "), "; if (block.len < answer) { block.isReal = false; } if (!new_blocks.empty() && new_blocks.back().isFake() && block.isFake()) { new_blocks.back().len += block.len; } else { new_blocks.push_back(block); //Block{block.len, block.isReal}); } } // dd << "\n"; blocks = new_blocks; for (Block& block : blocks) { block.compute(answer); } } // for (Block block : blocks) { // dd << "(" << block.len << ", " << block.isReal << "), "; // } // dd << "\n"; // 1) inside every block for (int i = 0; i < (int) blocks.size(); i++) { int s = blocks[i].len; if (blocks[i].isFake()) { mark(0, (long long) s * (s - 1) / 2); // overflow! } else { vector<int> cnt = blocks[i].getPrefCount(answer); int nxt = s-1; for (int v = 0; v < (int) cnt.size(); v++) { // s-1, s-2, ... mark(v, (long long) cnt[v] * (nxt + (nxt-cnt[v]+1)) / 2); nxt -= cnt[v]; } // debug() << imie(nxt); assert(nxt == -1); // for (int d = 1; d < s; d++) { // don't skip d=1 // int needed = (d - 2) / (answer - 1); // mark(needed, s - d); // } } } // 2) pairs of blocks if (!anyChange) { sumQ = prevSumQ; } else for (int i = 0; i < (int) blocks.size(); i++) { int need_between = 0; for (int j = i + 1; j < (int) blocks.size(); j++) { if (i + 2 <= j && !blocks[j-1].isFake()) { need_between += (blocks[j-1].len - 1) / (answer - 1); } // two different blocks Block L = blocks[i]; Block R = blocks[j]; if (L.isFake() && R.isFake()) { markF(need_between, F{0, (long long) L.len * R.len}); // overflow! } else if(L.isFake()) { // R.compute(answer); for (int val = 0; val < (int) R.prefs.size(); val++) { markF(need_between + val, R.prefs[val] * L.len); } // vector<int> cnt = R.getPrefCount(answer); // for (int val = 0; val < (int) cnt.size(); val++) { // mark(need_between + val, (long long) L.len * cnt[val]); // } } else if (R.isFake()) { // L.compute(answer); // vector<int> cnt = L.getPrefCount(answer); // for (int i = 0; i < (int) cnt.size(); i++) { // debug() << imie(i) imie(cnt[i]) imie(L.prefs[i].a) imie(L.prefs[i].b) imie(cnt.size()) imie(L.prefs.size()); // assert(cnt[i] == L.prefs[i].value(answer)); // } for (int val = 0; val < (int) L.prefs.size(); val++) { markF(need_between + val, L.prefs[val] * R.len); // mark(need_between + val, (long long) R.len * L.prefs[val].value(answer)); //cnt[val]); } } else { // L.compute(answer); // R.compute(answer); // vector<int> one = L.getPrefCount(answer); // vector<int> two = R.getPrefCount(answer); for (int v1 = 0; v1 < (int) L.prefs.size(); v1++) { for (int v2 = 0; v2 < (int) R.prefs.size(); v2++) { markQ(need_between + v1 + v2, L.prefs[v1] * R.prefs[v2]); // mark(need_between + v1 + v2, (long long) one[v1] * two[v2]); } } // for (int x = 1; x <= blocks[i].len; x++) { // for (int y = 1; y <= blocks[j].len; y++) { // int one = max(0, x - 2) / (answer - 1); // int two = max(0, y - 2) / (answer - 1); // mark(need_between + one + two, 1); // } // } } } } total[answer] += sumQ.value(answer); prevSumQ = sumQ; } } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> A(n), B(m); total.resize(n + m + 1); for (int& x : A) cin >> x; for (int& x : B) cin >> x; solve(A, B); solve(B, A); long long sum_all = (long long) n * (n + 1) / 2 * m * (m + 1) / 2; // overflow! // debug() << imie(sum_all); for (int i = n + m; i >= 3; i--) { total[i] -= total[i-1]; sum_all -= total[i]; } total[2] = sum_all; const int MOD = 1'000'000'007; for (int i = 1; i <= n + m; i++) { cout << total[i] % MOD << " "; // cout << total[i] - total[i-1] + (i == 2 ? extra : 0LL) << " "; } cout << endl; }
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 | // [3A] Splatanie ciagow, Kamil Debowski #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } vector<long long> total; struct Q { long long a, b, c; // ax^2 + bx + c long long value(int x) const { return c + x * (b + x * a); } Q operator * (long long x) const { return Q{a * x, b * x, c * x}; } void operator *= (long long x) { a *= x; b *= x; c *= x; } void operator += (const Q& he) { a += he.a; b += he.b; c += he.c; } }; struct F { long long a, b; // a * x + b long long value(int answer) const { return a * answer + b; } void operator += (const F& he) { a += he.a; b += he.b; } void operator *= (long long x) { a *= x; b *= x; } F operator * (long long x) const { return F{a * x, b * x}; } Q operator * (const F& he) const { return Q{a * he.a, a * he.b + b * he.a, b * he.b}; } }; struct Block { int len; bool isReal; bool isFake() const { return !isReal; } vector<F> prefs; bool shouldCompute(int answer) const { int max_val = (len - 1) / (answer - 1); return (int) prefs.size() != max_val + 1; } bool compute(int answer) { int max_val = (len - 1) / (answer - 1); if((int) prefs.size() == max_val + 1) { return false; } prefs = vector<F>(max_val + 1, F{1, -1}); prefs[0] = F{1, 0}; if (max_val > 0) { prefs.back() = F{-max_val, len + max_val - 1}; } return true; } vector<int> getPrefCount(int answer) const { int max_val = (len - 1) / (answer - 1); vector<int> cnt(max_val + 1, answer - 1); cnt[0] = answer; // min(len, answer); if (max_val > 0) { cnt.back() = len - (answer) - (long long) (max_val - 1) * (answer - 1); } return cnt; } }; void solve(vector<int> a, vector<int> b) { vector<int> block_lens{1}; for (int i = 0; i + 1 < (int) a.size(); i++) { int block = 2; while (i + 2 < (int) a.size() && bool(a[i] < a[i+1]) == bool(a[i+1] < a[i+2])) { i++; block++; } block_lens.push_back(block - 1); } block_lens.push_back(1); vector<Block> blocks; for (int len : block_lens) { blocks.push_back(Block{len, true}); } Q prevSumQ{0,0,0}; for (int answer = 2; answer <= int(a.size() + b.size()); answer++) { // cerr << answer << " "; bool anyChange = false; for (const Block& block : blocks) { if (block.shouldCompute(answer)) { anyChange = true; } } Q sumQ{0, 0, 0}; auto markQ = [&](int need, Q q) { int B = b.size(); need = max(need, 1); if (need <= B) { q *= (long long) (B - need + 1) * (B - need + 2) / 2; sumQ += q; } }; auto markF = [&](int need, F f) { int B = b.size(); need = max(need, 1); if (need <= B) { f *= (long long) (B - need + 1) * (B - need + 2) / 2; // sumQ += f; sumQ.b += f.a; sumQ.c += f.b; } }; auto mark = [&](int need, int copies) { int B = b.size(); need = max(need, 1); if (need <= B) { total[answer] += (long long) copies * (B - need + 1) * (B - need + 2) / 2; } // for (int k = need; k <= B; k++) { // total[answer] += (long long) copies * (B - k + 1); // } }; if (anyChange) { vector<Block> new_blocks; // debug dd = debug(); // dd << imie(answer); for (Block block : blocks) { // dd << "(" << block.len << ", " << block.isReal << "), "; if (block.len < answer) { block.isReal = false; } if (!new_blocks.empty() && new_blocks.back().isFake() && block.isFake()) { new_blocks.back().len += block.len; } else { new_blocks.push_back(block); //Block{block.len, block.isReal}); } } // dd << "\n"; blocks = new_blocks; for (Block& block : blocks) { block.compute(answer); } } // for (Block block : blocks) { // dd << "(" << block.len << ", " << block.isReal << "), "; // } // dd << "\n"; // 1) inside every block for (int i = 0; i < (int) blocks.size(); i++) { int s = blocks[i].len; if (blocks[i].isFake()) { mark(0, (long long) s * (s - 1) / 2); // overflow! } else { vector<int> cnt = blocks[i].getPrefCount(answer); int nxt = s-1; for (int v = 0; v < (int) cnt.size(); v++) { // s-1, s-2, ... mark(v, (long long) cnt[v] * (nxt + (nxt-cnt[v]+1)) / 2); nxt -= cnt[v]; } // debug() << imie(nxt); assert(nxt == -1); // for (int d = 1; d < s; d++) { // don't skip d=1 // int needed = (d - 2) / (answer - 1); // mark(needed, s - d); // } } } // 2) pairs of blocks if (!anyChange) { sumQ = prevSumQ; } else for (int i = 0; i < (int) blocks.size(); i++) { int need_between = 0; for (int j = i + 1; j < (int) blocks.size(); j++) { if (i + 2 <= j && !blocks[j-1].isFake()) { need_between += (blocks[j-1].len - 1) / (answer - 1); } // two different blocks Block L = blocks[i]; Block R = blocks[j]; if (L.isFake() && R.isFake()) { markF(need_between, F{0, (long long) L.len * R.len}); // overflow! } else if(L.isFake()) { // R.compute(answer); for (int val = 0; val < (int) R.prefs.size(); val++) { markF(need_between + val, R.prefs[val] * L.len); } // vector<int> cnt = R.getPrefCount(answer); // for (int val = 0; val < (int) cnt.size(); val++) { // mark(need_between + val, (long long) L.len * cnt[val]); // } } else if (R.isFake()) { // L.compute(answer); // vector<int> cnt = L.getPrefCount(answer); // for (int i = 0; i < (int) cnt.size(); i++) { // debug() << imie(i) imie(cnt[i]) imie(L.prefs[i].a) imie(L.prefs[i].b) imie(cnt.size()) imie(L.prefs.size()); // assert(cnt[i] == L.prefs[i].value(answer)); // } for (int val = 0; val < (int) L.prefs.size(); val++) { markF(need_between + val, L.prefs[val] * R.len); // mark(need_between + val, (long long) R.len * L.prefs[val].value(answer)); //cnt[val]); } } else { // L.compute(answer); // R.compute(answer); // vector<int> one = L.getPrefCount(answer); // vector<int> two = R.getPrefCount(answer); for (int v1 = 0; v1 < (int) L.prefs.size(); v1++) { for (int v2 = 0; v2 < (int) R.prefs.size(); v2++) { markQ(need_between + v1 + v2, L.prefs[v1] * R.prefs[v2]); // mark(need_between + v1 + v2, (long long) one[v1] * two[v2]); } } // for (int x = 1; x <= blocks[i].len; x++) { // for (int y = 1; y <= blocks[j].len; y++) { // int one = max(0, x - 2) / (answer - 1); // int two = max(0, y - 2) / (answer - 1); // mark(need_between + one + two, 1); // } // } } } } total[answer] += sumQ.value(answer); prevSumQ = sumQ; } } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> A(n), B(m); total.resize(n + m + 1); for (int& x : A) cin >> x; for (int& x : B) cin >> x; solve(A, B); solve(B, A); long long sum_all = (long long) n * (n + 1) / 2 * m * (m + 1) / 2; // overflow! // debug() << imie(sum_all); for (int i = n + m; i >= 3; i--) { total[i] -= total[i-1]; sum_all -= total[i]; } total[2] = sum_all; const int MOD = 1'000'000'007; for (int i = 1; i <= n + m; i++) { cout << total[i] % MOD << " "; // cout << total[i] - total[i-1] + (i == 2 ? extra : 0LL) << " "; } cout << endl; } |