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

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    A = list(map(int, input().split()))
    m = {}
    for i in range(N):
        x = i - A[i]
        if m.get(x) == None:
            m[x] = [1, 0]
        else:
            m[x][0] += 1
    for i in range(N):
        y = i + A[i]
        if m.get(y) != None:
            m[y][1] += m[y][0]
    ans = 0
    for key in m:
        ans += m[key][1]
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
import sys
input = sys.stdin.readline
n = int(input())
c = Counter()
ans = 0
for i, x in enumerate(int(x) for x in input().split()):
    ans += c[i - x]
    c[i + x] += 1
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
L = [0] * N
R = list()
for i in range(N):
  R.append(i-A[i])
  if(i+A[i]<=N-1):
    L[i+A[i]] += 1
for r in R:
  if(r > 0):
    ans += L[r]
print(ans)

Solution (Python):
n=int(input())
a=map(int,input().split())  
b,c=[],[]
for i,q in enumerate(a):
  b.append(i+q)
  c.append(i-q)
from collections import*
b=Counter(b).items()
c=Counter(c)
z=0
for d,e in b:z+=e*c[d]
print(z)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
C = sorted([-A[i] - i for i in range(n)])
from bisect import *
print(sum(bisect(C, b) - bisect_left(C, b) for b in [A[i] - i for i in range(n)]))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
l = [i+a[i] for i in range(n)]
r = [j-a[j] for j in range(n)]
import collections
cl = collections.Counter(l)
cr = collections.Counter(r)
ans = 0
for i in cl.keys():
    if i in cr.keys():
        ans += cl[i]*cr[i]
print(ans)

Solution (Python):
from sys import stdin

n = int(stdin.readline().rstrip())
an = list(map(int, input().split()))

l = {}
r = {}

for i in range(n):
    x = i + an[i]
    if x not in l:
        l[x] = [i]
    else:
        l[x].append(i)

    x = i - an[i]
    if x not in r:
        r[x] = [i]
    else:
        r[x].append(i)

sum = 0
for k in l:
    if k in r:
        sum += len(l[k])*len(r[k])

print(sum)

Solution (Python):
N=int(input())
count=0
a=list(map(int,input().split()))
w=[]
s=[]
num=[0]*N
maxi=max(a)
for i in range(N):
  s.append(i-a[i])
  if i+a[i]<N:
    num[i+a[i]]+=1
for i in range(N):
  if s[i]>=0:
    count+=num[s[i]]
print(count)

Solution (Python):
from collections import defaultdict
 
n = int(input())
a = list(map(int, input().split()))
ap = defaultdict(int)
for i in range(n):
    ap[i - a[i]] += 1

ans= 0
for i in range(n):
    ans += ap[a[i] + i]
print(ans)

Solution (Python):
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)

Solution (Python):
N=int(input())
count=0
a=list(map(int,input().split()))
w=[]
s=[]
num=[0]*N
maxi=max(a)
for i in range(N):
  s.append(1+i-a[i])
  if 1+i+a[i]<N:
    num[1+i+a[i]]+=1
for i in range(N):
  if s[i]>=0:
    count+=num[s[i]]
print(count)

Solution (Python):
n=int(input())
alist=list(map(int,input().split()))

xdic={}
ydic={}

for i,a in enumerate(alist):
    
    
    tmpx=(i+1)+a
    tmpy=(i+1)-a
    if tmpx in xdic:
        xdic[tmpx]=xdic[tmpx]+1
    else:
        xdic[tmpx]=1
    if tmpy > 0:
        if tmpy in ydic:
            ydic[tmpy]=ydic[tmpy]+1
        else:
            ydic[tmpy]=1

output=0
for y in ydic:
    if y in xdic:
        output+= xdic[y]*ydic[y]

print(output)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

from collections import defaultdict
ar = defaultdict(list)
al = defaultdict(list)

for i in range(n):
    al[i+a[i]].append(i)
    ar[i-a[i]].append(i)

