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

#define ll long long
#define ve vector
#define fi first
#define se second
#define ld double
#define all(x) x.begin(), x.end()

using namespace std;

typedef pair<int, int> pii;

const int MAXN = 1e5 + 10;
const int K = 1000;

struct decomp
{
    ve<ve<int>> blocks;
    int sz = 0;
    void push_back(int x)
    {
        blocks.push_back({x});
        sz++;
        if (blocks.size() > 100)
            rebuild();
    }
    void rebuild()
    {
        ve<int> temp;
        int j = 0;
        ve<int> all;
        for (auto &i : blocks)
            for (auto j : i)
                all.push_back(j);
        blocks.clear();
        blocks.resize(all.size() / K + 1);
        for (int i = 0; i < all.size(); i++)
            blocks[i / K].push_back(all[i]);
        for (auto &i : blocks)
            sort(all(i));
    }
    decomp split(int x)
    {
        decomp res;
        for (auto &i : blocks)
        {
            if (!i.empty() && i.back() >= x)
            {
                auto it = upper_bound(all(i), x);
                res.blocks.push_back(ve<int>(it, i.end()));
                res.sz += res.blocks.back().size();
                i.erase(it, i.end());
                sz -= res.blocks.back().size();
            }
        }
        return res;
    }
    int cnt(int x)
    {
        int res = 0;
        for (auto &i : blocks)
        {
            res += upper_bound(all(i), x) - i.begin();
        }
        return res;
    }
    void merge(decomp x)
    {
        for (auto &i : x.blocks)
        {
            if(i.size()){
            blocks.push_back(i);
            sz += i.size();
            }
        }
        if (blocks.size() > 100)
            rebuild();
    }
    void clear()
    {
        sz = 0;
        blocks.clear();
    }
    void swap(decomp &x)
    {
        blocks.swap(x.blocks);
        std::swap(sz, x.sz);
    }
};

decomp t[65][130];

void solve()
{
    int n, m;
    cin >> n >> m;
    ve<int> v(n);
    for (auto &i : v)
    {
        cin >> i;
    }
    reverse(all(v));
    int sz = 0;
    ve<int> res(n);
    int lstans = 0;
    for (auto it : v)
    {
        int cm = sz++ / 2;
        for (int i = 1; i <= 64; i++)
        {
            cm -= t[i][0].sz;
        }
        int rsum = 0;
        for (int sum = 1; sum <= 64 && cm > 0; sum++)
        {
            int csum = 0;
            for (int i = 1; i <= sum; i++)
                csum += t[i][sum].sz;
            if (csum >= cm)
            {
                rsum = sum;
                break;
            }
            else
                cm -= csum;
        }
        int l = -1, r = sz - 1, opt = -1;
        if (cm > 0)
        {
            while (l <= r)
            {
                int mid = (l + r + 1) >> 1;
                int cnt = 0;
                for (int i = 1; i <= rsum; i++)
                {
                    cnt += t[i][rsum].cnt(mid);
                }
                if (cnt == cm)
                    opt = mid;
                if (cnt <= cm)
                {
                    l = mid + 1;
                }
                else
                    r = mid - 1;
            }
        }
        l = opt;
        int ans = max(0, cm) * rsum;
        for (int i = 1; i <= 64; i++)
            for (int j = 1; j < rsum; j++)
            {
                ans += j * t[i][j].sz;
                if (sz == n)
                    for (auto &k : t[i][j].blocks)
                        for (auto &kt : k)
                            res[kt] = j;
            }
        ans = m - ans;
        res[sz - 1] = max(-1, ans);
        if (ans < 0)
        {
            t[it][0].push_back(sz - 1);
            continue;
        }
        for (int i = 1; i <= 64; i++)
        {
            if (rsum > 0)
                t[i][0].merge(t[i][rsum].split(l));
            for (int j = 129; j > rsum; j--)
            {
                t[i][0].merge(t[i][j]);
                t[i][j].clear();
            }
            for (int j = 129 - i; j >= 0; j--)
            {
                t[i][j].swap(t[i][j + i]);
            }
        }
        t[it][min(129, ans + it)].push_back(sz - 1);
    }
    for (int i = 1; i <= 64; i++)
        for (int j = 1; j <= 128; j++)
            for (auto k : t[i][j].blocks)
                for (auto kt : k)
                    res[kt] = j - i;
    reverse(all(res));
    for (auto &i : res)
        cout << i << " ";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    //    cin >> T;
    while (T--)
        solve();
}