E - 山型の気温パターンを数えよ / Count Mountain-Shaped Temperature Patterns Editorial by admin Editorial for printing
Claude 4.6 Opus (Thinking)Overview
Given temperature data over \(N\) days, for each query \(K\), find the number of contiguous subarrays of length \(K\) that form a “mountain pattern” (strictly monotonically increasing followed by strictly monotonically decreasing).
Analysis
Problems with the Naive Approach
Scanning all subarrays of length \(K\) for each query \(K\) takes \(O(N \times Q)\) time, which results in TLE when \(N\) and \(Q\) are large.
Key Insight: Think from Peak Positions
A mountain pattern always has a “peak (summit).” For each position \(p\) as a peak, we consider how many mountain patterns it contributes to windows of each length \(K\).
Preprocessing: Lengths of Increasing and Decreasing Runs
- \(\text{inc}[p]\): length of the strictly monotonically increasing sequence ending at position \(p\)
- \(\text{dec}[p]\): length of the strictly monotonically decreasing sequence starting at position \(p\)
For example, if the temperatures are [1, 3, 5, 4, 2], then \(\text{inc}[2]=3\) (positions 0,1,2 are increasing) and \(\text{dec}[2]=3\) (positions 2,3,4 are decreasing).
Analysis of Peak \(p\)’s Contribution
Let \(a = \text{inc}[p] - 1\), \(b = \text{dec}[p] - 1\) (requiring \(a, b \geq 1\)). The number of starting positions where peak \(p\) forms a mountain pattern in a window of length \(K\) is, with \(x = K - 2\):
\[f(x) = \min(a, x) + \min(b, x) - x \quad (x = 1, 2, \ldots, a+b-1)\]
This is a piecewise linear function. Letting \(\text{lo} = \min(a,b)\) and \(\text{hi} = \max(a,b)\): - \(K \in [3,\ \text{lo}+2]\): \(f = K - 2\) (linear increase) - \(K \in [\text{lo}+3,\ \text{hi}+2]\): \(f = \text{lo}\) (constant) - \(K \in [\text{hi}+3,\ a+b+1]\): \(f = a+b+2-K\) (linear decrease)
Algorithm
- Preprocessing: Compute \(\text{inc}[i]\) and \(\text{dec}[i]\) in \(O(N)\)
- Accumulate using second-order difference arrays: For each peak \(p\), add the above 3-phase piecewise linear function to the second-order difference array in \(O(1)\)
- Use a difference array
diff1[K]for the constant part and a difference arraydiff2[K]for the slope part - To add \(\alpha K + \beta\) over the interval \([L, R]\), perform range addition of \(\alpha\) to
diff2and \(\beta\) todiff1
- Use a difference array
- Reconstruction: Restore the difference arrays using prefix sums, and obtain the answer for each \(K\) as \(\text{ans}[K] = \text{diff2}[K] \times K + \text{diff1}[K]\)
- Query Response: Output \(\text{ans}[K_j]\) for each query \(K_j\)
Complexity
- Time complexity: \(O(N + Q)\) (preprocessing \(O(N)\), processing each peak \(O(1) \times O(N)\), reconstruction \(O(N)\), query response \(O(Q)\))
- Space complexity: \(O(N)\)
Implementation Notes
Second-order difference array technique: When adding a piecewise linear function \(f(K) = \alpha K + \beta\) over an interval \([L, R]\), by managing the slope \(\alpha\) and intercept \(\beta\) in separate difference arrays, range addition becomes possible in \(O(1)\).
The peak condition is \(\text{inc}[p] \geq 2\) and \(\text{dec}[p] \geq 2\) (at least one increase to the left and at least one decrease to the right).
Be careful about mixing 0-indexed and 1-indexed. The code performs internal calculations using 0-indexed.
The minimum value of \(K\) is 3 (at least 3 elements are needed for 1 increasing step + 1 decreasing step).
Source Code
import sys
def main():
input_data = sys.stdin.buffer.read().split()
idx = 0
N = int(input_data[idx]); idx += 1
Q = int(input_data[idx]); idx += 1
S = [int(input_data[idx + i]) for i in range(N)]
idx += N
queries = []
for j in range(Q):
K = int(input_data[idx]); idx += 1
queries.append(K)
# For each position i (0-indexed), compute:
# inc[i] = length of strictly increasing run ending at i
# dec[i] = length of strictly decreasing run starting at i
inc = [1] * N # inc[i]: how many consecutive strictly increasing elements ending at i
for i in range(1, N):
if S[i] > S[i-1]:
inc[i] = inc[i-1] + 1
else:
inc[i] = 1
dec = [1] * N # dec[i]: how many consecutive strictly decreasing elements starting at i
for i in range(N-2, -1, -1):
if S[i] > S[i+1]:
dec[i] = dec[i+1] + 1
else:
dec[i] = 1
# A window [l, l+K-1] (0-indexed) is a mountain pattern if there exists p with l < p < l+K-1
# such that S[l] < ... < S[p] > ... > S[l+K-1].
#
# For a peak at position p (0-indexed), the mountain pattern requires:
# - inc[p] >= 2 (at least one increase step to the left)
# - dec[p] >= 2 (at least one decrease step to the right)
#
# The window [l, l+K-1] with peak at p requires:
# - l <= p - 1 (i.e., p >= l+1, so inc from l to p covers at least 2)
# - l+K-1 >= p+1 (i.e., p <= l+K-2)
# - inc[p] >= p - l + 1 (strictly increasing from l to p)
# - dec[p] >= (l+K-1) - p + 1 (strictly decreasing from p to l+K-1)
#
# So: l >= p - inc[p] + 1 and l <= p - 1
# l+K-1 >= p+1 and l+K-1 <= p + dec[p] - 1
# => l >= p - dec[p] + 1 + 1 - K + K -1 ... let me redo.
#
# From inc: l >= p - (inc[p] - 1) = p - inc[p] + 1
# From dec: l+K-1 <= p + dec[p] - 1, so l <= p + dec[p] - K
# From structure: l <= p-1 and l >= (p+1) - (K-1) = p - K + 2
#
# So l ranges: max(p - inc[p] + 1, p - K + 2) <= l <= min(p - 1, p + dec[p] - K)
#
# For a given peak p, the minimum K for which this peak can contribute is:
# K_min = 3 (at minimum)
# The maximum K: l must satisfy l >= p - inc[p] + 1 and l <= p + dec[p] - K and l <= p-1 and l >= p-K+2
# Maximum K when the range is widest: K_max = inc[p] + dec[p] - 1
#
# For a given K, number of valid l for peak p:
# low = max(p - inc[p] + 1, p - K + 2)
# high = min(p - 1, p + dec[p] - K)
# count = max(0, high - low + 1)
#
# For peak p with inc[p] >= 2 and dec[p] >= 2:
# Let A = inc[p], B = dec[p]. Max K = A + B - 1.
# For K in [3, A+B-1], count(K) = min(K-2, A-1, B-1, A+B-1-K) + ...
# Actually: high - low + 1 = min(p-1, p+B-K) - max(p-A+1, p-K+2) + 1
# = min(A-1, K-2) + min(B-1, K-2) - (K-2) ... let me just use difference array.
# Use difference array over K for answer.
# For each peak p, add contributions over K = 3..A+B-1.
ans = [0] * (N + 2) # ans[K] = number of mountain patterns of length K
for p in range(1, N - 1):
A = inc[p]
B = dec[p]
if A < 2 or B < 2:
continue
# K ranges from 3 to A+B-1
# count(K) = min(A-1, K-2) + min(B-1, K-2) - (K-2)
# This is a piecewise linear function. Let's break into segments.
# Phases depend on ordering of A and B. Let a=A-1, b=B-1 (both >=1).
a = A - 1
b = B - 1
# K from 3 to a+b+1
# count(K) = min(a, K-2) + min(b, K-2) - (K-2)
# Let x = K-2, x from 1 to a+b-1
# f(x) = min(a,x) + min(b,x) - x
# x in [1, min(a,b)]: f = x + x - x = x => increases 1..min(a,b)
# x in [min(a,b)+1, max(a,b)]: f = min(a,b) + x - x = min(a,b) or min(a,b) + ...
# Actually if a <= b:
# x in [1,a]: f = x
# x in [a+1,b]: f = a + x - x = a
# x in [b+1,a+b-1]: f = a + b - x
lo = min(a, b)
hi = max(a, b)
# Phase 1: K=3..lo+2, f increases from 1 to lo (K-2 from 1 to lo)
# Phase 2: K=lo+3..hi+2, f = lo (constant)
# Phase 3: K=hi+3..a+b+1, f decreases from lo-1 to 1
# Use difference array on ans[K]
# Phase 1: K=3..lo+2, ans[K] += (K-2) => add +1 slope starting at K=3
K1_start = 3
K1_end = lo + 2
# f(K) = K-2 for K in [K1_start, K1_end]
# diff of f: f(3)=1, f(4)=2, ... increment +1 each step
# Use second-order difference array for linear segments
# Instead, let me use a simpler approach with difference of difference.
# Let d[K] = ans[K] - ans[K-1]. Then to add a linear ramp, adjust d.
# Actually let me just use two-level difference array.
# I'll accumulate into ans using range updates.
# For a linear function c(K) = alpha + beta*(K - K_start) over [K_start, K_end]:
# Use diff1 for constant part and diff2 for slope part.
pass
# Let me redo with proper difference arrays
diff1 = [0] * (N + 3) # for constant additions
diff2 = [0] * (N + 3) # for slope additions (coefficient of K)
for p in range(1, N - 1):
A = inc[p]
B = dec[p]
if A < 2 or B < 2:
continue
a = A - 1
b = B - 1
lo = min(a, b)
hi = max(a, b)
# Phase 1: K in [3, lo+2], f(K) = K-2
if lo >= 1:
L, R = 3, lo + 2
# f(K) = K - 2 = 1*K + (-2)
diff2[L] += 1; diff2[R+1] -= 1
diff1[L] += -2; diff1[R+1] -= -2
# Phase 2: K in [lo+3, hi+2], f(K) = lo (only if lo+3 <= hi+2, i.e., hi >= lo+1, i.e., a != b)
if lo + 3 <= hi + 2:
L, R = lo + 3, hi + 2
diff1[L] += lo; diff1[R+1] -= lo
# Phase 3: K in [hi+3, a+b+1], f(K) = a+b - (K-2) = a+b+2 - K
if hi + 3 <= a + b + 1:
L, R = hi + 3, a + b + 1
# f(K) = (a+b+2) - K = -1*K + (a+b+2)
diff2[L] += -1; diff2[R+1] -= -1
diff1[L] += (a+b+2); diff1[R+1] -= (a+b+2)
# Reconstruct ans[K] = sum of (slope_coeff * K + const_coeff)
# First prefix sum diff1 and diff2
for i in range(1, N + 2):
diff1[i] += diff1[i-1]
diff2[i] += diff2[i-1]
result = [0] * (N + 2)
for K in range(3, N + 1):
result[K] = diff2[K] * K + diff1[K]
out = []
for K in queries:
out.append(str(result[K]))
sys.stdout.write('\n'.join(out) + '\n')
main()
This editorial was generated by claude4.6opus-thinking.
posted:
last update: