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
// Author: Kajetan Ramsza
#include "bits/stdc++.h"
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; }

void dbg_out() { cerr<<'\n'; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); }

#ifdef DEBUG
#define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 
#endif

const int inf = 1e9 + 7;

int n, m;
vi a, b;
vector<vi> dp[2][2];

void calc_dp(int start_a, int start_b, int maxi) {
	dbg(start_a, start_b, maxi);
	rep(i,0,2) rep(j,0,2) 
		rep(x,start_a,n+1) rep(y,start_b,m+1)
			dp[i][j][x][y] = inf;
	rep(i,0,2) rep(j,0,2) dp[i][j][start_a][start_b] = 0;
	rep(i,0,2) dp[0][i][start_a+1][start_b] = 1;
	rep(i,0,2) dp[1][i][start_a][start_b+1] = 1;

	rep(i,start_a,n+1) rep(j,start_b,m+1) {
		if(i < n) { [[likely]]
			if(i > start_a) { [[likely]]
				if(a[i] > a[i-1]) {
					dp[0][0][i+1][j] = min(dp[0][0][i+1][j], dp[0][0][i][j] + 1);
					if(dp[0][0][i][j] < maxi) dp[0][1][i+1][j] = 1;
				}
				else {
					dp[0][1][i+1][j] = min(dp[0][1][i+1][j], dp[0][1][i][j] + 1);
					if(dp[0][1][i][j] < maxi) dp[0][0][i+1][j] = 1;
				}
			}

			if(j > start_b) { [[likely]]
				if(a[i] > b[j-1]) {
					dp[0][0][i+1][j] = min(dp[0][0][i+1][j], dp[1][0][i][j] + 1);
					if(dp[1][0][i][j] < maxi) dp[0][1][i+1][j] = 1;
				}
				else {
					dp[0][1][i+1][j] = min(dp[0][1][i+1][j], dp[1][1][i][j] + 1);
					if(dp[1][1][i][j] < maxi) dp[0][0][i+1][j] = 1;
				}
			}
		}

		if(j < m) { [[likely]]
			if(i > start_a) { [[likely]]
				if(b[j] > a[i-1]) {
					dp[1][0][i][j+1] = min(dp[1][0][i][j+1], dp[0][0][i][j] + 1);
					if(dp[0][0][i][j] < maxi) dp[1][1][i][j+1] = 1;
				}
				else {
					dp[1][1][i][j+1] = min(dp[1][1][i][j+1], dp[0][1][i][j] + 1);
					if(dp[0][1][i][j] < maxi) dp[1][0][i][j+1] = 1;
				}
			}

			if(j > start_b) { [[likely]]
				if(b[j] > b[j-1]) {
					dp[1][0][i][j+1] = min(dp[1][0][i][j+1], dp[1][0][i][j] + 1);
					if(dp[1][0][i][j] < maxi) dp[1][1][i][j+1] = 1;
				}
				else {
					dp[1][1][i][j+1] = min(dp[1][1][i][j+1], dp[1][1][i][j] + 1);
					if(dp[1][1][i][j] < maxi) dp[1][0][i][j+1] = 1;
				}
			}
		}
	}
}

vi res;
vector<bool> vis;
vector<vi> num;

int index(int l1, int r1, int l2, int r2) {
	return l1 * n * m * m + r1 * m * m + l2 * m + r2;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin>>n>>m;
	a.resize(n);
	b.resize(m);
	rep(i,0,n) {
		cin>>a[i]; a[i]--;
	}
	rep(i,0,m) {
		cin>>b[i]; b[i]--;
	}
	res.assign(n+m+1, 0);
	vis.assign(n*n*m*m, 0);
	num.assign(n, vi(m, 0));
	rep(i,0,2) rep(j,0,2) dp[i][j].assign(n+1, vi(m+1));
	rep(i,1,n+m+1) {
		rep(ln,0,n) rep(lm,0,m) {
			if(num[ln][lm] == (n-ln) * (m-lm)) {
				continue;
			}
			calc_dp(ln,lm,i);
			rep(rn,ln,n) rep(rm,lm,m) {
				// dbg(rn, rm);
				// dbg(dp[0][0][rn+1][rm+1], dp[0][1][rn+1][rm+1]);
				// dbg(dp[1][0][rn+1][rm+1], dp[1][1][rn+1][rm+1]);
				int ind = index(ln,rn,lm,rm);
				if(!vis[ind] && (
					dp[0][0][rn+1][rm+1] <= i ||
					dp[0][1][rn+1][rm+1] <= i ||
					dp[1][0][rn+1][rm+1] <= i ||
					dp[1][1][rn+1][rm+1] <= i)) {
					
					res[i]++;
					vis[ind] = true;
					num[ln][lm]++;
				}  
			}
		}
	}
	rep(i,1,n+m+1) cout<<res[i]<<' ';
	cout<<'\n';
}