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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <iostream>
#include <cstdio>
#include <algorithm> 
#include <assert.h>

using namespace std;

int speed[500001]; //grass speed
long long day[500001]; //days
long long height[500001]; //height

struct node {
  long long total_speed;
  int total_count;
  long long total_sum;
  long long height;
  long long cut_day;
  int min, max;
  bool is_valid_height;
};

class Tree {
  public:
  
  node* nodes;
  long long node_count;
  long long fields;
  int *speed;
  
  Tree(long long node_count, int *speed, long long len) {
    this->node_count = node_count;
    this->fields = len;
    this->speed = speed;
    
    this->nodes = new node[2 * node_count];
    for(int i = 0; i < node_count; i++) this->nodes[i].min = 99999999;
    for(int i = 1; i < node_count; i++)
    {
      this->nodes[i + node_count].min = i;
      this->nodes[i + node_count].max = i;
    }
    for(int i = 1; i < node_count; i++) init(i);
    for(int i = 0; i < len; i++) {
      this->put(speed[i], speed[i]);
    }
  }
  
  void init(long long number) {
    long long nr = node_count + number;
    do {
      if (this->nodes[nr].max < number) this->nodes[nr].max = number;
      if (this->nodes[nr].min > number) this->nodes[nr].min = number;
      nr = nr/2;
    } while (nr > 0);
  }
  
  void put(long long speed, int number) {
    long long nr = node_count + number;
    this->nodes[nr].is_valid_height = true;
    do {
      this->nodes[nr].total_speed += speed;
      this->nodes[nr].total_sum = 0;
      this->nodes[nr].cut_day = 0;
      this->nodes[nr].total_count += 1L;
      this->nodes[nr].height = 0;
      nr = nr/2;
      this->nodes[nr].is_valid_height = false;
    } while (nr > 0);
  }
  
  void cut(long long low, long long height, long long day) {
    long long nr = 1; 

    while (nr != node_count + low) {
      if (this->nodes[nr].is_valid_height) {
        this->nodes[nr].is_valid_height = false;
        this->nodes[nr * 2 + 1].is_valid_height = true;
        this->nodes[nr * 2 + 1].height = this->nodes[nr].height;
        this->nodes[nr * 2 + 1].cut_day = this->nodes[nr].cut_day;
        
        
        this->nodes[nr * 2].is_valid_height = true;
        this->nodes[nr * 2].height = this->nodes[nr].height;
        this->nodes[nr * 2].cut_day = this->nodes[nr].cut_day;
      }

      if (this->nodes[nr].min == low) {
        this->nodes[nr].is_valid_height = true;
        this->nodes[nr].height = height;
        this->nodes[nr].cut_day = day; 
        break;
      } else if (this->nodes[nr * 2].max >= low) {
        nr = nr * 2;
        this->nodes[nr + 1].is_valid_height = true;
        this->nodes[nr + 1].height = height;
        this->nodes[nr + 1].cut_day = day; 
      } else {
        nr = nr * 2 + 1;  
      }
    }
    if (nr == node_count + low) {
      this->nodes[nr].is_valid_height = true;
      this->nodes[nr].height = height;
      this->nodes[nr].cut_day = day; 
    }
    
  }
  
  node get_height(int number) {
    long long nr = node_count + number; 
    node h_node = this->nodes[nr];
    do {
      if (this->nodes[nr].is_valid_height) {
        h_node = this->nodes[nr];
      }
      //printf("%d %lld %d\n", number, this->nodes[nr].height, this->nodes[nr].is_valid_height);
      nr = nr/2;
    } while (nr > 0);
    return h_node;
  }
  