x = al.keys()

ans = 0
for i in x:
    ans += len(ar[i]) * len(al[i])

print(ans)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
x = dict()
ans = 0

for i in range(n):
    tmp = i + A[i]
    if tmp not in x:
        x[tmp] = 1
    else:
        x[tmp] += 1

for j in range(n):
    l = j - A[j]
    if l in x:
        ans += x[l]

print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
L = [i+x for i, x in enumerate(A)]
R = [i-x for i, x in enumerate(A) if i-x > 0]

from collections import Counter
P = Counter(L)
Q = Counter(R)
 
ans = 0
for k, v in P.items():
  if k in Q:
    ans += v*Q[k]
print(ans)

Solution (Python):
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
cnt_i = defaultdict(lambda :0)
cnt_j = defaultdict(lambda :0)
for i in range(n):
    temp_i = a[i] + i + 1
    temp_j = i + 1 - a[i]
    cnt_i[temp_i] += 1
    cnt_j[temp_j] += 1

ans = sum(v * cnt_j[i] for i,v in cnt_i.items())
print(ans)

Solution (Python):
from collections import Counter
n = int(input())
d_1 = Counter()
d_2 = Counter()
A = map(int, input().split())
for i, a in enumerate(A):
    i += 1
    d_1[i - a] += 1
    d_2[i + a] += 1

c = 0
for k, v in d_1.items():
    if d_2[k] > 0:
        c += v * d_2[k]
print(c)

Solution (Python):

import bisect

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


b = [a[i] - (1 + i) for i in range(0, n)]
b, idx = zip(*sorted(zip(b, range(1, n + 1))))

ans = 0
for i in range(n):
    
    r = bisect.bisect_right(b, -b[i] - 2 * idx[i])
    l = bisect.bisect_left(b, -b[i] - 2 * idx[i])
    ans += r - l
    
    
print(ans)

Solution (Python):

from collections import defaultdict


def solve(*args: str) -> str:
    n = int(args[0])
    A = list(map(int, args[1].split()))

    ret = 0
    X = defaultdict(int)
    for i, a in enumerate(A):
        X[a+i] += 1

        if 0 <= i-a:
            ret += X[i-a]

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
from collections import Counter
N = int(input())

A = list(map(int, input().split()))
ans = 0

plus = []
minus = []

for i, j in enumerate(A):
    
    plus.append(i+j)
    minus.append(i-j)




plus_count = Counter(plus)
minus_count = Counter(minus)




for i,j in plus_count.items():
    
    if i in minus_count:
        ans += j * minus_count[i]
        
print(ans)

Solution (Python):
from collections import Counter

n = int(input())

a = list(map(int,input().split()))
ans = 0
cc = Counter()

for i,val in enumerate(a):
    if i - val > 0:
        ans+=cc[i-val]
    cc[i+val]+=1

print(ans)

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))

agents = {}
n_pair = 0

for i, a in enumerate(a_list):
  x = i - a
  if x not in agents:
    agents[x] = 0
  agents[x] += 1

for i, a in enumerate(a_list):
  x = i + a
  if x in agents:
    n_pair += agents[x]
print(n_pair)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
d={}
e={}
c=0

for i in range(n):
    if a[i]+i not in d:
        d[a[i]+i]=1
    else:
        d[a[i]+i]+=1
    if i-a[i] not in e:
        e[i-a[i]]=1
    else:
        e[i-a[i]]+=1
for i in d.keys():
    if i not in e:
        continue
    else:
        c+=d[i]*e[i]  

print(c)

Solution (Python):

import collections
n = int(input())
a = [int(i) for i in input().split()] 

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

"""
A = [[a[i], i+1] for i in range(n)]

A.sort()

dp = [0 for _ in range(n)]
dp[0] = 0
if abs(A[0][1] - A[1][1]) == A[0][0] + A[1][0]:
    dp[1] = 1
else:
    dp[1] = 0
for i in range(2, n):
    dp[i] = 
"""


