Galebickosikasa

C++: white belt

Mar 1st, 2021 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  2. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <unordered_map>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <random>
  13. #include <chrono>
  14. #include <cassert>
  15. #include <sstream>
  16. #include <fstream>
  17. #include <iomanip>
  18.  
  19. #define fi first
  20. #define se second
  21. #define pb push_back
  22. #define ll long long
  23. #define ld long double
  24. #define hm unordered_map
  25. #define pii pair<int, int>
  26. #define sz(a) (int)a.size()
  27. #define all(a) a.begin(), a.end()
  28. #define cinv(v) for (auto& x: v) cin >> x
  29. #define fr(i, n) for (int i = 0; i < n; ++i)
  30. #define fl(i, l, n) for (int i = l; i < n; ++i)
  31.  
  32. // #define int ll
  33.  
  34. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  35. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  36.  
  37. using namespace std;
  38.  
  39. #ifdef LOCAL
  40.     #define dbg(x) cerr << #x << " : " << x << '\n'
  41. #else
  42.     #define dbg(x)
  43. #endif
  44.  
  45. //tg: @runningcherry
  46.  
  47. template <typename T1, typename T2> ostream& operator << (ostream& out, const pair<T1, T2>& v) {
  48.     out << v.fi << ", " << v.se;
  49.     return out;
  50. }
  51.  
  52. template<typename T> ostream& operator << (ostream& out, const vector<T>& v) {
  53.     for (auto& x: v) out << x << " ";
  54.     return out;
  55. }
  56.  
  57. template <typename T1, typename T2> ostream& operator << (ostream& out, const map<T1, T2>& v) {
  58.     for (auto& x: v) out << x << '\n';
  59.     return out;
  60. }
  61.  
  62. template<typename T> ostream& operator << (ostream& out, const set<T>& v) {
  63.     for (auto& x: v) out << x << " ";
  64.     return out;
  65. }
  66.  
  67. template<typename T> ostream& operator << (ostream& out, const multiset<T>& v) {
  68.     for (auto& x: v) out << x << " ";
  69.     return out;
  70. }
  71.  
  72. template <typename T1, typename T2> istream& operator >> (istream& in, pair<T1, T2>& a) {
  73.     in >> a.fi >> a.se;
  74.     return in;
  75. }
  76.  
  77. const ll inf = (ll) 2e9;
  78. const ld pi = asin (1) * 2;
  79. const ld eps = 1e-8;
  80. const ll mod = (ll)1e9 + 7;
  81. const ll ns = 97;
  82.  
  83. const int maxn = 2e5 + 20;
  84.  
  85. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  86.  
  87. struct Date {
  88.     int year, month, day;
  89.  
  90.     inline bool operator < (const Date& other) const {
  91.         if (year == other.year) {
  92.             if (month == other.month) {
  93.                 return day < other.day;
  94.             }
  95.             return month < other.month;
  96.         }
  97.         return year < other.year;
  98.     }
  99. };
  100.  
  101. ostream& operator<< (ostream& out, const Date& date) {
  102.     out << setw (4) << setfill ('0') << date.year << '-' <<
  103.     setw (2) << setfill ('0') << date.month << '-' <<
  104.     setw (2) << setfill ('0') << date.day;
  105.     return out;
  106. }
  107.  
  108. map <Date, set<string>> kek;
  109.  
  110. void check_next_char (istream& stream, const string& date) {
  111.     if (stream.peek () != '-') {
  112.         throw runtime_error ("Wrong date format: " + date);
  113.     }
  114.     stream.ignore (1);
  115. }
  116.  
  117. Date get_date (istream& stream) {
  118.     string date;
  119.     stream >> date;
  120.     if (!stream) throw runtime_error ("Wrong date format: " + date);
  121.     stringstream in (date);
  122.     int year, month, day;
  123.     in >> year;
  124.     check_next_char (in, date);
  125.     in >> month;
  126.     check_next_char (in, date);
  127.     in >> day;
  128.     if (!in) throw runtime_error ("Wrong date format: " + date);
  129.     char x;
  130.     in >> x;
  131.     if (in) throw runtime_error ("Wrong date format: " + date);
  132.     if (month < 1 || 12 < month) throw runtime_error ("Month value is invalid: " + to_string (month));
  133.     if (day < 1 || 31 < day) throw runtime_error ("Day value is invalid: " + to_string (day));
  134.     return {year, month, day};
  135. }
  136.  
  137. void read () {
  138.     string line;
  139.     if (!getline (cin, line)) exit (0);
  140.     if (line.empty ()) return;
  141.     stringstream stream (line);
  142.     string command;
  143.     stream >> command;
  144.     if (command == "Add") {
  145.         auto date = get_date (stream);
  146.         string event;
  147.         stream >> event;
  148.         kek[date].insert (event);
  149.     } else if (command == "Del") {
  150.         auto date = get_date (stream);
  151.         string event;
  152.         stream >> event;
  153.         if (!stream) {
  154.             int n = sz (kek[date]);
  155.             cout << "Deleted " << n << " events\n";
  156.             kek.erase (date);
  157.         } else {
  158.             if (kek[date].count (event)) {
  159.                 cout << "Deleted successfully\n";
  160.             } else {
  161.                 cout << "Event not found\n";
  162.             }
  163.             kek[date].erase (event);
  164.         }
  165.     } else if (command == "Find") {
  166.         auto date = get_date (stream);
  167.         for (const auto& s: kek[date]) cout << s << '\n';
  168.     } else if (command == "Print") {
  169.         for (const auto& ptt: kek) {
  170.             for (const auto& s: ptt.se) {
  171.                 cout << ptt.fi << ' ' << s << '\n';
  172.             }
  173.         }
  174.     } else {
  175.         throw runtime_error ("Unknown command: " + command);
  176.     }
  177. }
  178.  
  179. signed main () {
  180.     ios_base::sync_with_stdio(false);
  181.     cin.tie(nullptr);
  182.     cout.tie(nullptr);
  183.     while (1) {
  184.         try {
  185.             read ();
  186.         } catch (exception& ex) {
  187.             cout << ex.what () << '\n';
  188.         }
  189.     }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. }
  197.  
Add Comment
Please, Sign In to add comment