Submission
- Build index on the number of occurrences
- Otherwise, in linear query, one case will exceed the time limit
#include <bits/stdc++.h>
using namespace std;
string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);
// Complete the freqQuery function below.
vector<int> freqQuery(vector<vector<int>> queries) {
map<int, int> m;
map<int, int> c;
vector<int> res;
for(int i = 0; i < queries.size(); ++i) {
if(queries[i][0] == 1) {
auto it = m.find(queries[i][1]);
if(it == m.end()){
m.insert(make_pair(queries[i][1], 1));
if(c.find(1) == c.end()) c.insert(make_pair(1, 1));
else c[1]++;
}
else {
c[it->second]--;
if(c[it->second] == 0) c.erase(it->second);
it->second++;
if(c.find(it->second) == c.end()) c.insert(make_pair(it->second, 1));
else c[it->second]++;
}
} else if (queries[i][0] == 2) {
auto it = m.find(queries[i][1]);
if(it != m.end()) {
c[it->second]--;
if(c[it->second] == 0) c.erase(it->second);
it->second--;
if(it->second == 0) m.erase(it);
else c[it->second]++;
}
} else if (queries[i][0] == 3) {
if(c.find(queries[i][1]) != c.end()) res.push_back(1);
else res.push_back(0);
}
}
return res;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string q_temp;
getline(cin, q_temp);
int q = stoi(ltrim(rtrim(q_temp)));
vector<vector<int>> queries(q);
for (int i = 0; i < q; i++) {
queries[i].resize(2);
string queries_row_temp_temp;
getline(cin, queries_row_temp_temp);
vector<string> queries_row_temp = split(rtrim(queries_row_temp_temp));
for (int j = 0; j < 2; j++) {
int queries_row_item = stoi(queries_row_temp[j]);
queries[i][j] = queries_row_item;
}
}
vector<int> ans = freqQuery(queries);
for (int i = 0; i < ans.size(); i++) {
fout << ans[i];
if (i != ans.size() - 1) {
fout << "\n";
}
}
fout << "\n";
fout.close();
return 0;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}
vector<string> split(const string &str) {
vector<string> tokens;
string::size_type start = 0;
string::size_type end = 0;
while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));
start = end + 1;
}
tokens.push_back(str.substr(start));
return tokens;
}