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:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]"):
    minus = {}
    plus = {}
    i = 0
    for n in A:
        i += 1
        if -n - i in plus:
            plus[-n - i] += 1
        else:
            plus[-n - i] = 1
        if n - i in minus:
            minus[n - i] += 1
        else:
            minus[n - i] = 1

    count = 0
    for p in plus:
        if p in minus:
            count += plus[p] * minus[p]
    print(count)

    return


# Generated by 1.1.7.1 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():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# import bisect
from collections import Counter, deque, OrderedDict,defaultdict
# from copy import copy, deepcopy
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import unittest
from io import StringIO
import sys
sys.setrecursionlimit(10 ** 5 + 10)


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


def resolve():

    N = int(input())
    A = list(map(int, input().split()))
    d=defaultdict(int)
    cnt=0
    for k,v in enumerate(A):
        d[k+v]+=1
        cnt+=d[k-v]

    print(cnt)


resolve()

Submission status: Accepted

Solution code:
n = int(input())

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

c = {}
for i in range(n):
    if a[i] + i + 1 not in c.keys():
        c[a[i]+i+1] = 1
    else:
        c[a[i]+i+1] += 1

#print(c)

ans = 0
for i in range(n):
    if i + 1 - a[i] not in c.keys():
        pass
    else:
        ans += c[i + 1 - a[i]]

print(ans)

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=nl()

ans=0

nums = dict()

for i in range(n):
  num_i = i+a[i]
  if num_i not in nums:
    nums[num_i]=0
  nums[num_i]+=1

for j in range(n):
  num_j = j-a[j]
  if num_j in nums:
    ans+=nums[num_j]

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
 
A=list(map(int,input().split()))
#B=[]
#C=[]
D=[0]*1000000
E=[0]*1000000
 
ans=0
 
for i in range(N):
  if A[i]<500000:
  #B.append(A[i]+i+1)
  #C.append(-A[i]+i+1)
  #if -A[i]+i>=0:
  	D[A[i]+i]+=1
  	E[-A[i]+i]+=1
   #print(E[-A[i]+i],-A[i]+i)

for i in range(1000000):
  #ans+=C.count(i)
  ans+=D[i]*E[i]
 
print(ans)

Submission status: Accepted

Solution code:
import sys
#import cython

def main():
    read = lambda: sys.stdin.readline().rstrip()
    import collections
    
    d = collections.defaultdict(int)
    N = int(read())
    A = list(map(int,read().split()))

    cnt = 0
    for i, x in enumerate(A):
        d[i + x] += 1
        if d[i-x] > 0: cnt += d[i-x]
    
    print(cnt)
    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    from collections import defaultdict
    n, *a = map(int, open(0).read().split())
    d = defaultdict(lambda: [0, 0])

    ans = 0
    for i, x in enumerate(a, 1):
        d[i + x][0] += 1
        d[i - x][1] += 1
        ans += d[i + x][1] + d[i - x][0]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from collections import defaultdict
    n, *a = map(int, open(0).read().split())
    d = defaultdict(lambda: {'R': 0, 'L': 0})

    for i, x in enumerate(a, 1):
        d[i + x]['R'] += 1
        d[i - x]['L'] += 1

    ans = 0
    for key, dic in d.items():
        l = dic['L']
        r = dic['R']
        ans += l * r

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from heapq import heapify, heappop, heappush
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

