当前位置:网站首页>【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;
}
边栏推荐
- flink. CDC sqlserver. You can write the DEM without connector in sqlserver again
- Postman interface test IV
- Introduction to energy Router: Architecture and functions for energy Internet
- VS Code指定扩展安装位置
- Parameter sniffing (2/2)
- Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
- Basic chapter: take you through notes
- Phpcms realizes PC website access to wechat native payment
- Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
- Three years after graduation
猜你喜欢
ArcGIS operation: converting DWG data to SHP data
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
Some thoughts on the testing work in the process of R & D
【ORM框架】
XML配置文件解析与建模
Postman interface test IV
Use of JSON extractor originals in JMeter
A wave of open source notebooks is coming
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
ES类和对象、原型
随机推荐
Fiddler break point
字节跳动 Kitex 在森马电商场景的落地实践
The new activity of "the arrival of twelve constellations and goddesses" was launched
ES6中的函數進階學習
ORM -- query type, association query
flink. CDC sqlserver. You can write the DEM without connector in sqlserver again
Postman interface test VII
Methods of adding centerlines and centerlines in SolidWorks drawings
Postman interface test III
web3.0系列之分布式存储IPFS
Gauss elimination
Three years after graduation
Mongodb creates an implicit database as an exercise
MCU与MPU的区别
Applet popup half angle mask layer
Horizontal split of database
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Or in SQL, what scenarios will lead to full table scanning
The landing practice of ByteDance kitex in SEMA e-commerce scene