Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <deque>
- #include <iostream>
- #include <map>
- #include <set>
- #include <vector>
- using namespace std;
- struct Client {
- Client(int32_t client_, int16_t room_count_)
- : client { client_ }
- , room_count { room_count_ }
- {
- }
- int32_t client = -1;
- int16_t room_count = -1;
- };
- class Booking {
- public:
- void BOOK(const int64_t time,const string& hotel_name,const int32_t client_id,const int16_t room_count)
- {
- current_time = time;
- if (data_hotels.find(hotel_name) == data_hotels.end()) {
- data_hotels[hotel_name] = data_hotels.size();
- data_time.push_back({ { time, Client(client_id, room_count) } });
- } else {
- const int32_t time_id = data_hotels[hotel_name];
- data_time[time_id].push_back({ time, Client(client_id, room_count) });
- }
- }
- int32_t CLIENTS(const string& hotel_name)
- {
- try {
- return count_clientsOrRooms(data_hotels.at(hotel_name));
- } catch (out_of_range&) {
- return 0;
- }
- }
- int32_t ROOMS(const string& hotel_name)
- {
- try {
- return count_clientsOrRooms(data_hotels.at(hotel_name), true);
- } catch (out_of_range&) {
- return 0;
- }
- }
- private:
- int64_t current_time = -2000000000000000001;
- map<string, int32_t> data_hotels; //hotel, hotel_id
- vector<deque<pair<int64_t, Client>>> data_time; //[hotel_id] time,(client,room_count)
- int32_t count_clientsOrRooms(const int32_t time_id, const bool check = false)
- {
- int64_t minTime = current_time - 86399;
- auto& foundData = data_time.at(time_id);
- while (foundData.begin()->first < minTime)
- foundData.pop_front();
- if (foundData.empty())
- return 0;
- int32_t count_rooms = 0;
- set<int32_t> count_clients;
- for (int i = 0; i < foundData.size(); ++i) {
- if (check)
- count_rooms += foundData[i].second.room_count;
- else
- count_clients.emplace(foundData[i].second.client);
- }
- return check ? count_rooms : count_clients.size();
- }
- };
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- Booking manager;
- int query_count;
- cin >> query_count;
- for (int query_id = 0; query_id < query_count; ++query_id) {
- string query_type;
- cin >> query_type;
- switch (query_type[0]) {
- case 'B': {
- int64_t time;
- string hotel_name;
- int32_t client_id;
- int16_t room_count;
- cin >> time >> hotel_name >> client_id >> room_count;
- manager.BOOK(time, hotel_name, client_id, room_count);
- break;
- }
- case 'C': {
- string hotel_name;
- cin >> hotel_name;
- cout << manager.CLIENTS(hotel_name) << '\n';
- break;
- }
- case 'R': {
- string hotel_name;
- cin >> hotel_name;
- cout << manager.ROOMS(hotel_name) << '\n';
- break;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement