Source Code
//#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>

void Fast()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
void File() {
#ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
#endif
}
typedef long long ll;
struct ah
{
    int x,y;
    bool operator <(ah o)const
    {
        if(x==o.x)return y>o.y;
        return x<o.x;
    }
};
void solve()
{
    int n,x;
    set<ah>s;
    cin>>n;
    vector<int>v(n+1),res(n+1);
    ah p;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i];
        p.x=i+v[i];
        p.y=i;
        s.insert(p);
    }
    int mn=n+1;
    auto it=s.end();
    do
    {
        it--;
        mn=min(it->y,mn);
        res[it->y]=mn;
        
    }while(it!=s.begin());
    
    for(int i=1;i<=n;i++)
    {
        p.x=i-v[i];
        p.y=0;
        auto it=s.upper_bound(p);
        if(it==s.end())x=-1;
        else x=res[it->y];
        cout<<x<<" ";
    }
    cout<<'\n';
}
int main()
{
    File();
    Fast();

    int t=1;
    cin>>t;
    for (int i = 1; i <= t;i++ )
    {
        //cout<<"Case "<<i<<": ";
        solve();
    }

}
Copy
Easy challenge? Ahmed_Aladdin
GNU G++17
143 ms
12.5 MB
Wrong Answer