62032023-11-07 20:31:35UVinceHáromszögekcpp11Accepted 40/40209ms5364 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
	int n,q;
	cin>>n>>q;
	vector<int> a(n);
	for (int i = 0; i<n; i++) cin>>a[i];
	sort(a.begin(), a.end());

	while (q--){
		int b,c;
		cin>>b>>c;
		int mina=abs(b-c)+1, maxa=b+c-1, l=0, r=n, il=0, ir=n;

		while(il+1<r){
			int cur = (il+r)/2;
			if (a[cur]<=mina){
				il=cur;
				continue;
			}
			if (r==cur) {r--;break;}
			r=cur;
		}
		if (a[il]<mina) il++;

		while(l+1<ir){
			int cur = (l+ir)/2;
			if (a[cur]<=maxa){
				l=cur;
				continue;
			}
			if (ir==cur) {ir=il;break;}
			ir=cur;
		}
		if (a[il] > maxa){
			cout<<"0\n";
			continue;
		}
		//cout<<il<<" "<<ir<<"\n";
		cout<<ir-il<<"\n";
	}
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1816 KiB
2Accepted0/03ms2004 KiB
3Accepted0/016ms2288 KiB
4Accepted4/44ms2432 KiB
5Accepted2/24ms2540 KiB
6Accepted1/1209ms3316 KiB
7Accepted1/1153ms3440 KiB
8Accepted2/2151ms3652 KiB
9Accepted2/2152ms4148 KiB
10Accepted1/1187ms4336 KiB
11Accepted1/1137ms4320 KiB
12Accepted2/2141ms4332 KiB
13Accepted2/2171ms4396 KiB
14Accepted1/1189ms4488 KiB
15Accepted1/1158ms4716 KiB
16Accepted1/1180ms4968 KiB
17Accepted1/1181ms5012 KiB
18Accepted1/1145ms4908 KiB
19Accepted2/2187ms5012 KiB
20Accepted2/2184ms5364 KiB
21Accepted3/3153ms5360 KiB
22Accepted5/5188ms5228 KiB
23Accepted5/5178ms5324 KiB