ans = 0
A = [[i+1, a[i]] for i in range(n)]

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


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


for i in set(L):
    ans += cl[i] * cr[i]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A1 = [[-a-i,i] for i, a in enumerate(A)]
A2 = [[a-i,i] for i, a in enumerate(A)]
A1.sort()
A2.sort()
i = j = 0
cnt = 0
while i < N and j < N:
    if A1[i][0] == A2[j][0]:
        buf1 = 1
        for k in range(i+1, N):
            if A1[k][0] != A1[i][0]:
                break
            buf1 += 1
        buf2 = 1
        for k in range(j+1, N):
            if A2[k][0] != A2[j][0]:
                break
            buf2 += 1
        cnt += buf1 * buf2
        i += buf1
        j += buf2
    elif A1[i][0] < A2[j][0]:
        i += 1
    else:
        j += 1
print(cnt)

Solution (Python):
from collections import Counter

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

iA = [i+n for i, n in enumerate(A)]
jA = [i-n for i, n in enumerate(A)]
dict1 = Counter(iA)
dict2 = Counter(jA)

ans = 0
for key, value in dict1.items():
    ans += value * dict2[key]
print(ans)

Solution (Python):
from collections import Counter

n = int(input())
h = list(map(int, input().split()))
a = []
b = []
for i in range(n):
    a.append(i + h[i])
    b.append(i - h[i])

x = Counter(a)
y = Counter(b)

z = set(a)
ans = 0
for i in z:
    ans += x[i] * y[i]

print(ans)

Solution (Python):
def e_this_message_will_self_destruct_in_5s():
    N = int(input())
    A = [int(i) for i in input().split()]

    add = [j + a for j, a in enumerate(A, 1)]
    diff = sorted([k - a for k, a in enumerate(A, 1)])

    from bisect import bisect_left as bleft
    return sum([bleft(diff, p + 1) - bleft(diff, p) for p in add])

print(e_this_message_will_self_destruct_in_5s())

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

Solution (Python):
from collections import defaultdict
n, *A = map(int, open(0).read().split())
ans, d = 0, defaultdict(int)
for i, a in enumerate(A, 1):
    x, y = i+a, i-a
    d[x] += 1
    ans += d[y]
print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
ans=0
L={}
R={}
for i in range(N):
    if a[i]+i in L:
         k=L.get(a[i]+i)
         del L[a[i]+i]
         L[a[i]+i]=k+1
    else:
        L[a[i]+i]=1
    if i-a[i]>0:
        if i-a[i] in R:
             k=R.get(i-a[i])
             del R[i-a[i]]
             R[i-a[i]]=k+1
        else:
            R[i-a[i]]=1
for x in L.keys():
    if x in R:
        ans+=L.get(x)*R.get(x)
print(ans)

Solution (Python):
import collections
N=int(input())
n_list=input().split()
count=0
L_list=[]
R_list=[]
for i,item in enumerate(n_list) :
  L_list.append(str(i+int(item)))
  R_list.append(str(i-int(item)))
L_dic=collections.Counter(L_list)
R_dic=collections.Counter(R_list)
count=0
for i in range(N) :
  L_count=L_dic[str(i)]
  R_count=R_dic[str(i)]
  count+=L_count*R_count
print(count)

Solution (Python):

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

    
    

    dic = {}
    ans = 0
    for i in range(N):
        L = i + A[i]
        R = i - A[i]
        if L not in dic:
            dic[L] = 1
        else:
            dic[L] += 1
        if R in dic:
            ans += dic[R]
    print(ans)

if __name__ == "__main__":
    resolve()

Solution (Python):
def e_this_message_will_self_destruct_in_5s():
    from collections import Counter
    N = int(input())
    A = [int(i) for i in input().split()]

    add = [j + a for j, a in enumerate(A, 1)]
    diff = [k - a for k, a in enumerate(A, 1)]
    count = Counter(diff)
    return sum([count[p] for p in add])

