XKC's basketball team

题目链接:https://nanti.jisuanke.com/t/41387

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+mw i +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∗10
5 ,0≤m≤10 9).

The following line contain nn integers w_1…w_n(0\leq w_i \leq 10^9)w
1 …w n
(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
题目大意:第i个元素的愤怒值定义为:在i右侧的所有满足a[j]>=a[i]+m 中最大的j-i+1,若不存在这样的数则值为−1。输出每个元素的愤怒值。

思路:用线段树维护区间最大值。那么要查询的其实就是[i+1,n]内满足a[j]>=a[i]+m的最大的j,因此考虑优先查询右子树,最后返回下标即可。

#include<iostream>
#include<cstdio>
using namespace std;

const int maxn=5e5+5;

struct node
{
	int l,r,MAX;
}tree[maxn<<2];

int n,m;
int a[maxn];

void build(int i,int l,int r)
{
	tree[i].l=l,tree[i].r=r;
	if(l==r)
	{
		scanf("%d",&tree[i].MAX);
		a[l]=tree[i].MAX;
		return ;
	}
	int mid=l+r>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
	tree[i].MAX=max(tree[i<<1].MAX,tree[i<<1|1].MAX);
}

int query(int i,int l,int r,int v)
{
	if(tree[i].l==tree[i].r)
		return tree[i].l;
	int mid=tree[i].l+tree[i].r>>1;
	if(tree[i<<1|1].MAX>=v)
		return query(i<<1|1,l,r,v);
	else if(l<=mid&&tree[i<<1].MAX>=v)
		return query(i<<1,l,r,v);
	else
		return -1;
}

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

 

posted @ 2019-09-10 15:59  Shmilky  阅读(31)  评论(0编辑  收藏  举报