当前位置:网站首页>【acwing】789. Range of numbers (binary basis)
【acwing】789. Range of numbers (binary basis)
2022-07-07 10:15: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 <<" ";// Whether the element pointed to by the current pointer is equal to the queried element , If equal to, it means that the element exists , Returns the left endpoint of the element
else{
cout << "-1 -1" << endl;
continue;
}
l = 0, r = n - 1;// Determine the position of the right endpoint of the queried element
while(l < r){
int mid = (l + r + 1)>> 1;
if(a[mid] <= x){
l = mid;
}
else{
r = mid - 1;
}
}
cout << l << endl;
}
return 0;
}
边栏推荐
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
- Parameter sniffing (2/2)
- C#记录日志方法
- Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
- Apprentissage avancé des fonctions en es6
- Delete a record in the table in pl/sql by mistake, and the recovery method
- ArcGIS operation: batch modify attribute table
- Analyze Android event distribution mechanism according to popular interview questions (I)
- 一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
猜你喜欢
ORM--查询类型,关联查询
高数_第1章空间解析几何与向量代数_向量的数量积
Leetcode exercise - 113 Path sum II
Bean 作⽤域和⽣命周期
How to cancel automatic saving of changes in sqlyog database
“十二星座女神降临”全新活动推出
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
Fiddler break point
Pit encountered by vs2015 under win7 (successful)
随机推荐
能源路由器入门必读:面向能源互联网的架构和功能
【acwing】786. 第k个数
Mongodb creates an implicit database as an exercise
ES类和对象、原型
[untitled]
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
Guid primary key
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Internship log - day04
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
Inno Setup 打包及签名指南
ORM -- grouping query, aggregation query, query set queryset object properties
Why does the starting service report an error when installing MySQL? (operating system Windows)
Differences between MCU and MPU
ORM model -- creation and query of data records
C#记录日志方法
The method of word automatically generating directory
Deadlock caused by non clustered index in SQL Server