# ここから書き始める
n = int(input())
a = list(map(int, input().split()))
d = {}
ans = 0
for i in range(n):
    if i > 0 and d.get(a[i] - i) != None:
        ans += d.get(a[i] - i)

    if d.get(-i-a[i]) == None:
        d[-i-a[i]] = 1
    else:
        d[-i-a[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(n) for n in input().split()]

D = dict()
ans = 0
for i, a in enumerate(A):
    val = i - a
    if val in D.keys():
        ans += D[val]
        
    val = i + a
    if val not in D.keys():
        D[val] = 0
    D[val] += 1
    
print(ans)

Submission status: Accepted

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

print(ans)
#print(L)
#print(R)

Submission status: Accepted

Solution code:
from collections import Counter

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

S1 = Counter()
S2 = Counter()
for i,a in enumerate(A):
  S1[i+1+a] += 1
  S2[(i+1)-a] += 1

res = 0
for k,v in S1.items():
  if S2[k] > 0:
    res += v*S2[k]

print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
l = {}
r = {}
for i in range(n):
    p = i+a[i]
    q = i-a[i]
    if p < n:
        if p in l:
            l[p] = l[p]+1
        else:
            l[p] = 1
    if q > 0:
        if q in r:
            r[q] = r[q]+1
        else:
            r[q] = 1
ans = 0
for i in l:
    if i in r:
        ans+= l[i]*r[i]
print(ans)

Submission status: Accepted

Solution code:
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 list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
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
# show_flg = True


def main():
    N = I()
    A = LI()
    plus_Ai_minus_i = defaultdict(int)
    minus_Aj_minus_j = defaultdict(int)

    for i in range(len(A)):
        plus_Ai_minus_i[A[i] - i] += 1
        minus_Aj_minus_j[-A[i] - i] += 1

    ans = 0

    for k, cnt1 in plus_Ai_minus_i.items():
        cnt2 = minus_Aj_minus_j[k]
        ans += cnt1 * cnt2

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int,input().split()))
    sa,wa = {},{}
    for i in range(n):
        if a[i]+i+1 not in wa.keys():
            wa[a[i]+i+1]=1
        else:
            wa[a[i]+i+1]+=1
        if i-a[i] >0:
            if i+1-a[i] not in sa.keys():
                sa[i+1-a[i]]=1
            else:
                sa[i+1-a[i]]+=1
    ans = 0
    for k in sa.keys():
        if k in wa.keys():
            ans += sa[k]*wa[k]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
# from collections import defaultdict
from collections import Counter
# from queue import Queue
# sys.setrecursionlimit(2000000)

def main():
    N = int(input())
    As = [int(s) for s in input().split()]

    i_plus_As = [(i+1)+As[i] for i in range(N)]
    counter = Counter(i_plus_As)

    ans = 0
    for i in range(N):
        i_value = ( i + 1 )- As[i]
        if i_value <= 0:
            continue

        ans += counter[i_value]

    print(ans)

    return 0

if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

Solution code:

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

    H = [0]*N
    for i in range(0,N):
        if i >= A[i]:
            H[i-A[i]] += 1

    ans = 0
    for k in range(0,N):
        Bk = k+A[k]
        if Bk < N:
            ans += H[Bk]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
import bisect
import numpy as np
import math
from collections import deque
from collections import defaultdict
from math import gcd

N = int(input())
A = list(map(int, input().split()))
left = defaultdict(int)
right = defaultdict(int)
for i in range(N):
    num = i + 1
    left[A[i]+num] += 1
    right[-A[i]+num] += 1

ans = 0
for k, v in left.items():

    ans += right[k]*v

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

r = [(i+1)+a_i for i, a_i in enumerate(A)]
l = [(j+1)-a_j for j, a_j in enumerate(A)]
nr = Counter(r)
nl = Counter(l)

ans = 0
for k, v in nl.items():
    ans += nr[k]*v

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = [int(i) for i in input().split(" ")]
r = [i+1-j for i, j in enumerate(a)]
l = [j+i+1 for i, j in enumerate(a)]
nl = Counter(l)
nr = Counter(r)
 
ans = 0
for k, v in nl.items():
    ans += nr[k]*v
print(ans)

Submission status: Accepted

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

dic1 = {}
dic2 = {}

sum = 0

for i in range(N):
    v = i-A[i]
    if v < 0:
        continue
    if not v in dic1:
        dic1[v] = 0
    dic1[v] += 1

for i in range(N):
    v = i+A[i]
    if not v in dic2:
        dic2[v] = 0
    dic2[v] += 1


for k, v in dic1.items():
    if not k in dic2:
        continue
    sum += dic1[k]*dic2[k]

