题目描述
啊~啊~~啊~~~啊~~~~先吼几声吧~~~~~.
输入两个数n和q,分别表示n个正整数和q次查找
每次查找一个数字x,输出有多少数比x小,有多少个数比x大.
输入格式
第一行输入 n和q(整数)
第二行输入 n个正整数
第三行输入 q个正整数
输出格式
q行 每行两个整数
为小于、大于的数
样例输入
10 2 2 8 3 4 1 9 2 7 4 6 5 1
样例输出
6 4 0 9
提示
比5小的有1、2、2、3、4、4
比5大的有6、7、8、9
数据范围:
1≤n≤1051≤n≤105
1≤q≤1051≤q≤105
1≤x≤109
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
int e[2102105];
int w[2102105];
ll fb[1101000];
ll fe[1101000];
int n,m,s,r,k,t,num,p,ans;
const double pi=acos(-1.0);
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>m>>n;
for(int i=1;i<=m;i++)
{
cin>>e[i];
}
sort(e+1,e+1+m);
while(n--)//查询
{
int a;
cin>>a;
cout<<lower_bound(e+1,e+1+m,a)-e-1<<" "<<m+e-upper_bound(e+1,e+1+m,a)+1<<endl;
} //第一个二分函数查询的是第一个大于等于a的数的位置,但由于其包括了e这个数组的本身地址所以得-e
return 0;//第二个二分函数 查询的是第一个大于a的数
}
需要注意的是所谓二分函数里的纯粹查找结果是由原地址和所查找到的位置组成;比如此题中的的
lower_bound(e+1,e+1+m,a)这个查询结果可以看成(e+查询到的在数组中的所在位置)