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
263
264
265
266
267
268
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
#include <list>
#include <cassert>
#include <sstream>

#include <string>

#ifndef DEBUG
#define DEBUG 0
#endif
#define dprint(fmt, ...) do { if (DEBUG) printf(fmt, ##__VA_ARGS__); } while (0)

using namespace std;

typedef long long LL;

struct Upcoming_merge;

/**
 * Klienci dla ktorych zapiekanki beda pieczone jedna po drugiej sa grupowani.
 */
struct Group {
    LL k;
    LL wait;
    LL wait_updated_at_d;
    LL t_first; // czas kiedy przychodzi pierwszy klient z tej grupy
    LL t_last; // kiedy przychodzi ostatni

    set<Upcoming_merge>::iterator my_merge;

    Group () {}
    Group (LL k_, LL wait_, LL tf, LL tl) : k(k_), wait(wait_), wait_updated_at_d(0), t_first(tf), t_last(tl) {}

    string to_string() const {
        stringstream ss;
        ss << k << " " << wait << " (at " << wait_updated_at_d << ") " << t_first << ".." << t_last;
        return ss.str();
    }

    LL k2() const {
        if (k & 1L) {
            return (k - 1L) / 2L * k;
        }
        return k / 2L * (k - 1L);
    }

    LL d_first(LL d) {
        return t_first - d;
    }
    LL d_last(LL d) {
        return t_first + (k - 1L) * d;
    }
};

// Grupy moga sie laczyc przy dostatecznie duzej zmianie d. Chemy wiedziec kiedy to bedzie.
struct Upcoming_merge {
    LL at_d;
    // wskaznik do wczesniejszej grupy
    list<Group>::iterator group;

    Upcoming_merge(LL at_d_, list<Group>::iterator group_) : at_d(at_d_), group(group_) {}

    string to_string() const {
        stringstream ss;
        ss << "m at " << at_d << " t0 " << group->t_first;
        return ss.str();
    }

    bool operator<(const Upcoming_merge& other) const {
        if (at_d != other.at_d) {
            return at_d < other.at_d;
        }
        return group->t_first < other.group->t_first;
    }
};

struct World {
    list<Group> groups;
    set<Upcoming_merge> merges;

    LL sum_k2; // suma po k(k+1)/2 po kazdej grupie
    LL res;
    LL d; // czas pieczenia w piekarniku

    LL next_merge() const {
        if (merges.empty()) {
            return 9223372036854775807L;
        } else {
            return merges.cbegin()->at_d;
        }
    }

    void print() {
        dprint("res %Ld sum_k2 %Ld d %Ld\n", res, sum_k2, d);
        dprint("groups: (%lu)\n", groups.size());
        for (auto& group : groups) {
            dprint("%s\n", group.to_string().c_str());
        }
        dprint("merges: (%lu)\n", merges.size());
        for (auto& merge : merges) {
            dprint("%s\n", merge.to_string().c_str());
        }
        dprint("---\n");
    }

    set<Upcoming_merge>::iterator upcoming_merge_with_next(list<Group>::iterator it) {
        if (it == --(groups.end())) {
            return merges.end();
        }
        list<Group>::iterator next = it;
        ++next;

        LL k = it->k;
        LL next_merge_at = (next->t_first - it->t_first + k - 1) / k;

        dprint("a merge between %s and %s will be at %Ld\n", it->to_string().c_str(), next->to_string().c_str(), next_merge_at);

        return merges.insert(Upcoming_merge(next_merge_at, it)).first;
    }

