Problem:


Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5

Sample Output 3

22



Sample input:
6
2 3 3 1 3 1


Sample output:
3

Submission status: Accepted

Solution code:
class Map(dict):
    def __missing__(self, key):
        return 0


def main():
    int(input())

    d = Map()
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        d[i + height] += 1
        ans += d[i - height]

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030

Submission status: Accepted

Solution code:
class Map(dict):
    default_value = 0

    def __missing__(self, key):
        self[key] = self.default_value
        return self.default_value


def main():
    int(input())

    d = Map()
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        d[i + height] += 1
        ans += d[i - height]

    return ans


if __name__ == '__main__':
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030

Submission status: Accepted

Solution code:
class Map(dict):
    def __missing__(self, key):
        default_value = 0
        self[key] = default_value
        return default_value


def main():
    int(input())

    d = Map()
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        d[i + height] += 1
        ans += d[i - height]

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
import collections
n = int(raw_input())
hs = map(int, raw_input().split(' '))
def f(nums):
	cc,count = collections.Counter(), 0

	#j - i = aj + ai  =>  ai + i = j - aj
	for i in range(len(nums)-1, -1, -1):
		count += cc[nums[i] + i]
		cc[i - nums[i]]+=1
	return count
		


print f(hs)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    import numpy as np
    from collections import Counter
    N = int(input())
    A = np.array(list(map(int, input().split())))

    count = 0

    tmp = np.arange(N)
    X = A-tmp
    Y = A+tmp

    # print(X)
    # print(Y)
    XX = Counter(list(X))
    YY = Counter(list(Y))

    count = 0
    # print(XX)
    # print(YY)
    for number, c in XX.items():
        count += YY[-number] * c
        # count +=
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())

J=[0]*N
j=0
while j<N:
    J[j]=j-A[j]
    j+=1

import collections
C = collections.Counter(J)

ans=0
i=0
while i<N:
    ans+=C[i+A[i]]
    i+=1

print(ans)

Submission status: Accepted

Solution code:
import collections
goukei = 0

N = int(input())
Ai = list(map(int, input().split()))
number = [j for j in range(1, N+1, 1)]
plus_Ai = [x + y for (x, y) in zip(Ai, number)]
minus_Ai = [x - y for (y, x) in zip(Ai, number)]
c = collections.Counter(plus_Ai)

for i in minus_Ai:
    goukei += c[i]
print(goukei)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
seq = [(int(x),i) for i, x in enumerate(input().split())]
seq1 =(i-x for x,i in seq)
seq2 =(i+x for x,i in seq)

d1 = dict()
d2 = dict()
s1 = set()
s2 = set()
for val in seq1:
    try:
        d1[val] += 1
    except:
        d1[val] = 1
for val in seq2:
    try:
        d2[val] += 1
    except:
        d2[val] = 1

ans = 0
for key in d1:
    try:
        ans += d1[key] * d2[key]
    except:
        pass

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
d = {}
x = 0

for i, a in enumerate(A):
    if i+a not in d:
        d[i+a] = 1
    else:
        d[i+a] += 1
    if i-a in d:
        x += d[i-a] 

print(x)

Submission status: Accepted

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    """
    6
    2 3 3 1 3 1

    j-i = A[i]+A[j]
    A[i]+i = j-A[j]
    L[i] = A[i]+i
    R[i] = i-A[i]
    """
    N = read_int()
    A = read_ints()
    L = sorted(a+i for i, a in enumerate(A))
    R = sorted(i-a for i, a in enumerate(A))
    i, j = 0, 0
    count = 0
    total = 0
    prev_a = None
    for a in L:
        if a != prev_a:
            count = 0
            while j < len(R) and a >= R[j]:
                if a == R[j]:
                    count += 1
                else:
                    count = 0
                j += 1
        total += count
        prev_a = a
    return total


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter as cc
N = int(input())
A = list(map(int,input().split()))

ans = 0
A_p = cc(A+np.arange(N))
A_n = cc(np.arange(N) - A)
for k in range(1,N+1):
    ans += A_p[k]*A_n[k]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

minus = [0] * N
plus = [0] * N

for i in range(N):
    minus[i] = (i + 1) - A[i]
    plus[i] = (i + 1) + A[i]

count = Counter(minus)
most = count.most_common()
num = len(most)

countPP = Counter(plus)
#mostPP = countPP.most_common()
#check = mostPP[:][0]
ans = 0

for i in range(num):
    elm = most[i]
    fir = elm[0]
    sec = elm[1]
    # if (fir in check):

    aa = countPP[fir]
    #    print(aa)
    ans += aa * sec

    # try countPP[tar]:

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
X = {}
for i,a in enumerate(A):
  v = i + a
  if not v in X:
    X[v] = [i]
  else:
    X[v].append(i)

#print(X)
out = 0
for i,a in enumerate(A):
  v = i - a
  #print("look for:", v, "index", i)
  if not v in X:
    continue
  cand = X[v]
  #print(cand)
  lb = 0
  ub = len(cand)
  while lb < ub:
    #print(lb, ub)
    m = (lb + ub)//2
    if cand[m] >= i:
      lb = m + 1
    else:
      ub = m
  #print(lb, ub)
  out += len(cand) - lb
print(out)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
d = dict()
ans = 0

d[n-1-a[-1]] = 1

for i in range(n-2,-1,-1):
    if a[i] + i in d:
        ans += d[a[i]+i]
    
    if i - a[i] in d:
        d[i-a[i]] += 1
    else:
        d[i-a[i]] = 1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=[0 for i in range(N)]
for i in range(N):
    x=a[i]
    a[i]=i-x
    b[i]=i+x
from collections import Counter
a=dict(Counter(a))
b=dict(Counter(b))
ans=0
for key in a.keys():
    if key in b.keys():
        ans+=a[key]*b[key]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int, input().split()))
d={}
c=0

for i,a in enumerate(A):
  if i+a not in d:
    d[i+a]=1
  else:
    d[i+a]+=1
  if i-a in d:
    c+=d[i-a]

print(c)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    L=list(map(int,input().split()))
    L_i,L_j=[],[]
    L_memo = [0]*N
    cnt=0
    for i, j in enumerate(L):
        if i+j<N:
            L_memo[i+j]+=1
        if i-j>0:
            cnt+=L_memo[i-j]        
    print(cnt)
main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = {}
ans = 0
for i in range(n):
    if i-a[i] in d:
        ans += d[i-a[i]]
    if i+a[i] in d:
        d[i+a[i]] += 1
    else:
        d[i+a[i]] = 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0

l = {}
r = {}
for i in range(1,n+1):
    if i-a[i-1] in l:
        l[i-a[i-1]]+=1
    else:
        l[i-a[i-1]]=1

    
    
    if i+a[i-1] in r:
        r[i+a[i-1]]+=1
    else:
        r[i+a[i-1]]=1
    

for i in l:
    if i in r:
        ans += l[i]*r[i]
    


print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
aa = list(map(int, input().split()))
ar = [a+i for i, a in enumerate(aa)]
arc = Counter(ar)
al = [i-a for i, a in enumerate(aa)]
alc = Counter(al)
ans = 0
for k, c in arc.items():
    ans += c * alc[k]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b = [0] * 10**6
ans=0
for i in range(n):
    if a[i]<(2*(10**5)):
        if i-a[i]>=0:
            ans+=b[i-a[i]]
        b[a[i]+i]+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split())) 

"""
A[i] + A[j] = j - i
A[i] + i = j - A[j]

a = A[i] + i
b = j - A[j]
"""

ans = 0
dic = {}

for i in range(N-1, -1, -1):
    wa = A[i] + i
    if wa in dic:
        ans += dic[wa]
    sa = i - A[i]
    if sa in dic:
        dic[sa] += 1
    else:
        dic[sa] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_index = []
for i in range(n):
    a_index.append((i, a[i]))
dic1 = {}
dic2 = {}
for i in range(n):
    one = a_index[i][0] + a_index[i][1]
    two = a_index[i][0] - a_index[i][1]
    if one not in dic1:
        dic1[one] = 1
    else:
        dic1[one] += 1
    if two not in dic2:
        dic2[two] = 1
    else:
        dic2[two] += 1
