当前位置:网站首页>【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;
}
边栏推荐
- Or in SQL, what scenarios will lead to full table scanning
- Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
- Integer inversion
- Win10安装VS2015
- Before joining the chain home, I made a competitive product analysis for myself
- request对象对请求体,请求头参数的解析
- Using keras in tensorflow to build convolutional neural network
- ISP、IAP、ICP、JTAG、SWD的编程特点
- Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
- Bit operation ==c language 2
猜你喜欢
基于gis三维可视化技术的智慧城市建设
Basic chapter: take you through notes
The method of word automatically generating directory
Garbage disposal method based on the separation of smart city and storage and living digital home mode
The new activity of "the arrival of twelve constellations and goddesses" was launched
Fiddler simulates the interface test
Win10安装VS2015
内存==c语言1
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
AI moves from perception to intelligent cognition
随机推荐
Guys, how can mysql-cdc convert the upsert message to append only
STM32中AHB总线_APB2总线_APB1总线这些是什么
Three years after graduation
Use of JSON extractor originals in JMeter
Apprentissage avancé des fonctions en es6
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
ArcGIS operation: converting DWG data to SHP data
Please ask me a question. I started a synchronization task with SQL client. From Mysql to ADB, the historical data has been synchronized normally
Garbage disposal method based on the separation of smart city and storage and living digital home mode
【ORM框架】
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
Postman interface test IV
ArcGIS operation: batch modify attribute table
ORM -- query type, association query
Advanced function learning in ES6
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
Huffman encoded compressed file
VS Code指定扩展安装位置
C socke server, client, UDP
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)