Editorial for Inaho II
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
The solution to this problem is simply an
One needs an efficient method to sum an
In addition, instead of using an
A sample recursive function in C++ is shown.
int binary_indexed_tree[10000005], prefix_product_array[11];
int query(const int N, int current_dimension, int pos, int idx[])
{
if (current_dimension == N)
return binary_indexed_tree[pos];
int sum = 0;
for (int x = idx[current_dimension]; x > 0; x-=x&-x)
sum += query(N, current_dimension+1, pos + (x-1) * prefix_product_array[current_dimension], idx);
return sum;
}
However, this function is too slow and TLEs. Instead of calling said recursive function query()
calls instead of adds them. Implementation is left as an exercise for the reader. Although the time complexity is the same for complexity analysis, the latter solution has a smaller constant factor.
An update statement should be approached similarly. A recursive function can be utilized to update a certain index. In fact, the above recursive function can be slightly modified into an update function, and this will not TLE since this function only needs to be called once per update statement.
An update recursive function in C++ is shown below.
int binary_indexed_tree[10000005], prefix_product_array[11], dimension_length[11], actual_array[10000005];
void update(const int N, int current_dimension, int pos, int idx[], int value)
{
if (current_dimension == N)
{
binary_indexed_tree[pos] += value;
return;
}
for (int x = idx[current_dimension]; x <= dimension_length[current_dimension]; x+=x&-x)
update(N, current_dimension+1, pos + (x-1) * prefix_product_array[current_dimension], idx, value);
}
Note that the time limit was set very strict such that solutions which were not optimized TLE's. On a worst case, the intended solution in C++ takes approximately 2 seconds.
Time Complexity:
Comments