注意到题目给出的寻找 并求 ,实际上等价于建一棵大根笛卡尔树的过程。
证明:在笛卡尔树中,一个节点的左、右边界分别是它的父节点和某个祖先节点。显然,除了位于整棵树最左侧链(左边界为 0)和最右侧链(右边界为 0)的节点外,任意一个节点的 恰好就是它的父节点的权值。
由于需要给出一种构造,所以考虑强制规定笛卡尔树的形态来简化问题:
- 钦定 为根,并且没有右儿子。这样最右侧链只有 自身。
- 钦定所有贡献为 的节点全部挂在树的最左侧链上。
- 从大到小()考虑剩下的节点放置。对于节点 ,可以选择:
- 挂在当前左链的最下端:对 的贡献为 ,并提供 个空闲的右孩子位置。
- 挂在之前已放置的节点 下:对 的贡献为 ,消耗 个位置,并提供 个新的空闲位置。
这样就变成了凑数字的问题,考虑贪心+暴搜,为了保证复杂度,必须加入两个剪枝:
-
如果把剩下的节点全塞进当前最大的几个插槽里,都凑不够剩下的 ,直接回溯。具体实现可以通过树状数组维护当前空闲位置,利用倍增快速查询。若位置不足,由于大节点放入后会产生等于自身权值的新位置,可以用等差数列求和 计算出理论最大值。
-
在 DFS 过程中,剩余需要凑的 绝对不能为 。因为唯一能提供 贡献的位置是权值为 的节点,而它是最后一个被处理的,它产生位置时已经没有比它更小的节点可以挂载了。
Code
#include<bits/stdc++.h>
using namespace std;
#define rint register int
#define ll long long
int n,ans[2000005],lc[2000005],rc[2000005],tot,a[2000005];
ll K;
struct node_slot{
int u,type;
};
vector<node_slot>slots[2000005];
struct BIT{
int cnt[2000005];
ll sum[2000005];
inline int lowbit(rint x){
return x&-x;
}
inline void update(rint x,rint y){
rint z=x;
while(x<=n){
cnt[x]+=y;
sum[x]+=1ll*z*y;
x+=lowbit(x);
}
}
inline int query_cnt(rint x){
rint res=0;
while(x){
res+=cnt[x];
x-=lowbit(x);
}
return res;
}
inline ll query_sum(rint x){
ll res=0;
while(x){
res+=sum[x];
x-=lowbit(x);
}
return res;
}
}T;
inline int get(ll val){
if(val<=0)return 0;
val=min(val,1ll*n);
rint C=T.query_cnt(val);
if(!C)return 0;
rint pos=0,cur=0;
for(rint i=21;i>=0;i--){
if(pos+(1<<i)<=n&&cur+T.cnt[pos+(1<<i)]<C){
pos+=(1<<i);
cur+=T.cnt[pos];
}
}
return pos+1;
}
inline ll calc(rint mx){
if(!mx)return 0;
rint sz=T.query_cnt(n);
if(sz<=mx){
ll sum=T.query_sum(n);
rint k=mx-sz;
ll q=k/2,r=k%2;
sum+=1ll*(2*mx-q+1)*q;
if(r)sum+=mx-q;
return sum;
}else{
rint target=sz-mx,pos=0,ccnt=0;
ll csum=0;
for(rint i=21;i>=0;i--)if(pos+(1<<i)<=n&&ccnt+T.cnt[pos+(1<<i)]<=target)pos+=(1<<i),ccnt+=T.cnt[pos],csum+=T.sum[pos];
ll lsum=T.query_sum(n)-csum;
rint res=target-ccnt;
lsum-=1ll*res*(pos+1);
return lsum;
}
}
inline bool dfs_solve(rint x,ll k){
if(!x)return !k;
rint c=get(min(1ll*n,k));
while(c>0){
if(k-c!=1){ // 剪枝:剩余价值不能为1
T.update(x,2),T.update(c,-1);
if(k-c<=calc(x-1)){ // 上限剪枝
ans[x]=c;
if(dfs_solve(x-1,k-c))return 1;
}
T.update(c,1),T.update(x,-2);
}
c=get(c-1);
}
if(k!=1){
T.update(x,1);
if(k<=calc(x-1)){
ans[x]=0;
if(dfs_solve(x-1,k))return 1;
}
T.update(x,-1);
}
return 0;
}
inline void dfs(rint cur){
if(!cur)return ;
dfs(lc[cur]),a[++tot]=cur,dfs(rc[cur]);
}
inline void solve(){
cin>>n>>K;
if(n==1){
if(!K)cout<<"1\n";
else cout<<"-1\n";
return ;
}
if(n==2){
if(!K)cout<<"1 2\n";
else cout<<"-1\n";
return ;
}
for(rint i=1;i<=n;i++)T.cnt[i]=0,T.sum[i]=0,ans[i]=0,slots[i].clear();
T.update(n-1,1);
if(K>calc(n-2)||K==1){
cout<<"-1\n";
return ;
}
if(!dfs_solve(n-2,K)){
cout<<"-1\n";
return ;
}
for(rint i=1;i<=n;i++)lc[i]=rc[i]=0;
lc[n]=n-1;
rint lt=n-1;
slots[n-1].push_back({n-1,1});
for(rint x=n-2;x>=1;x--){
rint c=ans[x];
if(!c){
lc[lt]=x;
lt=x;
slots[x].push_back({x,1});
}else{
node_slot s=slots[c].back();
slots[c].pop_back();
if(!s.type)lc[s.u]=x;
else rc[s.u]=x;
slots[x].push_back({x,0});
slots[x].push_back({x,1});
}
}
tot=0,dfs(n);
for(rint i=1;i<=n;i++)cout<<a[i]<<(i==n?"":" ");
cout<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
rint T;cin>>T;
while(T--)solve();
return 0;
}