STL balancing tree data structure container

Source: Internet
Author: User

1. Map

Key can correspond to only one ing value. It supports insertion, deletion, search (count, find), modification (delete this value first, and then insert the modified value), sorting, ing (discrete) multimap the same key value can have multiple ing

View code

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <map>

using namespace std;
map<int, int>mp;

int main( )
{
int N,M, t;
char str[1000];
while( scanf("%d%d",&N,&M) != EOF )
{
mp.clear( );
for( int i = 1; i <= N; i++)
{
scanf("%d",&t);
mp[t]++;
}
for( int i = 1; i <= M; i++)
{
scanf("%s",str);
if( str[0] == 'D' )
scanf("%d",&t),mp.erase(t);
else if( str[0] == 'F')
{
scanf("%d",&t);
if( mp.find(t) != mp.end( ))
puts("1");
else
puts("-1");
}
else if( str[0] == 'P')
{
map<int,int>::iterator iter = mp.begin( );
printf("%d",iter->first);
for( int i = 1; i < iter->second; i++)
printf(" %d",iter->first);
while( iter++ != mp.end( ))
{
for( int i = 1; i <= iter->second; i++)
printf(" %d",iter->first);
}
puts("");
}
}
}
return 0;

}

2. Set/Multiset

A set is a container. The values of the elements contained in a set are unique. This is useful when collecting a specific value of data. Elements in the set are arranged in a certain order and used as instances in the set. A collection is organized by a linked list. The insert and delete operations are faster than the vector operations, but the query or addition of elements at the end is somewhat slow. The specific implementation adopts the data structure of the Binary Tree of the red and black trees. (Baidu encyclopedia)

The element values contained by Multiset are not unique.

View code

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
using namespace std;

multiset<int>p;

int main( )
{
int N,M, t;
char str[1000];
while( scanf("%d%d",&N,&M) != EOF )
{
p.clear( );
for( int i = 1; i <= N; i++)
{
scanf("%d",&t);
p.insert( t );
}
for( int i = 1; i <= M; i++)
{
scanf("%s",str);
if( str[0] == 'D' )
scanf("%d",&t),p.erase(t);
else if( str[0] == 'F')
{
scanf("%d",&t);
if( p.find(t) != p.end( ))
puts("1");
else
puts("-1");
}
else if( str[0] == 'P')
{
multiset<int>::iterator iter = p.begin( );
printf("%d",*iter);
while( ++iter != p.end( ))
{
printf(" %d",*iter);

}
puts("");
}
}
}
return 0;

}

3. hnoi 2004

Powerful set and lower_bound applications

View code

# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <set>
# Include <algorithm>

Using namespace STD;

# Define mod 1000000.
Set <int> P;
Typedef set <int >:: iterator it;

Const int INF = ~ 0u> 2;

// Const int INF = 0x7f7f7f7f;
/* An error is returned when the value is assigned. You can change the value to a larger or smaller value.
The reason may be that the inserted pet will be deleted when its feature value is set to this.
*/
Int main ()
{
Int N, A, B, C, sum = 0;
Scanf ("% d", & N );
P. insert (INF), P. insert (-INF );
While (n --)
{
Scanf ("% d", & A, & B );
If (P. Size () = 2)
P. insert (B), c =;
Else if (a = C)
P. insert (B );
Else
{
It x = P. lower_bound (B );
Int r = * X-B;
Int L = B-* (-- X );
If (L <= r)
Sum + = L, P. Erase (X );
Else
Sum + = r, P. Erase (++ X );
If (sum> = mod)
Sum % = MOD;
}
}
Printf ("% d \ n", sum );
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.