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

#define ll long long
#define fors(u, n, s) for(ll u=(s); u < (n); u++)
#define foru(u, n) fors(u, n, 0)
#define ir(a, b, x) (((a) <= (x)) && ((x) <= (b)))
#define vec vector
#define pb push_back

using namespace std;

int n;
#define K 21
#define N 100000
#define M 4000000

struct seg{
    int a;
    int b;

    bool operator<=(const seg rhs) const{
        return rhs.a<=a && rhs.b>=b;
    }

    bool operator>=(const seg rhs) const{
        return a<=rhs.a && rhs.b<=b;
    }

    bool operator==(const seg rhs) const{
        return ir(a, b, rhs.a) || ir(rhs.a, rhs.b, a);
    }

    bool operator!=(const seg rhs) const{
        return !(ir(a, b, rhs.a) || ir(rhs.a, rhs.b, a));
    }

    int to_int(){
        return b-a+1;
    }

    seg halfA(){
        int m=(a+b)/2;
        seg out;
        out.a=a;
        out.b=m;
        return out;
    }

    seg halfB(){
        int m=(a+b)/2;
        seg out;
        out.a=m+1;
        out.b=b;
        return out;
    }

    seg operator*(const seg rhs) const{
        seg out;
        out.a=max(a, rhs.a);
        out.b=min(b, rhs.b);
        return out;
    }

    bool valid(){
        return a<=b;
    }
};

seg make_seg(int a, int b){
    seg out;
    out.a=a;
    out.b=b;
    return out;
}

vec<int> type;
vec<int> addres_one;
vec<int> addres_two;
vec<int> value;


int tree_mem[8*N][K]; ///type 0
int lazy[8*N]; /// tpye 1
seg tree_seg[8*N];

void init(int pnt, seg segment){
    foru(i, K) tree_mem[pnt][i]=0;
    tree_mem[pnt][0]=segment.to_int();
    lazy[pnt]=0;
    tree_seg[pnt]=segment;

    if(segment.to_int() != 1){
        init(2*pnt, segment.halfA());
        init(2*pnt+1, segment.halfB());
    }
}

void add(int pnt, seg segment, int val){
    if(segment != tree_seg[pnt]) return ;
    if(segment >= tree_seg[pnt]){

        type.pb(1); addres_one.pb(pnt); addres_two.pb(-1); value.pb(lazy[pnt]);
        lazy[pnt] += val;

        for(int i=K-1; i>=0; i--){
            if(i-val<0) {
                type.pb(0); addres_one.pb(pnt); addres_two.pb(i); value.pb(tree_mem[pnt][i]);
                tree_mem[pnt][i]=0;
            }
            else {
                type.pb(0); addres_one.pb(pnt); addres_two.pb(i); value.pb(tree_mem[pnt][i]);
                tree_mem[pnt][i]=tree_mem[pnt][i-val];
            }
        }
        return;
    }
    add(2*pnt, segment, val);
    add(2*pnt+1, segment, val);
    if(lazy[pnt]!=0){
        add(2*pnt, make_seg(1, 2*n), lazy[pnt]);
        add(2*pnt+1, make_seg(1, 2*n), lazy[pnt]);
        type.pb(1); addres_one.pb(pnt); addres_two.pb(-1); value.pb(lazy[pnt]);
        lazy[pnt]=0;
    }
    foru(i, K) {
        type.pb(0); addres_one.pb(pnt); addres_two.pb(i); value.pb(tree_mem[pnt][i]);
        tree_mem[pnt][i]=tree_mem[2*pnt][i]+tree_mem[2*pnt+1][i];}
    return;
}

int a[N];
int b[N];

seg querry_tree[8*N];
vec<seg> recs_in[8*N];
int changes_before[8*N];

void init_querry_tree(int pnt, seg segment){
    //cout << "INIT QUERRY TREE " << pnt << " " << segment.a << " " << segment.b << endl;
    querry_tree[pnt]=segment;
    if(segment.to_int()!=1){
        init_querry_tree(2*pnt, querry_tree[pnt].halfA());
        init_querry_tree(2*pnt+1, querry_tree[pnt].halfB());
    }
}