print(e_this_message_will_self_destruct_in_5s())

Solution (Python):
n=int(input())
heights=list(map(int,input().split(" ")))
dic={}
res=0
for i in range(n):
    if heights[i]-i not in dic:
        dic[heights[i]-i]=1
    else:
        dic[heights[i]-i]+=1
for j in range(n):
    if -heights[j]-j in dic:
        res+=dic[-heights[j]-j]
print(res)

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

l = [0]*n
r = [0]*n

l = [i+a[i] for i in range(n)]
r = [i-a[i] for i in range(n)]
ld = {}
rd = {}

c = 0

for i in range(n):
	if l[i] in ld:
		ld[l[i]] += 1
	else:
		ld[l[i]] = 1
	if r[i] in rd:
		rd[r[i]] += 1
	else:
		rd[r[i]] = 1

for i in ld.keys():
	if i in rd:
		c += ld[i]*rd[i]	
	
print(c)

Solution (Python):
from sys import stdin
from sys import setrecursionlimit

setrecursionlimit(10 ** 7)

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))

dic = {}
point = 0

for i,j in enumerate(a):
    if i+j in dic:
        dic[i+j] += 1
    else:
        dic[i+j] = 1

for i,j in enumerate(a):
    if i-j in dic:
        point += dic[i-j]

print(point)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def main():
    N = I()
    H = LI()
    ans = 0
    cnt_plus_height_plus_idx = defaultdict(int)
    cnt_minus_height_plus_idx = defaultdict(int)

    for idx in range(N):
        cnt_plus_height_plus_idx[H[idx] + idx] += 1
        cnt_minus_height_plus_idx[- H[idx] + idx] += 1

    ans = 0

    for k, v in cnt_plus_height_plus_idx.items():
        if k in cnt_minus_height_plus_idx:
            ans += v * cnt_minus_height_plus_idx[k]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    base = [0] * N
    for i, a in enumerate(A):
        if i - a >= 0: base[i-a] += 1
    count = 0
    for i, a in enumerate(A):
        if i + a < N: count += base[i + a]
    print(count)

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
from sys import stdin
from sys import setrecursionlimit

setrecursionlimit(10 ** 7)

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))

dic = {}
point = 0

for i,j in enumerate(a):
    if i+j in dic:
        dic[i+j] += 1
    else:
        dic[i+j] = 1

for i,j in enumerate(a):
    if i-j in dic:
        point += dic[i-j]

print(point)

Solution (Python):
n, *A = map(int, open(0).read().split())
ans, d = 0, {}
for i, a in enumerate(A, 1):
    x, y = i+a, i-a
    d[x] = d.get(x, 0)+1
    ans += d.get(y, 0)
print(ans)

Solution (Python):
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])

L.sort()
R.sort()




temp = 0
ans = 0
j = 0
for i in range(N):
    if i >= 1 and L[i] == L[i-1]:
        ans += temp
        continue
    temp = 0
    while j < N and L[i] >= R[j]:
        if L[i] == R[j]:
            temp += 1
        j += 1
    ans += temp

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
res = 0
dict = {}
dict[a[0]+1] = 1
for i in range(1,n):
  res += dict.get(i+1-a[i],0)
  dict[a[i]+i+1] = dict.get(a[i]+i+1,0) + 1
