POJ_2182
I did not expect that the line segment tree could be used in this way. It is true that the line segment tree is too flexible ......
This question can be converted into querying from the back to the front. For example, there are x cows whose numbers are smaller than the front of the current cow, then it should rank x + 1 in the sequence of the remaining number. Therefore, we can maintain a line segment tree. The Node value indicates the number of remaining segments in the current interval. When we look for a number greater than x + 1, we can find the number from top to bottom, delete this number after finding it, that is, update the node value from the bottom up.
#include<stdio.h>
#include<string.h>
#define MAXD 8010
int tree[4 * MAXD], N, M, q[MAXD];
void init()
{
int i, j;
memset(tree, 0, sizeof(tree));
for(M = 1; M < N + 2; M <<= 1);
for(i = 0, j = M + 1; i < N; i ++, j ++)
tree[j] = 1;
for(i = M - 1; i; i --)
tree[i] = tree[2 * i] + tree[2 * i ^ 1];
}
void solve()
{
int i, j, k;
q[0] = 0;
for(i = 1; i < N; i ++)
scanf("%d", &q[i]);
for(i = N - 1; i >= 0; i --)
{
k = q[i] + 1;
for(j = 1; j < M;)
{
if(tree[2 * j] >= k)
j <<= 1;
else
{
k -= tree[2 * j];
j = (j << 1) + 1;
}
}
q[i] = j - M;
while(j ^ 1)
{
tree[j] --;
j >>= 1;
}
}
for(i = 0; i < N; i ++)
printf("%d\n", q[i]);
}
int main()
{
while(scanf("%d", &N) == 1)
{
init();
solve();
}
return 0;
}