E - 最短の棒が二本ある区間 / Intervals with Two Shortest Sticks 解説 by admin 印刷ページ
claude5.5opus-highOverview
Let \(m\) be the minimum value of an interval. This problem asks us to count the number of intervals such that \(m\) appears exactly \(2\) times and the lengths of all sticks in the interval are divisible by \(m\).
Analysis
Rephrasing the Conditions
The condition “all elements in the interval are divisible by \(m\), and \(m\) exists in the interval” is equivalent to “the \(\gcd\) of the interval is equal to \(m\).”
- If all elements are multiples of \(m\), each element is at least \(m\). Since \(m\) itself is in the interval, the minimum value automatically becomes \(m\).
- Therefore, the conditions can be summarized into the following \(2\):
- For some value \(v\), \(v\) appears exactly \(2\) times in the interval.
- All elements in the interval are divisible by \(v\).
Fixing the Pair of “Two Shortest Sticks”
In an interval that satisfies the conditions, the value of \(m\) is uniquely determined. Furthermore, the two positions \(i < j\) where \(m\) appears are also uniquely determined. Thus, if we count the valid intervals for each “pair of positions \((i, j)\) with the same value \(v\),” we can count without duplication.
From the condition that \(v\) appears exactly twice, \(i\) and \(j\) must be adjacent among all occurrences of the value \(v\). Let the positions of occurrence of the value \(v\) in increasing order be \(p_0 < p_1 < \dots < p_{k-1}\), and consider an adjacent pair \((p_t, p_{t+1})\).
- The interval must contain \([p_t, p_{t+1}]\).
- The interval must not contain \(p_{t-1}\) or \(p_{t+2}\) (containing them would mean \(v\) appears \(3\) or more times).
- All elements in the interval must be multiples of \(v\).
First, we check whether all elements in \([p_t, p_{t+1}]\) are multiples of \(v\) (i.e., whether \(\gcd = v\)). If this does not hold, no valid interval can be formed from this pair.
If it holds, the left endpoint and right endpoint can be chosen independently:
- Left endpoint \(l\): Can be extended to the left from \(p_t\) as long as the elements continue to be multiples of \(v\). However, it cannot go further left than \(p_{t-1}+1\).
- Right endpoint \(r\): Can be extended to the right from \(p_{t+1}\) similarly. However, it cannot go further right than \(p_{t+2}-1\).
Since there are \(p_t - l_{\min} + 1\) choices for the left endpoint and \(r_{\max} - p_{t+1} + 1\) choices for the right endpoint, the contribution of this pair is their product.
Concrete Example
Consider \(A = [4, 2, 6, 2, 8, 3]\) (0-indexed).
- The occurrences of the value \(2\) are at positions \(1, 3\). Since \(\gcd(2, 6, 2) = 2\), this satisfies the condition.
- Extending to the left: since \(4\) is a multiple of \(2\), \(l_{\min} = 0\).
- Extending to the right: \(8\) is a multiple, but \(3\) is not, so \(r_{\max} = 4\).
- The contribution is \((1 - 0 + 1) \times (4 - 3 + 1) = 4\).
- Since all other values appear only once, the answer is \(4\).
Issues with the Naive Approach
Checking all intervals would take \(O(N^2)\), which is too slow for \(N = 200000\). Also, extending one element at a time to the left and right for each pair would take \(O(N^2)\) in the worst case.
Therefore, we speed up the following two operations:
- Computing the range \(\gcd\)
- Searching for the boundary up to which elements continue to be multiples of \(v\)
Algorithm
- Range GCD via Sparse Table
- Precompute
sp[k][i]as \(\gcd(A_i, \dots, A_{i+2^k-1})\). - Since \(\gcd\) is idempotent (including duplicate elements does not change the result), the \(\gcd\) of an interval \([l, r]\) can be found in \(O(\log A)\) by overlapping two blocks of length \(2^k\).
- Precompute
- Grouping by Value
- Sort the pairs of (value, position) to construct the list of positions \(p\) for each value.
- Processing Each Adjacent Pair
- If \(\gcd(A_{p_t..p_{t+1}}) \ne v\), skip this pair.
- Set the left and right bounds to \(lo = p_{t-1}+1\) (\(0\) if it does not exist) and \(hi = p_{t+2}-1\) (\(N-1\) if it does not exist).
- Extension via Doubling: The property “all elements are multiples of \(v\)” satisfies monotonicity: expanding the interval may break the property, but it will never restore it once broken. Therefore, we try \(b\) in descending order, and if combining the block from
sp[b]with the accumulated \(\gcd\) is still divisible by \(v\), we advance by \(2^b\). This finds the boundary position using \(O(\log N)\) \(\gcd\) operations. - Clip the found boundaries with \(lo\) and \(hi\), and add \((p_t - l + 1)(r - p_{t+1} + 1)\) to the answer.
- Notes on Correctness
- The elements strictly inside \([p_t, p_{t+1}]\) are multiples of \(v\) and are not equal to \(v\) (since \(p_t\) and \(p_{t+1}\) are adjacent occurrences). Thus, all elements strictly inside are greater than \(v\), and the minimum value is \(v\), appearing exactly \(2\) times.
- The extended parts are also multiples of \(v\), and by the restrictions \(lo\) and \(hi\), they do not introduce any new occurrences of \(v\).
Complexity
- Time Complexity: \(O(N \log N \log A)\)
- Constructing the Sparse Table and performing doubling for each pair (at most \(N\) pairs in total) require \(O(N \log N)\) \(\gcd\) operations, each taking \(O(\log A)\) time. Sorting takes \(O(N \log N)\) time.
- Space Complexity: \(O(N \log N)\) (for the Sparse Table)
Implementation Details
The answer can be up to around \(O(N^2)\), so count it using
long long. Be sure not to forget casting when computing the product.When extending to the left,
sp[b][cur-(1<<b)]represents the interval \([cur-2^b, cur-1]\). Verify that the index is at least \(0\).When extending to the right,
sp[b][cur+1]represents the interval \([cur+1, cur+2^b]\). Verify that \(cur + 2^b \le N-1\). Here,currepresents the “last included position.”In doubling, only update when
ng(the combination of the accumulated \(\gcd\)gwith the new block) is divisible by \(v\). If it is not divisible, do not advance, and try the next smaller \(b\).Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
vector<vector<ll>> sp;
int LG;
ll qg(int l,int r){ // inclusive
int k=31-__builtin_clz(r-l+1);
return __gcd(sp[k][l],sp[k][r-(1<<k)+1]);
}
int main(){
scanf("%d",&n);
vector<ll>a(n);
for(auto&x:a)scanf("%lld",&x);
LG=1;while((1<<LG)<=n)LG++;
sp.assign(LG,vector<ll>(n));
sp[0]=a;
for(int k=1;k<LG;k++){
for(int i=0;i+(1<<k)<=n;i++){
sp[k][i]=__gcd(sp[k-1][i],sp[k-1][i+(1<<(k-1))]);
}
}
vector<pair<ll,int>> v(n);
for(int i=0;i<n;i++)v[i]={a[i],i};
sort(v.begin(),v.end());
ll ans=0;
int s=0;
while(s<n){
int e=s;
while(e<n&&v[e].first==v[s].first)e++;
ll val=v[s].first;
vector<int> p;
for(int t=s;t<e;t++)p.push_back(v[t].second);
int k=p.size();
for(int t=0;t+1<k;t++){
int i=p[t],j=p[t+1];
if(qg(i,j)!=val)continue;
int lo=(t>0?p[t-1]+1:0);
int hi=(t+2<k?p[t+2]-1:n-1);
// left extension
int cur=i; ll g=a[i];
for(int b=LG-1;b>=0;b--){
if(cur-(1<<b)>=0){
ll ng=__gcd(g,sp[b][cur-(1<<b)]);
if(ng%val==0){g=ng;cur-=(1<<b);}
}
}
int l=max(cur,lo);
// right extension: cur is last index included
cur=j; g=a[j];
for(int b=LG-1;b>=0;b--){
if(cur+(1<<b)<=n-1){
ll ng=__gcd(g,sp[b][cur+1]);
if(ng%val==0){g=ng;cur+=(1<<b);}
}
}
int r=min(cur,hi);
ans+=(ll)(i-l+1)*(ll)(r-j+1);
}
s=e;
}
printf("%lld\n",ans);
}
This editorial was generated by claude5.5opus-high.
投稿日時:
最終更新: