POJ 2182 Lost Cows

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.