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
#include <stdio.h>
#include <algorithm>

using namespace std;

struct Grass {
    int speed;
    long long sum;
};

struct CompareSpeed {
    bool operator()(const Grass &a, const Grass &b) {
        return a.speed < b.speed;
    }
};

class Tree {
    private:
        const static long long NO_HEIGHT = -1;

        Grass *grass;
        int start;
        int size;
        long long day;
        long long height;
        long long sum;
        Tree *left, *right;

        long long getSpeed(int start, int size) {
            return this->grass[start + size - 1].sum - (start == 0? 0 : this->grass[start - 1].sum);
        }

    public:
        Tree(int start, int size, Grass *grass) {
            this->grass = grass;
            this->start = start;
            this->size = size;
            this->day = 0;
            this->height = Tree::NO_HEIGHT;
            this->sum = 0;
            if(size == 1) {
                this->left = NULL;
                this->right == NULL;
            } else {
                this->left = new Tree(start, size / 2, grass);
                this->right = new Tree(start + size / 2, size - size / 2, grass);
            }
        }

        void addDayHeight(int start, long long day, long long height) {
            if(this->start == start) {
                this->day = day;
                this->height = height;
                this->sum = height * this->size;
            } else {
                if(start < this->left->start + this->left->size) {
                    this->right->addDayHeight(this->right->start, day, height);
                    if(this->height != Tree::NO_HEIGHT) {
                        this->left->addDayHeight(this->left->start, this->day, this->height);
                    }
                    this->left->addDayHeight(start, day, height);
                } else {
                    if(this->height != Tree::NO_HEIGHT) {
                        this->left->addDayHeight(this->left->start, this->day, this->height);
                        this->right->addDayHeight(this->right->start, this->day, this->height);
                    }
                    this->right->addDayHeight(start, day, height);
                }
                this->height = Tree::NO_HEIGHT;
                this->day = day;
                this->sum = this->left->sum + (day - this->left->day) * this->getSpeed(this->left->start, this->left->size);
                this->sum += this->right->sum + (day - this->right->day) * this->getSpeed(this->right->start, this->right->size);
            }
        }

        long long getSum(int start, long long day) {
            if(this->height != Tree::NO_HEIGHT) {
                int size = this->size - (start - this->start);
                return (day - this->day) * this->getSpeed(start, size) + this->height * size;
            }
            if(this->start == start) {
                return this->sum + (day - this->day) * this->getSpeed(this->start, this->size);
            }

            if(start < this->left->start + this->left->size) {
                return this->right->getSum(this->right->start, day) + this->left->getSum(start, day);
            }
            return this->right->getSum(start, day);
        }

        long long getHeight(int position, long long day) {
            if(this->height != Tree::NO_HEIGHT) {
                return this->height + (day - this->day) * this->grass[position].speed;
            }
            if(position < this->left->start + this->left->size) {
                return this->left->getHeight(position, day);
            }
            return this->right->getHeight(position, day);
        }
};

class CompareHeight {
    public:
        const static int SPEED_OVERWRITE = -1;

    private:
        Tree *tree;
        Grass *grass;
        long long day;
        long long overwriteHeight;

        long long getHeight(const Grass &g) {
            if(g.speed == CompareHeight::SPEED_OVERWRITE) {
                return this->overwriteHeight;
            }
            return this->tree->getHeight(&g - grass, this->day);
        }

    public:
        CompareHeight(Tree *tree, Grass *grass, long long day, long long overwriteHeight) {
            this->tree = tree;
            this->grass = grass;
            this->day = day;
            this->overwriteHeight = overwriteHeight;
        }

        bool operator()(Grass &a, const Grass &b) {
            return this->getHeight(a) < this->getHeight(b);
        }
};

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    Grass *grass = new Grass[n];
    for(int i = 0; i < n; ++i) {
        scanf("%d", &grass[i].speed);
    }
    sort(grass, grass + n, CompareSpeed());

    grass[0].sum = grass[0].speed;
    for(int i = 1; i < n; ++i) {
        grass[i].sum = grass[i - 1].sum + grass[i].speed;
    }
    Tree *tree = new Tree(0, n, grass);
    tree->addDayHeight(0, 0, 0);

    for(int i = 0; i < m; ++i) {
        long long d, b;
        scanf("%lld %lld", &d, &b);
        Grass find = {
            speed: CompareHeight::SPEED_OVERWRITE,
        };
        Grass *start = lower_bound(grass, grass + n, find, CompareHeight(tree, grass, d, b));
        if(start == grass + n) {
            printf("0\n");
        } else {
            int position = start - grass;
            printf("%lld\n", tree->getSum(position, d) - (n - position) * b);
            tree->addDayHeight(position, d, b);
        }
    }

    return 0;
}