#include <bits/stdc++.h>
using namespace std;
const int N = 50001; typedef long long ll; int n; int a[ N ]; ll ans[ 2 ][ N ];
#define fi first
#define se second
#define pb push_back
typedef pair<int,int> pii;
ll tme; int ner, idx, ls, val, k, it, BITree[ N + 1 ];
/// Updates a node in Binary Index Tree (BITree) at given index
/// in BITree. The given value 'val' is added to BITree[i] and
/// all of its ancestors in tree.
/// Source: https://w...content-available-to-author-only...s.org/binary-indexed-tree-or-fenwick-tree-2/
void BITupdate( int index, int val, int n )
{
/// index in BITree[] is 1 more than the index in arr[]
index++;
/// Traverse all ancestors and add 'val'
while ( index <= n )
{
/// Add 'val' to current node of BI Tree
BITree[ index ] += val;
/// Update index to that of parent in update View
index += index & (-index);
}
}
/// Returns sum of arr[0..index]. This function assumes
/// that the array is preprocessed and partial sums of
/// array elements are stored in BITree[].
int BITquery( int index )
{
int sum = 0; /// Initialize result
/// index in BITree[] is 1 more than the index in arr[]
index++;
/// Traverse ancestors of BITree[index]
while( index > 0 )
{
/// Add current element of BITree to sum
sum += BITree[ index ];
// Move index to parent node in getSum View
index -= index & (-index);
}
return sum;
}
void solution1()
{
vector<pii> arr;
for( int j = 0, i = 1; i <= n; j = i++ )
{
arr.pb({a[ j ],i});
/* update the i-th index that it is initially present and the i-th
task has not been completed
1-not completed
0-completed
*/
BITupdate( i, 1, n );
}
/// sorting in increasing order
sort( arr.begin() , arr.end() );
for( int i = 0 ; i < n ; i++)
{
val = arr[i].fi;
idx = arr[i].se;
/// ner :- number of elements(tasks) remaining
ner = n-i;
/* find out number of task before current task which are not completed in the original array */
ls = BITquery( idx );
/* iteration required to make the current task zero = value of the task
so if the iteration is not the 1 less than val then we need to make it
come to iteration 1 less than the iteration required
eg:- val - 5 , it = 2 , then we make it = 4 by adding ner*(5-2-1) = ner*4
*/
if( val-1 > it )
{
tme += (val - 1 - it)*1ll*ner ;
}
/* in tme add the position of the current element
after considering how many task has been done.
*/
ans[0][idx] = ls + tme;
/* k is for counting how many elements have same value as the current element */
k = 0;
for( int j = i+1 ; j < n ; j++)
{
if( val == arr[j].fi )
{
/* if element has same value then it's iteration will be equal
to the iteration of current element
then just add the position of the task in tme to get it's answer
*/
ls = BITquery( arr[j].se );
k++;
ans[0][arr[j].se] = tme + ls;
}
else
{
break;
}
}
for( int j = i ; j < n ; j++)
{
if( val != arr[j].fi )
{
break;
}
/* updating values to -1 because the task have been completed
and won't be considered in next iteration */
BITupdate( idx, -1, n );
}
/* make iteration equal to current value , similar update tme
for representing time past till current iteration and update i
*/
tme += ner;
it = val;
i = i+k;
}
tme = 0, memset( BITree, 0, sizeof BITree ); // reset tme and bit
}
void solution2()
{
struct pending_tasks_t: vector< int >
{
int rank( int i )
{
return lower_bound( begin(), end(), i ) - begin();
}
void remove_task( int i )
{
erase( lower_bound( begin(), end(), i ) );
}
} p; ll time = n; vector< pair< int, int > > task;
for( int i = 0, j = 1; i < n; i = j++ )
if ( a[ i ] == 1 )
ans[ 1 ][ j ] = j;
else
task.emplace_back( a[ i ], j ), p.push_back( j );
sort( task.begin(), task.end() );
for( int i = 0, it = 2, k = 0, m = task.size(), remaining = m, t; i < m; i += k, it = t + 1, k = 0 )
{
time += ( ( t = task[ i ].first ) - it ) * ll( remaining ) + 1;
for( int j = i, q; j < m and task[ j ].first == t; j++, k++ )
q = task[ j ].second, ans[ 1 ][ q ] = time + p.rank( q );
for( int j = 0; j < k; j++ )
p.remove_task( task[ i + j ].second );
time += remaining - 1, remaining -= k;
}
}
inline void write_solution( int i )
{
const string eos = " \n";
for( int j = 1; j <= n; j++ )
cout << ans[ i ][ j ] << eos[ j < n ];
}
void write_counterexample( uint64_t seed, int test_cases )
{
cout << endl << test_cases << " random test case(s) generated." << endl;
cout << endl << "random number generator seed: " << seed << endl;
cout << endl << "A counterexample was found with different answers:";
for( int i = 0; i < n; i++ )
cout << ' ' << a[ i ];
cout << endl;
for( int i = 0, j = 1; i < 2; i = j++ )
cout << endl << "solution " << j << ": ", write_solution( i ), cout << endl;
}
int main()
{
int random; cin >> random;
if ( random )
{
int max_time, max_tests; uint64_t seed; cin >> n >> max_time >> max_tests >> seed;
assert( n > 0 and n < N and max_time > 0 and max_tests > 0 );
if ( seed == 0 )
seed = chrono::high_resolution_clock::now().time_since_epoch().count();
bool found = false; int test_cases = 0;
for( mt19937_64 random_number_generator( seed ); not found and test_cases < max_tests; test_cases++ )
{
for( int i = 0; i < n; i++ )
a[ i ] = 1 + random_number_generator() % max_time;
solution1(),
solution2();
for( int i = 1; i <= n and not found; i++ )
if ( ans[ 0 ][ i ] != ans[ 1 ][ i ] )
found = true;
}
if ( found )
write_counterexample( seed, test_cases );
else
cout << "No counterexample was found";
}
else
{
const function< void() > f[] = { solution1, solution2 };
int solution; cin >> solution >> n;
solution--, assert( ( solution == 0 or solution == 1 ) and ( n > 0 and n < N ) );
for( int i = 0; i < n; i++ )
cin >> a[ i ];
f[ solution ](), write_solution( solution );
}
}