ans = 0
for i in dic1.keys():
    if i in dic2:
        ans += dic2[i] * dic1[i]
for i in dic2.keys():
    if i in dic1:
        ans += dic1[i] * dic2[i]
        #print(i)
print(ans // 2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

L = {}
R = {}

for i in range(n):
    if i + a[i] in L:
        L[i + a[i]] = L[i + a[i]] + 1
    else:
        L[i + a[i]] = 1

    if i - a[i] in R:
        R[i - a[i]] = R[i - a[i]] + 1
    else:
        R[i - a[i]] = 1

#print(L,R,sep="\n")
ans = 0
for key in L.keys():
    if key in R:
        #print(L[key], R[key])
        ans += L[key] * R[key]
print(ans)

"""
ans = 0
for i in range(n):
    for j in range(i+1, n):
        if abs(i-j) == a[i]+a[j]:
            ans += 1
print(ans)
"""

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
# a+i と i-aのとりうる値の個数をカウントするが
# 一致するとしても[0, n)の範囲だけなのでDの大きさはnで十分
D = [0]*n
for i, a in enumerate(A):
    if 1+i+a < n:
        D[i+a] += 1
ans = 0
# i-aに対応するD[i-a]を加えるだけで答えが求まる
for i, a in enumerate(A):
    if 1+i-a >= 0:
        ans += D[i-a]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
"""
ai + i = j - aj
"""
cnt = [0] * (n + 1)
for i in range(n):
    if 0 <= a[i] + i < n+1:
        cnt[a[i] + i] += 1

for i in range(n):
    if i - a[i] >= 0:
        ans += cnt[i - a[i]]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
h = [int(x) for x in input().split(" ")]
 
L = [0] * N
R = [0] * N
cnt = 0
for i in range(0, N):
    L[i] = i + 1 + h[i]
    R[i] = i + 1- h[i]
LC = Counter(L)
RC = Counter(R)

cnt = 0
for key,values in LC.most_common():
    if key in RC:
        num = RC[key]
        cnt += values * num

print(cnt)

Submission status: Accepted

Solution code:
#e
n=int(input())
a=list(map(int,input().split()))

sa = [i-a[i] for i in range(n)]
wa = [i+a[i] for i in range(n)]

from collections import Counter

c_sa = Counter(sa)
c_wa = Counter(wa)
cand = set(c_sa.keys()) & set(c_wa.keys())
print( sum(c_sa[c]*c_wa[c] for c in cand) )

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))

li1 = [i+1-A[i] for i in range(N)]
li2 = [A[i]+i+1 for i in range(N)]

cnt = [0 for i in range(2*N+1)]
for i in li2:
    if i<len(cnt)-1:
        cnt[i] += 1

