Advertisement
goshansmails

Untitled

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