Hdu 3627 Giant

Source: Internet
Author: User

This question is about adding and deleting searches for sorted data in real time. It won't happen during the competition that day. I want to think that the balance tree can be done, but it is very difficult to write the Balance Tree.
The number of line segments can be done later. Although the data range is large, the data can be sorted and then discretized after all the data is read. The specific code is not written.
Today, my teammates found on the Internet a way to use map and set to get rid of this question. Previously, this method mainly used the upper_bound operations in map and set.
I forgot this thing. In this case, map and set can also be used to check the frontend and successor, but note that low_bound finds keys smaller than or equal. Note that this code is used
A map <int, set <int> set stores all coordinates, adds, deletes, and searches for subsequent operations. Because the elements to be searched are larger than x and larger than y
It is quite troublesome, and we need to search for it after loop x, but this will result in a relentless timeout. Then, there is an optimization to record the number of y, so when there is a large y, there is no need
Search, and then pass this question. However, if the size of x corresponding to a large y is small, this question cannot be solved. We can only use the line segment tree.
Now I think that using map and set to search for the front-end and the successor can actually get rid of some questions.

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

Map <int, set <int> MS; // stores x, y
Map <int, set <int> >:: iterator it;
Map <int, int> my; // stores the number of y
Set <int>: iterator msit;
Int main ()
{
Int nN;
Int nCase = 1;
Char szCmd [10];
Int nX, nY;
Int nTemp;

While (scanf ("% d", & nN), nN)
{
If (nCase> 1)
{
Printf ("\ n ");
}

Printf ("Case % d: \ n", nCase ++ );
Ms. clear ();
My. clear ();
While (nN --)
{
Scanf ("% s", szCmd );
Scanf ("% d", & nX, & nY );
If (szCmd [0] = 'A ')
{
If (my. find (nY) = my. end ())
{
My [nY] = 1;
}
Else
{
My [nY] ++;
}

If (ms. find (nX) = ms. end ())
{
MS [nX]. insert (nY );
}
Else
{
Msit = MS [nX]. find (nY );
If (msit = MS [nX]. end () // duplicate data appears
{
MS [nX]. insert (nY );
}
}
}
Else if (szCmd [0] = 'R ')
{
MS [nX]. erase (nY );
If (MS [nX]. size () = 0)
{
Ms. erase (nX );
}
My [nY] --;
If (my [nY] = 0)
{
My. erase (nY );
}
}
Else if (szCmd [0] = 'F ')
{
If (my. upper_bound (nY) = my. end ())
{
Printf ("-1 \ n ");
Continue;
}
While (true)
{
It = ms. upper_bound (nX );
If (it = ms. end () // a value greater than nX does not exist
{
Printf ("-1 \ n ");
Break;
}
NTemp = it-> first;
Msit = MS [nTemp]. upper_bound (nY );
If (msit = MS [nTemp]. end () // bigger than nY does not exist
{
NX = nTemp;
Continue; // Add x to continue future Query
}
Else
{
Printf ("% d \ n", nTemp, * msit );
Break;
}
}
}
}
}

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.