Language: C++

#include<map>
#include<cstdio>
#include<string>
#include<vector>
#include<numeric>
#include<iostream>

#define	pb			push_back
#define	rep(i,n)	for(int i=0;i<n;i++)

using namespace std;

typedef	vector<int>	vi;

struct Data{
	int id_dlr,val,t;
	Data(){}
	Data(int ID,int V,int T):id_dlr(ID),val(V),t(T){}
	bool isSameDealer(const Data &d){ return id_dlr==d.id_dlr; }
};

bool lt(const Data &d1,const Data &d2){
	if(d1.val!=d2.val)	return d1.val<d2.val;
	return d1.t<d2.t;
}

bool gt(const Data &d1,const Data &d2){
	if(d1.val!=d2.val)	return d1.val>d2.val;
	return d1.t<d2.t;
}

int n_dlr,n_itm;
vector<Data> lst_sell[1000],lst_buy[1000];
vi itm_hist[1000];
int dlr_sell[1000],dlr_buy[1000];

void deal(int id_itm,const string &type){
	if(type=="SELL"){
		Data a=lst_sell[id_itm][0];
		rep(i,lst_buy[id_itm].size()){
			Data b=lst_buy[id_itm][i];
			if(!a.isSameDealer(b) && a.val<=b.val){
				int val=(a.val+b.val)/2;
				dlr_sell[a.id_dlr]+=val;
				dlr_buy[b.id_dlr]+=val;
				itm_hist[id_itm].pb(val);
				lst_sell[id_itm].erase(lst_sell[id_itm].begin());
				lst_buy[id_itm].erase(lst_buy[id_itm].begin()+i);
				return;
			}
		}
	}
	else{	// BUY
		Data a=lst_buy[id_itm][0];
		rep(i,lst_sell[id_itm].size()){
			Data b=lst_sell[id_itm][i];
			if(!a.isSameDealer(b) && a.val>=b.val){
				int val=(a.val+b.val)/2;
				dlr_buy[a.id_dlr]+=val;
				dlr_sell[b.id_dlr]+=val;
				itm_hist[id_itm].pb(val);
				lst_buy[id_itm].erase(lst_buy[id_itm].begin());
				lst_sell[id_itm].erase(lst_sell[id_itm].begin()+i);
				return;
			}
		}
	}
}

int main(){
	for(int n;cin>>n,n;){
		n_dlr=0,n_itm=0;
		rep(i,n){
			lst_sell[i].clear(),lst_buy[i].clear();
			itm_hist[i].clear();
			dlr_sell[i]=dlr_buy[i]=0;
		}

		map<string,int> f_dlr,f_itm;
		string f_dlr_inv[1000],f_itm_inv[1000];
		rep(i,n){
			string dlr,type,itm;
			int val;	cin>>dlr>>type>>itm>>val;

			if(f_dlr.count(dlr)==0)	f_dlr_inv[n_dlr]=dlr,f_dlr[dlr]=n_dlr++;
			if(f_itm.count(itm)==0)	f_itm_inv[n_itm]=itm,f_itm[itm]=n_itm++;
			int id_dlr=f_dlr[dlr],id_itm=f_itm[itm];

			if(type=="SELL"){
				lst_sell[id_itm].pb(Data(id_dlr,val,i));
				sort(lst_sell[id_itm].begin(),lst_sell[id_itm].end(),lt);
			}
			else{	// BUY
				lst_buy[id_itm].pb(Data(id_dlr,val,i));
				sort(lst_buy[id_itm].begin(),lst_buy[id_itm].end(),gt);
			}
			deal(id_itm,type);
		}

		vector<string> ans;
		rep(i,n_itm)if(!itm_hist[i].empty()){
			int vmin=*min_element(itm_hist[i].begin(),itm_hist[i].end());
			int vmax=*max_element(itm_hist[i].begin(),itm_hist[i].end());
			int vavg=accumulate(itm_hist[i].begin(),itm_hist[i].end(),0)/itm_hist[i].size();
			char s[128];	sprintf(s,"%s %d %d %d",f_itm_inv[i].c_str(),vmin,vavg,vmax);
			ans.pb(s);
		}
		sort(ans.begin(),ans.end());
		rep(i,ans.size())	cout<<ans[i]<<endl;
		cout<<"--"<<endl;

		ans.clear();
		rep(i,n_dlr){
			char s[128];	sprintf(s,"%s %d %d",f_dlr_inv[i].c_str(),dlr_buy[i],dlr_sell[i]);
			ans.pb(s);
		}
		sort(ans.begin(),ans.end());
		rep(i,ans.size())	cout<<ans[i]<<endl;
		cout<<"----------"<<endl;
	}

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:91:33: error: 'sort' was not declared in this scope; did you mean 'short'?
   91 |                                 sort(lst_sell[id_itm].begin(),lst_sell[id_itm].end(),lt);
      |                                 ^~~~
      |                                 short
a.cc:95:33: error: 'sort' was not declared in this scope; did you mean 'short'?
   95 |                                 sort(lst_buy[id_itm].begin(),lst_buy[id_itm].end(),gt);
      |                                 ^~~~
      |                                 short
a.cc:102:35: error: 'min_element' was not declared in this scope
  102 |                         int vmin=*min_element(itm_hist[i].begin(),itm_hist[i].end());
      |                                   ^~~~~~~~~~~
a.cc:103:35: error: 'max_element' was not declared in this scope
  103 |                         int vmax=*max_element(itm_hist[i].begin(),itm_hist[i].end());
      |                                   ^~~~~~~~~~~
a.cc:108:17: error: 'sort' was not declared in this scope; did you mean 'short'?
  108 |                 sort(ans.begin(),ans.end());
      |                 ^~~~
      |                 short