ICPC 2017 - L. Visual Python++
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/2017/L-visual-python. Edit
competitive_programming/icpc/2017/L-visual-python/solution.tex to update the written solution and
competitive_programming/icpc/2017/L-visual-python/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.
Rapid City
event
sponsor
ICPC 2017
Problem L
Visual Python++
Time limit: 5 seconds
In the recently proposed Visual Python++ programming language, a block of statements is represented as
a rectangle of characters with top-left corner in row r1 and column c1 , and bottom-right corner in row r2
and column c2 . All characters at locations (r, c) with r1 ≤ r ≤ r2 and c1 ≤ c ≤ c2 are then considered to
belong to that block. Among these locations, the ones with r = r1 or r = r2 or c = c1 or c = c2 are called
a border.
Statement blocks can be nested (rectangles contained in other rectangles) to an arbitrary level. In a syntac-
tically correct program, every two statement blocks are either nested (one contained in the other) or disjoint
(not overlapping). In both cases, their borders may not overlap.
Programmers are not expected to draw the many rectangles contained in a typical program – this takes too
long, and Visual Python++ would not have a chance to become the next ICPC programming language. So a
programmer only has to put one character ‘p’ in the top-left corner of the rectangle and one character ‘y’ in
the bottom-right corner. The parser will automatically match up the appropriate corners to obtain the nesting
structure of the program.
Your team has just been awarded a five-hour contract to develop this part of the parser.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 105 ), the number of corner pairs. Each of the next
n lines contains two integers r and c (1 ≤ r, c ≤ 109 ), specifying that there is a top-left corner in row r and
column c of the program you are parsing. Following that are n lines specifying the bottom-right corners in
the same way. All corner locations are distinct.
Output
Display n lines, each containing one integer. A number j in line i means that the ith top-left corner and the
j th bottom-right corner form one rectangle. Top-left and bottom-right corners are each numbered from 1 to
n in the order they appear in the input. The output must be a permutation of the numbers from 1 to n such
that the matching results in properly nested rectangles. If there is more than one valid matching, any one
will be accepted. If no such matching exists, display syntax error.
Sample Input 1 Sample Output 1
2 2
4 7 1
9 8
14 17
19 18
Rapid City
event
sponsor
ICPC 2017
Sample Input 2 Sample Output 2
2 1
4 7 2
14 17
9 8
19 18
Sample Input 3 Sample Output 3
2 syntax error
4 8
9 7
14 18
19 17
Sample Input 4 Sample Output 4
3 syntax error
1 1
4 8
8 4
10 6
6 10
10 10
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 2017\\L. Visual Python++}
\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;
}