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
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<climits>

using namespace std;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) (int)(x).size()
#define SORT(x) sort((x).begin(),(x).end())
#define CLEAR(tab) memset(tab, 0, sizeof(tab))
#define REP(x, n) for(int x = 0; x < (n); x++)
#define FOR(x, b, e) for(int x = (b); x <= (e); x++)
#define FORD(x, b, e) for(int x = (b); x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)

const int MAX_BUF_SIZE = 16384;
char BUFOR[MAX_BUF_SIZE];
int BUF_SIZE, BUF_POS;
char ZZZ;
#define GET(ZZZ){ZZZ='a';if(BUF_POS<BUF_SIZE)ZZZ=BUFOR[BUF_POS++];\
else if(!feof(stdin)){BUF_SIZE=fread(BUFOR,1,MAX_BUF_SIZE,stdin);\
ZZZ=BUFOR[0];BUF_POS=1;}}
#define GI(WW){do{GET(ZZZ);}while(!isdigit(ZZZ));WW=ZZZ-'0';\
while(1){GET(ZZZ);if(!isdigit(ZZZ))break;WW=WW*10+(ZZZ-'0');}}
#define GC(WW){do{GET(ZZZ);}while(!isalpha(ZZZ));WW=ZZZ;}

//FAST IO

typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

const int MXN = 500010;
const int C = 2 * 262144;
const LL INF = LLONG_MAX / 10;

struct node {
    LL dA, hA, dB, sB, minB;

    node() {
        dA = hA = dB = sB = 0;
        minB = INF;
    }
} tree[2 * C + 10];

int n, m;
int a[MXN];
LL sum[2 * C + 10];
int cnt[2 * C + 10];
int p[2 * C + 10];

void init() {
    FOR(i, 0, 2 * C + 5)
        p[i] = n + 1;

    FOR(i, 1, n) {
        tree[C + i].minB = 0;
        sum[C + i] = a[i];
        p[C + i] = i;
        cnt[C + i] = 1;
    }

    FORD(i, C - 1, 0) {
        tree[i].minB = min(tree[2 * i].minB, tree[2 * i + 1].minB);
        p[i] = min(p[2 * i], p[2 * i + 1]);
        sum[i] = sum[2 * i] + sum[2 * i + 1];
        cnt[i] = cnt[2 * i] + cnt[2 * i + 1];
    }
}

LL newH, newD;

LL up(int w, LL dC, LL hC) {
    LL res = 0;
    if(dC > tree[w].dB)
        res = (newD - dC) * sum[w] + (hC - newH) * cnt[w];
    else
        res = (newD - tree[w].dB) * sum[w]
            + tree[w].sB - newH * cnt[w];

    tree[w].dA = tree[w].dB = newD;
    tree[w].hA = newH;
    tree[w].sB = newH * cnt[w];
    tree[w].minB = newH;

    return res;
}

void up2(int w, LL dC, LL hC) {
    LL newDB = max(tree[2 * w].dB, tree[2 * w + 1].dB);

	LL minBL, minBR;

    //dobrze?
	if(tree[2 * w].dB < dC)
		minBL = hC + (newDB - dC) * a[p[2 * w]];
	else
		minBL = tree[2 * w].minB + (newDB - tree[2 * w].dB) * a[p[2 * w]];
    if(tree[2 * w + 1].dB < dC)
		minBR = hC + (newDB - dC) * a[p[2 * w + 1]];
	else
		minBR = tree[2 * w + 1].minB + (newDB - tree[2 * w + 1].dB) * a[p[2 * w + 1]];	
	
	tree[w].minB = min(minBL, minBR);

    LL sBL, sBR;
    if(dC > tree[2 * w].dB)
        sBL = (newDB - dC) * sum[2 * w] + hC * cnt[2 * w];
    else
        sBL = (newDB - tree[2 * w].dB) * sum[2 * w] + tree[2 * w].sB;

    if(dC > tree[2 * w + 1].dB)
        sBR = (newDB - dC) * sum[2 * w + 1] + hC * cnt[2 * w + 1];
    else
        sBR = (newDB - tree[2 * w + 1].dB) * sum[2 * w + 1] + tree[2 * w + 1].sB;

    tree[w].sB = sBL + sBR;
    tree[w].dB = newDB;
}

LL _update(int w, int L, int R, int l, int r, LL dC, LL hC) {
    if(dC < tree[w].dA) {
        dC = tree[w].dA;
        hC = tree[w].hA;
    }

    if(l <= L && R <= r)
        return up(w, dC, hC);

    LL res = 0;
    int S = (L + R) / 2;
    if(l <= S)
        res += _update(2 * w, L, S, l, r, dC, hC);
    if(r > S)
        res += _update(2 * w + 1, S + 1, R, l, r, dC, hC);

    up2(w, dC, hC);
    return res;
}

LL update(int x, LL h, LL d) {
    if(x > n)
        return 0;
    newH = h;
    newD = d;
    return _update(1, 0, C - 1, x, n, 0, 0);
}

LL valMB(int w, LL d, LL tmpD, LL tmpH) {
    if(tmpD > tree[w].dB)
        return tmpH + (d - tmpD) * a[p[w]];
    else
        return tree[w].minB + (d - tree[w].dB) * a[p[w]];
}

int pocz(LL h, LL d) {
    int w = 1;
    int res = n + 1;

    LL tmpD = 0;
    LL tmpH = 0;

    while(w < C) {
        if(tree[w].dA > tmpD) {
            tmpD = tree[w].dA;
            tmpH = tree[w].hA;
        }
        if(valMB(2 * w + 1, d, tmpD, tmpH) >= h) {
            res = min(res, p[2 * w + 1]);
            w = 2 * w;
        }
        else
            w = 2 * w + 1;
    }

    if(tree[w].dA > tmpD) {
		tmpD = tree[w].dA;
		tmpH = tree[w].hA;
	}


	//czy OK?
    if( (w - C) && valMB(w, d, tmpD, tmpH) >= h)
        res = min(res, w - C);

	return res;
}

int main() {
	scanf("%d %d", &n, &m);

	FOR(i, 1, n)
        scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    init();

    FOR(i, 1, m) {
        LL di, bi;
        scanf("%lld %lld", &di, &bi);
        LL res = update(pocz(bi, di), bi, di);
		printf("%lld\n", res);
    }

	return 0;
}