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
#include<bits/stdc++.h>

using namespace std;

int n,m;
int a[300010];
int b[300010];
int ans[600010];
int tans = 1e9;

void frek(int pa, int pb, int ka, int kb, int last_dig, int last_step, int streak, int frans){
    if(frans >= tans) return;
    if(pa > ka && pb > kb){
        // if(frans == 1){
        //     cout << "XD" << endl;
        // }
        tans = min(tans,frans);
        return;
    }
    int new_step, new_streak;
    if(last_step == 0){
        if(pa <= ka){
            if(last_dig < a[pa]){
                new_streak = streak + 1;
                new_step = 1;
            }
            if(last_dig == a[pa]){
                new_streak = 1;
                new_step = 0;
            }
            if(last_dig > a[pa]){
                new_streak = streak + 1;
                new_step = -1;
            }
            frek(pa+1,pb,ka,kb,a[pa],new_step,new_streak,max(frans,new_streak));
        }
        if(pb <= kb){
            if(last_dig < b[pb]){
                new_streak = streak + 1;
                new_step = 1;
            }
            if(last_dig == b[pb]){
                new_streak = 1;
                new_step = 0;
            }
            if(last_dig > b[pb]){
                new_streak = streak + 1;
                new_step = -1;
            }
            frek(pa,pb+1,ka,kb,b[pb],new_step,new_streak,max(frans,new_streak));
        }
    }
    if(last_step == 1){
        if(pa <= ka){
            if(last_dig < a[pa]){
                new_streak = streak + 1;
                new_step = 1;
            }
            if(last_dig == a[pa]){
                new_streak = 1;
                new_step = 0;
            }
            if(last_dig > a[pa]){
                new_streak = 2;
                new_step = -1;
            }
            frek(pa+1,pb,ka,kb,a[pa],new_step,new_streak,max(frans,new_streak));
        }
        if(pb <= kb){
            if(last_dig < b[pb]){
                new_streak = streak + 1;
                new_step = 1;
            }
            if(last_dig == b[pb]){
                new_streak = 1;
                new_step = 0;
            }
            if(last_dig > b[pb]){
                new_streak = 2;
                new_step = -1;
            }
            frek(pa,pb+1,ka,kb,b[pb],new_step,new_streak,max(frans,new_streak));
        }
    }
    if(last_step == -1){
        if(pa <= ka){
            if(last_dig < a[pa]){
                new_streak = 2;
                new_step = 1;
            }
            if(last_dig == a[pa]){
                new_streak = 1;
                new_step = 0;
            }
            if(last_dig > a[pa]){
                new_streak = streak + 1;
                new_step = -1;
            }
            frek(pa+1,pb,ka,kb,a[pa],new_step,new_streak,max(frans,new_streak));
        }
        if(pb <= kb){
            if(last_dig < b[pb]){
                new_streak = 2;
                new_step = 1;
            }
            if(last_dig == b[pb]){
                new_streak = 1;
                new_step = 0;
            }
            if(last_dig > b[pb]){
                new_streak = streak + 1;
                new_step = -1;
            }
            frek(pa,pb+1,ka,kb,b[pb],new_step,new_streak,max(frans,new_streak));
        }
    }
}

int f(int pa, int ka, int pb, int kb){
    tans = 1e9;
    // cout << pa << " - " << ka << "    " << pb << " - " << kb << endl;
    frek(pa+1,pb,ka,kb,a[pa],0,1,1);
    frek(pa,pb+1,ka,kb,b[pb],0,1,1);
    return tans;
}

int main(){
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
    
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }

    for(int i = 0; i < m; i++){
        cin >> b[i];
    }

    for(int pa = 0; pa < n; pa++){
        for(int ka = pa; ka < n; ka++){
            for(int pb = 0; pb < m; pb++){
                for(int kb = pb; kb < m; kb++){
                    ans[f(pa,ka,pb,kb)]++;
                }
            }
        }
    }

    for(int i = 1; i <= n + m; i++){
        cout << ans[i] << " ";
    }
    cout << "\n";
    return 0;
}