print(res)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def debug(table, *args):
    ret = []
    for name, val in table.items():
        if val in args:
            ret.append('{}: {}'.format(name, val))
    print(' | '.join(ret), file=sys.stderr)


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def main():
    N = I()
    H = LI()
    ans = 0
    cnt_plus_height_minus_idx = defaultdict(int)
    cnt_minus_height_minus_idx = defaultdict(int)
    cnt_plus_height_plus_idx = defaultdict(int)
    cnt_minus_height_plus_idx = defaultdict(int)

    for idx in range(N):
        cnt_plus_height_minus_idx[H[idx] - idx] += 1
        cnt_minus_height_minus_idx[- H[idx] - idx] += 1
        cnt_plus_height_plus_idx[H[idx] + idx] += 1
        cnt_minus_height_plus_idx[- H[idx] + idx] += 1

    ans = 0

    for k, v in cnt_plus_height_minus_idx.items():
        if k in cnt_minus_height_minus_idx:
            ans += v * cnt_minus_height_minus_idx[k]

    
    for k, v in cnt_plus_height_plus_idx.items():
        if k in cnt_minus_height_plus_idx:
            ans += v * cnt_minus_height_plus_idx[k]

    print(ans // 2)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 0
l = [0]*(3*10**5+10)
for i in range(1,N):
  x = i-A[i]
  if x<0:
    continue
  else:
    l[x]+=1
if A[0]<=2*10**5:
  ans+=l[A[0]]
for i in range(1,N):
  x = i+A[i]
  if x<0:
    continue
  if x>2*10**5:
    continue
  
  if i-A[i]>=0:
    l[i-A[i]]-=1
  ans+=l[x]
  
print(ans)

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

Solution (Python):
from bisect import bisect, bisect_left

N = int(input())
A = list(map(int, input().split()))
I = [0]*N
J = [0]*N
i = 0
while i < N:
  I[i] = A[i]+i
  J[i] = i-A[i]
  i += 1
I.sort()
ans = 0
for j in J:
  ans += bisect(I, j)-bisect_left(I, j)
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

L = [i+x for i, x in enumerate(A)]
R = [i-x for i, x in enumerate(A)]
cnt_R = Counter(R)

ans = 0
for l in L:
	ans += cnt_R[l]
print(ans)

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



dic = {}
for i in range(N):
    if dic.get(A[i]+i) == None:
        dic[A[i]+i] = 1
    else:
        dic[A[i]+i] += 1
        
ans = 0
for j in range(N):
    if dic.get(j-A[j]) != None:
        ans += dic.get(j-A[j])
print(ans)

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

P = [None]*N
M = [None]*N

for i in range(N):
    P[i] = i + A[i]
    M[i] = i - A[i]

P.sort() 
M.sort()

P_ = []
M_ = []

for i in range(N):
    if i == 0 or P[i] != P[i-1]:
        ele = [P[i], 1]
    else:
        ele[1] += 1
    if i == N-1 or P[i] != P[i+1]:
        P_.append(ele)
        
for i in range(N):
    if i == 0 or M[i] != M[i-1]:
        ele = [M[i], 1]
    else:
        ele[1] += 1
    if i == N-1 or M[i] != M[i+1]:
        M_.append(ele)
        

cnt = 0
lp = len(P_)
lm = len(M_)
for i in range(lp):
    idx = P_[i][0]
    head = 0
    tail = lm
    while tail - head > 1:
        center = (tail + head)//2
        if idx > M_[center][0]:
            head = center
        elif idx < M_[center][0]:
            tail = center
        else:
            cnt += P_[i][1] * M_[center][1]
            break
            
print(cnt)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
d = {}
ans = 0

for i in range(n):
    l = i + a[i]
    r = i - a[i]
    if l not in d:
        d[l] = 1
    else:
        d[l] += 1
    if r in d:
        ans += d[r]

print(ans)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

R = {}

for i ,a in enumerate(A):
    val = i + a
    if not val in R:
        R[val] = 1
    else:
        R[val] += 1
ans = 0

for j, a in enumerate(A):
    val = j - a
    if val in R:
        ans += R[val]

print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
X = dict()
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)

Solution (Python):

n = int(input())

heights = list(map(int, input().split()))

values = dict()

for ii, h in enumerate(heights):
    c1 = ii - h
    if (c1) in values:
        values[c1].append((ii, h))
    else:
        values[c1] = [(ii, h)]

