Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分

B. Queue

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

codeforces.com/problemset/problem/91/B

Description

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.

Sample Input

6
10 8 5 3 50 45

Sample Output

2 1 0 -1 0 -1

HINT

题意

给你一个数列,让你找到最右边比这个数小的数的位置,如果没有就输出-1

题解:

直接线段树中二分,查询最小值,然后二分区间就好了~

代码

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[20];
const int inf=0x3f3f3f3f;
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}

//**************************************************************************************

int tr[maxn];
int a[maxn];
int ans[maxn];
int tmp;
void build(int x,int l,int r)
{
    if(l==r)
    {
        tr[x]=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    tr[x]=min(tr[x<<1],tr[x<<1|1]);
}
void query(int x,int l,int r,int t)
{
    if(l==r)
    {
        ans[tmp++]=l-t-1;
        return;
    }
    int mid=(l+r)>>1;
    if(tr[x<<1|1]<a[t])
        query(x<<1|1,mid+1,r,t);
    else
        query(x<<1,l,mid,t);
}
void update(int x,int l,int r,int t)
{
    if(l==r)
    {
        tr[x]=inf;
        return;
    }
    int mid=(l+r)>>1;
    if(t<=mid)
        update(x<<1,l,mid,t);
    else
        update(x<<1|1,mid+1,r,t);
    tr[x]=min(tr[x<<1],tr[x<<1|1]);
}
int main()
{
    int n=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    build(1,1,n);
    for(int i=1;i<=n;i++)
    {
        if(tr[1]>=a[i])
            ans[tmp++]=-1;
        else
            query(1,1,n,i);
        update(1,1,n,i);
    }
    for(int i=0;i<n;i++)
        printf("%d ",ans[i]);
}
时间: 2024-10-19 19:18:40

Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分的相关文章

Codeforces Beta Round #75 (Div. 2 Only)

Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A 1 #include<iostream> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 100005 7 typedef l

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #9 (Div. 2 Only)

Codeforces Beta Round #9 (Div. 2 Only) http://codeforces.com/contest/9 A gcd水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7

Codeforces Beta Round #12 (Div 2 Only)

Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define maxn 1000010 7 t

Codeforces Beta Round #29 (Div. 2, Codeforces format)

Codeforces Beta Round #29 (Div. 2, Codeforces format) http://codeforces.com/contest/29 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb p

Codeforces Beta Round #31 (Div. 2, Codeforces format)

Codeforces Beta Round #31 (Div. 2, Codeforces format) http://codeforces.com/contest/31 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb p

Codeforces Beta Round #49 (Div. 2)

Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define

Codeforces Beta Round #59 (Div. 2)

Codeforces Beta Round #59 (Div. 2) http://codeforces.com/contest/63 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define

Codeforces Beta Round #72 (Div. 2 Only)

Codeforces Beta Round #72 (Div. 2 Only) http://codeforces.com/contest/84 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #de