Advertisement
goshansmails

Untitled

Apr 7th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. class QuadraticFunction {
  10. public:
  11.     QuadraticFunction() {}
  12.     explicit QuadraticFunction(int a, int b, int c) :
  13.             a(a), b(b), c(c)
  14.     {
  15.         FindRoots();
  16.     }
  17.  
  18.     long double DefiniteIntegral(long double left, long double right) {
  19.         long double F_right = a * right * right * right / 3 + b * right * right / 2 + c * right;
  20.         long double F_left = a * left * left * left / 3 + b * left * left / 2 + c * left;
  21.         return F_right - F_left;
  22.     }
  23.  
  24.     long double DefiniteIntegralAbs(long double left, long double right) {
  25.         vector<long double> borders_and_roots = roots;
  26.         borders_and_roots.push_back(left);
  27.         borders_and_roots.push_back(right);
  28.         sort(borders_and_roots.begin(), borders_and_roots.end());
  29.         auto left_it = find(borders_and_roots.begin(), borders_and_roots.end(), left);
  30.         auto right_it = find(borders_and_roots.begin(), borders_and_roots.end(), right);
  31.  
  32.         long double sum = 0;
  33.         for (auto it = left_it; it != right_it; ++it) {
  34.             sum += abs(DefiniteIntegral(*it, *next(it)));
  35.         }
  36.         return sum;
  37.     }
  38.  
  39.     QuadraticFunction operator-(const QuadraticFunction& quadraticFunction) {
  40.         return QuadraticFunction(a - quadraticFunction.a, b - quadraticFunction.b, c - quadraticFunction.c);
  41.     }
  42.  
  43. private:
  44.     // a*x*x+b*x+c
  45.     int a;
  46.     int b;
  47.     int c;
  48.     vector<long double> roots;
  49.  
  50.     void FindRoots() {
  51.         if (a == 0) {
  52.             if (b != 0) {
  53.                 long double x0 = static_cast<long double> (-c) / b;
  54.                 roots.push_back(x0);
  55.             }
  56.             return;
  57.         }
  58.  
  59.         int d = b * b - 4 * a * c;
  60.         if (d == 0) {
  61.             long double x0 = static_cast<long double> (-b) / (2 * a);
  62.             roots.push_back(x0);
  63.         } else if (d > 0) {
  64.             long double x1 = (-b - sqrtl(d)) / 2 / a;
  65.             long double x2 = (-b + sqrtl(d)) / 2 / a;
  66.             if (x1 > x2) {
  67.                 swap(x1, x2);
  68.             }
  69.             roots = {x1, x2};
  70.         }
  71.     }
  72. };
  73.  
  74. struct Border{
  75.     int x;
  76.     int function_id;
  77. };
  78.  
  79. bool operator<(const Border& lhs, const Border& rhs) {
  80.     if (lhs.x == rhs.x) {
  81.         return lhs.function_id < rhs.function_id;
  82.     }
  83.     return lhs.x < rhs.x;
  84. }
  85.  
  86. int main() {
  87.  
  88.     int n = 0;
  89.     int m = 0;
  90.     cin >> n >> m;
  91.  
  92.     vector<Border> f_borders( n + 1);
  93.     for (Border& border : f_borders) {
  94.         int x;
  95.         cin >> x;
  96.         border = {x, 1};
  97.     }
  98.  
  99.     vector<QuadraticFunction> f_parts(n);
  100.     for (auto& part : f_parts) {
  101.         int a, b, c;
  102.         cin >> a >> b >> c;
  103.         part = QuadraticFunction(a, b, c);
  104.     }
  105.  
  106.     vector<Border> g_borders(m + 1);
  107.     for (Border& border : g_borders) {
  108.         int x;
  109.         cin >> x;
  110.         border = {x, 2};
  111.     }
  112.  
  113.     vector<QuadraticFunction> g_parts(m);
  114.     for (auto& part : g_parts) {
  115.         int a, b, c;
  116.         cin >> a >> b >> c;
  117.         part = QuadraticFunction(a, b, c);
  118.     }
  119.  
  120.     vector<Border> general_borders(n + m + 2);
  121.     merge(f_borders.begin(), f_borders.end(),
  122.           g_borders.begin(), g_borders.end(),
  123.           general_borders.begin()
  124.     );
  125.  
  126.     int pos_f = 0;
  127.     int pos_g = 0;
  128.     int left = general_borders[0].x;
  129.     int right = left;
  130.  
  131.     long double sum = 0;
  132.  
  133.     for (int i = 2; i < n + m + 1; ++i) {
  134.         left = right;
  135.         right = general_borders[i].x;
  136.         sum += (f_parts[pos_f] - g_parts[pos_g]).DefiniteIntegralAbs(left, right);
  137.         if (general_borders[i].function_id == 1) {
  138.             ++pos_f;
  139.         } else {
  140.             ++pos_g;
  141.         }
  142.     }
  143.  
  144.     cout << setprecision(10) << fixed
  145.         << sum << endl;
  146.  
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement