XKC's basketball team (+ monotone stack half)

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

The ability of the iii-th person is wiw_iwi , and if there is a guy whose ability is not less than wi+mw_i+mwi+m stands on his right , he will become angry. It means that the jjj-th person will make the iii-th person angry if j>ij>ij>i and wj≥wi+mw_j \ge w_i+mwjwi+m.

We define the anger of the iii-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-11 .

Please calculate the anger of every team member .

Input

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

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

Output

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

Sample input

6 1
3 4 5 6 2 10

Sample Output

43210-1 

solving ideas: Stack monotonically from the back half + maintains a monotonically increasing stack can then binary search!
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int n,m;
 4 const int maxn=5e5+5;
 5 int arr[maxn];
 6 int sta[maxn];
 7 int top=0;
 8 map<int,int> ma;
 9 
10 int Binary(int left,int right,int value){
11     int ans=-1;
12     while(left<=right){
13         int mid=left+right>>1;
14         if(arr[sta[mid]]>=value) ans=mid,right=mid-1;
15         else left=mid+1;
16     }
17     return ans;
18 }
19 
20 int main(){
21     ios::sync_with_stdio(false);
22     cin>>n>>m;
23     for(int i=1;i<=n;i++) cin>>arr[i];
24     int= Top . 1 ;
 25      STA [Top] = n-; mA [n-] = - . 1 ;
 26 is      for ( int I = N- . 1 ; I> = . 1 ; i-- ) {
 27          IF (ARR [I]> ARR [ STA [Top]] - m) mA [I] = - . 1 ;
 28          the else {
 29              int in Flag Binary = ( . 1 , Top, ARR [I] + m);    // find the position this number greater than or equal leftmost
 30  //             COUT << ARR [STA [In Flag]] << endl; 
31 is              mA [I] = STA [In Flag] -I- . 1 ;
 32          }
 33 is          IF(arr[i]>arr[sta[top]]) sta[++top]=i;
34     }
35     for(int i=1;i<=n;i++){
36         printf("%d%c",ma[i],i==n?'\n':' ');
37     }
38     return 0;
39 }
40 
41 //10 1
42 //3 4 5 6 2 20 10 30 40 50
View Code

 

Guess you like

Origin www.cnblogs.com/qq-1585047819/p/11516667.html
Recommended