Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.0 kB
2
Indexable
Never
#define MAXL    (10)
#define max_U 10//10005
#define max_M 50//50005

#include<unordered_map>
#include<set>
#include<string>
#include<vector>
#include<iostream>

using namespace std;

struct mess
{
	int id;
	int point;
	int par;
	int root_p;
	bool isDel;
	int uID;
	//mess(int _id, int _point, int _par, int _root, int _uID, bool _isDel)
	//{
	//	id = _id;
	//	point = _point;
	//	par = _par;
	//	root_p = _root;
	//	uID = _uID;
	//	isDel = _isDel;
	//}
};

struct U
{
	string name;
	int point;
};

struct comM
{
	bool operator()(mess a, mess b)
	{
		if(a.point == b.point) return a.id < b.id;
		return a.point > b.point;
	}
};

struct comU
{
	bool operator()(U a, U b)
	{
		if(a.point == b.point) return a.name < b.name;
		return a.point > b.point;
	}
};

mess poolMess[max_M];
U poolUser[max_U];
set<U, comU> sortUser;
set<mess, comM> sortMess;
unordered_map<string, int> hashName;
unordered_map<int, int> hashID;
vector<mess> storeChill[max_M];
int userID, meID;

void init()
{
	sortMess.clear();
	sortUser.clear();
	hashName.clear();
	hashID.clear();
	userID = meID = 0;
	return;
}

int check_User(char *str, int uPoint)
{
	if (hashName.find(str) == hashName.end())
	{
		hashName[str] = userID;					// create new user
		poolUser[userID].name = str;
		poolUser[userID].point = 0;
		userID++;
	}
	int id = hashName[str];					// increase the point of user
	sortUser.erase(poolUser[id]);			// update point in tree set of user
	poolUser[id].point += uPoint;
	sortUser.insert(poolUser[id]);
	return id;
}

void write_mess(int _id, int _point, int _par, int _root, int _uID, bool _isDel, int wid)
{
	poolMess[wid].id = _id;
	poolMess[wid].point = _point;
	poolMess[wid].par = _par;
	poolMess[wid].root_p = _root;
	poolMess[wid].uID = _uID;
	poolMess[wid].isDel = _isDel;
}

int writeMessage(char mUser[], int mID, int mPoint)
{
	int Uid = check_User(mUser, mPoint);		// User side

	// message side
	hashID[mID] = meID;
	write_mess(mID, mPoint, -1, mPoint, Uid, false, meID);	// create new message
	storeChill[meID].clear();							// prepare to store chill
	sortMess.insert(poolMess[meID]);					// tree set message
	meID++;
	cout << poolUser[Uid].point << endl;
	return poolUser[Uid].point;
}

int findpath(int id)
{
	int pid = id;
	while (poolMess[pid].par != -1)
	{
		pid = poolMess[pid].par;
	}
	return pid;
}

int commentTo(char mUser[], int mID, int mTargetID, int mPoint)
{
	int Uid = check_User(mUser, mPoint);

	// mess side
	int parID = hashID[mTargetID];	// get par 
	int RootId = findpath(parID);	// search root mess 
	sortMess.erase(poolMess[RootId]);	// update root mess in tree set
	poolMess[RootId].point += mPoint;
	sortMess.insert(poolMess[RootId]);

	hashID[mID] = meID;
	write_mess(mID, mPoint, parID, mPoint, Uid, false, meID);	// create new mess/ comment/ replies
	storeChill[parID].push_back(poolMess[meID]);		// add chill for parent node
	storeChill[meID].clear();							// prepare for new chil
	meID++;
	cout << poolMess[RootId].point << endl;
	return poolMess[RootId].point;
}

void delete_chil(int Mid, int Root)
{
	for(auto it : storeChill[Mid])
	{
		int deUid = hashID[it.id];
		if (!it.isDel)
		{
			int userID_del = poolMess[deUid].uID;
			// user
			sortUser.erase(poolUser[userID_del]);
			poolUser[userID_del].point -= poolMess[deUid].root_p;
			sortUser.insert(poolUser[userID_del]);

			// mess
			poolMess[deUid].isDel = true;
			poolMess[Root].point -= poolMess[deUid].root_p;
			delete_chil(deUid, Root);
		}
	}
}

int erase(int mID)
{
	int mesID = hashID[mID];
	int RootId = findpath(mesID);
	int userID_er = poolMess[mesID].uID;

	// mess
	sortMess.erase(poolMess[RootId]);
	// delete  child
	delete_chil(mesID, RootId);
	poolMess[mesID].isDel = true;

	poolMess[RootId].point -= poolMess[mesID].root_p;
	sortMess.insert(poolMess[RootId]);

	// user
	sortUser.erase(poolUser[userID_er]);
	poolUser[userID_er].point -= poolMess[mesID].root_p;
	sortUser.insert(poolUser[userID_er]);

	if (poolMess[mesID].par == -1)
	{
		cout << poolUser[userID_er].point << endl;
		return poolUser[userID_er].point;
	}
	cout << poolMess[RootId].point << endl;
	return poolMess[RootId].point;
}

void getBestMessages(int mBestMessageList[])
{
	int i = 0;
	for(auto it : sortMess)
	{
		mBestMessageList[i] = it.id;
		i++;
		if(i >= 5) break;
	}
	for (int i = 0; i < 5; i++)
	{
		cout << mBestMessageList[i] << " ";
	}
	cout << endl;
	return;
}

void getBestUsers(char mBestUserList[][MAXL + 1])
{
	int i = 0;
	for (auto it : sortUser)
	{
		int j = 0;
		for(; j < it.name.size(); j++)
		{
			mBestUserList[i][j] = it.name[j];
		}
		mBestUserList[i][j] = '\0';
		i++;
		if(i >= 5) break;
	}
	for (int i = 0; i < 5; i++)
	{
		int j = 0;
		while (mBestUserList[i][j] != '\0')
		{
			cout << mBestUserList[i][j];
			j++;
		}
		cout << " ";
	}
	cout << endl;
	return;
}
Leave a Comment