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
#include <cstdio>
#include <iostream>
#include <set>
#include <deque>
#include <climits>
#include <algorithm>


using namespace std;

int n, m;
long long t[200000];
long long currentTime;

#define max(a,b) (((a)>(b)?(a):(b)))

struct TE {
    long long capacity;
    long long t0;
    long long burnout;
    int ix;

    long long t() const{
        return capacity < 0 ? t0 : t0+capacity/burnout + 1;
    }

};

struct TESimpleCompare{
    bool operator() (const TE* lhs, const TE* rhs) const {
        return lhs->ix < rhs->ix;
    }
}; 

struct TECompare{
    bool operator() (const TE* lhs, const TE* rhs) const {
        long long tl = lhs->t(), tr = rhs->t();
        return tl<tr || (tl == tr && lhs->ix < rhs->ix);
    }
}; 

struct X{
    long long prepTime;
    long long val;
    long long multiplicator;
    X(long long prepTime, long long val, long long multiplicator) : prepTime(prepTime), val(val), multiplicator(multiplicator) {}
    X(){}
};

typedef set<TE*, TECompare> TSET;
typedef set<TE*, TESimpleCompare> TISET;


TE allTE[200000];
TSET s;
TISET ixSet;
deque<X> result;


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

    sort(t, t+n);
/*
for(int i = 0; i < n; i++){
    printf("%lli ", t[i]);
}
printf("\n");
*/

    for(int i = 0; i < n; i++){
        allTE[i].capacity = i == 0 ? t[i] : t[i]-t[i-1];
        allTE[i].t0 = 0;
        allTE[i].burnout = 1;
        allTE[i].ix = i;
        s.insert(allTE+i);
        ixSet.insert(allTE+i);
    }

    result.push_back(X(-1, 0, 0));

    while(!s.empty()){
// for(TSET::iterator it = s.begin(); it != s.end(); it++) printf("(ix:%i c:%lli b:%i t0:%lli t:%lli)  ", (*it)->ix, (*it)->capacity, (*it)->burnout, (*it)->t0, (*it)->t()); printf("\n");
// for(TISET::iterator it = ixSet.begin(); it != ixSet.end(); it++) printf("(ix:%i c:%lli b:%i t0:%lli t:%lli)  ", (*it)->ix, (*it)->capacity, (*it)->burnout, (*it)->t0, (*it)->t()); printf("\n");

        TSET::iterator first = s.begin();
        TE te = **first;
        s.erase(first);
        
        X prev  = *(result.end()-1);
        currentTime = te.t();
        if(prev.prepTime < currentTime){
            X x;
            x.prepTime = currentTime;
            x.val = prev.val + (x.prepTime-prev.prepTime)*prev.multiplicator;
            x.multiplicator = prev.multiplicator;
            result.push_back(x);
        }

        X& last = *(result.end()-1);
        
        te.capacity -= (last.prepTime - te.t0)*te.burnout;        

        TISET::iterator current = ixSet.find(&te);
        TISET::iterator next = ++current;
        if(next != ixSet.end())
        {
            TE& nextItem = **next;
            s.erase(&nextItem);

            long long newt0 = currentTime;
            nextItem.capacity -= (newt0-nextItem.t0)*nextItem.burnout;
            nextItem.t0 = newt0;
            nextItem.burnout += te.burnout;
            
            nextItem.capacity += te.capacity;
            s.insert(&nextItem);

            last.val -= te.capacity*(nextItem.ix-te.ix);
            last.multiplicator += te.burnout*(nextItem.ix-te.ix);
        } else {
            last.val -= te.capacity*(n-te.ix);
            last.multiplicator += te.burnout*(n-te.ix);
        }
        ixSet.erase(--current);

// for(deque<X>::iterator it = result.begin(); it != result.end(); it++) printf("(t:%i val:%lli m:%i)  ", it->prepTime, it->val, it->multiplicator); printf("\n");
// printf("\n");
        
// printf("(ix=%i cap=%lli) ", te.ix, te.capacity);
    }
// printf("\n");
/*
for(deque<X>::iterator it = result.begin(); it < result.end(); it++){
    printf("(prep=%i value=%lli mult=%i) ", it->prepTime, it->val, it->multiplicator);
}
printf("\n%i\n", (-1)/2);
*/

    result.push_back(X(INT_MAX, 0, 0));
    for(int i = 0; i < m; i++){
        long long d;
        cin>>d;
        int left = 0;
        int right = result.size()-2;
        while(left <= right){
            int mid = (left+right)/2;
            if(result[mid].prepTime <= d && result[mid+1].prepTime > d){
                cout<<result[mid].val + (d-result[mid].prepTime)*result[mid].multiplicator<<"\n";
                break;
            } else if( d < result[mid].prepTime){
                right = mid-1;
            } else
                left = mid + 1;
        }
    }

    return 0;
}