count = 0
for j, h in enumerate(heights):
    if (j + h) in values:
        count += len(values[j + h])
        
        
        


print(count)

Solution (Python):
from collections import Counter


N = int(input())
A = list(map(int,input().split()))
c=0
l=[]
r=[]
for i in range(N):
    l.append(A[i]+i+1)
    r.append(i+1-A[i])

C=Counter(r)

for x in l:
    c+=C.get(x,0)
print(c)

Solution (Python):
def main():
    from collections import defaultdict
    import sys
    input=sys.stdin.readline
    n=int(input())
    a=list(map(int,input().split()))
    ap=defaultdict(int)
    am=defaultdict(int)
    for i in range(n):
        ap[a[i]+i]+=1
        am[i-a[i]]+=1
    ans=0
    for k,v in ap.items():
        if k in am:
            ans+=v*am[k]
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
d={}
for i in range(1,N+1):
    if not i+A[i-1] in d:
        d[i+A[i-1]]=1
    else:
        d[i+A[i-1]]+=1

ans=0
for i in range(1,N+1):
    if i-A[i-1] in d:
        ans+=d[i-A[i-1]]

print(ans)

Solution (Python):
import collections
n=int(input())
a=list(map(int, input().split())) 
ans = 0
ru = []

for i in range(n):
    ru.append(int(i + 1 + a[i]))

c = collections.Counter(ru)


for i in range(n):
    tmp = i + 1 - a[i]
    ans += c[tmp]

print(ans)

Solution (Python):

from collections import defaultdict

N = int(input())
A = list(int(x) for x in input().split())

di = defaultdict(int)
for i in range(N):
    di[i+1+A[i]] += 1

dj = defaultdict(int)
for j in range(N):
    dj[j+1-A[j]] += 1

ans = 0
for k, v in di.items():
    ans += v * dj.get(k,0)
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = list(map(int,input().split()))
B = [(i + 1) + A[i] for i in range(N)]
C = [(i + 1) - A[i] for i in range(N)]
d = defaultdict(int)
for b in B:
    d[b] += 1
ans = 0
for c in C:
    ans += d[c]
print(ans)

Solution (Python):

from bisect import bisect_left
from collections import defaultdict


def solve(*args: str) -> str:
    n = int(args[0])
    A = list(map(int, args[1].split()))

    ret = 0
    X = defaultdict(list)
    for i, a in enumerate(A):
        X[a+i].append(i)

    for j, a in enumerate(A):
        if 0 <= j-a:
            ret += bisect_left(X[j-a], j)

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    a = list(map(int, readline().split()))

    x = [i - h for i, h in enumerate(a, 1)]
    y = [i + h for i, h in enumerate(a, 1)]

    from collections import Counter
    c = Counter(y)

    ans = 0
    for i, p in enumerate(x):
        ans += c[p]
        if y[i] == p:
            ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


import collections

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

d = dict(collections.Counter([i + A[i] for i in range(N)]))
ans = sum([d.get(i - A[i], 0) for i in range(N)])
    
print(ans)

Solution (Python):


import bisect
N = int(input())
A = list(map(int, input().split()))
X = [A[i]+i for i in range(N)]
Y = [j - A[j] for j in range(N)]
ans = 0
Y.sort()
for i in range(N):
    ans += bisect.bisect_right(Y,X[i]) - bisect.bisect_left(Y,X[i])
print(ans)


'''
bisect 二分探索モジュール
A = [1,1,3,3,3,4,5,6]
B = 3
bisect.bisect_left(A, b) 配列Aにbが左から何番目に入るかを返す 
bisect.bisect_right(A,b) 配列Aにbが右から何晩目に入るかを返す 
'''

Solution (Python):
n = int(input())
a = list(map(lambda x: int(x), input().split(' ')))

m = {}

count = 0

for i, v in enumerate(a):
  p = i + v

  if not p in m:
    m[p] = 1
  else:
    m[p] += 1

