HTML & JS
#include<bits/stdc++.h>
using namespace std;

bool comp(pair<int ,int>a,pair<int,int> b)
{
	if(a.first<b.first)
		return(1);
	else
		return(0);
}

int search(int f,int l,int a,vector<pair<int,int>> v)
{
	int m=(f+l)/2;
	if((a>=v[m].first)&&(a<v[m].second))
		return(0);
	if((l-f)==1)
	{
	 return(v[l].first-a);
	}
	else if(v[m].second>a)
		return(search(f,m,a,v));
	else
		return(search(m,l,a,v));
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
	int t;
	cin>>t;

	while(t--)
	{
		int n,m;
		cin>>n>>m;
		vector <pair<int ,int>> v;
		for(int i=0;i<n;i++)
		{
			int a,b;
			cin>>a>>b;
			v.push_back(make_pair(a,b));

		}
		sort(v.begin(),v.end(),comp);
		int A[m];
		for(int i=0;i<m;i++)
			cin>>A[i];
		for(int i=0;i<m;i++)
		{
			int a=A[i];
			
			if((a>=v[0].first)&&(a<v[0].second))
			{
				cout<<"0\n";
				continue;
			}
			else if(a<v[0].first)
			{
				cout<<(v[0].first-a)<<"\n";
				continue;
			}
			else if(a>=v[v.size()-1].second)
			{
				cout<<"-1\n";
				continue;
			}
			else
			{
				int g=search(0,v.size()-1,a,v);
				cout<<g<<"\n";
				continue;
			}
		}

	}
}

Generated URL:

Output:

0
0
2
-1
10
Report Bug