[The Preliminary Contest for ICPC Asia Xuzhou 2019 - 徐州网络赛E] XKC's basketball team

XKC's basketball team

XKC , the captain of the basketball team , is directing a train of nn team members. He makes all members stand in a row , and numbers them 1 \cdots n1⋯n from left to right.

The ability of the ii-th person is w_iwi​ , and if there is a guy whose ability is not less than w_i+mwi​+m stands on his right , he will become angry. It means that the jj-th person will make the ii-th person angry if j>ij>i and w_j \ge w_i+mwj​≥wi​+m.

We define the anger of the ii-th person as the number of people between him and the person , who makes him angry and the distance from him is the longest in those people. If there is no one who makes him angry , his anger is -1−1 .

Please calculate the anger of every team member .

Input

The first line contains two integers nn and m(2\leq n\leq 5*10^5, 0\leq m \leq 10^9)m(2≤n≤5∗105,0≤m≤109) .

The following  line contain nn integers w_1..w_n(0\leq w_i \leq 10^9)w1​..wn​(0≤wi​≤109) .

Output

A row of nn integers separated by spaces , representing the anger of every member .

样例输入

6 1
3 4 5 6 2 10

样例输出

4 3 2 1 0 -1

题意

cxk 有一支 n 个人组成的篮球队伍,每个人都有一个实力值 w。当某个人的右边出现了实力值超过他至少 m 的人,那么他就会感到愤怒,且愤怒值为能够使得他愤怒的最右边的人与他之间的人数。换句话说,对于队员 i,能够使得他愤怒的人 j (j > i)的实力值 w[j] >= w[i] + m,且愤怒值为 max(j)-i-1,特别的,若没有人能够使得他愤怒,他的愤怒值就为 -1。求出所有人的愤怒值。

思路

建一个线段树,维护区间最大值。每次查询优先查找右子树(因为要求 j 的值为最大),若右子树的区间的实力值的最大值不满足愤怒的条件,再查找左子树,若都不满足,则返回 -1。

代码

#include 
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 5e5+5;
int n, m;
int st[maxn<<2], a[maxn];

void pushUp(int rt)
{
    st[rt] = max(st[rt<<1], st[rt<<1|1]);
}

void build(int l, int r, int rt)
{
    if(l == r)
    {
        st[rt] = a[l];
        return;
    }
    int m = l+((r-l)>>1);
    build(l, m, rt<<1);
    build(m+1, r, rt<<1|1);
    pushUp(rt);
}

int query(int val, int L, int R, int l, int r, int rt)
{
    if(L > r || R < l || st[rt] < val)
        return -1;
    if(l == r)
        return l;

    int m = l + ((r-l)>>1);
    if(R > m && st[rt<<1|1] >= val)
        return query(val, L, R, m+1, r, rt<<1|1);
    if(L <= m && st[rt<<1] >= val)
        return query(val, L, R, l, m, rt<<1);
    return -1;
}

void read()
{
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        cin >> a[i];
}

void solve()
{
    build(1, n, 1);
    int ans = 0;
    for(int i = 1; i <= n; ++i)
    {
        ans = query(a[i]+m, i+1, n, 1, n, 1);
        if(~ans)
            ans = ans - i - 1;
        cout << ans;
        cout << ((i < n) ? ' ' : '\n');
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    read();
    solve();
    return 0;
}

 

你可能感兴趣的:(ACM,-,OJ)