4 solutions

  • 0
    @ 2022-4-6 20:09:28
    //lower_bound(a,a+n,flag)-a;大法得问问为什么我没用呢》?
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=1e6+5;
    ll n,m;
    int a[N];
    int y;
    int bs(int *a,int x){
        int l=0;
        int r=n;
        while(l<r){
          int mid=(l+r)>>1;
          if(a[mid]>=x)r=mid;
          else l=mid+1;
        }
        if(a[l]==x)return l+1;
        return -1;
    }
    int main(){
        cin>>n>>m;
        for(int i=0;i<n;i++) cin>>a[i];
        while(m--){
            cin>>y;
            cout<<bs(a,y)<<" ";
        }
        return 0;
    }
    ···

    Information

    ID
    139
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    # Submissions
    457
    Accepted
    57
    Uploaded By