    LL merge_with_next(list<Group>::iterator it, LL dd) {
        list<Group>::iterator next_it = it;
        ++next_it;
        assert (it != groups.end());
        assert (next_it != groups.end());
        assert (it->my_merge != merges.end());

        dprint("merging %s with %s\n", it->to_string().c_str(), next_it->to_string().c_str());

        LL rv = it->k2() + next_it->k2();
        LL rv_without_updated_at_this_d = (it->wait_updated_at_d != d + dd ? it->k2() : 0) + next_it->k2();

        dprint("rv %Ld rv without %Ld\n", rv, rv_without_updated_at_this_d);

        Group next = *next_it;

        dprint("res bef %Ld\n", res);
        res -= it->wait + it->k2() * (max(LL(0), d - it->wait_updated_at_d));
        dprint("res mid %Ld\n", res);
        res -= next_it->wait + next_it->k2() * (max(LL(0), d - next_it->wait_updated_at_d));
        dprint("res aft %Ld\n", res);

        // uwzglednij wszystkie zmiany implicite
        it->wait += it->k2() * (d + dd - it->wait_updated_at_d);
        next_it->wait += next_it->k2() * (d + dd - next_it->wait_updated_at_d);


        dprint("waits now %Ld %Ld\n", it->wait, next_it->wait);

        LL offset = it->d_last(d + dd) - next_it->d_first(d + dd);
        assert (offset >= 0);
        next_it->wait += next_it->k * offset;

        dprint("after offset next waits now %Ld\n", next_it->wait);

        it->k += next_it->k;
        it->wait += next_it->wait;
        it->t_last = next_it->t_last;
        it->wait_updated_at_d = d + dd;

        sum_k2 -= rv;
        sum_k2 += it->k2();
        res += it->wait;

        if (next_it->my_merge != merges.end()) {
            merges.erase(next_it->my_merge);
        }
        merges.erase(it->my_merge);
        groups.erase(next_it);

        // nowy merge
        it->my_merge = upcoming_merge_with_next(it);
        return rv_without_updated_at_this_d;
    }

    // roznica w d
    void update(LL dd) {
        if (dd == 0) {
            dprint("SKIPPING\n");
            return;
        }
        dprint("UPDATING by %Ld to %Ld\n", dd, d + dd);
//         fprintf(stderr, "here\n");
//         fflush(stderr);
        LL affected = sum_k2;

        while (!merges.empty() && merges.cbegin()->at_d <= d + dd) {
            affected -= merge_with_next(merges.cbegin()->group, dd);

//             dprint("after update:\n");
//             print();
        }
        res += affected * dd;
        d += dd;
        dprint("affected: %Ld new res %Ld new d %Ld\n", affected, res, d);
        dprint("\n");
    }
};

struct Query {
    LL val;
    LL result;
    int pos;
};

bool by_val(const Query& q1, const Query& q2) {
    return q1.val < q2.val;
}

bool by_pos(const Query& q1, const Query& q2) {
    return q1.pos < q2.pos;
}

int main() {
    World world;
    int n, m;
    scanf("%d%d", &n, &m);
    world.groups.emplace_back(1, 0, 0, 0);
    for (int i = 0; i < n; ++i) {
        LL t;
        scanf("%Ld", &t);
        if (t == world.groups.back().t_first) {
            ++world.groups.back().k;
        } else {
            world.groups.emplace_back(1, 0, t, t);
        }
    }
    world.res = 0;
    world.sum_k2 = 0;
    for (const auto& g : world.groups) {
        world.sum_k2 += g.k2();
    }
    world.d = 0;

    for (list<Group>::iterator it = world.groups.begin(); it != world.groups.end(); ++it) {
        it->my_merge = world.upcoming_merge_with_next(it);
    }

    world.print();

    vector<Query> ds(m);
    for (int i = 0; i < m; ++i) {
        scanf("%Ld", &ds[i].val);
        ds[i].pos = i;
    }
    sort(ds.begin(), ds.end(), by_val);
    LL last = 0;
    for (int i = 0; i < m; ++i) {
        LL next_merge = world.next_merge();
        dprint("next_merge: %Ld\n", next_merge);
        if (ds[i].val <= next_merge) {
            world.update(ds[i].val - last);
            ds[i].result = world.res;
            last = ds[i].val;
        } else {
            world.update(next_merge - last);
            last = next_merge;
            --i;
        }
    }
    sort(ds.begin(), ds.end(), by_pos);
    for (int i = 0; i < m; ++i) {
        printf("%Ld\n", ds[i].result);
    }
}