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
#define debug_printf(fmt, ...)
//#define debug_printf(fmt, ...) do { printf(fmt, __VA_ARGS__); } while (0)

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <climits>
#include <map>
#include <set>
#include <vector>
#include <deque>

const long long LLMAX = (std::numeric_limits<long long>()).max();
const long long LLMIN = (std::numeric_limits<long long>()).min();

///*
const int n_max = 500000;

int growth[n_max];

struct GrowthCutLess {
    GrowthCutLess(long long day_diff, long long cut, long long base_height) {
        this->day_diff = day_diff;
        this->cut = cut;
        this->base_height = base_height;
    }

    bool operator()(const int& a, const int& _) const {
        //debug_printf("GrowthCutLess %3d (%p), %3d (%p)   growth %p\n", a, &a, _, &_, growth);
        return base_height + day_diff * a < cut;
    }

private:
    long long day_diff;
    long long cut;
    long long base_height;
};

long long growth_agg_shifted_by_one[n_max + 1];

struct BaseHeight {
    int from;
    int to;
    long long day;
    long long base_height;

    static bool less(const BaseHeight* a, const BaseHeight* b) {
        return a->from < b->from;
    }
};

struct BaseHeightLess {
    bool operator()(const BaseHeight* a, const BaseHeight* b) const {
        return BaseHeight::less(a, b);
    }
};

struct BaseHeightCutLess {
    BaseHeightCutLess(long long di, long long bi) {
        this->di = di;
        this->bi = bi;
    }

    bool operator()(const BaseHeight* a, const BaseHeight* _) const {
        return a->base_height + (di - a->day) * growth[a->to] < bi;
    }

private:
    long long di;
    long long bi;
};

// struct BaseHeightPool {
//     void init(int n) {
//         for (int i=0; i < n; ++i) {
//             unallocated[i] = &buf[i];
//         }
//         unallocated_len = n;
//         unallocated_len_min = n;
//     }

//     BaseHeight* alloc() {
//         if (unallocated_len_min > unallocated_len - 1) { unallocated_len_min = unallocated_len - 1; }
//         return unallocated[--unallocated_len];
//     }

//     void free(BaseHeight* base_height) {
//         unallocated[unallocated_len++] = base_height;
//     }

//     int unallocated_len_min;

// private:
//     BaseHeight buf[n_max];
//     BaseHeight* unallocated[n_max];
//     int unallocated_len;
// } base_height_pool;

int main()
{
    std::ios_base::sync_with_stdio(false);

    int n, m; scanf("%d %d", &n, &m);
    for (int i=0; i < n; ++i) {
        int ai; scanf("%d", &ai);
        growth[i] = ai;
    }

    std::sort(growth, growth + n);

    growth_agg_shifted_by_one[0] = 0;
    for (int i=0; i < n; ++i) {
        growth_agg_shifted_by_one[i + 1] = growth_agg_shifted_by_one[i] + growth[i];
    }

    //base_height_pool.init(n);

    std::set<BaseHeight*, BaseHeightLess> base_heights;
    BaseHeight& base_height_0 = *(new BaseHeight);
    //BaseHeight& base_height_0 = *base_height_pool.alloc();
    base_height_0.from = 0;
    base_height_0.to = n - 1;
    base_height_0.day = 0;
    base_height_0.base_height = 0;
    base_heights.insert(&base_height_0);

    for (int i=0; i < m; ++i) {
        long long di, bi; scanf("%lld %lld", &di, &bi);

        // TODO Such lower_bound probably doesn't perform well.
        BaseHeightCutLess base_height_cut_less(di, bi);
        std::set<BaseHeight*, BaseHeightLess>::iterator found = std::lower_bound(base_heights.begin(), base_heights.end(), (const BaseHeight*) NULL, base_height_cut_less);
        long long weight = 0;
        if (found != base_heights.end()) {
            BaseHeight base_height = **found;

            std::vector<BaseHeight*> to_delete;
            for (std::set<BaseHeight*, BaseHeightLess>::iterator it2=found; it2 != base_heights.end(); ++it2) {
                BaseHeight& base_height2 = **it2;
                if (it2 != found) {
                    weight +=
                        (base_height2.base_height - bi) * (base_height2.to - base_height2.from + 1)
                        + (di - base_height2.day) * (
                            growth_agg_shifted_by_one[base_height2.to + 1]
                            - growth_agg_shifted_by_one[base_height2.from - 1 + 1]
                        );
                }
                to_delete.push_back(*it2);
            }
            base_heights.erase(found, base_heights.end());
            for (std::vector<BaseHeight*>::iterator it2=to_delete.begin(); it2 != to_delete.end(); ++it2) {
                delete *it2;
                //base_height_pool.free(*it2);
            }

            GrowthCutLess growth_cut_less(di - base_height.day, bi, base_height.base_height);
            int& growth_elt = *std::lower_bound(growth + base_height.from, growth + base_height.to + 1, -1, growth_cut_less);

            int from1 = base_height.from;
            int to1 = &growth_elt - growth - 1;
            if (from1 <= to1) {
                BaseHeight& base_height_1 = *(new BaseHeight);
                //BaseHeight& base_height_1 = *base_height_pool.alloc();
                base_height_1.from = from1;
                base_height_1.to = to1;
                base_height_1.day = base_height.day;
                base_height_1.base_height = base_height.base_height;
                base_heights.insert(&base_height_1);
            }

            int from2 = to1 + 1;
            BaseHeight& base_height_2 = *(new BaseHeight);
            //BaseHeight& base_height_2 = *base_height_pool.alloc();
            base_height_2.from = from2;
            base_height_2.to = n - 1;
            base_height_2.day = di;
            base_height_2.base_height = bi;
            base_heights.insert(&base_height_2);

            weight +=
                (base_height.base_height - bi) * (base_height.to - from2 + 1)
                + (di - base_height.day) * (
                    growth_agg_shifted_by_one[base_height.to + 1]
                    - growth_agg_shifted_by_one[from2 - 1 + 1]
                );
        }

        //for (std::set<BaseHeight*, BaseHeightLess>::iterator it2=base_heights.begin(); it2 != base_heights.end(); ++it2) {
        //    BaseHeight& base_height2 = **it2;
        //    debug_printf("%3d %3d %3lld %3lld\n", 1+base_height2.from, 1+base_height2.to, base_height2.day, base_height2.base_height);
        //}
        //debug_printf("\n", "");

        printf("%lld\n", weight);
    }
}
//*/



/*
const int n_max = 500000;
int growth[n_max];
long long state[n_max];

int main()
{
    std::ios_base::sync_with_stdio(false);

    int n, m; scanf("%d %d", &n, &m);
    for (int i=0; i < n; ++i) {
        int ai; scanf("%d", &ai);
        growth[i] = ai;
    }

    long long day = 0;
    for (int i=0; i < m; ++i) {
        long long di, bi; scanf("%lld %lld", &di, &bi);
        for (int j=0; j < n; ++j) {
            state[j] += (di - day) * growth[j];
            debug_printf("%3lld ", state[j]);
        }
        debug_printf("\n", "");
        long long weight = 0;
        for (int j=0; j < n; ++j) {
            if (state[j] > bi) {
                weight += state[j] - bi;
                state[j] = bi;
            }
            debug_printf("%3lld ", state[j]);
        }
        debug_printf("\n", "");
        printf("%lld\n", weight);
        day = di;
    }
}
*/