您的位置:首页 > 其它

【UVA】11525-Permutation(线段树水题)

2014-10-13 14:26 411 查看
1434365211525PermutationAcceptedC++0.1752014-10-13 06:27:29

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson pos<<1
#define rson pos<<1|1
const int maxn  = 55555;
int tr[maxn << 2];
int num[maxn << 2];
int array[maxn];
int sz,ss;
void BuildTree(int L,int R,int pos){
    if(L == R){
        tr[pos]  = 1;
        num[pos] = sz++;
        return;
    }
    int m = (L + R) >> 1;
    BuildTree(L,m,lson);
    BuildTree(m + 1,R,rson);
    tr[pos] = tr[lson] + tr[rson];
    return;
}
void UpDate(int aim,int l,int r,int pos){
    if(l == r){
        array[ss++] = num[pos];
        tr[pos] = 0;
        return;
    }
    int m = (l + r) >> 1;
    if(tr[lson] >= aim)
        UpDate(aim,l,m,lson);
    else
        UpDate(aim - tr[lson],m + 1,r,rson);
    tr[pos] = tr[lson] + tr[rson];
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,s;
        sz = 1;
        ss = 0;
        scanf("%d",&n);
        BuildTree(1,n,1);
        for(int i = 0; i < n; i++){
            scanf("%d",&s);
            UpDate(s + 1,1,n,1);
        }
        for(int i = 0; i < ss; i++){
            if(i) printf(" ");
            printf("%d",array[i]);
        }
        printf("\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: