Codeforces #367(Div.2)B Interesting drink【树状数组】

B. Interesting drink time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink “Beecola“, which can be bought in n different shops in the city. It’s known that the price of one bottle in the shop i is equal to xi coins.

Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of “Beecola“.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of shops in the city that sell Vasiliy’s favourite drink.

The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink in the i-th shop.

The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy plans to buy the drink.

Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins Vasiliy can spent on the i-th day.

Output

Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.

Example Input 5 3 10 8 6 11 4 1 10 3 11 Output 0 4 1 5 Note

On the first day, Vasiliy won’t be able to buy a drink in any of the shops.

On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.

On the third day, Vasiliy can buy a drink only in the shop number 1.

Finally, on the last day Vasiliy can buy a drink in any shop.

题目大意:有n个商店,每个商店都有这种饮品,并且给你每个商店对应的价格,然后有Q个询问,表示你有那么些钱,可以在几个商店选择购买。

思路:

1、简单树状数组,将输入进来的数都加入树中。

2、简单查询,对应Mi如果超过了Xi的极限数据的时候,我们直接离散化一下,将Mi设定为最大的Xi然后去查询即可。

Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int tree[1000005];//树
int n;
int lowbit(int x)//lowbit
{
    return x&(-x);
}
int sum(int x)//求和求的是比当前数小的数字之和,至于这里如何实现,很简单:int sum=sum(a[i]);
{
    int sum=0;
    while(x>0)
    {
        sum+=tree[x];
        x-=lowbit(x);
    }
    return sum;
}
void add(int x,int c)//加数据。
{
    while(x<=n)
    {
        tree[x]+=c;
        x+=lowbit(x);
    }
}
int main()
{
    n=1000000;
    int nn;
    while(~scanf("%d",&nn))
    {
        for(int i=1;i<=nn;i++)
        {
            int tmp;
            scanf("%d",&tmp);
            add(tmp,1);
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int x;
            scanf("%d",&x);
            if(x>n)x=n-1;
            printf("%d\n",sum(x));
        }
    }
}

    原文作者:B树
    原文地址: https://blog.csdn.net/mengxiang000000/article/details/52199300
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