Poj 2481 cows tree array for all users

Source: Internet
Author: User

See the interval

1 --------------

2 -------------

3 --------

4 -----

Sort by Y first and then by X;

After sorting out the order, the question is the same as that of stars.

A traversal of the sorted array can only exist in the elements that have already been traversed because of the fact that the range of the currently traversed element strong. Therefore, we can safely sum the tree array directly.

Then, the values of the vertices that govern X in the tree array are updated backward, because these locations all have a stronger interval than the left endpoint;

Then pay attention to the processing of the same interval.

Running for MSCode. It actually feels pretty fast *-*

View code

 # Include  <  Stdio. h  > 
# Include < String . H >
# Include < Algorithm >
Using Namespace STD;
# Define Maxcompute 100010
Int C [Max];
Int Val [Max];
Struct Point {
Int X, Y;
Int ID;
} A [Max];
Int CMP (point a, point B)
{
If (A. Y = B. Y)
Return A. x < B. X;
Return A. Y > B. Y;
}
Int Lowbit ( Int X)
{
Return X & ( - X );
}
Void Update ( Int X, Int D)
{
While (X <= Max)
{
C [x] + = D;
X + = Lowbit (X );
}
}
Int Sum ( Int X)
{
Int Ans = 0 ;
While (X > 0 )
{
Ans + = C [x];
X -= Lowbit (X );
}
Return Ans;
}
Int Main ()
{
Int N, I, X, Y;
While (Scanf ( " % D " , & N), n)
{
Memset (C, 0 , Sizeof (C ));
Memset (Val, 0 , Sizeof (VAL ));
For (I = 0 ; I < N; I ++ )
{
Scanf ( " % D " , & A [I]. X, & A [I]. y );
A [I]. ID = I;
A [I]. x ++ ;
}
Sort (a, + N, CMP );
Val [[ 0 ]. ID] = Sum ([ 0 ]. X );
Update ([ 0 ]. X, 1 );
For (I = 1 ; I < N; I ++ )
{
If (A [I]. x = A [I - 1 ]. X && A [I]. Y = A [I - 1 ]. Y)
{
Val [A [I]. ID] = Val [A [I - 1 ]. ID];
Update (A [I]. X, 1 );
}
Else
{
Val [A [I]. ID] = Sum (A [I]. X );
Update (A [I]. X, 1 );
}
}
For (I = 0 ; I < N; I ++ )
{
If ( ! I)
Printf ( " % D " , Val [I]);
Else Printf ( " % D " , Val [I]);
}
Printf ( " \ 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.