CodeForces 91B Queue (线段树单点操作)

来源:互联网 发布:女网络作家排行榜 编辑:程序博客网 时间:2024/05/29 11:00

Description

There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the1-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 suchj (i < j), thatai > aj. Thedispleasure 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 thei-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 bestrictly 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 thei-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.

Sample Input

Input
610 8 5 3 50 45
Output
2 1 0 -1 0 -1 
Input
710 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1 
Input
510 3 1 10 11
Output
1 0 -1 -1 -1 


线段树维护区间最小值, 循环一遍数组,每次找到比这个数小且在最右端的数,那么左边处理过的数可以更新成INF, 顺便更新最小值。那么查询之前, 先询问整个数组的最小是和当前值的比较,满足最小值小于当前值才有查询的必要,而且右边不成立就一定在左边。


#include <stdio.h>#include <iostream>#include <algorithm>#include <string>#include <string.h>#include <cmath>#include <cctype>#include <stack>#include <queue>#include <set>#define lson o<<1, l, m#define rson o<<1|1, m+1, rusing namespace std;typedef __int64 LL;const int maxn = 100005;const int MAX = 0x3f3f3f3f;int n, tg, b, k, a[maxn], mi[maxn << 2], ans[maxn];void up(int o) {    mi[o] = min(mi[o<<1], mi[o<<1|1]);}void build(int o, int l, int r) {    if(l == r) {        scanf("%d", &mi[o]);        a[l] = mi[o];        return;    }    int m = (l+r) >> 1;    build(lson);    build(rson);    up(o);}void query(int o, int l, int r) {    if(l == r) {        ans[k++] = l - b - 1;        return;    }    int m = (l+r) >> 1;    if(mi[o<<1|1] < tg) query(rson);    else query(lson);}void update(int o, int l, int r) {    if(l == r) mi[o] = MAX;    else {        int m = (l+r) >> 1;        if(b <= m) update(lson);        else update(rson);        up(o);    }}int main(){    cin >> n;    build(1, 1, n);    for(int i = 1; i <= n; i++) {        tg = a[i], b = i;        update(1, 1, n);        if(mi[1] >= tg) ans[k++] = -1;        else query(1, 1, n);    }    for(int i = 0; i < k; i++) printf("%d ", ans[i]);    return 0;}


1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 八个月母乳不够宝宝不吃奶粉怎么办 八个月宝宝吃母乳不吃奶粉怎么办 八个月宝宝戒奶不吃奶粉怎么办 刚满月的宝宝发烧38度怎么办 未满月的宝宝发烧38度怎么办 半月大的婴儿吃奶就漾奶怎么办 上司交给你不能完成的任务怎么办 电脑光驱里放入光碟放不出来怎么办 黑暗之魂3太难了怎么办 苹果手机下载的游戏闪退怎么办 宝宝两岁了不怎么爱拉大便怎么办? 小狗脖子发硬疼的直叫怎么办 厨房里有很多小虫子围着鸡蛋怎么办 狗生小狗后几天不吃饭怎么办 还没满月的小兔子突然死了怎么办 宝宝小鸡被蚊子咬后肿得很大怎么办 不知道是哪知兔子下的小兔怎么办 兔子生完小兔不吃东西了怎么办 人工喂养七天的小羊拉希怎么办 仔兔出生3天吃过奶就尿怎么办 小兔子买回来两天不拉屎怎么办 大狗生了小狗把小狗咬死了怎么办 狗妈妈一直咬小狗的脐带怎么办 狗狗体内驱虫驱不干净怎么办 打老鼠脚被老鼠咬了怎么办 天正画的cad打开显示空白怎么办 苹果手机信息被拉进群聊怎么办 空调的控制线的报验资料怎么办 窗窗户罩子护栏上的瓦楞板怎么办 酸洗好的带钢容易返锈怎么办 化肥撤到小树苗上现在变黑了怎么办 尿素液烧的太慢了怎么办 天堂鸟肥料施多了黄叶了怎么办 死水塘养的鱼每天在死怎么办 北京的阿姆斯肥把苗都烧死了怎么办 纱窗被老鼠咬了个洞 怎么办 低电量模式下动态墙纸不能用怎么办 吃的包装袋执行标准错了怎么办 退换东西的时候外包装坏了怎么办 闲鱼买家以与描述不符退货怎么办 闲鱼买家申请退款又签收了怎么办