  long long find_number(long long day, long long height) {
    long long begin = 0; 
    long long end = this->fields-1;
    long long mid = (begin + end)/2;
    long long total_height = 0;
    while (begin < end) {
      long long mid = (begin + end)/2;
      node h = get_height(this->speed[mid]);
      //printf("day %lld, cut day %lld,  speed %lld, mid %d, total %lld \n", day, h.cut_day, this->speed[mid], mid, h.height + (day - h.cut_day) * this->speed[mid]);
      total_height = h.height + (day - h.cut_day) * this->speed[mid];
      if (total_height < height) {
        begin = mid + 1;
      } else {
        end = mid;
      }
    }
    //for( int i = 0; i < this->fields-1; i ++){
      //printf("speed %d, cut_heaight %lld, cut_time %lld, total height %lld\n", speed[i], get_height(this->speed[i]).height, get_height(this->speed[i]).cut_day,
      //get_height(this->speed[i]).height+ (day - get_height(this->speed[i]).cut_day) * this->speed[i]);
    //}
    if(total_height < height && begin == this->fields-1)
    {
       return -1;

    }
    return this->speed[begin];
  }
  
  long long get_sum(int nr, long long day) {
    if (nr >= 2 * node_count) return 0;
    if (this->nodes[nr].is_valid_height || nr >= node_count) {
      return (day - this->nodes[nr].cut_day) * this->nodes[nr].total_speed + this->nodes[nr].total_count * this->nodes[nr].height;
    }
    //printf("%d\n", nr);
    return get_sum(nr * 2, day) + get_sum(nr * 2 + 1, day); 
  }
  long long count_higher(long long number, long long day, long long height) {
    long long sum = 0;
    long long nr = 1; 
    bool valid = this->nodes[nr].is_valid_height;
    long long hard_height = this->nodes[nr].height;
    long long hard_day = this->nodes[nr].cut_day;
    long long diff = 0;
    do {
      if(!valid) {
        valid = this->nodes[nr].is_valid_height;
        hard_height = this->nodes[nr].height;
        hard_day = this->nodes[nr].cut_day;
      }
      
      //printf("Liczba %d %d %d\n", nr, this->nodes[nr * 2].min, this->nodes[nr * 2].max);
      if (this->nodes[nr].min == number) {
        if (valid) {
          diff = (day - hard_day) * this->nodes[nr].total_speed + this->nodes[nr].total_count * hard_height - height * this->nodes[nr].total_count;
          sum += diff;
          //printf("A %lld\n", diff);
        } else {
          diff = get_sum(nr, day) - this->nodes[nr].total_count * height;
          sum += diff;
          //printf("B %lld\n", diff);
        }
        break;
      } else if (this->nodes[nr * 2].max >= number) {
        nr = nr * 2;
        if (valid) {
          diff = (day - hard_day) * this->nodes[nr+1].total_speed + this->nodes[nr+1].total_count * hard_height - height * this->nodes[nr+1].total_count;
          sum += diff;
          //printf("C %lld\n", diff);
        } else {
          diff = get_sum(nr + 1, day) - this->nodes[nr+1].total_count * height;
          sum += diff;
          //printf("D %lld,  min %d, max %d\n", diff, this->nodes[nr+1].min, this->nodes[nr+1].max);
        }
      } else {
        nr = nr * 2 + 1;  
      }
    } while(true);
    
    return sum;
  }
};

