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
题意:n个数,问存在一个i,j,其中i<j,a[j]>=a[i]+m,那么最远的j与i之间隔了几个数;
分析:
线段树,去维护最大值,和最大值的位置,优先去搜索线段树的右子树,因为这样保证了搜索的是最远的;
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N=5e5+10;
//const int N=100;
const int INF=0x3f3f3f3f;
struct node{
int val,ip,l,r;
}tr[N<<2];
int a[N];
void push_up(int id){
if(tr[id<<1].val>=tr[id<<1|1].val)
tr[id].ip=tr[id<<1].ip,tr[id].val=tr[id<<1].val;
else
tr[id].ip=tr[id<<1|1].ip,tr[id].val=tr[id<<1|1].val;
}
void build(int id,int l,int r){
tr[id].l=l,tr[id].r=r;
if(l==r) {
scanf("%d",&a[l]);
tr[id].ip=l;
tr[id].val=a[l];
return ;
}
int mid=(l+r)>>1;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
push_up(id);
}
int query(int id,int val){
if(tr[id].l==tr[id].r) return tr[id].ip;
int maxp=-1;
if(tr[id<<1|1].val>val)
return query(id<<1|1,val);
else
return query(id<<1,val);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int n,m;
scanf("%d%d",&n,&m);
build(1,1,n);
for(int i=1;i<=n;i++){
int ans=query(1,a[i]+m);
if(ans<=i)
printf("-1%c",i==n?'\n':' ');
else
printf("%d%c",ans-i-1,i==n?'\n':' ');
}
return 0;
}