print(sum)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    a_s = list(map(int, input().split()))

    # sums = [i+1 - a_s[i] for i in range(len(a_s))]
    #
    # # ans = [sums[sums[i]-1:-1].count(sums[i]) for i in range(len(a_s))]
    #
    # ans = 0
    # for i in range(len(a_s)):
    #     ans += sums[a_s[i]-i-1:-1].count(a_s[i]+i+1)
    #
    # # print(sum(ans))
    # print(ans)

    # 「番号+身長」「番号-身長」の計算結果をまとめたリストを作成(dict型のイメージ。結果が1になるデータが2個ある,2になるデータが4個ある・・等)
    # a_plus_list = [0 for i in range(len(a_s)+1)]
    # a_minus_list = [0 for i in range(len(a_s)+1)]
    # for i in range(len(a_s)+1):
    #     a_plus_list[i+1 + a_s[i]] += 1
    #     # a_minus_list[i+1 - a_s[i]] += 1

    a_plus_dict = {}
    a_minus_dict = {}
    for i in range(len(a_s)):
        plus = a_plus_dict.get(i+1 + a_s[i], 0)
        a_plus_dict[i+1 + a_s[i]] = plus + 1

        if i+1 <= a_s[i]:
            continue

        minus = a_minus_dict.get(i+1 - a_s[i], 0)
        a_minus_dict[i+1 - a_s[i]] = minus + 1

    ans = 0
    for key, val in a_plus_dict.items():

        target = a_minus_dict.get(key, 0)
        ans += target * val

    print(ans)

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """6
2 3 3 1 3 1"""
        output = """3"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """6
5 2 4 2 8 8"""
        output = """0"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """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"""
        output = """22"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
p = list(map(int, input().split()))
d = defaultdict(int)
ans = 0

for i in range(N):
    ans += d[i + 1 - p[i]]
    d[i + 1 + p[i]] += 1

print(ans)

Submission status: Accepted

Solution code:
#j-i=Ai+Aj Ai+i=-Aj+j
#Li=Ai+i
#Ri=-Ai+i
#とおくと、Li=Rjとなるとき、Aiが正なので、必ずi<jとなる
from collections import defaultdict

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

L=defaultdict(int)
R=defaultdict(int)

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

r=0
for k,v in L.items():
	j=R[k]
	r+=v*j

print(r)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[]
c=[]
for i in range(n):
  b.append(a[i]-(i+1))
  c.append(-(a[i]+i+1))
from collections import defaultdict
#cnt_b=Counter(b)
cnt_c=defaultdict(int)
ans=0
for i in range(n):
  v=cnt_c[b[i]]
  ans+=v
  cnt_c[c[i]]+=1
print(ans)

Submission status: Accepted

Solution code:
#j-i=Ai+Aj(j>i)
#j-Aj=Ai+i

#Lj=Ri=X
from bisect import bisect_left

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

    L={}
    R={}

    res=0

    for i in range(N):
        l=(i+1)-A[i]
        r=A[i]+i+1
        if (l in R.keys()):
            res+=len(R[l])
        if (r in R.keys()):
            R[r].append(i+1)
        else:
            R[r]=[i+1]


    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
X, Y = [], []
for i in range(N):
    X.append(i + 1 + A[i])
    Y.append(i + 1 - A[i])
XL = Counter(X)
c = 0
for y in Y:
    c += XL.get(y, 0)
print(c)

Submission status: Accepted

Solution code:
import sys
import math
sys.setrecursionlimit(10**9)

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

    dic = [{},{}]
    counted = [set(),set()]

    def count(x,type):
        type = type - 1
        if x <= 0:
            return

        if not x in counted[type]:
            dic[type][x] = 1
            counted[type].add(x)
        else:
            dic[type][x] += 1


    for i in range(N):
        type1 = (i+1)-A[i]
        type2 = (i+1)+A[i]

        count(type1,1)
        count(type2,2)

    ans = 0
    # print(dic,counted)
    for index,cnt in dic[0].items():
        if index in counted[1]:
            ans += cnt * dic[1][index]


    print(ans)



if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def SI(): return input().split()

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product


def solve():
    n = II()
    A = LI()

    memo = {}
    ans = 0
    for i in range(n):
        mae = (i+1) + A[i]
        ima = (i+1) - A[i]
        ans += memo.get(ima, 0)
        memo[mae] = memo.get(mae, 0) + 1
    # print(memo)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n = int(input())

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

memo = {}

ans = 0