void test3() {
  int grasses = 10;
  int speed[] = {2, 3, 3, 3, 4, 5, 6, 7, 8000, 10000};
  Tree *tree = new Tree(1048576, speed, grasses);
  node root = tree->nodes[1];

  
  //printf("total %lld\n", tree->count_higher(2, 10, 0));
  
  assert (tree->count_higher(2, 10, 0) == 10 * (18033)); //number day height
  tree->cut(2, 100, 300); //point height day 4*300 = 1200 -> 100
  
  assert (tree->count_higher(2, 301, 100) == 18033); //number day heigh
  
  assert (tree->find_number(400, 300) == 2); // day height 400+100
  
  //printf("XX\n");
  tree->cut(2, 0, 301);
  tree->cut(3, 100, 302);
  assert (tree->count_higher(3, 302, 0) == 900);
  assert (tree->count_higher(2, 302, 0) == 902);
  
  assert (tree->count_higher(2, 402, 0) == 902 + 100 * (18033));
  tree->cut(5, 500, 402);
  long long a10000 = (200*10000+500);
  long long a8000 = (200*8000+500);
  long long a7 = (200*7+500);
  long long a6 = (200*6+500);
  long long a5 = (200*5+500);
  long long a4 = (200*4+100+400);
  long long a3 = (200*3+100+300)*3;
  long long a2 = (200*2+100+300+2+200);
  assert (tree->count_higher(10000, 602, 0)== a10000);
  assert (tree->count_higher(8000, 602, 0)== a10000+a8000);
  assert (tree->count_higher(10, 602, 0)== a10000+a8000);
  assert (tree->count_higher(7, 602, 0)== a10000+a8000+a7);
  assert (tree->count_higher(6, 602, 0)== a10000+a8000+a7+a6);
  assert (tree->count_higher(5, 602, 0)== a10000+a8000+a7+a6+a5);  
  assert (tree->count_higher(4, 602, 0)== a10000+a8000+a7+a6+a5+a4);  
  assert (tree->count_higher(3, 602, 0)== a10000+a8000+a7+a6+a5+a4+a3);  
  //assert (tree->count_higher(2, 602, 0)== a10000+a8000+a7+a6+a5+a4+a3+a2);  
  //printf("total %lld %lld\n", tree->count_higher(2, 602, 0), a8000+a10000+a7+a6+a5+a4+a3+a2); // number day height 100*9

  //assert (tree->count_higher(2, 602, 0) ==a);
}

