Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- using namespace std;
- class QuadraticFunction {
- public:
- QuadraticFunction() {}
- explicit QuadraticFunction(int a, int b, int c) :
- a(a), b(b), c(c)
- {
- FindRoots();
- }
- long double DefiniteIntegral(long double left, long double right) {
- long double F_right = a * right * right * right / 3 + b * right * right / 2 + c * right;
- long double F_left = a * left * left * left / 3 + b * left * left / 2 + c * left;
- return F_right - F_left;
- }
- long double DefiniteIntegralAbs(long double left, long double right) {
- vector<long double> borders_and_roots = roots;
- borders_and_roots.push_back(left);
- borders_and_roots.push_back(right);
- sort(borders_and_roots.begin(), borders_and_roots.end());
- auto left_it = find(borders_and_roots.begin(), borders_and_roots.end(), left);
- auto right_it = find(borders_and_roots.begin(), borders_and_roots.end(), right);
- long double sum = 0;
- for (auto it = left_it; it != right_it; ++it) {
- sum += abs(DefiniteIntegral(*it, *next(it)));
- }
- return sum;
- }
- QuadraticFunction operator-(const QuadraticFunction& quadraticFunction) {
- return QuadraticFunction(a - quadraticFunction.a, b - quadraticFunction.b, c - quadraticFunction.c);
- }
- private:
- // a*x*x+b*x+c
- int a;
- int b;
- int c;
- vector<long double> roots;
- void FindRoots() {
- if (a == 0) {
- if (b != 0) {
- long double x0 = static_cast<long double> (-c) / b;
- roots.push_back(x0);
- }
- return;
- }
- int d = b * b - 4 * a * c;
- if (d == 0) {
- long double x0 = static_cast<long double> (-b) / (2 * a);
- roots.push_back(x0);
- } else if (d > 0) {
- long double x1 = (-b - sqrtl(d)) / 2 / a;
- long double x2 = (-b + sqrtl(d)) / 2 / a;
- if (x1 > x2) {
- swap(x1, x2);
- }
- roots = {x1, x2};
- }
- }
- };
- struct Border{
- int x;
- int function_id;
- };
- bool operator<(const Border& lhs, const Border& rhs) {
- if (lhs.x == rhs.x) {
- return lhs.function_id < rhs.function_id;
- }
- return lhs.x < rhs.x;
- }
- int main() {
- int n = 0;
- int m = 0;
- cin >> n >> m;
- vector<Border> f_borders( n + 1);
- for (Border& border : f_borders) {
- int x;
- cin >> x;
- border = {x, 1};
- }
- vector<QuadraticFunction> f_parts(n);
- for (auto& part : f_parts) {
- int a, b, c;
- cin >> a >> b >> c;
- part = QuadraticFunction(a, b, c);
- }
- vector<Border> g_borders(m + 1);
- for (Border& border : g_borders) {
- int x;
- cin >> x;
- border = {x, 2};
- }
- vector<QuadraticFunction> g_parts(m);
- for (auto& part : g_parts) {
- int a, b, c;
- cin >> a >> b >> c;
- part = QuadraticFunction(a, b, c);
- }
- vector<Border> general_borders(n + m + 2);
- merge(f_borders.begin(), f_borders.end(),
- g_borders.begin(), g_borders.end(),
- general_borders.begin()
- );
- int pos_f = 0;
- int pos_g = 0;
- int left = general_borders[0].x;
- int right = left;
- long double sum = 0;
- for (int i = 2; i < n + m + 1; ++i) {
- left = right;
- right = general_borders[i].x;
- sum += (f_parts[pos_f] - g_parts[pos_g]).DefiniteIntegralAbs(left, right);
- if (general_borders[i].function_id == 1) {
- ++pos_f;
- } else {
- ++pos_g;
- }
- }
- cout << setprecision(10) << fixed
- << sum << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement