Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // red-w2-booking
- #include <iostream>
- #include <vector>
- #include <deque>
- #include <map>
- #include <string>
- #include <sstream>
- #include <fstream>
- #include <algorithm>
- #include <unordered_set>
- #include "profile.h"
- using namespace std;
- struct Booking {
- long long time;
- int roomsCount;
- };
- class BookingManager {
- public:
- void book(long long time, const string& hotel, int client, int roomsCount) {
- current_time = time;
- entries[hotel].push_back({time, roomsCount});
- clientLastAccess[hotel][client] = time;
- roomCounts[hotel] += roomsCount;
- }
- int clientsWithinLastDay(const string& hotel) {
- auto& clients = clientLastAccess[hotel];
- for (auto it = clients.begin(); it != clients.end(); ) {
- if (current_time - it->second < SECONDS_IN_DAY) {
- ++it;
- } else {
- clients.erase(it++);
- }
- }
- return static_cast<int>(clients.size());
- }
- int roomsBookedWithinLastDay(const string& hotel) {
- const auto& hotelEntries = entries[hotel];
- int& start = startPositions[hotel];
- int& roomsCount = roomCounts[hotel];
- for (auto it = hotelEntries.begin() + start; it < hotelEntries.end() ; ++it) {
- if (current_time - it->time >= SECONDS_IN_DAY) {
- ++start;
- roomsCount -= it->roomsCount;
- } else {
- break;
- }
- }
- return roomsCount;
- }
- private:
- long long current_time = 0;
- static const long long SECONDS_IN_DAY = 24 * 3600;
- map<string, vector<Booking>> entries;
- map<string, int> startPositions;
- map<string, int> roomCounts;
- map<string, map<int, long long>> clientLastAccess;
- };
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- // {
- // BookingManager manager;
- // LOG_DURATION("book");
- // for (int i = 0; i < 100000; ++i) {
- // manager.book(i, "bulka", 2*i + 3, 3);
- // }
- // }
- // return 0;
- #ifdef __APPLE__
- auto cin = ifstream("bookings.txt");
- auto cout = ostringstream();
- #endif
- BookingManager manager;
- int query_count;
- cin >> query_count;
- string command;
- for (int i = 0; i < query_count; ++i) {
- cin >> command;
- if (command == "BOOK") {
- long long time;
- string hotel;
- int client, roomsCount;
- cin >> time >> hotel >> client >> roomsCount;
- manager.book(time, hotel, client, roomsCount);
- } else if (command == "CLIENTS") {
- string hotel;
- cin >> hotel;
- cout << manager.clientsWithinLastDay(hotel) << '\n';
- } else if (command == "ROOMS") {
- string hotel;
- cin >> hotel;
- cout << manager.roomsBookedWithinLastDay(hotel) << '\n';
- }
- }
- #ifdef __APPLE__
- cerr << (cout.str() == "3\n1\n27\n2\n59\n3\n122\n3\n248\n3\n496\n2\n483\n1\n0\n0\n1\n1\n3\n2\n") << endl;
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement