-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path432.cpp
More file actions
158 lines (141 loc) · 4.9 KB
/
432.cpp
File metadata and controls
158 lines (141 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
__________________________________________________________________________________________________
sample 40 ms submission
class AllOne final {
using many = unordered_set<string>;
struct strings final : private variant<monostate, string, many> {
int size() const noexcept {
return holds_alternative<many>(*this) ? get<many>(*this).size() : 1;
}
void erase(const string& key) noexcept {
get<many>(*this).erase(key);
}
void insert(const string& key) noexcept {
switch (this->index()) {
case 0:
static_cast<variant&>(*this) = key;
return;
case 1: {
this->emplace<many>({move(get<string>(*this)), key});
return;
}
default:
get<many>(*this).insert(key);
}
}
string get_any() const noexcept {
return holds_alternative<many>(*this) ? *get<many>(*this).cbegin() : get<string>(*this);
}
};
unordered_map<string, int> key_to_value;
unordered_map<int, tuple<strings, int, int>> value_to_info;
int max_value = 0;
int min_value = 0;
void remove(const string& key, const int value) noexcept {
const auto it = value_to_info.find(value);
auto& [keys, prev, next] = it->second;
if (keys.size() > 1) {
keys.erase(key);
return;
}
if (value == min_value) {
if (value == max_value) {
min_value = max_value = 0;
value_to_info.clear();
return;
}
get<1>(value_to_info[min_value = next]) = 0;
}
else if (value == max_value)
get<2>(value_to_info[max_value = prev]) = 0;
else
get<1>(value_to_info[next]) = prev,
get<2>(value_to_info[prev]) = next;
value_to_info.erase(it);
}
void register_value(const int value) noexcept {
if (!min_value) {
min_value = max_value = value;
return;
}
auto& [_, prev, next] = value_to_info[value];
if (value < min_value)
min_value = get<1>(value_to_info[next = min_value]) = value;
else if (value > max_value)
max_value = get<2>(value_to_info[prev = max_value]) = value;
}
public:
void inc(const string & key) noexcept {
const auto it = key_to_value.find(key);
int after;
if (it == key_to_value.end()) after = 1, key_to_value.emplace(key, 1);
else after = ++it->second, remove(key, after - 1);
get<0>(value_to_info[after]).insert(key);
register_value(after);
}
void dec(const string & key) noexcept {
const auto it = key_to_value.find(key);
if (it == key_to_value.end()) return;
const auto before = it->second;
remove(key, before);
if (before == 1) {
key_to_value.erase(it);
return;
}
const auto after = --it->second;
get<0>(value_to_info[after]).insert(key);
register_value(after);
}
string getMaxKey() const noexcept {
return min_value ? get<0>(value_to_info.find(max_value)->second).get_any() : "";
}
string getMinKey() const noexcept {
return min_value ? get<0>(value_to_info.find(min_value)->second).get_any() : "";
}
};
static const auto _ = []() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); return 0; }();
__________________________________________________________________________________________________
sample 24208 kb submission
class AllOne {
public:
/** Initialize your data structure here. */
AllOne() {
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
void inc(string key) {
if(m.count(key))
table.erase({m[key], key});
m[key]++;
table.insert({m[key], key});
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
void dec(string key) {
if(!m.count(key)) return;
table.erase({m[key], key});
m[key]--;
if(!m[key]){
m.erase(key);
} else {
table.insert({m[key], key});
}
}
/** Returns one of the keys with maximal value. */
string getMaxKey() {
return table.empty() ? "" : table.rbegin()->second;
}
/** Returns one of the keys with Minimal value. */
string getMinKey() {
return table.empty() ? "" : table.begin()->second;
}
private:
unordered_map<string, int> m;
set<pair<int, string>> table;
};
/**
* Your AllOne object will be instantiated and called as such:
* AllOne obj = new AllOne();
* obj.inc(key);
* obj.dec(key);
* string param_3 = obj.getMaxKey();
* string param_4 = obj.getMinKey();
*/
__________________________________________________________________________________________________