#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <sstream>
#include <algorithm>
using namespace std;
struct User {
string name;
int score;
int timestamp; // 上传时间,用于处理同分情况
User(string n, int s, int t) : name(n), score(s), timestamp(t) {}
// 自定义排序规则:分数降序,同分则时间戳升序(先上传的排前面)
bool operator<(const User& other) const {
if (score != other.score) {
return score > other.score; // 分数高的在前
}
return timestamp < other.timestamp; // 同分,时间早的在前
}
};
class RankingSystem {
private:
set<User> users; // 按排名规则自动排序
map<string, pair<int, int>> userInfo; // name -> {score, timestamp}
int currentTime;
public:
RankingSystem() : currentTime(0) {}
// 上传分数
void upload(const string& name, int score) {
// 如果用户已存在,先删除旧的
auto it = userInfo.find(name);
if (it != userInfo.end()) {
// 从set中删除旧记录
users.erase(User(name, it->second.first, it->second.second));
}
// 添加新记录
currentTime++;
userInfo[name] = {score, currentTime};
users.insert(User(name, score, currentTime));
}
// 查询指定用户的排名
int queryByName(const string& name) {
auto it = userInfo.find(name);
if (it == userInfo.end()) {
return 0; // 用户不存在
}
// 计算排名
User target(name, it->second.first, it->second.second);
int rank = 1;
for (const auto& user : users) {
if (user.name == name) {
return rank;
}
rank++;
}
return 0; // 未找到
}
// 查询从指定排名开始的用户
vector<string> queryByRank(int rank) {
vector<string> result;
if (rank < 1 || rank > users.size()) {
return result;
}
int count = 0;
int currentRank = 1;
for (const auto& user : users) {
if (currentRank >= rank && count < 10) {
result.push_back(user.name);
count++;
}
currentRank++;
if (count >= 10) break;
}
return result;
}
// 解析并处理命令
void processCommand(const string& command) {
size_t colonPos = command.find(':');
if (colonPos == string::npos) {
return;
}
string cmd = command.substr(0, colonPos);
string argsStr = command.substr(colonPos + 1);
if (cmd == "Upload") {
// 格式: Upload:A 20
size_t spacePos = argsStr.find(' ');
if (spacePos != string::npos) {
string name = argsStr.substr(0, spacePos);
int score = stoi(argsStr.substr(spacePos + 1));
upload(name, score);
}
} else if (cmd == "QueryByName") {
// 格式: QueryByName:A
string name = argsStr;
cout << queryByName(name) << endl;
} else if (cmd == "QueryByRank") {
// 格式: QueryByRank:2
int rank = stoi(argsStr);
vector<string> names = queryByRank(rank);
for (size_t i = 0; i < names.size(); i++) {
if (i > 0) cout << " ";
cout << names[i];
}
if (!names.empty()) {
cout << endl;
}
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
cin.ignore(); // 忽略换行符
RankingSystem system;
for (int i = 0; i < N; i++) {
string command;
getline(cin, command);
system.processCommand(command);
}
return 0;
}