codeforces Queries about less or equal elements 二分

来源:互联网 发布:开淘宝店的详细步骤图 编辑:程序博客网 时间:2024/06/11 12:53

   You are given two arrays of integers a andb. For each element of the second arraybj you should find the number of elements in arraya that are less than or equal to the valuebj.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arraysa and b.

The second line contains n integers — the elements of arraya ( - 109 ≤ ai ≤ 109).

The third line contains m integers — the elements of arrayb ( - 109 ≤ bj ≤ 109).

Output

Print m integers, separated by spaces: thej-th of which is equal to the number of such elements in arraya that are less than or equal to the valuebj.

Sample test(s)
Input
5 41 3 5 7 96 4 2 8
Output
3 2 1 4
Input
5 51 2 1 2 53 1 4 1 5
Output
4 2 4 2 5

code

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<stack>#include<queue>#include<fstream>#include <functional>#include <iomanip>#include<set>#include<vector>#include<map>#include <complex>#include <bitset>#define lson l,m,rt<<1#define lson m+1,r,rt<<1|1#define Max(a,b) a>b?a:b#define Min(a,b) a<b?a:b#define esp 1e-6#define LL long longusing namespace std;int a[200007],b[200007];int query(int l,int r,int j){    int p=l,q=r;    while(p<=q)    {        int m=q+((p-q)>>1);        if(b[j]>=a[m])        p=m+1;        else        q=m-1;    }    return p-1;}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        for(int i=0; i<n; i++)            scanf("%d",&a[i]);        sort(a,a+n);        int c[200007]={0};        for(int j=0; j<m; j++)        {            scanf("%d",&b[j]);           c[j]=query(0,n-1,j)+1;        }        for(int i=0;i<m;i++)            printf("%d ",c[i]);        printf("\n");    }}



0 0
原创粉丝点击