ans = 0
for i in li1:
    if i>=0:
        ans += cnt[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
 
N = int(input())
A = list(map(int, input().split()))
 
C_A1 = Counter([i + A[i] for i in range(N)])
C_A2 = Counter([i - A[i] for i in range(N)])

count = sum([C_A1[i]*C_A2[i] for i in range(N)])

print(count)

Submission status: Accepted

Solution code:
import collections
 
N = int(input())
A = list(map(int, input().split()))

A1 = []
A2 = []

for i in range(N):
    A1.append(i + A[i])
    A2.append(i - A[i])

C_A1 = collections.Counter(A1)
C_A2 = collections.Counter(A2)
ans = 0
for i in range(N):
    ans += C_A1[i]*C_A2[i]
    
print (ans)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
res = 0
counter = {}
for i in reversed(range(n)):
    x = A[i]
    res += counter.get(x + i, 0)
    counter[i - x] = counter.get(i - x, 0) + 1
print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
d = {}
for i in range(N):
    d[i] = 0
for i in range(N):
    j =  i - A[i] + 1
    if j >= 0:
        d[j] += 1
ans = 0
for i in range(N):
    b = A[i] + i + 1
    c = i - A[i] + 1
    if c >= 0:
        d[c] -= 1
    if 0 <= b <= N - 1:
        ans += d[b]
print(ans)

Submission status: Accepted

Solution code:

from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
lInfo = defaultdict(int)
rInfo = defaultdict(int)
cnt = 0

for i in range(n):
    lInfo[a[i]+i] += 1
    rInfo[i-a[i]] += 1

for k in lInfo.keys():
    cnt += lInfo[k] * rInfo[k]
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
# a+i と i-aのとりうる値の個数をカウントするが
# 一致するとしても[0, n)の範囲だけなのでDの大きさはnで十分
D = [0]*n
for i, a in enumerate(A):
    if i+a < n:
        D[i+a] += 1
ans = 0
# i-aに対応するD[i-a]を加えるだけで答えが求まる
for i, a in enumerate(A):
    if i-a >= 0:
        ans += D[i-a]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
D1 = {}
D2 = {}
def count(D, s):
    if s in D:
        D[s] += 1
    else:
        D[s] = 1
for i, a in enumerate(A):
        count(D1, a+i)
        count(D2, i-a)
ans = 0
for d, num in D1.items():
    if d in D2:
        ans += num * D2[d]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
ci = defaultdict(int)
cj = defaultdict(int)
for i in range(n):
    ci[a[i]+i] += 1
    cj[i-a[i]] += 1
cnt = 0
for k in ci.keys():
    cnt += ci[k] * cj[k]
print(cnt)

Submission status: Accepted

Solution code:
#n,m = map(int,input().split())
#h = list(map(int,input().split()))

#ab = [map(int,input().split()) for _ in range(m)]
#a,b = [list(i) for i in zip(*ab)]

n = int(input())
a = list(map(int,input().split()))

cnt=0
"""
for i in range(n-1):
    for j in range(i+1,n-1):
        if j-i==a[j]+a[i]:
            #print(i,j)
            cnt+=1

"""

R=dict()
L=dict()

for i in range(n):
    if a[i]+i+1 in R:
        R[a[i]+i+1]+=1
    else:
        R[a[i]+i+1]=1

    if i+1-a[i] in L:
        L[i+1-a[i] ]+=1
    else:
        L[i+1-a[i] ]=1


for i in R:
    if i in L:
        cnt+=R[i]*L[i]

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

L = {}
R = {}
for i, a in enumerate(A):
    x = i + 1 + a
    L[x] = L[x] + 1 if x in L else 1

    x = i + 1 - a
    R[x] = R[x] + 1 if x in R else 1

# print(L, R)

ans = 0
for x in L.keys():
    ans += L[x] * (R[x] if x in R else 0)

print(ans)

Submission status: Accepted

Solution code:
import collections

n = int(input())
A = list(map(int, input().split()))

L, R = [], []

for i in range(n):
    L.append(i+1+A[i])
    R.append(i+1-A[i])

cl = collections.Counter(L)
cr = collections.Counter(R)

ans = 0

for c in cl:
    ans += cl[c] * cr[c]

print(ans)

Submission status: Accepted

Solution code:
import bisect
inf = 10**15
mod = 10**9+7
n = int(input())
a = list(map(int, input().split()))
dicL = [a[i]+i for i in range(n)]
dicR = [i-a[i] for i in range(n)]
dicL.sort()
dicR.sort()
ans = 0
for x in range(n-1):
    indL = bisect.bisect_left(dicL, x)
    indR = bisect.bisect_left(dicR, x)
    countL = 0
    countR = 0
    while indL != n and dicL[indL] == x:
        countL+=1
        indL+=1
    while indR != n and dicR[indR] == x:
        countR+=1
        indR+=1
    ans+=countL*countR
print(ans)

Submission status: Accepted

Solution code:
n = int(input().strip())
a = list(map(int, input().split()))
dic = {}
ans = 0
for i in range(len(a)):
    if dic.get(i - a[i]):
        ans += dic[i - a[i]]
    if dic.get(a[i] + i):
        dic[a[i] + i] += 1
    else:
        dic[a[i] + i] = 1
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))

l1 = []
l2 = []

for i in range(N):
  l1.append(i+1+A[i])
  l2.append(i+1-A[i])

c1 = collections.Counter(l1)
c2 = collections.Counter(l2)

