2019亚洲区域赛徐州网络赛 E 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 i元素的愤怒值定义为:在 i i i右侧的所有满足 a [ j ] > = a [ i ] + m a[j]>=a[i]+m a[j]>=a[i]+m中最大的 j − i − 1 j-i-1 ji1,若不存在这样的数则值为 − 1 -1 1。输出每个元素的愤怒值。

思路:也算是签到题吧。用线段树维护区间最大值。那么要查询的其实就是 [ i + 1 , n ] [i+1,n] [i+1,n]内满足 a [ j ] > = a [ i ] + m a[j]>=a[i]+m a[j]>=a[i]+m的最大的 j j 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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值