当前位置:网站首页>【acwing】789. 数的范围(二分基础)
【acwing】789. 数的范围(二分基础)
2022-07-07 07:46:00 【percation】
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int a[N];
int n,q;
int main(){
scanf("%d%d",&n,&q);
for(int i = 0; i < n; i++){
scanf("%d",&a[i]);
}
while(q--){
int x;
scanf("%d",&x);
int l = 0, r = n - 1;
while(l < r){
int mid = l + r >> 1;
if(a[mid] >= x){
r = mid;
}
else{
l = mid + 1;
}
}
if(a[l] == x) cout << l <<" ";//当前指针所指向的元素是否等于所查询的元素,若等于则说明该元素存在,返回该元素的左端点
else{
cout << "-1 -1" << endl;
continue;
}
l = 0, r = n - 1;//判断所查询元素的右端点的位置
while(l < r){
int mid = (l + r + 1)>> 1;
if(a[mid] <= x){
l = mid;
}
else{
r = mid - 1;
}
}
cout << l << endl;
}
return 0;
}
边栏推荐
- Phpcms realizes PC website access to wechat native payment
- request对象对请求体,请求头参数的解析
- Arcgis操作: 批量修改属性表
- 官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
- UnityWebRequest基础使用之下载文本、图片、AB包
- Use of JSON extractor originals in JMeter
- Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
- arcgis操作:dwg数据转为shp数据
- ISP、IAP、ICP、JTAG、SWD的编程特点
- Some test points about coupon test
猜你喜欢
Delete a record in the table in pl/sql by mistake, and the recovery method
ORM--数据库增删改查操作逻辑
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Performance optimization record of the company's product "yunzhujia"
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
ORM -- database addition, deletion, modification and query operation logic
web3.0系列之分布式存储IPFS
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
中国首款电音音频类“山野电音”数藏发售来了!
fiddler-AutoResponder
随机推荐
Luogu p2482 [sdoi2010] zhuguosha
Advanced function learning in ES6
虚数j的物理意义
Embedded background - chip
Postman interface test II
fiddler-AutoResponder
China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
Guys, have you ever encountered the case of losing data when Flink CDC reads mysqlbinlog? Every time the task restarts, there is a probability of losing data
学习记录——高精度加法和乘法
ES6中的原型对象
Postman interface test VII
Introduction to uboot
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
运用tensorflow中的keras搭建卷积神经网络
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
Postman tutorial - scripting
基于gis三维可视化技术的智慧城市建设
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance