https://nanti.jisuanke.com/t/41387
解:
离散化+线段树。
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin); 6 #include <bitset> 7 #include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 23 #define fo(a,b,c) for(register int a=b;a<=c;++a) 24 #define fr(a,b,c) for(register int a=b;a>=c;--a) 25 #define mem(a,b) memset(a,b,sizeof(a)) 26 #define pr printf 27 #define sc scanf 28 #define ls rt<<1 29 #define rs rt<<1|1 30 typedef long long ll; 31 void swapp(int &a,int &b); 32 double fabss(double a); 33 int maxx(int a,int b); 34 int minn(int a,int b); 35 int Del_bit_1(int n); 36 int lowbit(int n); 37 int abss(int a); 38 //const long long INF=(1LL<<60); 39 const double E=2.718281828; 40 const double PI=acos(-1.0); 41 const int inf=(1<<30); 42 const double ESP=1e-9; 43 const int mod=(int)1e9+7; 44 const int N=(int)1e6+10; 45 46 int Max[N<<2]; 47 int ans[N]; 48 ll a[N],ta[N],tb[N]; 49 int LS(int n) 50 { 51 int m=0; 52 for(int i=1;i<=n;++i) 53 tb[++m]=ta[i]; 54 sort(tb+1,tb+1+m); 55 m=unique(tb+1,tb+1+m)-tb-1; 56 for(int i=1;i<=n;++i) 57 ta[i]=lower_bound(tb+1,tb+1+m,ta[i])-tb; 58 return m; 59 } 60 void up(int rt) 61 { 62 Max[rt]=max(Max[rt<<1],Max[rt<<1|1]); 63 } 64 65 void update_dot(int pos,int V,int l,int r,int rt) 66 { 67 if(l==r) 68 { 69 Max[rt]=max(Max[rt],V); 70 return; 71 } 72 73 int mid=(l+r)>>1; 74 if(pos<=mid) 75 update_dot(pos,V,l,mid,rt<<1); 76 else 77 update_dot(pos,V,mid+1,r,rt<<1|1); 78 up(rt); 79 } 80 81 int Quert_max(int L,int R,int l,int r,int rt) 82 { 83 if(L<=l&&r<=R) 84 return Max[rt]; 85 int mid=(l+r)>>1; 86 int ans=0; 87 if(L<=mid) 88 ans=max(ans,Quert_max(L,R,l,mid,rt<<1)); 89 if(R>mid) 90 ans=max(ans,Quert_max(L,R,mid+1,r,rt<<1|1)); 91 return ans; 92 } 93 94 int main() 95 { 96 int n; 97 ll m; 98 sc("%d%lld",&n,&m); 99 for(int i=1;i<=n;++i) 100 sc("%lld",&a[i]),ta[i]=a[i],ta[i+n]=a[i]+m; 101 LS(2*n); 102 ans[n]=-1; 103 update_dot(ta[n],n,1,2*n,1); 104 for(int i=n-1;i>=1;--i) 105 { 106 int temp=Quert_max(ta[i+n],2*n,1,2*n,1); 107 int pos=temp; 108 if(pos<i) 109 ans[i]=-1; 110 else 111 ans[i]=pos-i+1-2; 112 update_dot(ta[i],i,1,2*n,1); 113 } 114 for(int i=1;i<=n;++i) 115 pr("%d%c",ans[i],i==n?' ':' '); 116 return 0; 117 } 118 119 /**************************************************************************************/ 120 121 int maxx(int a,int b) 122 { 123 return a>b?a:b; 124 } 125 126 void swapp(int &a,int &b) 127 { 128 a^=b^=a^=b; 129 } 130 131 int lowbit(int n) 132 { 133 return n&(-n); 134 } 135 136 int Del_bit_1(int n) 137 { 138 return n&(n-1); 139 } 140 141 int abss(int a) 142 { 143 return a>0?a:-a; 144 } 145 146 double fabss(double a) 147 { 148 return a>0?a:-a; 149 } 150 151 int minn(int a,int b) 152 { 153 return a<b?a:b; 154 }