for i, v in enumerate(a):
  p = i - v

  if p in m:
    count += m[p]

print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
dic = {}
ans = 0
for i in range(n):
    r = i -a[i]
    ans += dic.get(r, 0)
    l = a[i] + i
    dic[l] = dic.get(l, 0) + 1
print(ans)

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

Solution (Python):
import collections
n=int(input())
s=list(map(int, input().split()))
L=[i+a for i,a in enumerate(s)]
R=[i-a for i,a in enumerate(s)] 

t=collections.Counter(L)
u=collections.Counter(R)
ans = 0
for i in t.keys():
  ans+=t[i]*u[i]
print(ans)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]

plus = {}
minus = {}

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

    if str((i+1)-a[i]) not in minus:
        minus[str((i+1)-a[i])] = 1
    else:
        minus[str((i+1)-a[i])] += 1

total = 0
for key, value in plus.items():
    if key in minus:
        total += value * minus[key]

print(total)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
L = [0]*n
R = [0]*n
cnt=0
for i in range(n):
    L[i] = i+a[i]
    R[i] = i-a[i]
z1=Counter(L)
z2=Counter(R)
cnt=0
for k1,v1 in z1.items():
    v2 = z2[k1]
    cnt += v1*v2
print(cnt)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
minus={}
plus={}
res=0
for i in range(0,n):
  pp=a[i]
  if i-pp in minus:
    minus[i-pp]+=1
  else:
    minus[i-pp]=1
  if i+pp in plus:
    plus[i+pp]+=1
  else:
    plus[i+pp]=1
for j in range(len(minus)):
  t=minus.popitem()
  try:
    res+=t[1]*plus[t[0]]
  except KeyError:
    pass
print(res)

Solution (Python):


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)]
L_dict={}
R_dict={}
count = 0
for i in range(N):
  if L[i] in L_dict:
    L_dict[L[i]] += 1
  else:
    L_dict[L[i]]  = 1
  if R[i] in R_dict:
    R_dict[R[i]] += 1
  else:
    R_dict[R[i]]  = 1
for i in L_dict.keys():
  if i in R_dict:
    count += L_dict[i] * R_dict[i]
print(count)

Solution (Python):
import bisect
n=int(input())
a=list(map(int, input().split()))
plus=[0]*n
minus=[0]*n
ans=0

for i in range(n):
    plus[i]=i+a[i]+1
    minus[i]=i-a[i]+1
p=sorted(plus)
m=sorted(minus)
for i in range(n):
    l_index=bisect.bisect_left(p,m[i])
    r_index=bisect.bisect_right(p,m[i])
    ans+=r_index-l_index
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
 
X = dict()
ans = 0
for i in range(N):
    L = i + A[i]
    if L not in X:
        X[L] = 1
    else:
        X[L] += 1

for j in range(N):
    R = j - A[j]
    if R in X:
        ans += X[R]

print(ans)

Solution (Python):
from collections import Counter

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

l = Counter([i-A[i] for i in range(N)])
r = Counter([i+A[i] for i in range(N)])

ans = 0
for k, v in l.items():
    ans += v*r[k]
print(ans)

Solution (Python):
import sys
from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())

L = defaultdict(int)
R = defaultdict(int)
for i, x in enumerate(A, 1):
    l = i + x
    r = i - x
    L[l] += 1
    R[r] += 1


ans = 0
for key, l_value in L.items():
    r_value = R[key]
    ans += l_value * r_value
print(ans)

Solution (Python):
N = int(input())
a = [int(x) for x in input().split()]
l = [0]*N
r = [0]*N
for i in range(N):
    l[i] = a[i] + i + 1
    r[i] = i + 1 - a[i]
from collections import defaultdict
d = defaultdict(int)
for i in range(N):
    d[r[i]] += 1
ans = 0
for i in range(N):
    d[r[i]] -= 1
    ans += d[l[i]]
print(ans)