#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
struct Node{
string name;
int views;
string category;
Node *left,*right;
Node(string _name,int _views,string _category):name(_name),views(_views),category(_category),left(NULL),right(NULL){}
}*L,*R;
unordered_map<string,Node*>ha;
void insert(Node* p)//左边是后面
{
p->right=L->right;
p->left=L;
L->right->left=p;
L->right=p;
}
void remove(Node *p)
{
p->left->right=p->right;
p->right->left=p->left;
}
void StreamerOnline(string a,string b, string c)
{
int cnt=stoi(b);
auto p=new Node(a,cnt,c);
insert(p);
ha[a]=p;
}
int main()
{
vector<string>streamerInformation={"Ninja", "100000", "Fortnite", "Pokimane", "40000", "Valorant"};
//vector<string>commands={"StreamerOnline","AOC","75000","Just Chatting","UpdateViews","Ninja","120000","Fortnite","UpdateCategory","Ninja","Fortnite","Warzone","StreamOffline","Ninja","Fortnite","ViewsInCategory","Fortnite","TopStreamInCategory","Valorant","TopStreamer"};
// vector<string>commands={"StreamerOnline","Bugha","75000","Fortnite","StreamerOnline","Tenzo","30000","Valorant","ViewsInCategory","Fortnite","TopStreamInCategory","Valorant"};
vector<string>commands={"UpdateViews", "Ninja", "120000", "Fortnite", "ViewsInCategory", "Fortnite"};
L=new Node("",-1,"");
R=new Node("",-1,"");
L->right=R;
R->left=L;
for(int i=0;i<streamerInformation.size();i++)
{
StreamerOnline(streamerInformation[i],streamerInformation[i+1],streamerInformation[i+2]);
i+=2;
}
for(int i=0;i<commands.size();i++)
{
string op=commands[i];
if(op=="StreamerOnline")
{
string a=commands[i+1];
string b=commands[i+2];
string c=commands[i+3];
StreamerOnline(a,b,c);
i+=3;
// for(auto p=R->left;p!=L;p=p->left)
// {
// cout<<p->name<<","<<p->views<<","<<p->category<<",";
// }
}
else if(op=="UpdateViews")
{
string a=commands[i+1];
string b=commands[i+2];
string c=commands[i+3];
if(!ha.count(a)) continue;
auto p=ha[a];
if(p->category!=c) continue;
p->views=stoi(b);
i+=3;
// for(auto p=R->left;p!=L;p=p->left)
// {
// cout<<p->name<<","<<p->views<<","<<p->category<<",";
// }
}
else if(op=="UpdateCategory")
{
string a=commands[i+1];
string b=commands[i+2];
string c=commands[i+3];
if(!ha.count(a)) continue;
auto p=ha[a];
if(p->category!=b) continue;
p->category=c;
i+=3;
// for(auto p=R->left;p!=L;p=p->left)
// {
// cout<<p->name<<","<<p->views<<","<<p->category<<",";
// }
}
else if(op=="StreamOffline")
{
string a=commands[i+1];
string b=commands[i+2];
if(!ha.count(a)) continue;
auto p=ha[a];
if(p->category!=b) continue;
remove(p);
ha.erase(a);
i+=2;
// for(auto p=R->left;p!=L;p=p->left)
// {
// cout<<p->name<<","<<p->views<<","<<p->category<<",";
// }
}
else if(op=="ViewsInCategory")
{
string a=commands[i+1];
int res=0;
for(auto p=L;p!=R;p=p->right)
{
if(p->category==a)
res+=p->views;
}
cout<<res<<endl;
i+=1;
}
else if(op=="TopStreamInCategory")
{
string a=commands[i+1];
bool hascat=false;
string res;
int sum=0;
for(auto p=L;p!=R;p=p->right)
{
if(p->category==a)
{
hascat=true;
if(p->views>sum)
{
sum=p->views;
res=p->name;
}
}
}
if(!hascat || sum==0) cout<<"null";
else cout<<res;
i+=1;
}
else if(op=="TopStreamer")
{
bool has_stream=false;
string res;
int sum=0;
for(auto p=L;p!=R;p=p->right)
{
if(p->views>sum)
{
sum=p->views;
res=p->name;
}
}
if(!sum) cout<<"null";
else cout<<res;
cout<<endl;
}
}
// 双向链表存所有节点,节点存name,views,category
// hash表存name所在的节点
// streamerOnline:加入链表尾
// updateviews: 修改name对应的节点
// updatecategory: 修改name对应的category
// streameroffline:删除name对应节点
// viewsinccategory:直接遍历吧。。
// topstreamerincategory:直接遍历吧。。
// topstreamer:直接遍历吧。。
return 0;
}
有狗
爬