题目链接:https://ac.nowcoder.com/acm/contest/883/J
题意:略
题解:map 加 list 模拟
代码:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define Pair pair<int, int>
#define int long long
#define fir first
#define sec second
#define It list<node>::iterator
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
namespace IO {
const int maxn = 14096;
char buf[maxn], t[50];
int bn = maxn, bi = maxn;
int read(char* s) {
while (bn) {
for (; bi < bn && buf[bi] <= ' '; bi++);
if (bi < bn) break;
bn = fread(buf, 1, maxn, stdin);
bi = 0;
}
int sn = 0;
while (bn) {
for (; bi<bn && buf[bi]>' '; bi++) s[sn++] = buf[bi];
if (bi < bn) break;
bn = fread(buf, 1, maxn, stdin);
bi = 0;
}
s[sn] = 0;
return sn;
}
bool read(int& x) {
if (!read(t)) return 0;
x = atoi(t);
return 1;
}
}
struct node {
int val;
string name;
};
char s[maxn]; int op, v; int q, m; string str;
list<node>lst;
map<string, It> mp;
signed main() {
int t; IO::read(t);
while (t--) {
IO::read(q); IO::read(m);
int num = 0; int cnt = 0;
mp.clear(); lst.clear();
while (q--) {
IO::read(op); IO::read(s); IO::read(v);
str = string(s);
if (op == 0) {
if (mp.find(str) == mp.end())num++;
else {
auto it = mp[str];
v = (*it).val;
lst.erase(it);
}
printf("%lld\n", v);
lst.push_back((node) { v, str });
auto it = lst.end(); it--;
mp[str] = it;
if (num > m) {
node n = *lst.begin();
mp.erase(mp.find(n.name));
lst.erase(lst.begin());
num--;
}
}
else {
if (mp.find(str) == mp.end())puts("Invalid");
else {
auto it = mp[str];
if (v == 0)printf("%lld\n", (*it).val);
else {
if (v == 1) {
it++;
if (it == lst.end())puts("Invalid");
else printf("%lld\n", (*it).val);
}
else {
if (it == lst.begin())puts("Invalid");
else { it--; printf("%lld\n", (*it).val); }
}
}
}
}
}
}
return 0;
}