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

using namespace std;
typedef long long int64;
#define DEBUG(x) cerr << #x << " = " << x << endl;
#define DEBUGV(x) cerr << #x << " = ["; for (auto& it : x) cout << it << (&it == &x.back() ? "" : ", "); cout << "]" << endl;
#define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x)
#define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e)))
const int64 INF = 1000000000001;
const double EPS = 10e-9;

struct Request {
    int id;
    int d;
    int64 result;

    friend ostream& operator<<(ostream& os, const Request& self);
};

ostream& operator<<(ostream& os, const Request& self) {
    os << "<id=" << self.id << ", d=" << self.d << ", res=" << self.result << ">";
    return os;  
}

struct Group {
    int64 id;
    int64 first;
    int count;
    int64 mergeRound;

    friend ostream& operator<<(ostream& os, const Group& self);

    int64 begin(int64 d) {
        return first < d ? 0 : first - d;
    }

    int64 end(int64 d) {
        int64 delta = max(0ll, d - first);
        return first + delta + (int64)(count - 1) * d;
    }

    int64 delay(int64 d) {
        int64 delta = max(0ll, d - first);
        return delta + (int64)(count - 1) ;
    }

    int64 multiplier() {
        return (int64)(count - 1) * count / 2;
    }
};

ostream& operator<<(ostream& os, const Group& self) {
    os << "<id=" << self.id << ", first=" << self.first << ", count=" << self.count << ", merge=" << self.mergeRound << ">";
    return os;  
}

struct GroupCompare {
    bool operator() (const list<Group>::iterator& a, const list<Group>::iterator& b) const {
        return (a->mergeRound == b->mergeRound) ? &(*a) < &(*b) : a->mergeRound < b->mergeRound;
    }
};

struct Groups {
    list<Group> groups;
    set<list<Group>::iterator, GroupCompare> groupQueue;

    int last_d = 1;
    int64 result = 0;
    int64 multiplier = 0;

    Groups(vector<int64>& t) {
        REP(x, t.size()) {
            Group g;
            g.id = x;
            g.first = t[x];
            g.count = 1;
            if (x == t.size() - 1) {
                g.mergeRound = INF;
            } else {
                g.mergeRound = t[x + 1] - t[x];
            }
            groups.push_back(g);
            groupQueue.insert(prev(groups.end()));
            if (g.first == 0) {
                result++;
            }
        }
        merge(0);
    }

    void merge(list<Group>::iterator a, list<Group>::iterator b, int64 d) {
        groupQueue.erase(a);
        groupQueue.erase(b);

        multiplier -= a->multiplier();
        multiplier -= b->multiplier();
        int64 delta = a->end(d) - b->begin(d);
        result += delta * b->count;

        a->count = a->count + b->count;
        auto next_b = next(b);
        if (next_b == groups.end()) {
            a->mergeRound = INF;
        } else {
            int64 p = 1;
            int64 k = INF;
            while (p < k) {
                int64 s = (p + k) / 2;
                if (a->end(s) > next_b->begin(s)) {
                    k = s;
                } else {
                    p = s + 1;
                }
            }
            a->mergeRound = p - 1;
        }
        groups.erase(b);
        groupQueue.insert(a);

        multiplier += a->multiplier();
    }

    void merge(int round) {
        int d = round + 1;

        while (true) {
            auto first = groups.begin();
            auto next_first = next(first);
            if (next_first == groups.end()) {
                return;
            }
            if (first->end(d) > next_first->begin(d)) {
                merge(first, next_first, d);
            } else {
                break;
            }
        }

        while (true) {
            auto it = groupQueue.begin();
            auto g = *it;
            if (g->mergeRound > round) {
                break;
            }
            
            if (g != groups.begin()) {
                auto prev_g = prev(g);
                if (prev_g->end(d) > g->begin(d)) {
                    merge(prev_g, g, d);
                    continue;
                }
            }

            auto next_g = next(g);
            merge(g, next_g, d);
        }
    }

    void grow(int d) {
        if (d <= last_d) {
            return;
        }
        int delta = d - last_d;
        result += delta * multiplier;
        auto first = groups.begin();
        if (first->first < d) {
            result += min(d - first->first, (int64)(d - last_d)) * first->count;
        }

        last_d = d;
        merge(d - 1);
    }
};

#ifndef CATCH_TEST
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

    int n, m;
    cin >> n >> m;
    vector<int64> t(n);
    vector<Request> requests(m);
    REP(x, n) {
        cin >> t[x];
    }
    REP(x, m) {
        requests[x].id = x;
        cin >> requests[x].d;
    }
    sort(requests.begin(), requests.end(), [](const Request & a, const Request & b) -> bool { 
        return a.d < b.d;
    });

    Groups groups(t);
    for (auto& r : requests) {
        groups.grow(r.d);
        r.result = groups.result;
    }


    sort(requests.begin(), requests.end(), [](const Request & a, const Request & b) -> bool { 
        return a.id < b.id;
    });

    for (auto r : requests) {
        cout << r.result << "\n";
    }

	return 0;
}
#endif