#include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) (int((x).size())) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define gc getchar_unlocked #define inf 1000000000 using namespace std; void wczytaj(int &a){ int c = gc(); while(c < '0' || c > '9') c = gc(); for(a = 0; c >= '0' && c <= '9'; c = gc()) a = 10*a+c-'0'; } struct info{ short x, y, kona, konb; info(short a, short b, short c, short d){ x = a, y = b, kona = c, konb = d; } }; void solve(){ int n, m; wczytaj(n), wczytaj(m); vector<int> a(n+1), b(m+1); FOR(i, 1, n) wczytaj(a[i]); FOR(i, 1, m) wczytaj(b[i]); vector<int> kub(n+m+1, 0); vector dp(2, vector(2, vector(n+1, vector(m+1, short(n+m+5)))));// [pra_ostatni][ostatni][kona][konb] vector odw(n+1, vector(m+1, false)); FOR(poca, 1, n) FOR(pocb, 1, m){ FOR(kona, poca-1, n) FOR(konb, pocb-1, m) odw[kona][konb] = 0, dp[0][0][kona][konb] = dp[0][1][kona][konb] = dp[1][0][kona][konb] = dp[1][1][kona][konb] = short(n+m+5); dp[0][0][poca-1][pocb-1] = 0; queue<info> vec; vec.emplace(0, 0, poca-1, pocb-1); FOR(limit, 1, n+m){ queue<info> q; swap(q, vec); while(ssize(q)){ auto [x, y, kona, konb] = q.front(); q.pop(); if(dp[x][y][kona][konb] > limit){ vec.emplace(x, y, kona, konb); continue; } if(!odw[kona][konb]){ odw[kona][konb] = 1; if(kona>=poca && konb>=pocb) ++kub[limit]; } int ost = -1, pra = -1; if(!y){ if(kona>=poca) ost = a[kona]; if(!x){if(kona-1>=poca) pra = a[kona-1];} else if(konb>=pocb) pra = b[konb]; } else{ if(konb>=pocb) ost = b[konb]; if(!x){if(kona>=poca) pra = a[kona];} else if(konb-1>=pocb) pra = b[konb-1]; } if(kona < n){ short tmp = dp[x][y][kona][konb]+1; if(pra < ost && ost > a[kona+1]) tmp = 2; if(pra > ost && ost < a[kona+1]) tmp = 2; short &dokad = dp[y][0][kona+1][konb]; if(tmp < dokad) dokad = tmp, q.emplace(y, 0, kona+1, konb); } if(konb < m){ short tmp = dp[x][y][kona][konb]+1; if(pra < ost && ost > b[konb+1]) tmp = 2; if(pra > ost && ost < b[konb+1]) tmp = 2; short &dokad = dp[y][1][kona][konb+1]; if(tmp < dokad) dokad = tmp, q.emplace(y, 1, kona, konb+1); } } } } FOR(i, 1, n+m) printf("%d ", kub[i]); } int main(){ solve(); 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 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) (int((x).size())) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define gc getchar_unlocked #define inf 1000000000 using namespace std; void wczytaj(int &a){ int c = gc(); while(c < '0' || c > '9') c = gc(); for(a = 0; c >= '0' && c <= '9'; c = gc()) a = 10*a+c-'0'; } struct info{ short x, y, kona, konb; info(short a, short b, short c, short d){ x = a, y = b, kona = c, konb = d; } }; void solve(){ int n, m; wczytaj(n), wczytaj(m); vector<int> a(n+1), b(m+1); FOR(i, 1, n) wczytaj(a[i]); FOR(i, 1, m) wczytaj(b[i]); vector<int> kub(n+m+1, 0); vector dp(2, vector(2, vector(n+1, vector(m+1, short(n+m+5)))));// [pra_ostatni][ostatni][kona][konb] vector odw(n+1, vector(m+1, false)); FOR(poca, 1, n) FOR(pocb, 1, m){ FOR(kona, poca-1, n) FOR(konb, pocb-1, m) odw[kona][konb] = 0, dp[0][0][kona][konb] = dp[0][1][kona][konb] = dp[1][0][kona][konb] = dp[1][1][kona][konb] = short(n+m+5); dp[0][0][poca-1][pocb-1] = 0; queue<info> vec; vec.emplace(0, 0, poca-1, pocb-1); FOR(limit, 1, n+m){ queue<info> q; swap(q, vec); while(ssize(q)){ auto [x, y, kona, konb] = q.front(); q.pop(); if(dp[x][y][kona][konb] > limit){ vec.emplace(x, y, kona, konb); continue; } if(!odw[kona][konb]){ odw[kona][konb] = 1; if(kona>=poca && konb>=pocb) ++kub[limit]; } int ost = -1, pra = -1; if(!y){ if(kona>=poca) ost = a[kona]; if(!x){if(kona-1>=poca) pra = a[kona-1];} else if(konb>=pocb) pra = b[konb]; } else{ if(konb>=pocb) ost = b[konb]; if(!x){if(kona>=poca) pra = a[kona];} else if(konb-1>=pocb) pra = b[konb-1]; } if(kona < n){ short tmp = dp[x][y][kona][konb]+1; if(pra < ost && ost > a[kona+1]) tmp = 2; if(pra > ost && ost < a[kona+1]) tmp = 2; short &dokad = dp[y][0][kona+1][konb]; if(tmp < dokad) dokad = tmp, q.emplace(y, 0, kona+1, konb); } if(konb < m){ short tmp = dp[x][y][kona][konb]+1; if(pra < ost && ost > b[konb+1]) tmp = 2; if(pra > ost && ost < b[konb+1]) tmp = 2; short &dokad = dp[y][1][kona][konb+1]; if(tmp < dokad) dokad = tmp, q.emplace(y, 1, kona, konb+1); } } } } FOR(i, 1, n+m) printf("%d ", kub[i]); } int main(){ solve(); return 0; } |