cppAcademy
FROM LEARNING TO COMPETING

Intermediate.
Put your skills to the test.

Bulgarian Informatics Group D · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group D also includes challenging national-level problems. The six 2017 National tasks shared by Groups C and D use their existing permanent URLs. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP D2024 · EasyGeometry and boundary updates

Rectangle After Sequential Cuts

National Olympiad — Regional round (2024 season)

Submission name: rectcut.cpp

Problem summary

Perform horizontal and vertical cuts, discarding the named side after each cut. Find the remaining area.

Input

x1 y1 x2 y2, cut count N, then H/V coordinate L/R/T/B for each cut.

Output

The final rectangle area.

Constraints

0≤N≤20; initial coordinates lie in [0,10^9]; 0<cut coordinate<10^9. Each cut crosses the current rectangle.

Worked example

Input and output for the worked example discussed below.

Input

1 2 20 30
2
V 3 L
H 20 T

Output

306

Solution analysis

  1. After each axis-aligned cut, the surviving shape remains a rectangle. Four boundary coordinates are therefore sufficient state.
  2. Removing L sets the left bound, R the right bound, B the bottom bound, and T the top bound to the cut coordinate. The letter names what is removed.
  3. The sample first leaves x in [3,20], then y in [2,20], giving (20−3)(20−2)=306. No-cut input leaves the original area unchanged.

Why it works

Each operation replaces exactly the boundary next to the removed side. Induction shows that the maintained bounds describe the final rectangle, whose width times height is its area.

Complexity: O(N) time and O(1) auxiliary space.

Watch out. Use long long because areas reach 10^18. Do not reverse removed and retained sides.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
using namespace std;
int main(){
    long long left;
    long long bottom;
    long long right;
    long long top;
    cin>>left>>bottom>>right>>top;
    int cuts;
    cin>>cuts;
    while(cuts-->0){
        char direction;
        char removed;
        long long position;
        cin>>direction>>position>>removed;
        // One cut replaces exactly one boundary.
        if(removed=='L'){
            left=position;
        }
        else if(removed=='R'){
            right=position;
        }
        else if(removed=='B'){
            bottom=position;
        }
        else{
            top=position;
        }
    }
    cout<<(right-left)*(top-bottom)<<'\n';
}

Source: Arena Olimpiici ↗