Language: C++

#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<algorithm>
#include<numeric>
#include<cstdlib>
using namespace std;
typedef string S;
typedef pair<int,S>P;
typedef list<P>L;
typedef map<char,L>B;
typedef map<char,vector<int> >C;
typedef map<S,pair<int,int> >D;
int SS(const P&l,const P&r){return l.first<r.first;}
int SB(const P&l,const P&r){return l.first>r.first;}
int main()
{
	int n,i,j,k,p,v;
	while(cin>>n,n)
	{
		B a,b;
		C c;
		D d;
		char y;
		S s,t,x;

		getline(cin,s);
		while(n--)
		{
			getline(cin,s);
			i=s.find(' ');
			j=s.find(' ',i+1);
			k=s.find(' ',j+1);
			x=s.substr(0,i);
			t=s.substr(i+1,j-i-1);
			y=s[j+1];
			p=atoi(s.substr(k+1).c_str());

			if(t=="SELL")
			{
				if(b[y].size() && 
					(
					stable_sort(b[y].begin(),b[y].end(),SB),
					b[y].front().first >= p && b[y].front().second!=x)
					)
				{
					v=(p+b[y].front().first)/2;
					c[y].push_back(v);
					d[b[y].front().second].first+=v;
					d[x].second+=v;
				}
				else a[y].push_back(P(p,x));
			}
			else
			{
				if(a[y].size() && 
					(
					stable_sort(a[y].begin(),a[y].end(),SS),
					a[y].front().first <= p && a[y].front().second!=x)
					)
				{
					v=(p+a[y].front().first)/2;
					c[y].push_back(v);
					d[a[y].front().second].second+=v;
					d[x].first+=v;
				}
				else b[y].push_back(P(p,x));
			}d.insert(make_pair(x,make_pair(0,0)));
		}

		for(C::iterator f=c.begin();f!=c.end();++f)
		{
			vector<int>::iterator g=f->second.begin(),
				h=f->second.end();
			cout<<f->first<<' '<<*min_element(g,h)
				<<' '<<accumulate(g,h,0)/f->second.size()
				<<' '<<*max_element(g,h)<<endl;
		}
		cout<<"--\n";
		for(D::iterator f=d.begin();f!=d.end();++f)
			cout<<f->first<<' '<<f->second.first<<' '<<f->second.second<<endl;
		cout<<"----------\n";
	}
	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:6:
/usr/include/c++/14/bits/stl_algo.h: In instantiation of 'void std::__stable_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = _List_iterator<pair<int, __cxx11::basic_string<char> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<int (*)(const pair<int, __cxx11::basic_string<char> >&, const pair<int, __cxx11::basic_string<char> >&)>]':
/usr/include/c++/14/bits/stl_algo.h:5030:36:   required from 'void std::stable_sort(_RAIter, _RAIter, _Compare) [with _RAIter = _List_iterator<pair<int, __cxx11::basic_string<char> > >; _Compare = int (*)(const pair<int, __cxx11::basic_string<char> >&, const pair<int, __cxx11::basic_string<char> >&)]'
 5030 |       _GLIBCXX_STD_A::__stable_sort(__first, __last,
      |                                    ^
a.cc:45:17:   required from here
   45 |                                         stable_sort(b[y].begin(),b[y].end(),SB),
      |                                         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4949:38: error: no match for 'operator-' (operand types are 'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >' and 'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >')
 4949 |       _TmpBuf __buf(__first, (__last - __first + 1) / 2);
      |                               ~~~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/string:48,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator.h:618:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__y.base() - __x.base())) std::operator-(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  618 |     operator-(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:618:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4949:38: note:   'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >' is not derived from 'const std::reverse_iterator<_Iterator>'
 4949 |       _TmpBuf __buf(__first, (__last - __first + 1) / 2);
      |                               ~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1790:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr decltype ((__x.base() - __y.base())) std::operator-(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1790 |     operator-(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1790:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4949:38: note:   'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >' is not derived from 'const std::move_iterator<_IteratorL>'
 4949 |       _TmpBuf __buf(__first, (__last - __first + 1) / 2);
      |                               ~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4953:45: error: no match for 'operator+' (operand types are 'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >' and '_DistanceType' {aka 'long int'})
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:627:5: note: candidate: 'template<class _Iterator> constexpr std::reverse_iterator<_Iterator> std::operator+(typename reverse_iterator<_Iterator>::difference_type, const reverse_iterator<_Iterator>&)'
  627 |     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:627:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4953:45: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and '_DistanceType' {aka 'long int'}
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note: candidate: 'template<class _Iterator> constexpr std::move_iterator<_IteratorL> std::operator+(typename move_iterator<_IteratorL>::difference_type, const move_iterator<_IteratorL>&)'
 1798 |     operator+(typename move_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4953:45: note:   mismatched types 'const std::move_iterator<_IteratorL>' and '_DistanceType' {aka 'long int'}
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:3598:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3598 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3598:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4953:45: note:   'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3616 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4953:45: note:   mismatched types 'const _CharT*' and 'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >'
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3635 |     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4953:45: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and '_DistanceType' {aka 'long int'}
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)'
 3652 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4953:45: note:   'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 3670 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4953:45: note:   'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >' is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3682 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_algo.h:4953:45: note:   'std::_List_iterator<std::pair<int, std::__cxx11::basic_string<char> > >' is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
 4953 |                                     __first + _DistanceType(__buf.size()),
      |                                     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~