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
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define PPC(x) __builtin_popcountll(x)
#define MSB(x) (63 - __builtin_clzll(x))
#define LSB(x) __builtin_ctzll(x)
#define ARG(x, i) (get<i>(x))
#define ithBit(m, i) ((m) >> (i) & 1)
#define pb push_back
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#define CLR(x) x.clear(), x.shrink_to_fit()
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;
 
template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b);	}
template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b);	}

const int maxN = 300'003, INF = maxN * 2;

vector <vector <int> > dpLeft[2], dpRight[2];

int k;

void initDp(int i, int j)
{
	dpLeft[0][i][j] = dpLeft[1][i][j] = dpRight[0][i][j] = dpRight[1][i][j] = INF;
}

void upd(int& state, vector <vector <int> > dp[2], bool rel, int i, int j)
{
	if (dp[rel][i][j] < k)
		remin(state, dp[rel][i][j] + 1);
	if (dp[!rel][i][j] < INF)
		remin(state, 2);
}

bool check(int* A, int n, int* B, int m)
{
	initDp(0, 0), initDp(0, 1), initDp(1, 0);
	dpLeft[0][1][0] = dpLeft[1][1][0] = 1;
	dpRight[0][0][1] = dpRight[1][0][1] = 1;

	FOR(i, 2, n+1)
	{
		initDp(i, 0);

		bool rel = A[i-1] < A[i];
		auto& state = dpLeft[rel][i][0];
		upd(state, dpLeft, rel, i-1, 0);
	}

	FOR(i, 2, m+1)
	{
		initDp(0, i);

		bool rel = B[i-1] < B[i];
		auto& state = dpRight[rel][0][i];
		upd(state, dpRight, rel, 0, i-1);
	}

	FOR(i, 1, n+1) FOR(j, 1, m+1)
	{
		int a = A[i], b = B[j];
		int aprev = A[i-1], bprev = B[j-1];

		initDp(i, j);

		bool rel = aprev < a;
		auto& state0 = dpLeft[rel][i][j];
		upd(state0, dpLeft, rel, i-1, j);

		rel = b < a;
		auto& state1 = dpLeft[rel][i][j];
		upd(state1, dpRight, rel, i-1, j);

		rel = a < b;
		auto& state2 = dpRight[rel][i][j];
		upd(state2, dpLeft, rel, i, j-1);

		rel = bprev < b;
		auto& state3 = dpRight[rel][i][j];
		upd(state3, dpRight, rel, i, j-1);
	}

	return min({dpLeft[0][n][m], dpLeft[1][n][m], dpRight[0][n][m], dpRight[1][n][m]}) < INF;
}

int f(int* A, int n, int* B, int m)
{
	int p = 1, q = n+m;
	while (q - p > 1)
	{
		k = (p + q) / 2;
		(check(A, n, B, m) ? q : p) = k;
	}
	return q;
}

int A[maxN], B[maxN];
long long res[maxN * 2];

void solve()
{
	int n, m;
	scanf ("%d%d", &n, &m);
	FOR(i, 1, n+1)
		scanf ("%d", A+i);
	FOR(i, 1, m+1)
		scanf ("%d", B+i);

	FOR(s, 0, 2)
	{
		dpLeft[s].resize(n+1, vector <int> (m + 1));
		dpRight[s].resize(n+1, vector <int> (m + 1));
	}

	FOR(i, 1, n+1) FOR(j, i, n+1) FOR(p, 1, m+1) FOR(q, p, m+1)
		res[f(A + i-1, j-i+1, B + p-1, q-p+1)]++;

	FOR(i, 1, n+m+1)
		printf("%lld ", res[i]);
	printf("\n");
}

int main()
{
	int t = 1;
//	scanf ("%d", &t);	
	FOR(tid, 1, t+1)
	{
		//printf("Case #%d: ", tid);
		solve();
	}
	return 0;
}