for i in range(n):
    diff = i - a[i]
    memo.setdefault(diff, 0)
    ans += memo[diff]
    sum = a[i] + i
    memo.setdefault(sum, 0)
    memo[sum] += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

memo = {}

ans = 0

for i in range(n):
    diff = i - a[i]
    if diff in memo:
         ans += memo[diff]
    sum = a[i] + i
    memo.setdefault(sum, 0)
    memo[sum] += 1

print(ans)

Submission status: Accepted

Solution code:
import collections
from collections import Counter
N=int(input())
a=input().split()
A=[int(number) for number in a]
pluss=[]
minuss=[]
number=1
for a in A:
    plus=a+number
    minus=number-a
    pluss.append(plus)
    minuss.append(minus)
    number+=1

m=Counter(minuss)

answer=0
for p in pluss:
    if p in m.keys():
        answer+=m[p]
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
m = {}
ans = 0
for i in range(n):
    x = A[i] + i   #i番目と当該数字の和
    if x not in m:
        m[x] = 0   #和の属性値を0とする
    m[x] += 1      #和の属性値に１を足す
    if i - A[i] in m: #i番目と当該数字の差
        ans += m[i - A[i]] #差の位置に当たる属性値を答えに足す
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnt = 0
d = defaultdict(int)

for i in range(n):
    cnt += d[i-a[i]]
    d[i + a[i]] += 1

print(cnt)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [(i+1)+a for i, a in enumerate(A)]
    from collections import Counter
    c = Counter()

    ans = 0
    for j in range(N):
        i = j
        v = (j+1) - A[j]
        ans += c[v]
        c[B[i]] += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
minus = Counter([i-a[i] for i in range(n)])
plus = Counter([i+a[i] for i in range(n)])

cnt = 0
for i in minus:
    if i in plus:
        cnt += minus[i] * plus[i]
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

n: int = int(input())
a = list(map(int, input().split()))
minus = Counter([i-a[i] for i in range(n)])
plus = Counter([i+a[i] for i in range(n)])

cnt: int = 0
i: int
for i in minus:
    if i in plus:
        cnt += minus[i] * plus[i]
print(cnt)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter


def rle(list):
    dict = {}
    for i in list:
        if i in dict.keys():
            dict[i] += 1
        else:
            dict[i] = 1
    return dict


n = int(input())
a = list(map(int, input().split()))
minus = Counter([i-a[i] for i in range(n)])
plus = Counter([i+a[i] for i in range(n)])

cnt = 0
for i in minus:
    if i in plus:
        cnt += minus[i] * plus[i]
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict


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

memo = defaultdict(int)
ans = 0
for i, x in enumerate(a, 1):
    ans += memo[i - x]
    memo[x + i] += 1

print(ans)

Submission status: Accepted

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

dict_diffs = dict()
for i in range(1, n+1):
  dict_diffs[i+a[i-1]] = dict_diffs.get(i+a[i-1], 0) + 1
total = 0
for j in range(1, n+1):
  total += dict_diffs.get(j-a[j-1], 0)
print(total)

Submission status: Accepted

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

dict_diffs1 = dict()
dict_diffs2 = dict()
for i in range(1, n+1):
  dict_diffs1[i+a[i-1]] = dict_diffs1.get(i+a[i-1], 0) + 1
  dict_diffs2[i-a[i-1]] = dict_diffs2.get(i-a[i-1], 0) + 1
total = 0
for j in range(1, n+1):
  total += dict_diffs1.get(j-a[j-1], 0)
  total += dict_diffs2.get(j+a[j-1], 0)
total = total//2
print(total)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

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

cnt = 0
d = defaultdict(int)
# j - i = Ai + Aj --> i + Ai = j - Aj
for i in range(n):
    cnt += d[i - A[i]]
    d[i + A[i]] += 1

print(cnt)

Submission status: Accepted

Solution code:
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

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

d = defaultdict(int)
res = 0
for i, a in enu(A):
    v1 = i+a
    v2 = i-a
    res += d[v2]
    d[v1] += 1
print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
ans = 0

dp = dict()
dm = dict()

for i,ai in enumerate(a):
    dp[i+ai] = dp.get(i+ai,0)+1
    dm[i-ai] = dm.get(i-ai,0)+1

