Title Description
thinking
It's an upgraded version of Mars.
In fact, the core idea is Cantor, but the full arrangement of 5e5 can not be accepted by birds, consider optimizing Cantor.
In fact, the number of Cantor is the number of arrays, according to n! = n (n 1) (n 2) 1n! = n* (n-1)* (n-2)* cdots*1n! = n(n_1)(n_2)1
Therefore, the first location can have n choices. Because the first number is selected, the second number is only (n-1) choice, and so on, exactly n. N! N! It corresponds to the decimal number. The process of the I I I bit is n_i+1n-i+1n_i+1.
Consider + m+m+m + m, i n fact, dictionary + m+m+m + m+m+m, you can unkownunkownunkown unkown system's lowest bit directly plus a m, let the post-analog addition carry can, the number a i + 1a_i + 1ai + 1ai + 1 + 1 1 on each bit of the decimal digit, corresponding to Cantor Expansion (a1+1) (a2+1) (a2+1) * (a1+1) * (a1) * (a2+1) * (a2+1) * (a2+1) * (a_2 + 1) * * \ CS * (dots * (a_n + 1 * (2+1)(a n+1)
Then the data structure is messy.
AC code
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #define gc getchar() #define ll long long using namespace std; const int N=5e5+10; template<class o> inline void qr(o &x) { x=0;char c=gc; while(c<'0'||c>'9')c=gc; while(c>='0'&&c<='9'){x=x*10+(c^48);c=gc;} } void qw(ll x) { if(x/10)qw(x/10); putchar(x%10+48); } ll a[N];int c[N],n; ll m; void add(int x){for(;x<=n;x+=x&-x)++c[x];} int ask(int x){int ans=0;for(;x;x-=x&-x)ans+=c[x];return ans;} struct segtree{int l,r,sum;}t[N*4];int cnt; void build(int &x,int l,int r) { x=++cnt; if(l==r){t[x].sum=1;return ;} int mid=(l+r)>>1; build(t[x].l,l,mid); build(t[x].r,mid+1,r); t[x].sum=t[t[x].l].sum+t[t[x].r].sum; } void change(int l,int r,int x,int pos) { --t[x].sum; if(l==r)return ; int mid=(l+r)>>1; if(pos<=mid)change(l,mid,t[x].l,pos); else change(mid+1,r,t[x].r,pos); } int query(int l,int r,int x,int k) { if(l==r)return l; int mid=(l+r)>>1; if(k<=t[t[x].l].sum)return query(l,mid,t[x].l,k); else return query(mid+1,r,t[x].r,k-t[t[x].l].sum); } int main() { //freopen("a4.in","r",stdin); // freopen("a4.out","w",stdout); qr(n),qr(m); for(int i=1;i<=n;i++)qr(a[i]); for(int i=n,x;i>=1;i--) { x=a[i];a[i]=ask(a[i]-1); add(x); } a[n]+=m; for(int i=n;i>=2;i--) { a[i-1]+=a[i]/(n-i+1); a[i]%=n-i+1; } int root=0; build(root,1,n); for(int i=1;i<=n;i++) { ++a[i]; a[i]=query(1,n,1,a[i]); qw(a[i]),putchar(' '); change(1,n,1,a[i]); } puts(""); return 0; }