听取WA声一片

P2249 【深基13.例1】查找

congcong2012 @ 2024-10-26 21:31:16

代码如下:

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
typedef long long ll;
inline int read(){int x=0,t=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') t=-1;ch=getchar();}while(ch>='0'&&ch<='9'){/*x=x*10+ch-'0';*/x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}return x*t;}
inline void write(int x){if(x<0){putchar('-');x=-x;}if(x>9) write(x/10);putchar(x%10+'0');}
int n,m,t,a[1145140]; 

int main(){
    n=read();
    m=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
    }
    while(m--){
        t=read();
        int l=0,r=n,mid,ans=0x7fffffff;
        while(l<r){
            mid=(l+r)>>1;
            if(a[mid]>=t){
                if(a[mid]==t) ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        if(ans!=0x7fffffff) printf("%d ",ans);
        else printf("-1 ");
    }
    return 0;
}

|