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