#include <iostream>
#include <algorithm>
class Rectangle {
public:
int x;
int y;
int width;
int height;
Rectangle(int x, int y, int width, int height) : x(x), y(y), width(width), height(height) {}
};
int find_overlapped_area(const std::vector<Rectangle>& rectangles) {
int overlapped_area = 0;
for (int i = 0; i < rectangles.size(); ++i) {
for (int j = i + 1; j < rectangles.size(); ++j) {
const Rectangle& rect1 = rectangles[i];
const Rectangle& rect2 = rectangles[j];
if (rect1.x > rect2.x + rect2.width ||
rect2.x > rect1.x + rect1.width ||
rect1.y > rect2.y + rect2.height ||
rect2.y > rect1.y + rect1.height) {
continue;
}
int intersection_width = std::min(rect1.x + rect1.width, rect2.x + rect2.width) - std::max(rect1.x, rect2.x);
int intersection_height = std::min(rect1.y + rect1.height, rect2.y + rect2.height) - std::max(rect1.y, rect2.y);
overlapped_area += intersection_width * intersection_height;
}
}
return overlapped_area;
}
int main() {
std::vector<Rectangle> rectangles;
rectangles.emplace_back(0, 0, 3, 3);
rectangles.emplace_back(2, 2, 4, 4);
rectangles.emplace_back(5, 5, 2, 2);
int overlapped_area = find_overlapped_area(rectangles);
std::cout << "Overlapped area: " << overlapped_area << std::endl;
return 0;
}