We need to understand the problem. There is a paper of dimensions 1 x n (a strip of length n). We perform q queries of two types:
- Fold at position pi (distance from left border). After folding, the left part (width pi) goes above the right part (width current_width - pi). So we effectively "fold" the paper. The paper is 1D but after folding it becomes layered (multiple layers stacked). The paper is essentially a sequence of segments (units?) but we care about the width of paper pieces after folding? Actually we need to interpret the folding operation: We have a long strip, we fold it. After folding, it becomes a stack of layers, each of which is a segment of the original strip. The width of the overall folded paper is now max(pi, current_width - pi)? Actually after folding, the physical width of the entire paper is the larger of the two parts? But the problem statement: "After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi)." This suggests that we fold the paper at position pi, so the left part is placed on top of the right part, and the resulting folded strip's width is? Usually if you fold a paper, the overall shape is like a U? But here the paper is 1D (a line) and you fold at a point, the left part is placed above the right part. So the paper is now two layers on top of each other, but the physical width of the resulting paper is the maximum of pi and current_width - pi? Actually think: after folding, the paper is still a strip? It says: "the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi)." So after folding, we have a pile of two layers: one of width pi, one of width (curr - pi). But these layers are aligned along the left? Or maybe the folding is like taking the left part and flipping it over the right part, so the left part lies on top of the right part but reversed? However the problem deals with "width" and positions. Also the second query: "Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper."
We need to interpret what "cuts" mean. After folding, the paper has multiple layers; when we make a cut (like a vertical cut) at a distance from the left border of the current sheet of paper (the folded paper, presumably physical left border), it cuts through all layers at that horizontal position. Then we consider the pieces between the two cuts (i.e., the segment from li to ri) across all layers, and sum up the widths of those pieces? Actually "total width of the paper pieces" likely means: For each layer (strip of paper), after making two cuts at distances li and ri from the left border, we cut through all layers, resulting in many pieces; we consider only the pieces that lie between the two cuts (i.e., from li to ri) in each layer, and we sum their widths (i.e., lengths along the strip). However note that the paper strip is 1D originally, but after folding it's layered. The "cut" is perpendicular to the strip orientation? Actually the paper is of dimensions 1 x n: width n, height 1? It's a rectangle. So it's 1 unit tall and n units wide. Folding: you fold along a vertical line? Possibly the paper lies horizontally, width along x-axis. Folding is like lifting the left part over the right part. After folding, the paper is stacked, so you have multiple layers, each layer is a segment of the original paper but possibly reversed? The cut is a vertical cut (since the left border is vertical). So you cut at some x-coordinate relative to the left border of the current (folded) paper. That cut goes through all layers that exist at that x-coordinate, cutting those layers into pieces. The "width of the paper pieces" presumably refers to the total length (along the original orientation) of the paper that is between the two cut lines.
But the problem might be simpler: It's about maintaining the mapping from the current coordinate (position in the folded paper's width) to the set of original segments that are at that coordinate, and we need to compute the total length of original paper that lies between two positions li and ri in the current folded paper's coordinate system.
Alternatively, maybe it's about maintaining the sequence of layers and their orientations, and we need to answer queries of total length of paper that lies within a horizontal interval after folds.
We need to re-read the problem statement carefully.
"Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types:
- Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi).
- Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper. "
The phrase "total width of the paper pieces" might be ambiguous: It could mean the sum of the widths (i.e., lengths) of the pieces that are between the two cuts. Since the paper is 1 unit tall, the area is essentially the width. But it's about widths.
Consider example:
Input:
7 4
1 3
1 2
2 0 1
2 1 2
Output:
4
3
Let's simulate.
Start: width = 7 (positions 0..7? Actually width n, so from left border, positions 0 to n? Usually distance from left border: distance li means from left border. So the paper extends from 0 to n (n units). The "width" is n. So positions are measured from left edge, ranging from 0 to current width.
Query1: Fold at pi=3. Current width=7. So left part length=3, right part length=4. After folding, the left part goes above the right part. How does the resulting physical sheet look? The two parts are stacked. The width of the folded sheet? If you place the left part on top of the right part, they are aligned at the left? But the left part is of length 3, the right part of length 4. Possibly they are placed such that the left edges align? However if the left part is placed above the right part, they could be aligned at the left border. Then the total width becomes max(3,4)=4. But if the left part is placed above the right part, the bottom layer (right part) is length 4, and the top layer (left part) is length 3, so the overall width is 4 (the longer one). Alternatively, they could be aligned at the fold line? Usually when you fold a paper, you bring the left part over the right, so that the left part's right edge aligns with the fold line? Actually imagine a strip of paper from 0 to 7. You fold at position 3. So you take the segment [0,3] and fold it over to lie on top of segment [3,7]. After folding, the left part is now reversed? Typically if you fold, the orientation might reverse, but the problem statement doesn't mention reversal. It just says "above". So likely it's simply stacking, not reversing. Also the cuts are made from the left border of the current sheet. If we aligned the left edges, then the left border of the folded sheet is at position 0 of the bottom layer? But the top layer's left border might also be at 0? Actually after folding, the overall sheet has a left border, which probably is the leftmost edge of the bottom layer (since that is the visible border). So bottom layer (right part) extends from 0 to 4? Wait, the original right part after folding: originally the right part was from pi to current width, i.e., positions 3 to 7, length 4. If we align its left edge at the fold line? Let's think.
We need to define the coordinate system after folding. The problem says: "After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi)." This suggests that the two parts are now stacked. The "rightmost part" is the bottom layer? Typically when you fold, the part that remains in place (the right part) is the bottom, and the left part is placed on top. The left part is placed above, so it's on top. So the bottom layer is the right part. The overall sheet's dimensions: the width is the maximum of the widths of the two layers, because the top layer might not cover the entire bottom. So the physical width becomes max(pi, current_width - pi). And the left border is aligned with the left border of the bottom layer? Possibly the bottom layer's left border is at the fold line? Actually after folding, the bottom layer (right part) is still in its original position? But we folded the left part over; so the right part remains where it was, but its left edge is originally at position pi (3). The left part originally from 0 to pi is folded over, so its right edge (originally at pi) is now at the fold line? Typically, you fold a paper by taking the left side and flipping it over to the right, so that the left part now lies on top of the right part, but its orientation may be reversed. After folding, the left part's original left edge (0) now lies somewhere? If you fold without shifting, you just rotate the left part around the fold line, so its left edge ends up at position pi + (pi?) Let's do physical simulation: Paper strip from 0 to 7. Fold at 3. You take the left part (0-3) and fold it to the right, over the right part. The left part rotates 180 degrees around the fold line (x=3). After folding, the left part is now covering the right part but reversed: its original left edge (0) ends up at position 3+3 = 6? Actually rotating a segment of length 3 around its right endpoint (at 3) to the right would place it extending from 3 to 6? But then it would be on top of the right part from 3 to 6, but the right part extends from 3 to 7. That would be a stack of two layers from 3 to 6, and only the bottom layer from 6 to 7. The left border of the whole assembly is at 3? Actually the leftmost point is at 3 (the fold line). But the problem says after fold, we consider the "current sheet of paper" which has a left border. In typical folding puzzles, the width changes: if you fold left over right, the new width becomes (current_width - pi) because the left part is now on top of the right part and the leftmost edge moves to the fold line. That is, the new width is max(pi, current_width - pi)? But from the example, let's compute.
Initial width =7.
After fold at 3: according to typical folding (like folding a strip to the right), the new width would be 4? Because the left part of length 3 folds onto the right part of length 4, and the overall width is the length of the part that is not covered? Actually if you fold left over right, the left part is placed on top of the right part, so the bottom part extends from the fold line to the right end. So the bottom layer goes from 0 to 4? If we consider the coordinate system after folding: we usually re-normalize so that the left border of the new sheet is the leftmost point of the bottom layer. After folding left over right, the bottom layer's leftmost point is at the fold line (originally at 3). So if we set the new coordinate 0 at that leftmost point, then the bottom layer occupies [0, current_width-pi] = [0,4]. The top layer occupies [0, pi] but maybe reversed? Actually the top layer is the left part folded over. Its physical placement: originally left part from 0 to pi, after folding it lies on top, but its leftmost point (original 0) is now at distance pi from the fold line? In folding left over right, you rotate the left part 180 degrees about the fold line; the left part ends up oriented reversed and placed to the right of the fold line, covering a segment of length pi starting at the fold line? Or starting at the fold line? Let's be precise: Suppose you have a strip from 0 to L. Fold at point p. Take the segment [0,p] and flip it over to lie on top of [p,L]. After flipping, the segment [0,p] will occupy the region from p to p+p? Actually imagine a physical strip: you hold it at the fold line and bring the left side over to the right. The left side rotates about the fold line. After folding, the left side will lie on top of the right side, but its orientation is reversed. The left side's original point 0 will now be at position p + p = 2p? Wait, if you rotate 180 degrees around point p, a point at distance d to the left of p (i.e., at p - d) will map to a point at distance d to the right of p (i.e., at p + d). So the left part [0,p] maps to [p, 2p] (reversed order). So after folding, the top layer occupies the region [p, 2p] (if 2p <= L? But here p=3, L=7 => 2p=6 <=7). So the top layer lies on top of the bottom layer from p to min(2p, L). The bottom layer is the original right part [p, L] = [3,7]. So from 3 to 6, both layers exist; from 6 to 7, only bottom layer. The leftmost point of the whole assembly is at p=3 (the fold line). The width (horizontal extent) is from 3 to 7 = 4 units. So new width = L - p = 4, which is current_width - pi. That is indeed max(p, L-p) if p <= L/2? Actually if p > L/2, then folding left over right would cause the left part to be longer, so after folding, the left part would extend beyond the right part? But the problem says "leftmost part ... must be above the rightmost part". So they always fold left part over right part? But if p > current_width/2, then the left part is larger; after folding left over right, the left part will cover the right part and extend beyond? But then the width might become p (the longer part) because the left part after folding will extend to the right? Let's simulate: L=10, p=8. Left part length 8, right part length 2. Fold left over right: left part [0,8] rotated about p=8? Actually p=8 is the fold point; left part [0,8], right part [8,10]. Rotating left part around p=8: point 0 maps to 16, which is beyond the original L. But physically you can't have paper extending beyond? Actually when folding, you typically bring the left part over; the left part would then lie on top of the right part, but since the left part is longer, part of it will extend beyond the right part's right edge. The overall width after folding would be the length of the left part after folding? After folding left over right, the left part's leftmost point after folding is at p - (L-p)? Hmm, physical folding: You have a strip from 0 to 10, fold at 8. You take the left part (0-8) and fold it to the right over the right part (8-10). Since the left part is longer, when you fold, the left part will extend beyond the right part. The fold line is at 8. The left part rotates; its points map to positions: a point at distance d left of 8 (position 8-d) goes to position 8+d. So the left part after folding occupies [8, 8+8] = [8,16]. But the original right part occupies [8,10]. So from 8 to 10, both layers; from 10 to 16, only top layer. The leftmost point of the whole assembly is at 8 (fold line). The rightmost point is at max(10,16)=16. So width = 16-8 = 8, which equals p. So width after folding = max(p, L-p). So indeed the new width = max(p, L-p). In the example with p=3 and L=7, max=4, which matches L-p. With p=8 and L=10, new width = 8 = p.
So general rule: After folding at pi, the new width becomes max(pi, current_width - pi). And the orientation: the bottom layer is the right part (if we fold left over right). Actually the bottom layer is the part that remains in place? The problem says "the leftmost part ... must be above the rightmost part". So left part goes on top of the right part. So the bottom layer is the right part, but if the left part is longer, after folding it extends beyond, so the overall assembly's bottom layer? Actually the bottom layer is the right part (original from pi to width). It stays at its original positions relative to the fold line? After folding, we realign the coordinate system so that the left border of the current sheet is at the leftmost point of the assembly. So the coordinates shift such that the left border is at the leftmost physical point. In the above examples, after folding left over right, the leftmost point is at the fold line (pi) if pi <= current_width/2? Actually if left part <= right part, then the leftmost point is at pi (fold line). If left part > right part, then the leftmost point is at current_width - pi? Let's compute: For p=8, L=10, after folding, the assembly extends from 8 (fold line) to 16. But is 8 the leftmost? Yes, since the bottom layer goes from 8 to 10 and top from 8 to 16, leftmost is 8. So leftmost = pi. For p=2, L=7, left part=2, right=5, assembly from 2 to 7, leftmost =2 = pi. So seems leftmost is always pi? Wait, consider folding right over left? But they always fold left over right. So leftmost after folding is pi (the fold line). However if we think of the coordinate system after folding, we typically set the left border to 0. So we need to map coordinates.
But the problem likely expects that after each fold, the width changes to max(pi, current_width - pi). And the mapping from old coordinates to new coordinates (the positions on the folded paper) is defined. For the second query, we are given li and ri distances from the left border of the current sheet. So we need to understand how the layers are arranged and how to compute the total length of original paper that lies between li and ri.
We need to interpret "total width of the paper pieces". Imagine we have a stack of several layers (since folds can happen multiple times). The current sheet has some total width W (the physical width). There are multiple layers (some may be reversed orientation). At any x coordinate (distance from left border) between 0 and W, there might be zero, one, or multiple layers of paper. The paper is originally a continuous strip of length n. After folds, each layer corresponds to some contiguous segment of the original strip, possibly reversed, and placed at certain x-interval. When we make a cut at distance x from the left border, we cut through all layers present at that x, perpendicular to the x-axis. This cut will cut the original strip at the points corresponding to those layers. If we make two cuts at li and ri, we effectively segment each layer into pieces. We consider the pieces that are between the two cuts (i.e., the part of each layer that lies in the interval [li, ri]). For each such piece, we take its width (i.e., its length along the x-axis? Actually the piece is a segment of the layer, and its "width" would be the length along the x-direction, which is the length of that part of the layer. But since the layer is originally a part of the 1xn strip, its length in the x-direction is the same as the length of that segment of the original strip, because the strip is oriented horizontally? However, if a layer is reversed, the x-coordinate mapping might be reversed, but the length of a segment in x-direction corresponds to the length on the original strip (since the paper is not stretched). So the total width of the paper pieces between the cuts would be the sum over layers of the length of the intersection of that layer's x-interval with [li, ri], but we must be careful because the same original piece might be counted multiple times if it is stacked? The phrase "paper pieces" likely means the physical pieces after cutting; if there are multiple layers, each layer is a separate piece? The problem might ask for the total area? But paper is 1 x n, area is n. After cutting, the total area of pieces between the cuts should be the sum of lengths (since height=1). But note that if multiple layers overlap, the total area of paper in that region is the sum of lengths of overlapping segments? Actually if layers are stacked, the physical paper has multiple sheets on top of each other; if you cut and then consider the pieces, you would have multiple pieces (one from each layer) that are separate physical pieces. So the total width (maybe they mean total length of paper) of those pieces is the sum of the lengths of each layer's portion within the cuts. So yes, it's essentially the total area of paper in that vertical strip between x=li and x=ri.
But is it simply the measure of the original strip that projects onto that x-interval, counting multiplicities for overlapping layers? For each point x, the number of layers covering it is the number of times the original strip passes over that x coordinate (due to folding). The total length of paper in [li, ri] would be ∫_{li}^{ri} (number of layers at x) dx. Since the paper is 1D, but folded, each point x corresponds to some points on the original strip. The mapping of the original strip to the x-coordinate after folds is a piecewise linear function, possibly with multiple values (multiple layers). The total "width" of paper pieces between cuts is the total length of original strip that lies in that x-range, counting multiplicities if overlapping? But careful: The original strip is a one-dimensional object; after folding, different parts of it may occupy the same x-coordinate (stacked). So the total length of paper along the x-direction (i.e., the measure of the projection onto the x-axis) is not directly the length of original strip; it's the integral of the density. Actually if we have a physical strip that is folded, its total length remains n. When we cut a vertical segment between x=li and ri, the total length of paper that lies within that segment is not simply the sum of lengths of intersection because the strip may go in and out of that segment multiple times (if folded). But the strip is continuous and lies in 2D? Actually it's a rectangle 1 x n: it's 2D but very thin. It has width (horizontal) and height 1 (vertical). The folding occurs along vertical lines, so the paper remains within the plane? Typically, folding a paper out of plane, but here we treat it as stacking layers; the paper is still in a 2D plane but layered? Actually it's a 2D rectangle; folding along vertical lines will cause parts to lie on top of each other, but they occupy the same region in the plane (overlapping). So the paper is stacked; the total area is still n (since height=1). If we cut along two vertical lines at x=li and x=ri, we are cutting through the stacked layers, resulting in multiple rectangular pieces (each layer gives a piece of dimensions 1 × (segment length) that lies between the cuts). The total width (or total area) of those pieces is the sum over layers of the length of the intersection of that layer's x-interval with [li, ri] multiplied by 1 (height). So it's exactly the sum of lengths of the parts of each layer that lie in that x-range. And because layers may be stacked, the same x-point may be covered by multiple layers; thus the total area is the integral of the layer count over x from li to ri. That's consistent.
So we need to maintain the current configuration of the paper after a series of folds, and answer queries: given li and ri, compute total length of paper (i.e., total area) between these vertical cuts.
Now the folds are only of type: fold left part over right part. So it's always folding the left side onto the right side. This is similar to repeatedly taking a prefix of the current paper and reflecting it over the fold line onto the suffix. This is reminiscent of maintaining the mapping of positions on the current paper to the original positions (and orientations) via a piecewise linear transformation that is an involution.
If we treat the paper as a segment [0, W] (the current width). At any time, we have a representation of the original strip as a set of intervals (segments) each with an orientation (direction) that map to contiguous x-ranges on the current paper. Initially, before any folds, there is one layer: the original strip, covering [0, n] with orientation not reversed (say from left to right). After a fold at position p (0 < p < W), we take the left part [0, p] and fold it over the right part. The left part is placed above the right part, meaning we add a new layer (or stack it) on top of the existing layers. But the existing layers? Actually the right part remains as a layer (maybe there are already multiple layers). The fold operation: we take the current configuration (a set of layers, each mapping an interval of x to a segment of the original strip, with an orientation) and we apply the fold: we take the left part of each layer? Wait, careful: The fold is applied to the whole paper, not per layer. The paper as a whole is folded. That means we take all layers that are currently present, and we fold the left part over the right part. But because layers are stacked, the left part of the entire stack is folded over the right part. So effectively, for each existing layer, its part that lies in [0, p] (the left part) is moved (reflected) and placed on top of the part that lies in [p, W] (the right part). So after the fold, the layers are merged: the previous right part remains as is (maybe some parts get additional layers on top), and the left part is added as a new layer on top, possibly reversed.
But also note that after the fold, the width becomes W' = max(p, W - p). The left border of the new sheet is at some coordinate. In the physical process, we then renormalize the coordinate system so that the left border is at the leftmost point of the assembly. That effectively shifts the coordinate origin. So we need to handle the coordinate transformation.
Alternatively, we can think of the process as maintaining a mapping from positions on the current paper to original positions (and orientation, and also count of layers) but perhaps we can think of the fold operation as updating the "function" f that maps current x coordinate to a multiset of original indices? But we need to efficiently answer queries of the total length of paper between li and ri. This sounds like we need to maintain the total measure of paper in intervals.
Observation: The paper is 1 unit tall, so total area = total length of original strip = n. After folds, the total area of paper in any vertical slice [li, ri] is simply the total length of original strip that projects to that x-interval, counting multiplicity. This could be computed if we know the distribution of paper along x. Since folds are symmetrical in a sense, maybe the process can be described by maintaining an interval [L, R] that represents the current segment of the original strip that is "unfolded" or the current orientation, but after multiple folds, the mapping becomes more complex.
Another perspective: The folds are always left over right. This is similar to repeatedly applying a transformation on the coordinate of the paper: after each fold, the coordinate system changes. We can think of the current paper as a representation of the original strip after a series of reflections. This is akin to the "fold" operation in problems like "Folding a Ribbon" or "Paper folding" where you maintain two numbers: the left offset and the right offset, and the direction of orientation. For example, after a series of folds, the original strip is represented as a sequence of segments that are either oriented forward or backward, and these segments are placed consecutively along the x-axis? But not exactly, because folding stacks layers on top, so the x-axis may have overlapping segments from different parts of the original strip.
However, note that when we fold, we place the left part on top of the right part, meaning that for x coordinates where both layers exist, the total layer count increases. So it's not just a permutation of the original strip; it's a stacking.
But maybe we can treat the process differently: The total width of paper pieces between cuts li and ri is simply the total area of paper in that region. Because the paper is of unit height, this is equal to the total length of the original strip that lies in that region, but counting multiplicities due to stacking. This can be computed by integrating the layer count.
We need to answer up to 1e5 queries, n up to 1e5. So we need an efficient data structure.
Perhaps the process can be simulated by maintaining the current set of intervals of the original strip that are present at each x-coordinate, but that could be many intervals after many folds.
But note that folds are always from the left side onto the right side. This is reminiscent of a binary representation. After several folds, the paper becomes a stack of layers that correspond to the binary decomposition of the original length? Or it might be similar to building a "folded" state that is essentially a representation of the original strip as a "folding sequence". Actually, consider the process: start with one layer covering [0,n]. After a fold at p, we get two layers: bottom layer covers [0, W-p] (if W-p >= p? Actually depending on which is larger). But the widths change.
Let's try to formalize.
Let the current state be defined by the set of layers. Each layer corresponds to a contiguous segment of the original paper (by position along the original strip), and has an orientation (either original direction or reversed). Also, each layer occupies a certain x-interval on the current sheet. However, due to folding, some x-intervals may be covered by multiple layers.
An important simplification: Because folds always involve the entire stack and are always from the left, the process can be represented as maintaining the current "folded state" as a sequence of layers from bottom to top, each associated with an interval of the current x-coordinate (the same interval for all layers? Actually not: different layers may have different x-ranges because when you fold a part that is longer than the other, the top layer may extend beyond the bottom layer). But perhaps we can think of the current paper as having a "base" layer that is the bottommost, which is the part that has never been folded? Or maybe after many folds, the layers are all aligned such that they all share the same left border? Let's simulate simple cases.
Case 1: Initial: width = n, one layer (call layer 0) covering [0,n] with orientation forward.
Fold at p (p < n). We'll have two cases:
-
If p <= n-p (i.e., left part <= right part): then new width = n-p. The bottom layer is the right part (original segment [p, n]) placed from x=0 to x=n-p (since after folding, the left border becomes the fold line). The top layer is the left part (original segment [0,p]) placed from x=0 to x=p, but possibly reversed? And orientation? Since we fold left over right, the left part is flipped over, so its orientation is reversed relative to the bottom layer's orientation. If we consider the original orientation as increasing from left to right, after folding and flipping, the mapping of x to original index for the top layer is: original index = p - x (if we set x from 0 at left border). Let's verify: For x measured from the new left border (which is the fold line), the top layer covers from x=0 to x=p. At x=0 (left border), the top layer corresponds to the point originally at p (the fold line). At x=p (right end of top layer), it corresponds to original point 0. So indeed the top layer is reversed. So for top layer, original index = p - x.
-
If p > n-p: new width = p. The bottom layer? Actually we fold left over right, but now left part is longer. The bottom layer is still the right part? The right part is [p, n] of length n-p. However, after folding, the new width is p, and the left border is at the fold line? Let's compute: physical assembly after folding left over right: bottom layer is the right part? Actually which part is on bottom? The problem says leftmost part above rightmost part. So left part is on top, right part is on bottom. So bottom layer is the right part, which originally is from p to n. After folding, where does the bottom layer lie? In the case p > n-p, the right part is shorter. In physical folding: you fold the longer left part over the shorter right part. The right part stays in place? But after folding, you have the assembly where the bottom layer is the right part, but its position relative to the new left border? The fold line is at p from the original left. After folding, you rotate the left part over. The right part originally occupies from p to n. If we set the new left border to be the leftmost point of the assembly, that is at p? But the right part's leftmost point is at p. So in the new coordinate system, the bottom layer occupies from x=0 to x=n-p? Actually the new width is p (max). If p > n-p, the new left border is at the original p? But p is the fold line; the bottom layer goes from p to n. The distance from leftmost (p) to rightmost of bottom is n-p. So bottom layer would occupy [0, n-p] in new coordinates? But then what about the region from n-p to p? That region is covered only by the top layer (the folded left part). So in this case, bottom layer covers [0, n-p], top layer covers [0, p]? That would mean top layer covers a larger x-range, including the bottom layer's region and beyond. But careful: The top layer is the left part after folding. The left part originally length p. After folding, its leftmost point (originally at 0) maps to position p + p? Actually with rotation about the fold line, the left part maps to the interval [p, 2p] if 2p <= n? But here 2p > n. So the mapping: For any point on left part at original coordinate a in [0,p], its new x-coordinate (distance from original left?) is p + (p - a) = 2p - a. So after folding, the left part occupies the interval [p, 2p] (but truncated at n if 2p > n?). Actually the rightmost point of the left part after folding is at x = 2p - 0 = 2p, which is beyond n. But the paper is only up to n? Wait, the original right part ends at n. So the combined physical sheet after folding will have leftmost at min(p, something) and rightmost at max(2p, n). But the bottom layer ends at n. So the overall sheet extends from p to max(n, 2p). Since p > n-p, we have p > n/2, so 2p > n. So overall rightmost = 2p. So the physical sheet spans from p to 2p. So the new width = 2p - p = p. Good. So the new left border is at x = p (original coordinate). The bottom layer (right part) originally spanned [p, n]. In the new coordinate system, its leftmost point is at p (distance 0 from left border), and its rightmost is at n (distance n-p from left border). So it occupies [0, n-p] in new coordinates. The top layer (left part) after folding spans from p to 2p, but note that from p to n, it overlaps with bottom; from n to 2p, it is only top layer. In new coordinates, it occupies [0, p]? Let's compute: new coordinate x' = actual coordinate - p. So actual coordinate = p + x'. The top layer occupies actual coordinates from p to 2p, so x' from 0 to p. So indeed the top layer occupies [0, p] in new coordinates. Bottom occupies [0, n-p] (since actual from p to n => x' from 0 to n-p). So bottom is a sub-interval of top.
Thus after folding, we have two layers:
- Bottom layer: original segment [p, n] (right part) with orientation? It was not flipped, so its mapping from new x' to original index: original index = p + x' (since x'=0 -> p, x'=n-p -> n). So orientation forward.
- Top layer: original segment [0, p] (left part) but reversed: original index = p - x'? Check: at x'=0 (fold line), original index = p; at x'=p (far right), original index = 0. So mapping: original = p - x'. So reversed.
Now note that the top layer covers a larger interval [0,p] while bottom covers [0, n-p] (if n-p < p). So for x' in [0, n-p], both layers exist; for x' in (n-p, p], only top layer exists.
Now subsequent folds are applied to this whole stack. The fold operation again takes the left part of the current sheet (from 0 to pi) and folds it over the right part. This will affect all layers. So the state after multiple folds can be represented as a set of layers, each with its own mapping from current x coordinate to original index, and each layer has its own x-range (maybe from L_i to R_i). However, due to the nature of folding (always left over right), the layers might always share the same left border? After the first fold, both layers have left border at 0 (new coordinates). After subsequent folds, they all start at 0? Let's examine: After a fold, the new left border is at the fold line, which is at some distance pi from the previous left border. But we renormalize to set left border to 0. So in the new coordinate system, all layers will have x' >= 0, and many may start at 0. However, some layers may not extend all the way to 0? Possibly if after a fold, the bottom layer might not cover the entire left part? But because we always fold the left part onto the right part, the part that remains as bottom is the right part of the previous state, which might not include the far left region if the previous state had a layer that didn't start at 0. However, after each fold, we shift coordinates so that the new left border is at the fold line. The bottom layer corresponds to the previous state's right part (the segment from pi to previous width). In the previous coordinate system, that right part may have started at pi (previous left border) and extended to previous width. After shifting, that becomes the new bottom layer covering from 0 to (previous width - pi). So indeed the bottom layer covers from 0 to something. The top layer is the folded left part, which also covers from 0 to something (max(pi, previous width - pi)? Actually top layer covers from 0 to pi (after folding, its leftmost is at the fold line, which becomes 0). So both top and bottom layers start at 0. So after any fold, all layers will have left endpoint at 0? Let's test with a second fold.
Suppose after first fold we have two layers: bottom covering [0, B] (where B = previous width - pi) and top covering [0, T] (where T = pi). Both start at 0. Now we apply a second fold at position p2 (relative to current width W = max(pi, previous width - pi) = max(T, B)). There are two cases: p2 <= W-p2 or not.
Case: W = max(T,B). Suppose T <= B (so W = B). Then current width = B. Bottom covers [0,B], top covers [0,T] with T <= B. Now fold at p2. The left part [0, p2] is folded over right part [p2, B]. This will affect both layers. The left part of each layer (the part that lies in [0, p2]) will be reflected and placed on top of the right part. So we will get new layers: the bottom will be the previous right parts (i.e., for each layer, the segment from p2 to its right end). The top will be the reflected left parts. After folding, we need to recompute the new x-range for each layer. Let's try to define the state more systematically.
We can think of the paper as a binary tree representing the folding process. Alternatively, we can observe that folding from the left repeatedly is akin to building a representation of the current mapping from current x to a set of original indices, which can be described by a "folded" representation of the original strip as a sequence of segments with alternating orientations, but overlapping.
Consider a simpler viewpoint: The total area between cuts li and ri is equal to the measure of the set of points on the original strip that map to the interval [li, ri] in the current coordinate system, counted with multiplicity equal to the number of times that original point is mapped (i.e., the number of layers it appears on). This is essentially the pushforward measure of the original length under the folding mapping.
If we can maintain the current transformation of the original coordinate as a function f that maps each original position to a set of current x coordinates? Or maintain the current mapping from current x to the original positions in each layer.
But note that the original paper is a continuous interval [0, n]. After folds, each layer corresponds to a sub-interval of [0, n] placed in some orientation onto a sub-interval of [0, W] (current width). So the set of layers can be represented as a list of intervals on the original strip, each with an orientation and a mapping function g_i: x -> original position, defined for x in [L_i, R_i] (current x). For a given x, the total "paper density" is the number of layers i such that x is within [L_i, R_i]. The total area between li and ri is sum over i of length of intersection of [li, ri] with [L_i, R_i]. That is essentially the sum over i of (min(R_i,ri)-max(L_i,li)) if positive.
So if we can maintain these intervals efficiently, we can answer queries by summing over intervals overlapping [li,ri]. However, the number of intervals could grow quickly. Each fold potentially doubles the number of layers? Because when we fold, we take each existing layer and split its interval into left and right parts, then reflect the left part and add it as a new layer on top. So the number of layers could double with each fold. With up to 1e5 folds, that's impossible.
Thus there must be a more clever way. Maybe the process is equivalent to maintaining a single interval with an orientation and offset, like a "folded strip" that is actually just a single layer but with some sections overlapped? Wait, if we always fold the left part over the right part, then after many folds, the paper is stacked, but perhaps the order of layers is consistent and the mapping from original position to current x is a piecewise linear function that is actually a permutation of the original interval onto the current width but with multiple points mapping to the same x? Actually it's not a function because multiple original points can map to the same x (overlap). But maybe the overlapping only occurs in a very structured way: at any x, the set of original positions present are consecutive? Or maybe we can treat the paper as a "folded" state where the original strip is folded like an accordion, and the layers are arranged in order from top to bottom, but their x-intervals are nested? For example, after the first fold, we have two layers: top covers [0, p] (reversed), bottom covers [0, W-p] (forward). They are nested: the bottom is a subset of the top if p > W-p? Actually if p > W-p, bottom is shorter, so top covers a superset. If p <= W-p, then bottom covers a larger interval? Let's compare: W = max(p, n-p). If p <= n-p, then W = n-p, bottom covers [0, n-p], top covers [0, p]. So top is subset of bottom. So in either case, one layer's interval is a subset of the other's (they both start at 0). So the layers are nested intervals all starting at 0. After subsequent folds, because each fold reflects the left part of each layer, the intervals will still all start at 0? Let's test with a second fold where we have two layers: bottom (B) and top (T) both starting at 0, with T subset of B (or vice versa). Suppose B > T. After folding at p2, we create new layers: for each existing layer, we take its right part (from p2 to its right end) to become a new bottom layer (after shift), and its reflected left part (from 0 to p2) becomes a new top layer. But careful: The fold operation applies to the whole stack: we take the left part [0, p2] of the entire stack and reflect it onto the right part. So effectively, we replace the current set of layers with two sets: one from the right parts (keeping orientation) and one from the left parts (reflected orientation). However, because the layers have different lengths, their right parts may be of different extents. After the fold, we shift coordinates so that the new left border is at the fold line. The new layers derived from the right parts will have x-range from 0 to (original length - p2) for each layer. But note that the original length for each layer is its previous right endpoint (since they start at 0). So for a layer that previously had interval [0, L], its right part after the fold becomes a layer covering [0, L - p2] (if L > p2), else if L <= p2, then that layer has no right part? Actually if L <= p2, then the entire layer is in the left part and will be folded; its right part is empty. So after fold, layers that are shorter than p2 disappear from the bottom set (they become only in the top set after reflection). Similarly, the left parts become reflected layers covering [0, min(p2, L)] (since the left part of a layer of length L is the interval [0, min(p2, L)]). After reflection, they become layers covering [0, min(p2, L)] (since after folding, the left part maps to a layer of same length as the left part, starting at 0). So after the fold, all new layers (both from left parts and right parts) have intervals starting at 0. So indeed after any number of folds, all layers are of the form covering [0, a_i] for some a_i. That is, each layer's x-interval is [0, L_i] for some length L_i. So all layers are "aligned to the left". This is because after each fold, we shift the coordinate system so that the left border is at the fold line, and the folded left part becomes a new layer(s) that start at the new left border (0). The right part also becomes layers starting at 0 because we shift. So all layers start at 0.
Thus the current state is characterized by a multiset of lengths L_i (each > 0) representing the lengths of the layers. Each layer also has an orientation (forward or reversed) and a mapping from x in [0, L_i] to original index: either f_i(x) = offset_i + s_i * x, where s_i = +1 for forward, -1 for reversed, and offset_i is such that f_i(0) = some original position, f_i(L_i) = another. Actually because all layers start at x=0, the mapping is linear: for each layer i, there is a start original position a_i and a direction d_i = +1 or -1 such that original coordinate = a_i + d_i * x, and the layer covers x from 0 to L_i, and the original segment covered is from a_i to a_i + d_i * L_i (with appropriate direction). Since the layer is a contiguous segment of the original paper, either in increasing or decreasing order.
Additionally, layers may be overlapping: because they all start at x=0, the layer with larger L_i covers a larger interval. So the set of layers is nested by length: some are longer, some shorter. However, they may not be strictly nested if some have same length? They can be nested because they all start at 0; so the coverage is just intervals [0, L_i]. So a layer with length L covers all x < L. So the total number of layers covering a given x is equal to the number of layers with L_i > x. So the density at x is count of layers with length > x.
Therefore, the total paper area (total width) between li and ri is: ∫_{x=li}^{ri} (number of layers with length > x) dx.
This is a crucial simplification! Because all layers are left-aligned, the overlapping structure is simple: it's just a set of lengths. The total area in an interval [li, ri] is the integral of the count of layers longer than x.
Now we need to also consider the orientation? Does orientation matter for the area? No, area just depends on presence of paper, not its orientation. So we only need to know, for each x, how many layers cover that x. That's determined solely by the lengths of layers. The lengths L_i are the lengths of each layer after each fold.
Now we also need to update the set of lengths when a fold occurs. How does a fold at position p affect the lengths? At the moment before fold, we have a set of layers, each with length L_i (all start at 0). Current width W = max_i L_i (since the overall width is the maximum length among layers, because the paper extends to the maximum L_i). Actually W is the maximum length: the overall sheet width is the maximum of the lengths of all layers, because the physical sheet's rightmost point is the maximum endpoint among layers. So W = max L_i.
Now we perform a fold at position p (with p < W). The operation: take the left part [0, p] of the whole stack and fold it over the right part [p, W]. After folding, the new state will again consist of layers all starting at 0, but we need to compute the new lengths for layers derived from the previous ones.
Let's analyze the transformation of a single layer of length L (starting at 0) when we fold the whole stack at p. For a given layer, its coverage is [0, L]. After folding, two contributions:
-
The right part of this layer (the part from x = p to x = L) becomes part of the new bottom layers (since it remains in place, but after shifting coordinates). After folding, the new coordinate system's origin is at x = p (the fold line). So the right part, originally covering x in [p, L], will be mapped to new x' = x - p, covering [0, L - p] (if L > p). This becomes a layer of length L - p, starting at 0, with orientation unchanged? The orientation: the mapping from new x' to original index: originally for this layer we had original = a + d * x. With x = p + x', we get original = a + d*(p + x') = (a + dp) + d * x'. So the new layer (from the right part) has orientation same d, and starts at a' = a + dp, length L' = L - p (if L > p). If L <= p, then the right part is empty, so no contribution from this layer to the bottom set.
-
The left part of this layer (x in [0, p]) is folded over: it gets reflected and placed on top of the right part. After folding, the left part is reversed. In the new coordinate system, the left part will be placed such that its leftmost point (originally at x=0) ends up at new x' = ? Actually after folding left over right, the left part is reflected about the fold line. The mapping: original x in [0, p] maps to new x' = p - x (if we think of the fold as flipping left part to the right, aligning its left edge to the fold line? Let's derive carefully.
Physical process: We have a layer covering [0, L] in current coordinates. We fold at p. The left part [0, p] is rotated 180 degrees over to the right side, and placed on top of the right part. The new coordinate system after folding has its origin at the fold line (the new left border). For a point originally at x in [0, p], after folding, its new x-coordinate (distance from new left border) is p - x. Because at x = p