Hdu 1556 Color the ball tree Array

Source: Internet
Author: User

This question indicates a given range of N. Color each vertex in A subinterval [A, B. Finally, we asked about the number of coloring times for each vertex.
This question seems to be able to be extended to multi-dimensional situations, but in multi-dimensional situations, you must use a tree array to sum up to speed up the process. In one-dimensional situations, you can simply sum up.
If you color the interval [A, B] for the first time, you can set nNum [nA] ++ and nNum [nB + 1. Because this has an I for any value of range [0, nA-1]
Both require nNum [1] + nNum [2] +... + nNum [I] = 0. For any value of range [nA, nB], I has nNum [1] + nNum [2] +... + nNum [I] = 0.
For any value of range [nB + 1, nN], nNum [1] + nNum [2] +... + nNum [I] = 0.
This is repeated multiple times. If the sum of nNum [1] + nNum [2] +... + nNum [I] represents the number of coloring times for each node I, then this question can be solved.
Use an example to verify that this is the case. The proof is omitted.
As for the tree array, there is a lot of information on the Internet. The tree array template is single, which is too convenient for coding.

The Code is as follows:
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;

Int nNum [100000 + 10];
Int nN;
Int LowBit (int nI)
{
Return nI & (-nI );
}

Void Add (int nI, int nAdd)
{
While (nI <= nN)
{
NNum [nI] + = nAdd;
NI + = LowBit (nI );
}
}

Int GetSum (int nI)
{
Int nAns = 0;

While (nI> 0)
{
NAns + = nNum [nI];
NI-= LowBit (nI );
}
Return nAns;
}

Int main ()
{
Int nA, nB;

While (scanf ("% d", & nN), nN)
{
Memset (nNum, 0, sizeof (nNum ));

For (int I = 1; I <= nN; ++ I)
{
Scanf ("% d", & nA, & nB );
Add (nA, 1 );
Add (nB + 1,-1 );
}
For (int I = 1; I <= nN; ++ I)
{
Printf ("% d % s", GetSum (I), I = nN? "\ N ":"");
}
}

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.