Skip to main content

Beautiful Solutions

· 2 min read
Sarthak Mohanty

using struct

when to use struct

  • when there are questions where limited containers are there and each container has specific needs
  • when its difficult to take inputs for a container
  • when pair, map are not sufficient and we tart thinking about tuple
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define ll long long

struct Rect
{
int x1, y1, x2, y2;
// Rect(int x1, int y1, int x2, int y2) : x1{x1}, y1{y1}, x2{x2}, y2{y2} {}
};

int area(Rect rect)
{
int ans = ((rect.x2 - rect.x1) * (rect.y2 - rect.y1));
return ans;
}

int inter(Rect b, Rect t)
{
Rect in;
int ar;

// y1
in.y1 = (t.y1 <= b.y1) ? b.y1 : t.y1;
// y2
if (t.y2 >= b.y1 && t.y2 >= b.y2)
{
in.y2 = b.y2;
}
else if (t.y2 >= b.y1 && t.y2 <= b.y2)
{
in.y2 = t.y2;
}

// x1
if (t.x1 >= b.x1 && t.x1 <= b.x2)
{
in.x1 = t.x1;
}
else if (t.x1 <= b.x1 && t.x1 <= b.x2)
{
in.x1 = b.x1;
}
// x2
in.x2 = (t.x2 >= b.x2) ? b.x2 : t.x2;

ar = area(in);

return ar;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);

// freopen("billboard.in", "r", stdin);
// freopen("billboard.out", "w", stdout);

int ans;
Rect b1, b2, t;
cin >> b1.x1 >> b1.y1 >> b1.x2 >> b1.y2;
cin >> b2.x1 >> b2.y1 >> b2.x2 >> b2.y2;
cin >> t.x1 >> t.y1 >> t.x2 >> t.y2;

int ib1 = inter(b1, t);
int ib2 = inter(b2, t);

// cerr << "b1 ar: " << area(b1) << endl;
ans = area(b1) + area(b2) - ib1 - ib2;

cout << ans;
return 0;
}