# Lena Sort

### Problem Statement :

```Lena developed a sorting algorithm described by the following pseudocode:

lena_sort(array nums) {
if (nums.size <= 1) {
return nums;
}
pivot = nums[0];
array less;
array more;
for (i = 1; i < nums.size; ++i) {
// Comparison
if (nums[i] < pivot) {
less.append(nums[i]);
}
else {
more.append(nums[i]);
}
}
sorted_less = lena_sort(less);
sorted_more = lena_sort(more);
ans = sorted_less + pivot + sorted_more;

return ans;
}
We consider a comparison to be any time some nums[i] is compared with pivot.

You must solve q queries where each query i consists of some len(i) and ci. For each query, construct an array of len(i) distinct elements in the inclusive range between 1 and 10^9 that will be sorted by lena_sort in exactly ci comparisons, then print each respective element of the unsorted array as a single line of leni space-separated integers; if no such array exists, print -1 instead.

Input Format

The first line contains a single integer denoting q (the number of queries).
Each line i of the q subsequent lines contains two space-separated integers describing the respective values of len(i) (the length of the array) and ci (the number of comparisons) for query i.

Constraints

1 <= q <= 10^5
1 <= len(i) <= 10^5
0 <= ci <= 10^9
1 <=  the sum of len(i) over all queries <= 10^6

Output Format

Print the answer to each query on a new line. For each query i, print len(i) space-separated integers describing each respective element in an unsorted array that Lena's algorithm will sort in exactly ci comparisons; if no such array exists, print -1 instead.```

### Solution :

```                            ```Solution in C :

In C++ :

#include <bits/stdc++.h>

using namespace std;

#ifdef WIN32
#define I64 "%I64d"
#else
#define I64 "%lld"
#endif

typedef long long ll;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int(s.size()))
#define fname "a"
#define MAXN 200002

int n, k;
int a[MAXN];
ll d[MAXN];
ll f[MAXN];

void go(int l, int r, int k, int ff) {
if (l > r) return;
if (l == r) {
a[l] = ff;
return;
}
int n = r - l + 1;
k -= n - 1;
for (int i = 0; i < n; ++i) {
if (d[i] + d[n - 1 - i] <= k && f[i] + f[n - 1 - i] >= k)
{
a[l] = ff + i;
int k1 = d[i];
int k2 = k - d[i];
if (k2 > f[n - 1 - i]) {
k1 += k2 - f[n - 1 - i];
k2 = f[n - 1 - i];
}
go(l + 1, l + i, k1, ff);
go(l + i + 1, r, k2, ff + i + 1);
return;
}
}
}

inline void solve()
{
scanf("%d%d", &n, &k);
if (k < d[n] || k > f[n]) {
puts("-1");
return;
}
go(0, n - 1, k, 0);
for (int i = 0; i < n; ++i)
printf("%d%c", a[i] + 1, " \n"[i + 1 == n]);
}

int main()
{
#ifdef LOCAL
freopen(fname".in", "r", stdin);
freopen(fname".out", "w", stdout);
#endif

for (int i = 2; i < MAXN; ++i) {
d[i] = i - 1 + d[i / 2] + d[(i - 1) / 2];
f[i] = 1LL * (i - 1) * i / 2;
}

int tt;
scanf("%d", &tt);
for (int t = 0; t < tt; ++t)
solve();

return 0;
}

In Java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
long[] mins = new long[100001];
long[] maxes = new long[100001];
mins[2] = 1;
maxes[2] = 1;
for (int i = 3; i <= 100000; i++) {
mins[i] = i-1+mins[(i-1)/2]+mins[(i-1)-(i-1)/2];
maxes[i] = maxes[i-1]+i-1;
}
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int a0 = 0; a0 < q; a0++){
int len = in.nextInt();
int c = in.nextInt();
if (maxes[len]<c||mins[len]>c) {
System.out.println(-1);
continue;
}
System.out.println(portion(len, c, 1, mins, maxes, new StringBuilder()));
}
}

public static StringBuilder portion(int len, long c, int offset, long[] mins, long[] maxes, StringBuilder ans) {
if (len==0) {
return ans;
}
if (len==1) {
ans.append(offset+" ");
return ans;
}
int pivot = 0;
c -= len-1;
while (mins[pivot]+mins[len-pivot-1]>c||maxes[pivot]+maxes[len-pivot-1]<c)
pivot++;
long newc = mins[pivot];
while (mins[len-pivot-1]>c-newc||maxes[len-pivot-1]<c-newc)
newc++;
ans.append((pivot+offset)+" ");
portion(pivot, newc, offset, mins, maxes, ans);
portion(len-pivot-1, c-newc, offset+pivot+1, mins, maxes, ans);
return ans;
}
}```
```

## Palindromic Subsets

Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a . Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms: 1 i j t: All letters in the inclusive range from i t

## Counting On a Tree

Taylor loves trees, and this new challenge has him stumped! Consider a tree, t, consisting of n nodes. Each node is numbered from 1 to n, and each node i has an integer, ci, attached to it. A query on tree t takes the form w x y z. To process a query, you must print the count of ordered pairs of integers ( i , j ) such that the following four conditions are all satisfied: the path from n

## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

## The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever