Source Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll mx[1000000];
ll a[1000000];
int main() {
    ios_base::sync_with_stdio(0);
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        mx[0] = -1e12;
        for (int i=1;i<=n;i++){
            cin>>a[i];
            mx[i] = max(mx[i-1], a[i]+i);
        }
        for (int i=1;i<=n;i++){
            ll target = i-a[i];
            int l=1, r=n, ans=-1;
            while(l<=r){
                int mid = (l+r)/2;
                if (mx[mid] >= target) ans = mid, r= mid-1;
                else l=mid+1;
            }
            cout<<ans;
            if (i<n) cout<<' ';
            else cout<<'\n';
        }
    }
}
Copy
Easy challenge? RedNextCentury
GNU G++17
64 ms
4.8 MB
Accepted