sum = 0
for i in c1.keys():
  sum += c1[i]*c2[i]
 
print(sum)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = [int(i) for i in input().split()]
l_i = [a[i] + i + 1 for i in range(len(a))]
r_j = [i - a[i] + 1 for i in range(len(a))]
dl = defaultdict(int)
dr = defaultdict(int)
for i in range(n):
    dl[l_i[i]] += 1
    dr[r_j[i]] += 1

ans = 0
for i in dl.keys():
    ans += dl[i] * dr[i]

print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))

L = [i + A[i] for i in range(N)]
R = [i - A[i] for i in range(N)]

countL = collections.Counter(L)
countR = collections.Counter(R)

print(sum([countL[n] * countR[n] for n in countL.keys()]))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int,input().split(" ")))

List1 = [i + A[i] for i in range(N)]
List2 = [i - A[i] for i in range(N)]

ans = 0

CList1 = Counter(List1)
CList2 = Counter(List2)

for i in range(N):
    ans += CList1[i]*CList2[i]
    
print (ans)

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int,input().split()))

ListK = [i+1+List[i] for i in range(N)]
ListL = [i+1-List[i] for i in range(N)]

from collections import Counter 
ans = 0

NewlistK = Counter(ListK)
NewlistL = Counter(ListL)

for i in range(N):
  ans += (NewlistK[i])*(NewlistL[i])

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
d = defaultdict(int)
ans = 0
for i in range(n):
    ans += d[i-a[i]]
    d[i+a[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))

prev = collections.defaultdict(list)

ans = 0
for i, h in enumerate(A):
    #i - i_prev = h + h_prev
    #i = h + h_prev + i_prev

    comp = i - h

    if comp in prev:
        ans += len(prev[comp])
    prev[i + h].append(0)

print(ans)

Submission status: Accepted

Solution code:
def count_j(arg_ls, th, b_idx, e_idx):
	if b_idx-1 == e_idx: return 0
	if b_idx == e_idx: return int(arg_ls[b_idx] > th)
	cent_idx = (e_idx+1 - b_idx) // 2 + b_idx
	cent_j = arg_ls[cent_idx]
	if cent_j > th:
		return count_j(arg_ls, th, b_idx, cent_idx-1) + (e_idx+1 - cent_idx)
	else:
		return count_j(arg_ls, th, cent_idx, e_idx)

n = int(input())
inp_ls = input().split(" ")
a_ls = [int(e) for e in inp_ls[:n]]

i_dic = {}
for i,a in enumerate(a_ls):
	val = -a+(i+1)
	if val not in i_dic: i_dic[val] = []
	i_dic[val].append(i+1)

cnt = 0
for i in range(len(a_ls)):
	val = a_ls[i] + (i+1)
	cand_ls = i_dic.get(val, [])
	result = count_j(cand_ls, (i+1), 0, len(cand_ls)-1)
	cnt += result

print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = map(int, input().split())
d = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += d[-i+a]
    d[-i-a] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
A = list(map(int, input().split()))
cnt = defaultdict(int)
ans = 0
for i in range(n):
    k = i-A[i]
    ans += cnt[k]
    cnt[i+A[i]]+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from itertools import product
import sys
import math
import bisect

sys.setrecursionlimit(200000)
input = sys.stdin.readline


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    # x: i-A[i]の列
    X = [i-A[i] for i in range(N)]
    # y: i+A[i]の列
    Y = [i+A[i] for i in range(N)]
    z = defaultdict(int)
    for y in Y:
        z[y] += 1
    return sum([z[x] for x in X])


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    print("{}".format(outputs))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))

P = []
Q = []

for i in range(N):
    P.append(i + 1 + A[i])
    Q.append(i + 1 - A[i])

P = Counter(P)
Q = Counter(Q)

res = 0

for v, c in P.items():
    if v in Q:
        res += c * Q[v]

print(res)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
p = Counter()
q = Counter()
for i, x in enumerate(input().split()):
    p[i-int(x)] += 1
    q[i+int(x)] += 1
print(sum(p[x]*q[x] for x in p.keys()))

Submission status: Accepted

