#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
static const int inf = 999999999;
static const int maxn = 1e5 + 5;
static const ll mod = 998244353;
struct Tree{
int l, r;
bool f;
}tr[maxn << 2];
int a[maxn], idx[maxn], mp[maxn * 10], ans[maxn];
void pushUp(int x){
tr[x].f = tr[x << 1].f | tr[x << 1 | 1].f;
}
void build(int now, int l, int r){
tr[now].l = l;
tr[now].r = r;
tr[now].f = 0;
if(l == r) return ;
int m = l + r >> 1;
build(now << 1, l, m);
build(now << 1 | 1, m + 1, r);
}
void update(int now, int x){
if(tr[now].l == tr[now].r){
tr[now].f = 1;
return ;
}
if(tr[now << 1].r >= x) update(now << 1, x);
else update(now << 1 | 1, x);
pushUp(now);
}
int query(int now, int i, int j){
if(i > j) return 0;
if(!tr[now].f) return 0;
if(tr[now].l == tr[now].r) return tr[now].l;
if(tr[now << 1].r >= j) return query(now << 1, i, j);
if(tr[now << 1 | 1].l <= i) return query(now << 1 | 1, i, j);
int res = query(now << 1 | 1, i, j); //先跑右孩子
if(res) return res;
return query(now << 1, i, j);
}
int main(){
int t;
scanf("%d", &t);
while(t--){
memset(mp, 0, sizeof(mp));
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d", &idx[i]);
a[i] = idx[i];
}
sort(a + 1, a + n + 1);
int cnt = unique(a + 1, a + n + 1) - a - 1;
for(int i = 1; i <= n; ++i) idx[i] = lower_bound(a + 1, a + cnt + 1, idx[i]) - a;
build(1, 1, cnt);
for(int i = n; i >= 1; --i){
int id = query(1, 1, idx[i] - 1);
ans[i] = mp[id];
update(1, idx[i]);
mp[idx[i]] = i; //多個符合條件選擇下標最小的那個
}
for(int i = 1; i < n; ++i) printf("%d ", ans[i]);
printf("0\n");
}
return 0;
}
主席樹區間查詢比某個值次小的數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.