void insert_querry(int pnt, seg x, seg y){
    if(querry_tree[pnt]<=x){
        recs_in[pnt].pb(y);
        return;
    }
    if(x!=querry_tree[pnt]) return;
    insert_querry(2*pnt, x, y);
    insert_querry(2*pnt+1, x, y);
}

void add_edge(int A, int B, int C, int D){
    /// A C
    /// B D
        /// A i B' i C'
        {seg l = make_seg(1, A);
        seg r = make_seg(A, 2*n);

        seg b_seg;
        if(B<A) b_seg=make_seg(B+1, 2*n);
        else b_seg=make_seg(1, B-1);

        seg c_seg;
        if(C<A) c_seg=make_seg(C+1, 2*n);
        else c_seg=make_seg(1, C-1);

        //add(tree, l*b_seg*c_seg, r*b_seg*c_seg, 1);
        insert_querry(1, l*b_seg*c_seg, r*b_seg*c_seg);}

        /// B i A' i D'
        {seg l = make_seg(1, B);
        seg r = make_seg(B, 2*n);

        seg a_seg;
        if(A<B) a_seg=make_seg(A+1, 2*n);
        else a_seg=make_seg(1, A-1);

        seg d_seg;
        if(D<B) d_seg=make_seg(D+1, 2*n);
        else d_seg=make_seg(1, D-1);

        //add(tree, l*a_seg*d_seg, r*a_seg*d_seg, 1);
        insert_querry(1, l*a_seg*d_seg, r*a_seg*d_seg);}

        /// A i B i C' i D'
        {seg l = make_seg(1, A) * make_seg(1, B);
        seg r = make_seg(A, 2*n) * make_seg(B, 2*n);

        bool valid=true;

        seg c_seg;
        if(C<min(A, B)) c_seg=make_seg(C+1, 2*n);
        else if(C>max(A, B)) c_seg=make_seg(1, C-1);
        else valid=false;

        seg d_seg;
        if(D<min(A, B)) d_seg=make_seg(D+1, 2*n);
        else if(D>max(A, B)) d_seg=make_seg(1 , D-1);
        else valid=false;

        //if(valid&&(l*c_seg*d_seg).valid()&&(r*c_seg*d_seg).valid()) add(tree, l*c_seg*d_seg, r*c_seg*d_seg, 1);
        if(valid&&(l*c_seg*d_seg).valid()&&(r*c_seg*d_seg).valid()) insert_querry(1, l*c_seg*d_seg, r*c_seg*d_seg);}

}

int output[K];

void dfs(int pnt){
    changes_before[pnt]=type.size();
    for(auto i : recs_in[pnt]){
        add(1, i, 1);
    }
    if(querry_tree[pnt].to_int()==1) {
            foru(i, K) output[i]+=tree_mem[1][i];
    }else{
        dfs(2*pnt); dfs(2*pnt+1);
    }
    while(type.size()!=changes_before[pnt]){
        int f = type.size()-1;
        if(type[f]==0){
            tree_mem[addres_one[f]][addres_two[f]]=value[f];
        }else if(type[f]==1){
            lazy[addres_one[f]]=value[f];
        }else{
            cout << "PANIK" << endl;
        }
        type.resize(f); addres_one.resize(f); addres_two.resize(f); value.resize(f);
    }
}

int main()
{
    cin >> n; int k; cin >> k;

    foru(i, n) cin >> a[i];
    foru(i, n) cin >> b[i];

    init_querry_tree(1, make_seg(1, 2*n));
    init(1, make_seg(1, 2*n));
    foru(i, n-1){ ///ADDING EDGES
        ///A C
        ///B D
        add_edge(a[i], b[i], a[i+1], b[i+1]);
        add_edge(a[i+1], b[i+1], a[i], b[i]);
    }
    add_edge(a[n-1], b[n-1], a[0], b[0]);
    add_edge(a[0], b[0], a[n-1], b[n-1]);

    foru(i, K) output[i]=0;

    dfs(1);

    cout << output[0] + output[2] - 4*n*n + n*(2*n+1) << " ";
    fors(i, k, 1) cout << output[2*(i+1)] << " ";

    return 0;
}