EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2024
  3. Final Round

Codey and Rectangles 2

https://www.hackerrank.com/contests/codenection-2024-final-round-closed-category/challenges/cn24-19

Question

Codey is playing with rectangles again, but this time it's even more fun! Codey decorates the floor with n rectangular sheets of paper. Each side of the sheets is parallel to the x-axis or y-axis.

Each sheet's position on the floor is defined by four integers:

  • aia_iai​ and bib_ibi​: The x-coordinates of the left and right edges of the sheet.

  • cic_ici​ and did_idi​: The y-coordinates of the bottom and top edges of the sheet.

Help Codey calculate the total area covered by the rectangular sheets of paper on the floor.

Input Format

The first line contains an integer n, where n represents the number of rectangular sheet of paper Codey has.

The following n lines contain four integers ai,bi,cia_i,b_i,c_iai​,bi​,ci​ and did_idi​, where and represent the left and right edges of the sheet on the x-axis, while cic_ici​ and did_idi​ represent the bottom and top edges of the sheet on the y-axis.

Constraints

2≤n≤1002 \le n \le 1002≤n≤100
0≤ai≤bi≤1000 \le a_i \le b_i \le 1000≤ai​≤bi​≤100
0≤ci≤di≤1000 \le c_i \le d_i \le 1000≤ci​≤di​≤100

Output Format

Output an integer, the total area covered by the sheets of paper.

Sample Inputs:

Input

2
1 3 0 2
0 2 1 3

Output

7

Explanation

The first rectangle spans from (1,0)(1,0)(1,0) to (3,2)(3,2)(3,2), covering an area of 4 square units. The second rectangle spans from (0,1)(0,1)(0,1) to (2,3)(2, 3)(2,3), covering an area of 4 square units.

Since there is an overlap of 111 square unit, the total area covered by both sheets is 4+4−1=74 + 4 - 1 = 74+4−1=7 square units.


Solution - Easier Rectangles?
  1. Create a 100 * 100 array (yes, it is not that large)

  2. Parse through the range and fill all the coordinate ranges to 1 (double for loop).

  3. Count the total number which is 1. Problem solved.

Here's the code:

array = [[0 for _ in range(100)] for _ in range(100)]

n = int(input().strip())
for _ in range(n):
    x1, x2, y1, y2 = map(int, input().split())
    for i in range(x1, x2):
        for j in range(y1, y2):
            array[i][j] = 1

print(sum(row.count(1) for row in array))
PreviousCodey and SymbolNextCodey and Jutsu

Last updated 2 months ago

This question is even easier than preliminary round, .

🇲🇾
Codey and Rectangles