for wa,i in dm.items():
    ans += i*dp.get(wa,0)

print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
hList = list(map(int, input().split()))
count = 0
n_p = collections.Counter([ i + hList[i] for i in range(len(hList))])

for i in range(N):
  if i - hList[i] > 0:
    count += n_p.get(i - hList[i], 0)

print(count)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    As = [int(i) for i in input().split()]

    N_minus_A = [0] * N
    N_plus_A = [0] * N

    for i in range(N):
        minus = i + 1 - As[i]
        plus = i + 1 + As[i]
        if 0 < minus < N:
            N_minus_A[minus] += 1
        if 0 < plus < N:
            N_plus_A[plus] += 1
    print(sum([N_minus_A[i] * N_plus_A[i] for i in range(N)]))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import Counter

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

i_l=[]
j_l=[]
for i in range(n):
  i_l.append(i+a[i])
  j_l.append(i-a[i])

ci=Counter(i_l)
cl=Counter(j_l)

ans=0
for k,v in ci.items():
  ans+=v*cl[k]

print(ans)

Submission status: Accepted

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

add=[]
sub=[]
for i in range(n):
    add.append(i+a[i])
    sub.append(i-a[i])

add_dic={}
sub_dic={}

for l in add:
    if l in add_dic:
        add_dic[l]+=1
    else:
        add_dic[l]=1

for l in sub:
    if l in sub_dic:
        sub_dic[l]+=1
    else:
        sub_dic[l]=1

ans=0

for i,k in add_dic.items():
    if i in sub_dic:
        ans+=k*sub_dic[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
dic = {}
ans = 0
for i in range(N):
    if A[i] + i in dic:
        dic[A[i] + i] += 1
    if A[i] + i not in dic:
        dic[A[i] + i] = 1
    if i - A[i] in dic:
        ans += dic[i - A[i]]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

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

NA=[]

cnt=0
d={}
for i in range(N):
    if A[i]-i in d:
        cnt+=d[A[i]-i]
    if not -A[i]-i in d:
        d[-A[i]-i]=1
    else:
        d[-A[i]-i]+=1

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())

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

NA=[]

for i in range(N):
    NA.append(A[i]-i)

from collections import Counter

d=Counter(NA)

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

print(cnt)

Submission status: Accepted

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

P = [i + a for i, a in enumerate(A, start=1)]
Q = [j - a for j, a in enumerate(A, start=1)]

ans = 0
QC = Counter(Q)
for p in P:
    ans += QC[p]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
L_dict, R_dict = defaultdict(int), defaultdict(int)
i = 0
for ipt in input().split():
    L_dict[i + int(ipt)] += 1
    R_dict[i - int(ipt)] += 1
    i += 1
cnt = 0
for Lk, Lv in L_dict.items():
        cnt += R_dict[Lk] * Lv
print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
L_dict, R_dict = defaultdict(int), defaultdict(int)
i = 0
for ipt in input().split():
    L_dict[i + int(ipt)] += 1
    R_dict[i - int(ipt)] += 1
    i += 1
cnt = 0
for Lk, Lv in L_dict.items():
    try:
        cnt += R_dict[Lk] * Lv
    except KeyError:
        pass
print(cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = [int(x) for x in input().split()]

L = dict(); R = dict()
for i in range(N):
    L[i + A[i]] = L.get(i + A[i], 0) + 1
    R[i - A[i]] = R.get(i - A[i], 0) + 1

ans = 0
# i + A[i] >= 2, i - A[i] <= N
for i in range(N):
    ans += L.get(i, 0) * R.get(i, 0)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]

L = dict(); R = dict()
for i in range(N):
    l = i + A[i]
    if l in L:
        L[l] += 1
    else:
        L[l] = 1
    r = i - A[i]
    if r in R:
        R[r] += 1
    else:
        R[r] = 1

ans = 0
for x in R:
    if x in L:
        ans += L[x] * R[x]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a1, a2 = [], []
for idx, ai in enumerate(map(int, input().split())):
    a1.append(ai+idx)
    a2.append(idx-ai)
cnt = Counter(a2)
res = 0
for idx, i in enumerate(a1):
    cnt[a2[idx]] -= 1
    res += cnt[i]
print(res)