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
|
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
void findrange(int y,map<int,vector<long long int>>&m,pair<int,int>p[],int low,int high)
{
//cout<<low<<" "<<high<<endl;
if(low<=high)
{
int mid=low+(high-low)/2;
if(y>=p[mid].first && y<=p[mid].second)
{
m[y].push_back(p[mid].second-p[mid].first+1);
findrange(y,m,p,mid+1,high);
findrange(y,m,p,low,mid-1);
}
else if(y>p[mid].first)
{
findrange(y,m,p,mid+1,high);
}
else
{
findrange(y,m,p,low,mid-1);
}
}
}
bool cond(pair<int,int>a,pair<int,int>b)
{
if(a.first<b.first)
return true;
if(a.first==b.first)
{
if(a.second<b.second)
return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,x,y;
cin>>n;
pair<int,int>p[n]; //size,first,second
for(int i=0;i<n;i++)
{
cin>>x>>y;
p[i].first=x;
p[i].second=y;
}
sort(p,p+n,cond);
map<int,vector<long long int>>m; //no,size
int query;
cin>>query;
for(int i=0;i<query;i++)
{
cin>>x>>y;
if(m[y].size()==0)
{
findrange(y,m,p,0,n-1); //binary search
sort(m[y].begin(),m[y].end());
}
if(m[y].size()>=x)
cout<<m[y][x-1]<<endl;
else
cout<<-1<<endl;
}
}
|