AcWing-3203.Draw a picture.

Drawing a rectangle from (x1, y1) to (x2, y2) on a piece of paper that defines a rectangular coordinate system means to color the area between the abscissa range from x1 to x2 and the ordinate range from y1 to y2 .

The following figure shows an example of drawing two rectangles.

The first rectangle is (1,1) to (4,4), represented in green and purple.

The second rectangle is (2,3) to (6,5), represented in blue and purple.

In the picture, a total of 15 units of area are painted, of which the purple part is painted twice, but only once when calculating the area.

In the actual coloring process, all the rectangles are painted in a uniform color, and the different colors shown in the figure are only for convenience of illustration.

Insert picture description here
Given all the rectangles to be drawn, how many units of the area are colored.
Input format
The first line of input contains an integer n, which represents the number of rectangles to be drawn.

The next n rows, each with 4 non-negative integers, represent the abscissa and ordinate of the lower left corner of the rectangle to be drawn, and the abscissa and ordinate of the upper right corner of the rectangle to be drawn.
Output format
Output an integer, indicating how many units of the area are painted.
Data range
1≤n≤100,
0≤ abscissa, ordinate≤100

Input example:
2
1 1 4 4
2 3 6 5
Output example:
15

simulation

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    int n,x1,y1,x2,y2;
    bool flag[110][110];
    memset(flag,false,sizeof(flag));
    cin>>n;
    while(n--){
    
    
        cin>>x1>>y1>>x2>>y2;
        for(int i=x1;i<x2;i++){
    
    
            for(int j=y1;j<y2;j++){
    
    
                flag[i][j]=true;
            }
        }
    }
    int res=0;
    for(int i=0;i<=100;i++){
    
    
        for(int j=0;j<=100;j++){
    
    
            if(flag[i][j])
            res++;
        }
    }
    cout<<res<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51768569/article/details/113835161