#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;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()
const int mod = 1000000007;
const int N = 800005;
ll a[N+2], dp[N+2], where[N+2];
struct Segtree{
vector<pair<int, int>> t;
int off = 1;
Segtree(int n){
while(off < n)off <<= 1;
t.resize(2 * off, {0, 0});
}
pair<int, int> query(int n, int ql, int qh, int nl, int nh){
if(ql > nh || nl > qh)return {0, 0};
if(ql <= nl && qh >= nh)return t[n];
int m = (nl + nh) >> 1;
return max(query(n * 2, ql, qh, nl, m), query(n * 2 + 1, ql, qh, m + 1, nh));
}
void update(int u, int v){
u += off, v += off;
swap(t[u], t[v]);
t[v].first++;
t[v].second = v - off;
while(u >>= 1)t[u] = max(t[u * 2], t[u * 2 + 1]);
while(v >>= 1)t[v] = max(t[v * 2], t[v * 2 + 1]);
}
};
void solve(){
int n, m;
cin>>n>>m;
for(int i = 0; i < n; i++){
cin>>a[i];
}
Segtree segtree(n + 1);
int maxidx = 0;
for(int i = 0; i < n; i++){
pair<int, int> ans = segtree.query(1, 1, a[i], 0, segtree.off - 1);
if(ans == (pair<int, int>){0, 0}){
segtree.update(0, a[i]);
where[a[i]] = ++maxidx;
cout<<where[a[i]]<<' ';
continue;
}
segtree.update(ans.second, a[i]);
where[a[i]] = where[ans.second];
cout<<where[a[i]]<<' ';
}
assert(maxidx <= m + 1);
cout<<'\n';
for(int i = 1; i <= n; i++){
cout<<where[i]<<' ';
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int tt = 1;
//cin>>tt;
while(tt--){
solve();
cout<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |