AcWing one question every day during winter vacation-Day37 drawing

On a piece of paper that defines a rectangular coordinate system, draw one from (x 1, y 1) to (x 2, y 2) (x1,y1) to (x2,y2)(x1,y 1 ) to ( x 2 ,The rectangle of y 2 ) refers to the range of abscissa fromx 1 to x 2 x1 to x2x 1 to x 2 , the ordinate ranges fromy 1 to y 2 y1 to y2Color the area between y 1 and y 2 .

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.

p21.png

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, 1≤n≤100,1n1 0 0 ,
0 ≤ abscissa, ordinate ≤ 100 0 ≤ abscissa, ordinate ≤ 1000Horizontal sitting standard , ordinate sitting standard. 1 0 0
Input Sample:

2
1 1 4 4
2 3 6 5

Sample output:

15

Analysis: Mark the colored square every time, and finally traverse the entire square. If the square is colored, add it.

Code:

#include<bits/stdc++.h>
using namespace std;
const int N=105;
int n,x_1,y_1,x_2,y_2,res,mx,my;
bool st[N][N];
int main(){
    
    
    cin>>n;
    for(int i=0;i<n;i++){
    
    
        cin>>x_1>>y_1>>x_2>>y_2;
        mx=max(mx,x_2);
        my=max(my,y_2);
        for(int i=x_1;i<x_2;i++)
            for(int j=y_1;j<y_2;j++)
                st[i][j]=1;
    }
    for(int i=0;i<=mx;i++)
        for(int j=0;j<=my;j++)
            if(st[i][j]) res++;
    cout<<res<<endl;
}

Guess you like

Origin blog.csdn.net/messywind/article/details/113856843