ICPC 2019 - F. Directing Rainfall
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
Source-first archive entry
This page is built from the copied files in competitive_programming/icpc/2019/F-directing-rainfall. Edit
competitive_programming/icpc/2019/F-directing-rainfall/solution.tex to update the written solution and
competitive_programming/icpc/2019/F-directing-rainfall/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 F
Directing Rainfall
Time limit: 15 seconds
Porto and the nearby Douro Valley are famous for producing port wine. Wine lovers from all over
the world come here to enjoy this sweet wine where it is made. The International Consortium of Port
Connoisseurs (ICPC) is organizing tours to the vineyards that are upstream on the Douro River. To make
visits more pleasurable for tourists, the ICPC has recently installed sun tarps above the vineyards. The
tarps protect tourists from sunburn when strolling among the vines and sipping on a vintage port.
Unfortunately, there is a small problem with the tarps. Grapes need sunlight and water to grow. While
the tarps let through enough sunlight, they are entirely waterproof. This means that rainwater might not
reach the vineyards below. If nothing is done, this year’s wine harvest is in peril!
The ICPC wants to solve their problem by puncturing the tarps so that they let rainwater through to the
vineyards below. Since there is little time to waste before the rainy season starts, the ICPC wants to
make the minimum number of punctures that achieve this goal.
We will consider a two-dimensional version of this problem. The vineyard to be watered is an interval
on the x-axis, and the tarps are modeled as line segments above the x-axis. The tarps are slanted, that is,
not parallel to the x- or y-axes (see Figure F.1 for an example). Rain falls straight down from infinitely
high. When any rain falls on a tarp, it flows toward the tarp’s lower end and falls off from there, unless
there is a puncture between the place where the rain falls and the tarp’s lower end—in which case the
rain will fall through the puncture instead. After the rain falls off a tarp, it continues to fall vertically.
This repeats until the rain hits the ground (the x-axis).
(a) Tarps are shown as black slanted (b) An optimal solution: by punctur-
line segments and the vineyard as a ing two tarps in the locations of the red
green line segment at the bottom. circles, some rain (shown in blue) that
starts above the vineyard will reach
the vineyard.
Figure F.1: Illustration of Sample Input 1.
For legal reasons you have to ensure that at least some of the rain that reaches the vineyard originated
from directly above the vineyard. This is to prevent any vineyard from stealing all their rain from
neighboring vineyards (see the second sample input for an example).
Input
The first line of input contains three integers `, r and n, where (`, r) (0 ≤ ` < r ≤ 109 ) is the interval
representing the vineyard and n (0 ≤ n ≤ 5 · 105 ) is the number of tarps. Each of the following n lines
describes a tarp and contains four integers x1 , y1 , x2 , y2 , where (x1 , y1 ) is the position of the tarp’s lower
end and (x2 , y2 ) is the position of the higher end (0 ≤ x1 , x2 ≤ 109 , x1 6= x2 , and 0 < y1 < y2 ≤ 109 ).
The x-coordinates given in the input (`, r, and the values of x1 and x2 for all tarps) are all distinct. The
tarps described in the input will not intersect, and no endpoint of a tarp will lie on another tarp.
Output
Output the smallest number of punctures that need to be made to get some rain falling from above the
vineyard to the vineyard.
Sample Input 1 Sample Output 1
10 20 5 2
32 50 12 60
30 60 8 70
25 70 0 80
15 30 28 40
5 20 14 25
Sample Input 2 Sample Output 2
2 4 2 1
3 2 0 3
5 2 1 5
Editorial
Rendered from the copied solution.tex file. The original TeX source remains
available below.
Key Observations
Write the structural observations that make the problem tractable.
State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
If the constraints matter, explain exactly which part of the solution they enable.
Algorithm
Describe the data structures and the state maintained by the algorithm.
Explain the processing order and why it is sufficient.
Mention corner cases explicitly if they affect the implementation.
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
State the first key claim.
Proof.
Provide a concise proof.
Lemma 2.
State the next claim if needed.
Proof.
Provide a concise proof.
Theorem.
The algorithm outputs the correct answer for every valid input.
Proof.
Combine the lemmas and finish the argument.
Complexity Analysis
State the running time and memory usage in terms of the input size.
Implementation Notes
Mention any non-obvious implementation detail that is easy to get wrong.
Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
Code
Exact copied C++ implementation from solution.cpp.
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Source Files
Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}
\title{ICPC World Finals 2019\\F. Directing Rainfall}
\author{}
\date{}
\begin{document}
\maketitle
\section*{Problem Summary}
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
\section*{Key Observations}
\begin{itemize}[leftmargin=*]
\item Write the structural observations that make the problem tractable.
\item State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
\item If the constraints matter, explain exactly which part of the solution they enable.
\end{itemize}
\section*{Algorithm}
\begin{enumerate}[leftmargin=*]
\item Describe the data structures and the state maintained by the algorithm.
\item Explain the processing order and why it is sufficient.
\item Mention corner cases explicitly if they affect the implementation.
\end{enumerate}
\section*{Correctness Proof}
We prove that the algorithm returns the correct answer.
\paragraph{Lemma 1.}
State the first key claim.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Lemma 2.}
State the next claim if needed.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.
\paragraph{Proof.}
Combine the lemmas and finish the argument.
\section*{Complexity Analysis}
State the running time and memory usage in terms of the input size.
\section*{Implementation Notes}
\begin{itemize}[leftmargin=*]
\item Mention any non-obvious implementation detail that is easy to get wrong.
\item Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
\end{itemize}
\end{document}
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}