CF91B:Queue(线段树,区间最值)

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

There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.

The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.

The airport manager asked you to count for each of n walruses in the queue his displeasure.

Input

The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai(1 ≤ ai ≤ 109).

Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.

Output

Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.

Examples
input
6
10 8 5 3 50 45
output
2 1 0 -1 0 -1 
input
7
10 4 6 3 2 8 15
output
4 2 1 0 -1 -1 -1 
input
5
10 3 1 10 11
output
1 0 -1 -1 -1 
题意:给出N个数字,对于每个a[i],输出在它右边最远的且比它小的数与它本身的距离,没有则输出-1。

思路:建个线段树保存着区间最小值,然后查询时右子树优先即可。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# define lson l, m, id<<1
# define rson m+1, r, id<<1|1
using namespace std;
const int maxn = 1e5;
int imin[maxn<<2], a[maxn+1], cnt=1;

void build(int l, int r, int id)
{
    if(l == r)
    {
        scanf("%d",&imin[id]);
        a[cnt++] = imin[id];
        return;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    imin[id] = min(imin[id<<1], imin[id<<1|1]);
}

int query(int num ,int pos, int l, int r, int id)
{
    if(l == r)
        return a[l]<num?l:-1;
    int m = (l+r)>>1, ans=-1;
    if(imin[id<<1|1] < num)
        ans =  query(num, pos, rson);
    else if(m > pos && imin[id<<1] < num)//中位数比pos大,才执行左子树。
        ans = query(num, pos, lson);
    return ans;
}

int main()
{
    int n;
    scanf("%d",&n);
    build(1, n, 1);
    for(int i=1; i<=n; ++i)
    {
        int ans = query(a[i], i, 1, n, 1);
        printf("%d ",ans==-1?-1:ans-i-1);
    }
    return 0;
}



转载于:https://www.cnblogs.com/junior19/p/6729896.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值