All ICPC entries
Competitive Programming

ICPC 2025 - L. Walking on Sunshine

We may move anywhere in the plane. Only movement with a southward component is painful, and even that cost disappears inside the shaded rectangles. We must minimize the total painful distance needed to get from the co...

Source sync Apr 19, 2026
Track ICPC
Year 2025
Files TeX, C++, statement assets
Folder competitive_programming/icpc/2025/L-walking-on-sunshine
ICPC2025TeXC++statement textstatement pdf

Source-first archive entry

This page is built from the copied files in competitive_programming/icpc/2025/L-walking-on-sunshine. Edit competitive_programming/icpc/2025/L-walking-on-sunshine/solution.tex to update the written solution and competitive_programming/icpc/2025/L-walking-on-sunshine/solution.cpp to update the implementation.

The website does not replace those files with hand-maintained HTML. It reads the copied source tree during the build and exposes the exact files below.

Problem Statement

Copied statement text kept beside the solution archive for direct reference.

Problem L
                                        Walking on Sunshine
                                           Time limit: 2 seconds
I’m walking on sunshine, and it don’t feel good – my eyes hurt!
Baku has plenty of sunshine. If you walk away from the sun, or at least perpendicular to its rays, it does
not shine in your eyes. For this problem assume that the sun shines from the south. Walking west or
east or in any direction between west and east with a northward component avoids looking into the sun.
Your eyes will hurt if you walk in any direction with a southward component.
Baku also has many rectangular areas of shade, and staying in these protects your eyes regardless of
which direction you walk in. For example, Figure L.1 shows two shaded areas.
Find the minimum distance you need to walk with the sun shining in your eyes to get from the contest
location to the awards ceremony location.
                                 y
                                                                                  N
                             9
                             8
                                                                             W

                                                                                      N
                                                                                      E
                                                                             N

                             7         •
                                                                      W

                                                                                           E
                                     contest
                             6
                                                                             SW

                                                                                      SE

                             5                                                    S

                             4
                             3
                             2
                             1                                •
                                                       awards ceremony
                                                                                               x
                                       1       2   3     4   5    6      7   8    9   10 11

           Figure L.1: Sample Input 1 and a path that minimizes the sun shining in your eyes.

Input

The first line of input contains five integers n, xc , yc , xa , and ya , where n (0 ≤ n ≤ 105 ) is the number of
shaded areas, (xc , yc ) is the location of the contest, and (xa , ya ) is the location of the awards ceremony
(−106 ≤ xc , yc , xa , ya ≤ 106 ). The sun shines in the direction (0, 1) from south towards north. You
look into the sun if you walk in direction (x, y) for any y < 0 and any x.
The next n lines describe the shaded areas, which are axis-aligned rectangles. Each of these lines
contains four integers x1 , y1 , x2 , and y2 (−106 ≤ x1 < x2 ≤ 106 ; −106 ≤ y1 < y2 ≤ 106 ).
The southwest corner of the rectangle is (x1 , y1 ) and its northeast corner is (x2 , y2 ). The rectangles
describing the shaded areas do not touch or intersect.

Output

Output the minimum distance you have to walk with the sun shining in your eyes. Your answer must
have an absolute or relative error of at most 10−7 .

49th ICPC World Championship Problem L: Walking on Sunshine © ICPC Foundation                                 23

 Sample Input 1                                     Sample Output 1
 2 1 7 5 1                                          3.0
 3 6 5 9
 2 3 6 5

Explanation of Sample 1: Figure L.1 shows an optimal path from the contest location to the awards
ceremony location with 5 segments. On the first segment you walk away from the sun. On the second
and fourth segments you walk towards the sun but in a shaded area. On the third and fifth segments you
walk towards the sun outside the shaded areas. The total length of these two segments is 3.

 Sample Input 2                                     Sample Output 2
 2 0 10 10 0                                        7.0
 2 7 3 8
 4 3 8 5

 Sample Input 3                                     Sample Output 3
 2 11 -1 -1 11                                      0.0
 2 7 3 8
 4 3 8 5

 Sample Input 4                                     Sample Output 4
 3 1 5 9 5                                          0.0
 -5 6 2 9
 4 7 12 8
 1 1 7 3

 Sample Input 5                                     Sample Output 5
 3   1   7   9 3                                    0.0
 2   6   3   8
 4   4   5   6
 6   2   7   4

 Sample Input 6                                     Sample Output 6
 1 0 0 0 0                                          0.0
 -5 -5 5 5

49th ICPC World Championship Problem L: Walking on Sunshine © ICPC Foundation                      24

Editorial

Rendered from the copied solution.tex file. The original TeX source remains available below.

Key Observations

  • If the destination is not south of the start, the answer is immediately $0$: we can move only east, west, and north.

  • Horizontal movement is always free. Northward movement is always free. Only the southward part of our motion matters.

  • Because horizontal motion is free, whenever a shaded rectangle intersects some $y$-range we may move sideways for free and use that rectangle to descend through that entire vertical interval without paying.

  • Therefore the $x$-coordinates are irrelevant. The answer is simply: the total required southward drop, minus the total length of the union of the shaded $y$-intervals that intersect that drop.

Algorithm

  1. If $y_a \ge y_c$, print $0$.

  2. Otherwise, the total unavoidable southward range is $[y_a,y_c]$. For each rectangle, intersect its vertical interval with this range. If the intersection is nonempty, store that interval.

  3. Sort the stored intervals by their lower endpoint and merge them. Let $U$ be the total merged length.

  4. The answer is \[ (y_c-y_a)-U. \]

Correctness Proof

We prove that the algorithm returns the correct answer.

Lemma 1.

Any path from the contest to the awards ceremony must have total southward displacement at least $y_c-y_a$ when $y_a < y_c$.

Proof.

The starting $y$-coordinate is $y_c$ and the final one is $y_a$. Northward motion cannot reduce the required net drop, so the total southward component of the path is at least $y_c-y_a$.

Lemma 2.

Every shaded $y$-interval intersecting $[y_a,y_c]$ can be traversed with zero pain.

Proof.

Horizontal motion is free, so before descending through a shaded interval we may move sideways into the corresponding rectangle at no cost. While inside shade, even southward movement is free. After leaving that interval we may again move sideways for free. Thus the entire covered vertical interval contributes zero painful distance.

Theorem.

The algorithm outputs the correct answer for every valid input.

Proof.

By Lemma 1, any valid path must pay for at least the part of the necessary southward drop that is not covered by shade. By Lemma 2, every shaded subinterval of $[y_a,y_c]$ can indeed be used to make that part of the descent free. Therefore the minimum painful distance is exactly the uncovered length of $[y_a,y_c]$, which is what the algorithm computes.

Complexity Analysis

Sorting the rectangle intervals costs $O(n \log n)$. Merging them is linear. The memory usage is $O(n)$.

Implementation Notes

  • Because the rectangles are disjoint, the merge step is especially simple, but the standard interval union code already handles the general case.

  • The answer is printed as a floating-point number only because the judge allows that format; in this solution it is actually the length of a union of intervals.

Code

Exact copied C++ implementation from solution.cpp.

C++ competitive_programming/icpc/2025/L-walking-on-sunshine/solution.cpp

Exact copied implementation source.

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    long long xc, yc, xa, ya;
    cin >> n >> xc >> yc >> xa >> ya;

    vector<pair<long long, long long>> segs;
    segs.reserve(n);

    if (ya >= yc) {
        for (int i = 0; i < n; ++i) {
            long long x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
        }
        cout << fixed << setprecision(10) << 0.0 << '\n';
        return 0;
    }

    long long need_low = ya;
    long long need_high = yc;
    for (int i = 0; i < n; ++i) {
        long long x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        long long l = max(need_low, y1);
        long long r = min(need_high, y2);
        if (l < r) {
            segs.push_back({l, r});
        }
    }

    sort(segs.begin(), segs.end());
    long long covered = 0;
    long long cur_l = 0, cur_r = 0;
    bool started = false;
    for (const auto& seg : segs) {
        if (!started) {
            cur_l = seg.first;
            cur_r = seg.second;
            started = true;
        } else if (seg.first <= cur_r) {
            cur_r = max(cur_r, seg.second);
        } else {
            covered += cur_r - cur_l;
            cur_l = seg.first;
            cur_r = seg.second;
        }
    }
    if (started) {
        covered += cur_r - cur_l;
    }

    double ans = double((need_high - need_low) - covered);
    cout << fixed << setprecision(10) << ans << '\n';
    return 0;
}

Source Files

Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.

TeX write-up competitive_programming/icpc/2025/L-walking-on-sunshine/solution.tex

Exact copied write-up source.

Raw file
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}

\title{ICPC World Finals 2025\\L. Walking on Sunshine}
\author{}
\date{}

\begin{document}
\maketitle

\section*{Problem Summary}

We may move anywhere in the plane. Only movement with a southward component is painful, and even that
cost disappears inside the shaded rectangles. We must minimize the total painful distance needed to get
from the contest to the awards ceremony.

\section*{Key Observations}

\begin{itemize}[leftmargin=*]
    \item If the destination is not south of the start, the answer is immediately $0$: we can move only east,
    west, and north.
    \item Horizontal movement is always free. Northward movement is always free. Only the southward part
    of our motion matters.
    \item Because horizontal motion is free, whenever a shaded rectangle intersects some $y$-range we may
    move sideways for free and use that rectangle to descend through that entire vertical interval without
    paying.
    \item Therefore the $x$-coordinates are irrelevant. The answer is simply:
    the total required southward drop, minus the total length of the union of the shaded $y$-intervals that
    intersect that drop.
\end{itemize}

\section*{Algorithm}

\begin{enumerate}[leftmargin=*]
    \item If $y_a \ge y_c$, print $0$.
    \item Otherwise, the total unavoidable southward range is $[y_a,y_c]$.
    For each rectangle, intersect its vertical interval with this range.
    If the intersection is nonempty, store that interval.
    \item Sort the stored intervals by their lower endpoint and merge them.
    Let $U$ be the total merged length.
    \item The answer is
    \[
    (y_c-y_a)-U.
    \]
\end{enumerate}

\section*{Correctness Proof}

We prove that the algorithm returns the correct answer.

\paragraph{Lemma 1.}
Any path from the contest to the awards ceremony must have total southward displacement at least
$y_c-y_a$ when $y_a < y_c$.

\paragraph{Proof.}
The starting $y$-coordinate is $y_c$ and the final one is $y_a$.
Northward motion cannot reduce the required net drop, so the total southward component of the path is at
least $y_c-y_a$. \qed

\paragraph{Lemma 2.}
Every shaded $y$-interval intersecting $[y_a,y_c]$ can be traversed with zero pain.

\paragraph{Proof.}
Horizontal motion is free, so before descending through a shaded interval we may move sideways into the
corresponding rectangle at no cost. While inside shade, even southward movement is free. After leaving
that interval we may again move sideways for free. Thus the entire covered vertical interval contributes
zero painful distance. \qed

\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.

\paragraph{Proof.}
By Lemma 1, any valid path must pay for at least the part of the necessary southward drop that is not
covered by shade. By Lemma 2, every shaded subinterval of $[y_a,y_c]$ can indeed be used to make that
part of the descent free. Therefore the minimum painful distance is exactly the uncovered length of
$[y_a,y_c]$, which is what the algorithm computes. \qed

\section*{Complexity Analysis}

Sorting the rectangle intervals costs $O(n \log n)$. Merging them is linear. The memory usage is $O(n)$.

\section*{Implementation Notes}

\begin{itemize}[leftmargin=*]
    \item Because the rectangles are disjoint, the merge step is especially simple, but the standard interval
    union code already handles the general case.
    \item The answer is printed as a floating-point number only because the judge allows that format; in this
    solution it is actually the length of a union of intervals.
\end{itemize}

\end{document}