Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "test_runner.h"
- #include <algorithm>
- #include <iostream>
- #include <optional>
- #include <string>
- #include <string_view>
- #include <vector>
- #include <ctime>
- using namespace std;
- class Date {
- private:
- class Year {
- public:
- Year (int year_) : year (year_) {}
- int year;
- bool is_leap () const {
- if (year % 400 == 0) return true;
- if (year % 100 == 0) return false;
- return year % 4 == 0;
- }
- };
- class Month {
- private:
- Year& year;
- public:
- Month (Year& year_, int month_) : year (year_), month (month_) {}
- int month;
- int get_count_days () const {
- vector <bool> is31 {true, false, true, false, true, false, true, true, false, true, false, true};
- if (month == 2) {
- if (year.is_leap ()) return 29;
- return 28;
- } else {
- if (is31[static_cast<uint32_t>(month - 1)]) return 31;
- return 30;
- }
- }
- };
- Year year;
- Month month;
- int day;
- public:
- Date (int year_, int month_, int day_) : year (year_), month (year, month_), day (day_) {}
- bool operator< (const Date& other) const {
- return tie (year.year, month.month, day) < tie (other.year.year, other.month.month, other.day);
- }
- void setDay (int day_) {
- day = day_;
- }
- int get_count_days () const {
- return month.get_count_days ();
- }
- };
- class BudgetManager {
- public:
- BudgetManager () {
- dates.reserve (36525);
- for (int year = 2000; year <= 2099; ++year) {
- for (int month = 1; month <= 12; ++month) {
- Date date (year, month, 1);
- for (int day = 1; day <= date.get_count_days (); ++day) {
- date.setDay (day);
- dates.push_back (date);
- }
- }
- }
- cost = spending = vector <double> (dates.size ());
- }
- double ComputeIncome (Date from, Date to) {
- int from_ = get_ind (from), to_ = get_ind (to);
- double sum = 0;
- for (int i = from_; i <= to_; ++i) sum += cost[i] - spending[i];
- return sum;
- }
- void Earn (Date from, Date to, double value) {
- int from_ = get_ind (from), to_ = get_ind (to);
- double cnt = to_ - from_ + 1;
- value /= cnt;
- for (int i = from_; i <= to_; ++i) cost[i] += value;
- }
- void Spend (Date from, Date to, double value) {
- int from_ = get_ind (from), to_ = get_ind (to);
- double cnt = to_ - from_ + 1;
- value /= cnt;
- for (int i = from_; i <= to_; ++i) spending[i] += value;
- }
- void PayTax (Date from, Date to, double percentage) {
- int from_ = get_ind (from), to_ = get_ind (to);
- for (int i = from_; i <= to_; ++i) cost[i] *= 1.0 - percentage / 100;
- }
- private:
- vector <Date> dates;
- vector <double> cost, spending;
- int get_ind (Date date) const {
- return static_cast<int>(lower_bound(dates.begin (), dates.end (), date) - dates.begin ());
- }
- };
- Date parse_date (istream& in = cin) {
- int year, month, day;
- char x;
- in >> year >> x >> month >> x >> day;
- return {year, month, day};
- }
- int main() {
- ios_base::sync_with_stdio (false);
- cin.tie (nullptr);
- cout.precision (25);
- int q;
- cin >> q;
- BudgetManager bm;
- while (q--) {
- string t;
- cin >> t;
- if (t.front () == 'E') {
- Date from = parse_date ();
- Date to = parse_date ();
- int value;
- cin >> value;
- bm.Earn (from, to, value);
- } else if (t.front () == 'C') {
- Date from = parse_date ();
- Date to = parse_date ();
- cout << bm.ComputeIncome (from, to) << '\n';
- } else if (t.front () == 'S') {
- Date from = parse_date ();
- Date to = parse_date ();
- int value;
- cin >> value;
- bm.Spend (from, to, value);
- } else {
- Date from = parse_date ();
- Date to = parse_date ();
- int tax;
- cin >> tax;
- bm.PayTax (from, to, tax);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement