To solve this problem, we need to efficiently handle a series of fold operations on a 1-dimensional sheet of paper and answer queries about the total width of the paper pieces between two cuts after these folds. The key challenge is to model the transformations caused by each fold and efficiently compute the required results for the queries.
Approach
- Modeling Folds: Each fold operation splits the current sheet into two parts, reverses one part, and combines them based on which part is longer. This transformation changes the current coordinate system and affects how subsequent operations are performed.
- Tracking Intervals: Maintain a list of intervals representing the current sheet in the original coordinate system, along with their direction (forward or reversed). Each interval is a tuple
(start, end, reversed) where start and end are positions in the original sheet, and reversed indicates if the interval is mirrored.
- Handling Queries: For each query, determine the segments of the original sheet that lie between the given cuts in the current coordinate system and sum their lengths to get the total width.
Solution Code
import sys
def main():
n, q = map(int, sys.stdin.readline().split())
segments = [(0, n, False)] # list of (start, end, reversed)
current_width = n
for _ in range(q):
parts = sys.stdin.readline().split()
if not parts:
continue
if parts[0] == '1':
p = int(parts[1])
new_width = max(p, current_width - p)
left = []
right = []
remaining = p
# Split into left and right parts
i = 0
while i < len(segments) and remaining > 0:
seg = segments[i]
seg_start, seg_end, seg_rev = seg
seg_len = seg_end - seg_start
if seg_len <= remaining:
left.append(seg)
remaining -= seg_len
i += 1
else:
# Split this segment
if not seg_rev:
split = seg_start + remaining
left.append((seg_start, split, False))
right.append((split, seg_end, False))
else:
split = seg_end - remaining
left.append((split, seg_end, True))
right.append((seg_start, split, True))
remaining = 0
i += 1
break
right += segments[i:]
# Reverse the left segments
reversed_left = []
for seg in reversed(left):
s, e, rev = seg
reversed_left.append((s, e, not rev))
# Determine new segments
if new_width == p:
# left part is longer, new segments are reversed left + right
segments[:] = reversed_left + right
else:
# right part is longer, new segments are right + reversed left
segments[:] = right + reversed_left
current_width = new_width
else:
li = int(parts[1])
ri = int(parts[2])
res = 0
# Find all segments covered by [li, ri)
current_pos = 0
for seg in segments:
seg_start, seg_end, seg_rev = seg
seg_len = seg_end - seg_start
if current_pos >= ri:
break
if current_pos + seg_len <= li:
current_pos += seg_len
continue
# Calculate overlap
start_in_seg = max(li, current_pos)
end_in_seg = min(ri, current_pos + seg_len)
if start_in_seg >= end_in_seg:
current_pos += seg_len
continue
# Map to original
a = start_in_seg - current_pos
b = end_in_seg - current_pos
if seg_rev:
a_mapped = seg_end - a
b_mapped = seg_end - b
a, b = b, a
a_mapped, b_mapped = b_mapped, a_mapped
else:
a_mapped = seg_start + a
b_mapped = seg_start + b
res += b_mapped - a_mapped
current_pos += seg_len
print(res)
if __name__ == "__main__":
main()
Explanation
- Folding: Each fold operation splits the current intervals into left and right parts. The left part is reversed and combined with the right part based on which part is longer. This ensures that the current intervals correctly represent the transformed sheet after the fold.
- Query Handling: For each query, the code iterates through the current intervals, mapping the query range to the original sheet's coordinates and summing the lengths of the overlapping segments. This provides the total width of the paper pieces between the cuts, considering all layers created by previous folds.