Problem Statement You are given a sequence of N N positive integers A = ( A 1 , A 2 , ……
- User
Problem Statement You are given a sequence of N N positive integers A
( A 1 , A 2 , … , A N ) A=(A 1 ,A 2 ,…,A N ).
Find the number of pairs of integers ( l , r ) (l,r) satisfying 1 ≤ l ≤ r ≤ N 1≤l≤r≤N such that the subsequence ( A l , A l + 1 , … , A r ) (A l ,A l+1 ,…,A r ) forms an arithmetic progression.
A sequence ( x 1 , x 2 , … , x ∣ x ∣ ) (x 1 ,x 2 ,…,x ∣x∣ ) is an arithmetic progression if and only if there exists a d d such that x i + 1 − x i
d
( 1 ≤ i < ∣ x ∣ ) x i+1 −x i =d (1≤i<∣x∣). In particular, a sequence of length 1 1 is always an arithmetic progression.
Constraints 1 ≤ N ≤ 2 × 1 0 5 1≤N≤2×10 5
1 ≤ A i ≤ 1 0 9 1≤A i ≤10 9
All input values are integers. Input The input is given from Standard Input in the following format:
N N A 1 A 1
A 2 A 2
… … A N A N
Output Print the answer.
Sample 1 Inputcopy Outputcopy 4 3 6 9 3 8 There are eight pairs of integers ( l , r ) (l,r) satisfying the condition: ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) , ( 4 , 4 ) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 4 ) , ( 1 , 3 ) (1,1),(2,2),(3,3),(4,4),(1,2),(2,3),(3,4),(1,3).
Indeed, when ( l , r )
( 1 , 3 ) (l,r)=(1,3), ( A l , … , A r )
( 3 , 6 , 9 ) (A l ,…,A r )=(3,6,9) is an arithmetic progression, so it satisfies the condition. However, when ( l , r )
( 2 , 4 ) (l,r)=(2,4), ( A l , … , A r )
( 6 , 9 , 3 ) (A l ,…,A r )=(6,9,3) is not an arithmetic progression, so it does not satisfy the condition.
Sample 2 Inputcopy Outputcopy 5 1 1 1 1 1 15 All pairs of integers ( l , r )
( 1 ≤ l ≤ r ≤ 5 ) (l,r) (1≤l≤r≤5) satisfy the condition.
Sample 3 Inputcopy Outputcopy 8 87 42 64 86 72 58 44 30 22 Türkçe konuş ve sadece idea ve hint verki nasıl yazmak olur?
- Assistant
Elbette, yardımcı olayım.
Fikir: Dizideki ardışık farkları hesaplayarak, aritmetik ilerleyişin devam ettiği segmentleri bulabilirsin. Her eşit fark serisi için, o segmentin oluşturabileceği alt aralıkların sayısını ekleyebilirsin.
İpucu:
- Dizideki her iki ardışık öğe arasındaki farkları bir dizi olarak kaydet.
- Bu farklar dizisinde ardışık eşit değerlerin uzunluklarını belirle.
- Her eşit fark serisi için, o serinin oluşturduğu aralık sayısını formülle hesapla (örneğin, uzunluk
kolan bir seri içink*(k+1)/2). - Tek elemanlı aralıkları da hesaba katmayı unutma.