Source Code
#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds; // pb_ds
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>


using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define EPS (1e-9)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define debug(x) cerr << #x << ": " << (x) << endl;

void _IO_() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#ifdef ELARABY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

//             N  E  S  W  NW  NE SE SW
//int dr[] = {-1, 0, 1, 0, -1, -1, 1, 1};
//int dc[] = {0, 1, 0, -1, -1, 1, 1, -1};


template<typename T>
void input(vector<T> &v) {
    for (T &i: v) {
        cin >> i;
    }
}

template<typename T>
void output(const vector<T> &v, const int inc = 0) {
    for (int i = 0; i < int(v.size()); ++i) {
        if (i) cout << ' ';
        cout << v[i] + inc;
    }
    cout << '\n';
}

template<typename T>
void output(const vector<pair<T, T>> &v, const int inc1 = 0, const int inc2 = 0) {
    for (int i = 0; i < int(v.size()); ++i) {
        cout << v[i].first + inc1 << ' ' << v[i].second + inc2 << '\n';
    }
}

void output(const string &s, const int st, const int ed) {
    for (int i = st; i < ed; ++i) {
        cout << s[i];
    }
    cout << '\n';
}
//////////////////------PROBLEM SOLUTIONS START FROM HERE------///////////////////


int main() {
    _IO_();
//    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        vector<int> a(n);
        vector<pair<int, int>> v(n);
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            v[i].first = i + a[i];
            v[i].second = i;
        }
        sort(all(v));
        for (int i = n - 2; i >= 0; --i) {
            v[i].second = min(v[i].second, v[i + 1].second);
        }

        for (int i = 0; i < n; ++i) {
            const int key = i - a[i];
            const int it = lower_bound(all(v), make_pair(key, -1)) - v.begin();
            if (it >= v.size()) cout << -1 << ' ';
            else cout << v[it].second + 1 << ' ';
        }
        cout << '\n';
    }

    return 0;
}
Copy
Easy challenge? Omar_Elaraby
GNU G++17
124 ms
4.0 MB
Accepted