Source Code
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define all(v) v.begin(), v.end()
#define pb push_back    
#define sz(x) (int)(x).size()

#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
  cout << vars << " = ";
  string delim = "";
  (..., (cout << delim << values, delim = ", "));
  cout << '\n';
}

inline void solve() {
  // (i - j) <= (ai + aj)
  // -j - aj <= ai - i
  int n;
  cin >> n;
  vi ans(n + 1, -1), a(n + 1);
  set<pii> vals;
  for(int i = 1; i <= n; ++i) {
    cin >> a[i];
    vals.insert({a[i] - i, i});
  }
  for(int j = 1; j <= n; ++j) {
    int x = -j - a[j];
    vector<pii> v;
    for(auto it = vals.lower_bound({x, -1}); it != vals.end(); it = next(it)) {
      v.pb(*it);
    }
    for(auto p : v) {
      vals.erase(p);
      ans[p.second] = j;
    }
  }
  for(int i = 1; i <= n; ++i) {
    cout << ans[i] << ' ';
  }
  cout << '\n';
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  // freopen("input.txt", "r", stdin);
  int T = 1;
  cin >> T;
  while(T--) {
    solve();
  }
}
Copy
Easy challenge? noomaK
GNU G++17
428 ms
13.4 MB
Accepted