#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define ll long long using namespace std; vector lena_sort(const vector& nums, int* cmp) { if (nums.size() <= 1) { return nums; } int pivot = nums[0]; vector less; vector more; for (int i = 1; i < nums.size(); ++i) { // Comparison (*cmp)++; if (nums[i] < pivot) { less.push_back(nums[i]); } else { more.push_back(nums[i]); } } vector sorted_less = lena_sort(less, cmp); vector sorted_more = lena_sort(more, cmp); vector ans; for(int i=0;i<(int)sorted_less.size();i++) ans.push_back(sorted_less[i]); ans.push_back(pivot); for(int i=0;i<(int)sorted_more.size();i++) ans.push_back(sorted_more[i]); return ans; } ll maxx[111111]; ll minn[111111]; // L-1+ int ans[111111]; bool Solve(ll ct, ll L, int offset, int arr_st_id) { if (L==1) { if (ct != 0) return false; ans[arr_st_id]=offset; return true; } //printf("%lld\n",L); // pick an x such that // L-1+maxx[x-1]+minn[L-x-1]=ct // offset,offset+1...offset+L-1 // offset,...,offset+x-1 for(int x=0;x<=L-1;x++) { ll mx,mn; mx=maxx[x]; mn=minn[L-1-x]; if (L-1+mx+mn<=ct && L-1+mx+maxx[L-1-x]>=ct) { //printf("Possible\n"); bool tmp1 = Solve(mx, x, offset,arr_st_id+1); bool tmp2 = Solve(ct-mx-L+1, L-1-x,offset+x+1, arr_st_id+x+1); ans[arr_st_id]=offset+x; return true; } if (L-1+minn[x]+minn[L-1-x]<=ct && L-1+minn[x]+maxx[L-1-x]>=ct) { //printf("Possible\n"); bool tmp1 = Solve(minn[x], x, offset, arr_st_id+1); bool tmp2 = Solve(ct-minn[x]-L+1, L-1-x,offset+x+1,arr_st_id+x+1); ans[arr_st_id]=offset+x; return true; } } return false; } int main() { while(0) { int n; cin>>n; vector A(n); for(int i=0;i cmps; do { int cmp=0; lena_sort(A,&cmp); cmps.insert(cmp); }while(next_permutation(A.begin(),A.end())); for(set::iterator it = cmps.begin(); it != cmps.end(); ++it) { printf("%d ",*it); } printf("\n"); } minn[0]=maxx[0]=0; for(int i=1;i<=100000;i++) { minn[i]=i-1+minn[(i-1)/2]+minn[i-1-(i-1)/2]; maxx[i]=i-1+maxx[i-1]; } // L-1+f(x)+f(L-1-x) int Q; cin>>Q; for(int q=1;q<=Q;q++) { int len,c; cin>>len>>c; //printf("%lld and %lld\n",minn[len],maxx[len]); bool ok = Solve(c,len,0,0); if (!ok) { printf("-1\n"); } else { for(int i=0;i