Solution code:
import math
import copy

n = int(input())
#n, k = [int(i) for i in input().split()]
#table = [[int(i) for i in input().split()] for j in range(n)]
#table = [list(input()) for n in range(h)]
num = [int(i) for i in input().split()]
#num = [int(i) for i in range(n)]
# s = [input() for i in range(n)]
#s = input()
# list = list(s)
# dp = [[0 for _ in range(2)] for _ in range(13)]
#s = [input() for i in range(n)]

#a = [[0 for _ in range(n+1)] for _ in range(n+1)]
#b = [[0 for _ in range(n+1)] for _ in range(n+1)]

kotae = 0
temp = 0
a = {}
b = {}

for i in range(n):
    temp = i - num[i]
    if (temp in a):
        a[temp] += 1
    else:
        a[temp] = 1

for i in range(n):
    temp = i + num[i]
    if (temp in b):
        b[temp] += 1
    else:
        b[temp] = 1

for i in range(200000):
    if ((i in a) and (i in b)):
        kotae += a[i] * b[i]

print(kotae)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
B = {}
C = {}

for i in range(N):
    if i-A[i] in B:
        B[i-A[i]] += 1
    else:
        B[i-A[i]] = 1
    if i+A[i] in C:
        C[i+A[i]] += 1
    else:
        C[i+A[i]] = 1
#print(B)
#print(C)
ans = 0
for i in B:
    #print(i) i=Key
    if i in C:
        ans += B[i]*C[i]
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
B = collections.defaultdict(int)
C = collections.defaultdict(int)

for i in range(N):
    B[i-A[i]] += 1 
    C[i+A[i]] += 1
#print(B)
#print(C)
ans = 0
for i in B:
    #print(i) i=Key
    ans += B[i]*C[i]
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = LI()
    c = collections.defaultdict(int)
    r = 0
    for i,t in enumerate(a):
        r += c[t-i]
        c[-t - i] += 1

    return r


print(main())

Submission status: Accepted

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
B = collections.defaultdict(int)
C = collections.defaultdict(int)

for i in range(N):
    B[i-A[i]] += 1 
    C[i+A[i]] += 1
#print(B)
#print(C)
ans = 0
for i in B:
    #print(i) i=Key
    if i in C:
        ans += B[i]*C[i]
print(ans)

Submission status: Accepted

Solution code:
import math
from collections import defaultdict

def main():
    N = int(input())
    Al = [int(x) for x in input().split()]
    center = int(N/2)
    cnt = 0
    m = defaultdict(int)
    for i in range(N):
        m[i+Al[i]] += 1
        cnt += m[i-Al[i]]
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))

ans = 0
B = [0]*N
C = [0]*N

for i in range(N):
  b = i+A[i]
  c = i-A[i]
  if 0 < b < N:
    B[b] += 1
  if 0 < c < N:
    C[c] += 1

for i in range(N):
  ans += B[i]*C[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
L = [0]*N
R = [0]*N
for i in range(N):
    l = i - A[i]
    r = i + A[i]
    if 0<l<N:
        L[l] += 1
    if 0<r<N:
        R[r] += 1
# print(L)
# print(R)
c = 0
for i in range(N):
    c += L[i]*R[i]
print(c)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = [int(height) for height in input().split()]

m = Counter(i - elem for i, elem in enumerate(A))
res = 0
for i, elem in enumerate(A):
    res += m[i + elem]

print(res)

Submission status: Accepted

Solution code:
# n = int(input())
# height = [int(x) for x in input().split()]
# cnt =0

# for i_index, i in enumerate(height[:-1]):
#     if i+i_index >= n:
#         continue

#     for j_index, j in enumerate(height[i_index+i+1:]):
#         if j+i == j_index+i+1:
#             cnt += 1
#             # print(i,j,j_index)
            
# print(cnt)
def count(dic,x):
    if x in dic:
        dic[x]+=1
    else:
        dic[x]=1
 
N=int(input())
A=list(map(int,input().split()))
dic1={}
dic2={}
s=0
for i in range(N):
    count(dic1,i-A[i])
    count(dic2,i+A[i])
for x in dic1:
    if x in dic2:
        s+=dic1[x]*dic2[x]
print(s)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from collections import Counter

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    n = int(input())
    a = list(map(int, input().split()))
    m = Counter([i - elem for i, elem in enumerate(a)])
    ans = 0
    for i, elem in enumerate(a):
        ans += m[i + elem]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=[int(a) for a in input().split()]
X=[A[i-1]+i for i in range(1,N+1)]
Y=[-A[j-1]+j for j in range(1,N+1)]
cnt={}
for x in X:
    cnt[x]=[0,0]
for y in Y:
    cnt[y]=[0,0]
for x in X:
    cnt[x][0]+=1
for y in Y:
    cnt[y][1]+=1
ans=0
for key in cnt:
    p,q=cnt[key]
    ans+=p*q
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())
A = list(map(int,input().split()))
L = [0] * (N+1)
R = [0] * (N+1)
for i in range(N):
    xl = i+1+A[i]
    xr = i+1-A[i]
    if 1 <= xl <= N-1:
        L[xl] += 1
    if 1 <= xr <= N-1:
        R[xr] += 1

