求助:
这个代码运行后到sort那一行会死循环。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,m,k,q,a,b,pos;
int p[100010];
int findd;
int f(int x){
int l=1,r=k;
while (l<r){
int mid=(l+r)/2;
if (p[mid]>x) r=mid-1;
else l=mid;
}
if (p[l]==x) return -1;
return l;
}
signed main(){
cin>>n>>m>>k>>q;
for(int i=1;i<=k;i++){
cin>>a>>b;
p[i]=(a-1)*m+b;
}
sort(p+1,p+k+1);
for(int i=1;i<=q;i++){
cin>>a>>b;
pos=(a-1)*m+b;
if(f(pos)==-1){
cout<<-1<<endl;
}else{
cout<<pos-f(pos)<<endl;
}
}
return 0;
}
样例:
4 5 5 6
4 3
1 3
3 3
2 5
3 2
1 3
1 4
2 3
1 1
2 4
4 5