1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 500006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , k; int A[MAXN] , Q[MAXN];
int T[MAXN << 2]; void add( int rt , int l , int r , int x , int c ) { if( l == r ) { T[rt] = c; return; } int m = l + r >> 1; if( x <= m ) add( rt << 1 , l , m , x , c ); else add( rt << 1 | 1 , m + 1 , r , x , c ); T[rt] = max( T[rt << 1] , T[rt << 1 | 1] ); } int que( int rt , int l , int r , int L , int R ) { if( L <= l && R >= r ) return T[rt]; int m = l + r >> 1 , re = 0; if( L <= m ) re = max( re , que( rt << 1 , l , m , L , R ) ); if( R > m ) re = max( re , que( rt << 1 | 1 , m + 1 , r , L , R ) ); return re; }
vi G[MAXN]; int deg[MAXN] , as[MAXN];
void solve() { cin >> n >> k; rep( i , 1 , n ) scanf("%d",A + i) , Q[A[i]] = i; rep( i , 1 , n ) { int t = que( 1 , 1 , n , Q[i] , min( Q[i] + k - 1 , n ) ); if( t ) G[Q[i]].pb( Q[t] ) , ++ deg[Q[t]]; t = que( 1 , 1 , n , max( 1 , Q[i] - k + 1 ) , Q[i] ); if( t ) G[Q[i]].pb( Q[t] ) , ++ deg[Q[t]]; add( 1 , 1 , n , Q[i] , i ); } priority_queue<int> q; rep( i , 1 , n ) if( !deg[i] ) q.push( i ); vi re; while( !q.empty() ) { int u = q.top(); q.pop(); for( int v : G[u] ) { -- deg[v]; if( !deg[v] ) q.push( v ); } re.pb( u ); } reverse( all( re ) ); rep( i , 1 , n ) as[re[i - 1]] = i; rep( i , 1 , n ) printf("%d\n",as[i]); }
signed main() {
solve(); }
|