void test2() {
  int grasses = 10;
  int speed[] = {2, 3, 3, 3, 4, 5, 6, 7, 8000, 10000};
  Tree *tree = new Tree(1048576, speed, grasses);
  node root = tree->nodes[1];
  assert (root.min == 1);
  assert (root.max == 1048575);

  assert (tree->find_number(2, 15000) == 8000);
  assert (tree->find_number(3, 10) == 4);
  assert (tree->find_number(300, 800) == 3);
  
  tree->cut(2, 100, 300); //point height day 4*300 = 1200 -> 100
  assert (tree->find_number(400, 300) == 2); // day height 400+100
  assert (tree->find_number(400, 400) == 3); // 100 + 100*3
  assert (tree->find_number(400, 500) == 4); // 100 + 100*4
  assert (tree->find_number(400, 600) == 5); // 100 + 100*5
  assert (tree->find_number(400, 700) == 6); // 100 + 100*6
  assert (tree->find_number(400, 800) == 7); // 100 + 100*7
  assert (tree->find_number(400, 800000) == 8000); // 100 + 100*7
  assert (tree->find_number(400, 800100) == 8000); // 100 + 100*7
  assert (tree->find_number(400, 800101) == 10000); // 100 + 100*7
  assert (tree->find_number(400, 1000100) == 10000);
  tree->cut(4, 200, 400); //point height day 4*300 = 1200 -> 100
  assert (tree->find_number(500, 300) == 2); // day height 200+200
  assert (tree->find_number(500, 400) == 2); // 100 + 100*3
  assert (tree->find_number(500, 500) == 2); // 100 + 100*4
  assert (tree->find_number(500, 600) == 3); // 100 + 100*5
  assert (tree->find_number(500, 700) == 5); // 100 + 100*6
  assert (tree->find_number(500, 800) == 6); // 100 + 100*7
  assert (tree->find_number(500, 800000) == 8000); // 100 + 100*7
  assert (tree->find_number(500, 800100) == 8000); // 100 + 100*7
  assert (tree->find_number(500, 800101) == 8000); // 100 + 100*7
  assert (tree->find_number(500, 1000100) == 10000);
  //printf("%d\n", tree->find_number(310, 10000));
}
void test() {
  int grasses = 10;
  int speed[] = {2, 3, 3, 3, 4, 5, 6, 7, 8000, 10000};
  Tree *tree = new Tree(1048576, speed, grasses);
  node root = tree->nodes[1];
  assert (root.min == 1);
  assert (root.max == 1048575);
  assert (root.total_speed == 18033);

  assert (root.total_count == 10);
  assert (root.height == 0);
  assert (root.is_valid_height == false);
  tree->cut(7000, 10, 0);
  assert (tree->get_height(7000).height == 10);
  assert (tree->get_height(10000).height == 10);
  assert (tree->get_height(6999).height == 0);
  assert (tree->get_height(1).height == 0);
  assert (tree->get_height(7001).height == 10);
  
  tree->cut(9000, 20, 0);
  
  assert (tree->get_height(1).height == 0);
  assert (tree->get_height(6999).height == 0);
  assert (tree->get_height(7000).height == 10);
  assert (tree->get_height(7001).height == 10);
  
  assert (tree->get_height(8999).height == 10);
  assert (tree->get_height(9000).height == 20);
  assert (tree->get_height(9001).height == 20);
  assert (tree->get_height(10000).height == 20);  
  assert (tree->get_height(10001).height == 20);
  
  tree->cut(3, 3, 0);
  
  assert (tree->get_height(1).height == 0);
  assert (tree->get_height(2).height == 0);
  assert (tree->get_height(3).height == 3);
  assert (tree->get_height(4).height == 3);
  
  assert (tree->get_height(8999).height == 3);
  assert (tree->get_height(9000).height == 3);
  assert (tree->get_height(9001).height == 3);
  assert (tree->get_height(10000).height == 3);  
  assert (tree->get_height(10001).height == 3);

  tree->cut(15, 4, 0);
  
  assert (tree->get_height(1).height == 0);
  assert (tree->get_height(2).height == 0);
  assert (tree->get_height(3).height == 3);
  assert (tree->get_height(4).height == 3);
  assert (tree->get_height(14).height == 3);
  assert (tree->get_height(15).height == 4);
  assert (tree->get_height(8999).height == 4);
  assert (tree->get_height(9000).height == 4);
  assert (tree->get_height(9001).height == 4);
  assert (tree->get_height(10000).height == 4);  
  assert (tree->get_height(10001).height == 4);

  tree->cut(100, 5, 0);
  
  assert (tree->get_height(1).height == 0);
  assert (tree->get_height(2).height == 0);
  assert (tree->get_height(3).height == 3);
  assert (tree->get_height(4).height == 3);
  assert (tree->get_height(14).height == 3);
  assert (tree->get_height(15).height == 4);
  assert (tree->get_height(99).height == 4);
  assert (tree->get_height(100).height == 5);
  assert (tree->get_height(101).height == 5);
  assert (tree->get_height(8999).height == 5);
  assert (tree->get_height(9000).height == 5);
  assert (tree->get_height(9001).height == 5);
  assert (tree->get_height(10000).height == 5);  
  assert (tree->get_height(10001).height == 5);

}

void solve(int *speed, long long *day, long long * height, int grasses, int cuts) {
  sort(speed, speed + grasses);
  Tree *tree = new Tree(1048576, speed, grasses);
  
  for(int i = 0; i < cuts; i++) {
     long long h = height[i];
     long long d = day[i];
     long long nr = tree->find_number(d, h);
     if(nr == -1)
    {
     printf("0\n");
       
     } else {
        printf("%lld\n", tree->count_higher(nr, d, h));
       tree->cut(nr, h, d);
     }
  }
}

int main() {
  int n; //powierzchnia
  int m; //skoszenia
  scanf("%d", &n);
  scanf("%d", &m);
  
  for(int i = 0; i < n; i++) //speed
    scanf("%d", &speed[i]);  
    
  for(int i = 0; i < m; i++) //days
    scanf("%lld %lld", &day[i], &height[i]);

  
  
  //test();
  //test2();
  //test3();
  solve(speed, day, height, n, m);
  return 0;
}