ans = 0
for i in range(1,N+1):
    ans += L[i] * R[i]

print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list((map(int,input().split())))

index = list(range(1,N+1))

R = [index[i] - A[i] for i in range(N)]
L = [index[j] + A[j] for j in range(N)]

R_dict = collections.Counter(R)
L_dict = collections.Counter(L)

ans=0
for i in R_dict.keys()&L_dict.keys():
    ans+=R_dict[i]*L_dict[i]

print(ans)

Submission status: Accepted

Solution code:
def count(dic,x):
    if x in dic:
        dic[x]+=1
    else:
        dic[x]=1

N=int(input())
A=list(map(int,input().split()))
dic1={}
dic2={}
s=0
for i in range(N):
    count(dic1,i-A[i])
    count(dic2,i+A[i])
for x in dic1:
    if x in dic2:
        s+=dic1[x]*dic2[x]
print(s)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
x = {}
ans = 0
for i in range(n):
    R = i + a[i]
    if not R in x:
        x[R]=1
    else: x[R]+=1
    
for i in range(n):
    L = i - a[i]
    if L in x:
        ans+=x[L]
print(ans)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    aa=list(map(int,input().split()))
    ll={}
    c=0
    for i,a in enumerate(aa,1):
        x=i-a
        if x in ll:
            c+=len(ll[x])
        y=i+a
        if y in ll:
            ll[y].append(i)
        else:
            ll[y]=[i]
    print(c)
main()

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
li = list(map(int,input().split()))
 
r = [i-a for i, a in enumerate(li)]
l = [a+i for i, a in enumerate(li)]
#print(r)
#print(l)

c1 = dict(Counter(r))
c2 = dict(Counter(l))
#print(c1)
#print(c2)

cnt = 0
for k in c2.keys():
    if k in c1:
        cnt += c2[k] * c1[k]

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int,input().split()))

B1 = [i-a for i, a in enumerate(A)]
B2 = [a+i for i, a in enumerate(A)]

c1 = dict(Counter(B1))
c2 = dict(Counter(B2))

SUM = 0
for k in c2.keys():
    if k in c1:
        SUM += c2[k] * c1[k]

print(SUM)

Submission status: Accepted

Solution code:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

from collections import defaultdict
n=n0()
a=[0]
a.extend(n1())

m1=[i+a[i] for i in range(n+1)]
m2=[j-a[j] for j in range(n+1)]

d1=defaultdict(list)
d2=defaultdict(list)
for i in range(1,n+1):
    d1[m1[i]].append(i)
    d2[m2[i]].append(i)

ans=0
for i in d1:
    ans+=len(d1[i])*len(d2[i])
            
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

N = int(input())
A = np.array(list(map(int, input().split())))
num = np.arange(1,N+1)
P = num - A
Q = num + A
P.sort()
Q.sort()

ind_left = np.searchsorted(P, Q, side='left')
ind_right = np.searchsorted(P, Q, side='right')
cnt = np.sum(ind_right - ind_left)
print(cnt)