Official

B - Overlapping sheets Editorial by en_translator


By the constraints of the problem, we do not need to consider the region outside \(0\leq x\leq 100\) and \(0\leq y\leq 100\). Also, for any pair \((i,j)\) \((1\leq i\leq 100, 1\leq j\leq 100)\), the region within \(i-1\leq x\leq i\) and \(j-1\leq y\leq j\) is either entirely covered by a sheet or not covered at all. Therefore, it is sufficient to consider the following problem.

  • There is a \(100\times 100\) grid. Let us call the cell in the \(i\)-th row from the top and \(j\)-th column from the left cell \((i,j)\). Suppose that each cell is initially painted white.
  • For each \(1\leq k\leq N\) in order, the \(i\)-th operation paints cell \((i,j)\) for all integer pairs \((i,j)\) with \(A_k\leq i\leq B_k-1\) and \(C_k\leq j\leq D_k-1\).
  • Find the number of cells painted black after the \(N\) operations.

The process can be actually simulated with for statements, and one can use for statements and if statements to count the cells ending up being painted black, in order to find the answer.

The simulation requires at most about \(N\times (100\times 100)\leq 10^6\), which is fast enough even with a naive implementation. Therefore, the problem has been solved.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n,a,b,c,d,ans=0;
  bool g[100][100]={};

  cin>>n;
  for(int k=0;k<n;k++){
    cin>>a>>b>>c>>d;
    for(int i=a;i<b;i++)for(int j=c;j<d;j++)g[i][j]=true;
  }

  for(int i=0;i<100;i++)for(int j=0;j<100;j++)if(g[i][j])ans++;
  cout<<ans<<endl;

	return 0;
}

Sample code in Python:

n=int(input())
g=[[False for i in range(100)]for i in range(100)]

for k in range(n):
    a,b,c,d=map(int, input().split())
    for i in range(a,b):
        for j in range(c,d):
            g[i][j]=True

ans=0
for i in range(100):
    for j in range(100):
        if g[i][j]==True:
            ans+=1
print(ans)

posted:
last update: