https://nanti.jisuanke.com/t/41387
解:
离散化+线段树。
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
#include <bitset>
#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
#define fo(a,b,c) for(register int a=b;a<=c;++a)
#define fr(a,b,c) for(register int a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
void swapp(int &a,int &b);
double fabss(double a);
int maxx(int a,int b);
int minn(int a,int b);
int Del_bit_1(int n);
int lowbit(int n);
int abss(int a);
//const long long INF=(1LL<<60);
const double E=2.718281828;
const double PI=acos(-1.0);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; int Max[N<<];
int ans[N];
ll a[N],ta[N],tb[N];
int LS(int n)
{
int m=;
for(int i=;i<=n;++i)
tb[++m]=ta[i];
sort(tb+,tb++m);
m=unique(tb+,tb++m)-tb-;
for(int i=;i<=n;++i)
ta[i]=lower_bound(tb+,tb++m,ta[i])-tb;
return m;
}
void up(int rt)
{
Max[rt]=max(Max[rt<<],Max[rt<<|]);
} void update_dot(int pos,int V,int l,int r,int rt)
{
if(l==r)
{
Max[rt]=max(Max[rt],V);
return;
} int mid=(l+r)>>;
if(pos<=mid)
update_dot(pos,V,l,mid,rt<<);
else
update_dot(pos,V,mid+,r,rt<<|);
up(rt);
} int Quert_max(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return Max[rt];
int mid=(l+r)>>;
int ans=;
if(L<=mid)
ans=max(ans,Quert_max(L,R,l,mid,rt<<));
if(R>mid)
ans=max(ans,Quert_max(L,R,mid+,r,rt<<|));
return ans;
} int main()
{
int n;
ll m;
sc("%d%lld",&n,&m);
for(int i=;i<=n;++i)
sc("%lld",&a[i]),ta[i]=a[i],ta[i+n]=a[i]+m;
LS(*n);
ans[n]=-;
update_dot(ta[n],n,,*n,);
for(int i=n-;i>=;--i)
{
int temp=Quert_max(ta[i+n],*n,,*n,);
int pos=temp;
if(pos<i)
ans[i]=-;
else
ans[i]=pos-i+-;
update_dot(ta[i],i,,*n,);
}
for(int i=;i<=n;++i)
pr("%d%c",ans[i],i==n?'\n':' ');
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}