E - 最短の棒が二本ある区間 / Intervals with Two Shortest Sticks 解説 by admin 印刷ページ
Gemini 3.8 Flash (High)Overview
This problem asks us to find the number of contiguous subarrays such that the minimum value \(m\) of the subarray appears exactly \(2\) times, and all elements in the subarray are multiples of \(m\).
By fixing the positions of the two elements that attain the minimum value, and combining a monotonic stack, a Sparse Table (for range GCD queries), and binary search, we can solve this problem efficiently in \(O(N (\log N + \log(\max A)))\).
Analysis
1. Rephrasing the Conditions
Let \(m\) be the minimum value of the subarray \([L, R]\). - Condition 1: There are exactly \(2\) positions in the subarray such that \(A_k = m\). - Condition 2: Every element in the subarray is a multiple of \(m\) (\(A_k \pmod m = 0\)). - This can be rephrased as: “The greatest common divisor (GCD) of the entire subarray is a multiple of \(m\).”
2. Focusing on the Two “Bars” Attaining the Minimum
A valid subarray must contain exactly two bars attaining the minimum value \(m\). Let the indices of these two bars from left to right be \(i\) and \(j\) (\(i < j\)).
In this case, no element less than or equal to \(m\) can exist between \(i\) and \(j\) (\(i < k < j\)). If such an element existed: - If there is a value strictly smaller than \(m\), the minimum of the subarray would no longer be \(m\). - If there is a value equal to \(m\), the subarray would contain \(3\) or more occurrences of \(m\).
Therefore, \(j\) must be the first element to the right of \(i\) that is less than or equal to \(A_i\), and it must satisfy \(A_j = A_i\). Since there is at most one such \(j\) for each \(i\), there are at most \(N\) pairs \((i, j)\) in total that we need to consider.
3. Determining the Range of the Endpoints \(L\) and \(R\)
Fixing the pair \((i, j)\), let us summarize the conditions for a subarray \([L, R]\) (\(L \le i < j \le R\)), with these two elements as its minimum, to be valid:
- Elements between \(i\) and \(j\):
- All elements \(k\) such that \(i + 1 \le k \le j - 1\) must be multiples of \(m\). If this is not satisfied, no valid subarray containing this pair \((i, j)\) can exist.
- Conditions on the left endpoint \(L\):
- To avoid including a third occurrence of \(m\) (or any value smaller than \(m\)) in the subarray, \(L\) must be strictly to the right of the nearest element to the left of \(i\) that is less than or equal to \(m\). Let this boundary index be \(L_1\); then \(L_1 < L \le i\).
- Furthermore, all elements in the subarray \([L, i - 1]\) must be multiples of \(m\).
- Conditions on the right endpoint \(R\):
- Similarly, \(R\) must be strictly to the left of the nearest element to the right of \(j\) that is less than or equal to \(m\). Let this boundary index be \(R_1\); then \(j \le R < R_1\).
- Furthermore, all elements in the subarray \([j + 1, R]\) must be multiples of \(m\).
4. Independence and Monotonicity
The conditions that \(L\) and \(R\) must satisfy do not interfere with each other and are completely independent. Therefore, if the number of valid choices for \(L\) is \(C_L\) and the number of valid choices for \(R\) is \(C_R\), the number of valid subarrays formed by this pair \((i, j)\) is \(C_L \times C_R\).
Moreover, the condition that “all elements in the subarray are multiples of \(m\)” exhibits monotonicity: - As we extend \(L\) to the left starting from \(i\), more elements are included in the subarray, so the GCD can only decrease. Once it ceases to be a multiple of \(m\), extending it further to the left will never make it a multiple of \(m\) again. - Therefore, the boundary of \(L\) (the leftmost position it can be extended to) can be found using binary search. The same applies to \(R\).
Algorithm
- Precomputation:
- Sparse Table: Build a Sparse Table to query the GCD of any arbitrary range in \(O(1)\).
- Monotonic Stack: For each element \(i\), precompute the following positions in \(O(N)\):
- The nearest index \(L_1\) to the left of \(i\) such that \(A_k \le A_i\) (
prev_le[i]) - The nearest index \(j\) to the right of \(i\) such that \(A_k \le A_i\) (
next_le[i])
- The nearest index \(L_1\) to the left of \(i\) such that \(A_k \le A_i\) (
- Searching and Counting Pairs \((i, j)\):
- For each \(i = 1, \dots, N\), let \(j = \text{next\_le}[i]\).
- If \(j > N\) or \(A_j \ne A_i\), skip this \(i\).
- Set \(m = A_i\), and check whether the GCD of the range \([i + 1, j - 1]\) is a multiple of \(m\) (if not, skip).
- Use binary search to find the minimum \(L \in [L_1 + 1, i]\) such that all elements in the range \([L, i - 1]\) are multiples of \(m\). The number of valid values for \(L\) is \((i - L_{\min} + 1)\).
- Similarly, use binary search to find the maximum \(R \in [j, R_1 - 1]\) such that all elements in the range \([j + 1, R]\) are multiples of \(m\). The number of valid values for \(R\) is \((R_{\max} - j + 1)\).
- Add \((i - L_{\min} + 1) \times (R_{\max} - j + 1)\) to the answer.
Complexity
- Time Complexity:
- Sparse Table construction: \(O(N \log N \log(\max A))\) (computing each GCD takes \(O(\log(\max A))\))
- Precomputation with monotonic stack: \(O(N)\)
- Binary search for each \(i\): Binary search takes \(O(\log N)\) steps, and each step queries the GCD in \(O(1)\), resulting in \(O(N \log N)\) overall.
- In total, it is \(O(N (\log N + \log(\max A)))\), which runs comfortably within the time limit.
- Space Complexity:
- Sparse Table size: \(O(N \log N)\)
- Various arrays and stacks: \(O(N)\)
- In total, it is \(O(N \log N)\).
Implementation Notes
Handling Integer Overflow:
- The number of valid subarrays can be up to approximately \(N(N + 1) / 2 \approx 2 \times 10^{10}\), so make sure to use a 64-bit integer type (
long longin C++) for the variableansstoring the answer.
- The number of valid subarrays can be up to approximately \(N(N + 1) / 2 \approx 2 \times 10^{10}\), so make sure to use a 64-bit integer type (
Fast Range GCD Queries:
- Since \(\gcd\) satisfies the associative, commutative, and idempotent (\(\gcd(x, x) = x\)) properties, we can use a Sparse Table with overlapping ranges to process range queries in \(O(1)\).
Handling Empty Ranges:
In the binary search, when \(mid = i\), the range \([mid, i - 1]\) becomes an empty range. Since “no extra elements are included,” this should always be considered valid (i.e., multiples of \(m\)). The same logic applies when \(mid = j\).
Source Code
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <stack>
using namespace std;
const int MAXN = 200005;
const int K = 19;
long long st[K][MAXN];
int lg[MAXN];
void build_st(int n, const vector<long long>& A) {
lg[1] = 0;
for (int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;
for (int i = 1; i <= n; i++) st[0][i] = A[i];
for (int j = 1; j < K; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
st[j][i] = std::gcd(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
}
}
}
long long query_gcd(int L, int R) {
int j = lg[R - L + 1];
return std::gcd(st[j][L], st[j][R - (1 << j) + 1]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
if (!(cin >> n)) return 0;
vector<long long> A(n + 1);
for (int i = 1; i <= n; i++) {
cin >> A[i];
}
build_st(n, A);
vector<int> prev_le(n + 1, 0);
stack<int> s;
for (int i = 1; i <= n; i++) {
while (!s.empty() && A[s.top()] > A[i]) {
s.pop();
}
if (!s.empty()) {
prev_le[i] = s.top();
} else {
prev_le[i] = 0;
}
s.push(i);
}
while (!s.empty()) s.pop();
vector<int> next_le(n + 1, n + 1);
for (int i = n; i >= 1; i--) {
while (!s.empty() && A[s.top()] > A[i]) {
s.pop();
}
if (!s.empty()) {
next_le[i] = s.top();
} else {
next_le[i] = n + 1;
}
s.push(i);
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
int j = next_le[i];
if (j > n || A[j] != A[i]) continue;
long long m = A[i];
if (i + 1 <= j - 1) {
if (query_gcd(i + 1, j - 1) % m != 0) {
continue;
}
}
int L1 = prev_le[i];
int low = L1 + 1, high = i;
int ans_l = i;
while (low <= high) {
int mid = low + (high - low) / 2;
if (mid == i || query_gcd(mid, i - 1) % m == 0) {
ans_l = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
int R1 = next_le[j];
low = j;
high = R1 - 1;
int ans_r = j;
while (low <= high) {
int mid = low + (high - low) / 2;
if (mid == j || query_gcd(j + 1, mid) % m == 0) {
ans_r = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
ans += (long long)(i - ans_l + 1) * (ans_r - j + 1);
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
投稿日時:
最終更新: