博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Queue CodeForces - 91B(线段树)
阅读量:4136 次
发布时间:2019-05-25

本文共 2598 字,大约阅读时间需要 8 分钟。

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
这个题和2019徐州网络赛的E一样的思路。这是在单调队列专题看到的。但是我用线段树写的
代码如下:

#include
#define ll long longusing namespace std;const int maxx=1e5+100;struct node{
int l; int r; int sum;}p[maxx<<2];int a[maxx],ans[maxx];int n;inline void pushup(int cur){
p[cur].sum=min(p[cur<<1].sum,p[cur<<1|1].sum);}inline void build(int l,int r,int cur){
p[cur].l=l; p[cur].r=r; p[cur].sum=0; if(l==r) return ; int mid=l+r>>1; build(l,mid,cur<<1); build(mid+1,r,cur<<1|1);}inline void update(int v,int pos,int cur){
int L=p[cur].l; int R=p[cur].r; if(L==R) {
p[cur].sum=v; return ; } int mid=L+R>>1; if(pos<=mid) update(v,pos,cur<<1); else update(v,pos,cur<<1|1); pushup(cur);}inline int query(int v,int cur){
int L=p[cur].l; int R=p[cur].r; if(v<=p[cur].sum) return 0; if(L==R) return L; int ans=0; ans=query(v,cur<<1|1); if(ans==0) ans=query(v,cur<<1); return ans;}int main(){
int x; while(~scanf("%d",&n)) {
for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,n,1); for(int i=n;i>=1;i--) {
x=query(a[i],1); if(x==0) ans[i]=-1; else ans[i]=x-i-1; update(a[i],i,1); } for(int i=1;i<=n;i++) cout<
<<" "; cout<

努力加油a啊,(o)/~

转载地址:http://uktvi.baihongyu.com/

你可能感兴趣的文章