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
import math
import collections
import itertools
import bisect
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

N = I()
A = LI()

l = []
r = []
for i, a in enumerate(A):
    l.append(i+1+a)
    r.append(i+1-a)

rr = collections.Counter(r)
ans = 0
for ll in l:
    ans += (rr[ll])

print(ans)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
count = 0
b = []
c = []

for i in range(n):
    b.append(a[i]+i)
    c.append(i-a[i])
t = Counter(c) 
for s in b:
    count += t[s]

print(count)

Solution (Python):
def test():
    import numpy as np
    N = 10000
    print(N)
    A = np.random.randint(0, 10, [N])
    print(count(N,A))


def count(N, A):
    
    
    
    left = dict()
    right = dict()
    for i in range(N):
        l = i+1 + A[i]
        r = i+1 - A[i]
        left[l] = left.get(l, 0) + 1
        right[r] = right.get(r, 0) + 1

    counter = 0
    for k, v in left.items():
        if k in right:
            counter += right[k] * v

    return counter

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

    result = count(N,A)
    print(result)

if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
from collections import Counter

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

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

cnt_L = Counter(L)
cnt_R = Counter(R)
ans = 0
for l, cnt in cnt_L.items():
    ans += cnt * cnt_R.get(l, 0)
print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
A = list(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)

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

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

Solution (Python):
from collections import Counter

n = int(input())
a = list(map(int,input().split()))
b = []
c = []
ans = 0
for i in range(n):
    b.append(i+1-a[i])
    c.append(i+1+a[i])
B = Counter(b)
for i in c:
    ans += B.get(i,0)
    
print(ans)

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

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

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

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

Solution (Python):
import sys, math
import bisect, heapq
import collections

sys.setrecursionlimit(10 ** 6)

input = sys.stdin.readline

gets = lambda: input().rstrip()
getn = lambda: int(input())
getmul = lambda: (int(i) for i in input().split())
getlist = lambda: [int(i) for i in input().split()]

n = getn()
A = getlist()

before = {}
res = 0
for i, a in enumerate(A):
    res += before.get(i - a, 0)
    before[i + a] = before.get(i + a, 0) + 1

print(res)

Solution (Python):
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
a=[0]+a
d=defaultdict(int)

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

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

Solution (Python):
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))
l1=defaultdict(int)
l2=defaultdict(int)
ans=0
for i,h in enumerate(a):
  l1[i+1-h]+=1
  l2[i+1+h]+=1
for k,v in l2.items():
  ans+=v*l1[k]
print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
iA_i = [i+a for i,a in enumerate(A)]
jA_j = [i-a for i,a in enumerate(A)]
from collections import defaultdict as ddict
iA_i_dict = ddict(int)
jA_j_dict = ddict(int)

for iai in iA_i:
    iA_i_dict[iai] += 1
for jaj in jA_j:
    if jaj > 0:
        jA_j_dict[jaj] += 1
ans = 0
for i in iA_i_dict.keys():
    ans += iA_i_dict[i] * jA_j_dict[i]
print(ans)

Solution (Python):
N = int(input())
dic = {}
ans = 0

for i, j in enumerate(input().split()):
    ind = int(i) + 1
    tall = int(j)

    delta = ind - tall

    ans += dic.get(delta, 0)  

    addKey = ind + tall

    dic[addKey] = dic.get(addKey, 0) + 1

print(ans)

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

dic_minus = {}
dic_plus = {}

for i in range(N):
    dic_minus.setdefault((i+1)-A[i], []).append(i+1)
    dic_plus.setdefault((i+1)+A[i], []).append(i+1)

ans = 0
for minus in dic_minus:
    len_minus = len(dic_minus[minus])
    if dic_plus.setdefault(minus, "hoge") != "hoge":
        ans += len_minus * len(dic_plus[minus])

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
left_dic = {}
def add_dic(p):
    if not p in left_dic:
        left_dic[p] = 1
    else:
        left_dic[p] += 1
for i in range(n):
    add_dic(a[i]+i)
def cnt_dic(p):
    if not p in left_dic:
        return 0
    return left_dic[p]
ans = 0
for j in range(n):
    ans += cnt_dic(j-a[j])
print(ans)

Solution (Python):
import sys
input = lambda:sys.stdin.readline().strip()
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
ans = 0
for i in range(n):
    if i+a[i]<n:
        b[i+a[i]]+=1
    if i-a[i]>=0:
        ans+=b[i-a[i]]
print(ans)

Solution (Python):
from collections import Counter
n,*a=map(int,open(0).read().split())
l=[0]*n
r=[0]*n
for i,c in enumerate(a):
  l[i]+=c+i+1
  r[i]+=i+1-c
L=Counter(l)
R=Counter(r)
def count(A):
  ret=0
  for i in A.values():
    ret+=i*(i-1)//2
  return ret
print(count(L+R)-count(L)-count(R))

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

d = {}

for i, a in enumerate(A):
    tmp = i + 1 - a
    if tmp in d.keys():
        d[tmp] += 1
    else:
        d[tmp] = 1



for i, a in enumerate(A):
    tmp = a + i + 1
    tmptmp = i + 1 - a
    if tmp in d.keys():
        ans += d[tmp]
    if tmptmp in d.keys():
        d[tmptmp] = max(0, d[tmptmp] - 1)

print(ans)

Solution (Python):
import bisect
if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    score = {}

    for i in range(n):
        s = a[i] + i + 1
        if s not in score:
            score[s] = [i]
        else:
            score[s].append(i)
    ans = 0
    for j in range(n):
        s = (j + 1) - a[j]
        if s in score:
            size = len(score[s])
            if size > 0:
                tmp = bisect.bisect_left(score[s], j)
                ans += tmp
    print(ans)

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

Solution (Python):
n = int(input())
a = [0] + list(map(int,input().split()))
b = [0]*(n+1)
c = [0]*(n+1)

for i in range(1,n+1):
    b[i] = a[i] + i
    c[i] = i - a[i]
'''
ans = 0
for i in range(n):
    for j in range(i+1,n):
        if b[i] == c[j]:
            ans += 1
print(ans)
これだと(多分)無理。
'''
bdic = {}
cdic = {}
'''
print(b)
print(c)
'''
for i in range(1,n+1):
    if b[i] not in bdic:
        bdic[b[i]] = 0
    bdic[b[i]] += 1

    if c[i] not in cdic:
        cdic[c[i]] = 0
    cdic[c[i]] += 1
bb = list(bdic.items())
cc = list(cdic.items())
bb.sort()
cc.sort()

bval = [bb[i][1]for i in range(len(bb))]
bkey = [bb[i][0]for i in range(len(bb))]

cval = [cc[i][1]for i in range(len(cc))]
ckey = [cc[i][0]for i in range(len(cc))]
'''
print(bval)
print(bkey)
print(cval)
print(ckey)
'''

def bisearch(data,value):
    left = 0
    right = len(data) - 1
    while left <= right:
        mid = (left + right)//2
        if data[mid] == value:
            return mid
        elif data[mid] < value:
            left = mid + 1
        else:
            right = mid - 1
    return -1
 
ans = 0
for i in range(len(bb)):
    if bisearch(ckey,bkey[i]) > -1:
        ans += bval[i]*cval[bisearch(ckey,bkey[i])]
print(ans)

Solution (Python):
n = int(input())
a_list = list(map(int,input().split()))
a_plus = {}
a_minus = {}
for i in range(1,n):
  a_minus[i] = 0
  
for i in range(n):
  minus = (i+1) - a_list[i]
  if 2 <= minus:
    a_minus[minus] += 1
  
count = 0

for j in range(n):
  plus = (j+1) + a_list[j]
  if plus <= n - 1:
    count += a_minus[plus]

    
print(count)

Solution (Python):
from collections import Counter
N = int(input())
An = list(map(int,input().split()))
c = 0
Bn = []
Cn = []
for k in range(N):    
    Bn.append(An[k]+k)
    Cn.append(k-An[k])
Cj = Counter(Cn)
for i in Bn:
    c+=Cj[i]
print(c)

Solution (Python):
N=int(input())
listA=list(map(int,input().split()))
ans=0
L=[]
R=[]
L1=[0]*N
R1=[0]*N
for i in range(N):
  L+=[i+listA[i]]
  R+=[i-listA[i]]
  try:
    L1[i+1+listA[i]]+=1
  except:
    pass
  try:
    if i-listA[i]>0:
      R1[i+1-listA[i]]+=1
  except:
    pass
for i in range(N):
  ans+=L1[i]*R1[i]
print(ans)

Solution (Python):
def main(sample_file = ''):

    """ convenient functions
    
    
    
    
    
    """

    """convenient decorator
    
    
        
        
        
        
        
        
        
        
        
        
    """
    
    
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product 
    
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  
        b, e = 2, 0  
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):   
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict 
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest 
    from copy import deepcopy, copy 
    import operator
    from operator import itemgetter 
    
    
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd 
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)
    def make_divisors(n, reversed=False):
        """create list of divisors
        
        Args:
            number (int): number from which list of divisors is created
            reversed (bool, optional): ascending order if False. Defaults to False.
        
        Returns:
            list: list of divisors
        """
        divisors = set()
        for i in range(1, int(n**0.5)+1):
            if n % i == 0:
                divisors.add(i)
                divisors.add(n//i)
        return sorted(list(divisors),reverse=reversed)
    
    
    
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    AtoZ = list('abcdefghijklmnopqrsruvwxyz')
    modpow = lambda a, n, p = MOD: pow(a, n, p) 
    def modinv(a, p = MOD):
        
        
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        
        
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    
    
    
    
    

    def BellmanFord(N, M, ABC, vertex_start, vertex_end, value_if_inf = -1, find_shortest = False):
        """to calculate furthest or shortest length between vertex_start and vertex_end using BellmanFord algorithm
        
        Args:
            N (int): number of vertices
            M (int): number of edges
            ABC (list): [(ai, bi, ci) for _ in range(N)] where i-th edge is directed from vertex ai to vertex bi and the length is ci 
            vertex_start (int): start vertex. usually use 0.
            vertex_end (int): end vertex. usually use N-1.
            value_if_inf (int or string as you like, optional): value you want when the furthest (or shortest) distance is infinite (or -infinite). Defaults to -1.
            find_shortest (bool, optional): choose False to find furthest path. Defaults to False.
        
        Returns:
            int or string: normally int (but can be str if you set value_if_inf to str)
            
        Example:
                N, M, P = R()
                ABC = [R() for _ in range(M)]
                ABC = [(a-1, b-1, c-P) for a, b, c in ABC]
                print(BellmanFord(N, M, ABC, 0, N-1, value_if_inf = 'inf'))

        """

        def make_reachable_list(N, M, ABC, vertex_start, vertex_end):
            reachable_to_direct = defaultdict(list)
            reachable_from_direct = defaultdict(list)
            reachable_from_start = [False] * N
            reachable_to_end = [False] * N
            reachable_from_start[vertex_start] = True
            reachable_to_end[vertex_end] = True
            reachable_from_both_sides = [False] * N
            dfs_from_start = []
            dfs_to_end = []
            for a, b, c in ABC:
                reachable_to_direct[a].append(b)
                reachable_from_direct[b].append(a)
                if a == vertex_start:
                    dfs_from_start.append(b)
                    reachable_from_start[b] = True
                if b == vertex_end:
                    dfs_to_end.append(a)
                    reachable_to_end[a] = True
            while dfs_from_start:
                v = dfs_from_start.pop()
                for i in reachable_to_direct[v]:
                    if not reachable_from_start[i]:
                        reachable_from_start[i] = True
                        dfs_from_start.append(i)
            while dfs_to_end:
                v = dfs_to_end.pop()
                for i in reachable_from_direct[v]:
                    if not reachable_to_end[i]:
                        reachable_to_end[i] = True
                        dfs_to_end.append(i)
            for i in range(N):
                reachable_from_both_sides[i] = reachable_from_start[i] and reachable_to_end[i]
            return reachable_from_both_sides
        
        reachable_from_both_sides = make_reachable_list(N, M, ABC, vertex_start, vertex_end)
        
        if find_shortest:
            dist = [INF for i in range(N)]
        else:
            dist = [-INF for i in range(N)]
        
        dist[vertex_start] = 0
        for i in range(N):
            updated = False
            for a, b, c in ABC:
                if not reachable_from_both_sides[a]:
                    continue
                elif find_shortest:
                    update_condition = dist[a] + c < dist[b]
                else:
                    update_condition = dist[a] + c > dist[b]
                if dist[a] != INF and update_condition:
                    dist[b] = dist[a] + c
                    updated = True
                    if i == N-1:
                        return value_if_inf
            if not updated:
                break
        return dist[vertex_end]

    """ initialize variables and set inputs
    
        
        
            
            
    
        
        
        
        
        
        
        
    
        
        
        
        
    
        
        
    """

    
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    r_float = lambda: float(r())
    R = lambda: list(map(int, r().split()))
    R_str = lambda: r().split()
    R_map = lambda: map(int, r().split())
    R_float = lambda: list(map(float, r().split()))
    R_tuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    
    
    
    
    
    
    
    """

    
    if sample_file:
        sys.stdin = open(sample_file)
        
    
    
    
    N = r_int()
    A = R()
    d = defaultdict(int)
    for i, a in enumerate(A):
        d[i-a] += 1
    ans = 0
    for i, a in enumerate(A):
        d[i-a] -= 1 
        ans += d[i+a]
    print(ans)

        
        
    

    
    
    

    """memo: how to use defaultdict of list
    
    Dic = defaultdict(list)
    
    Dic[x].append(y)
    
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    
    >>> a = 5
    >>> bin_str_a = format(a, '
    >>> print(bin_str_a)
    0b0101 
    """
    
    """memo: how to solve the problem
    creating simple test/answer
    greed
    simple dp
    graph
    """

if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

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

q1=[]
q2=[]

for i in range(n):
  q1.append(i+a[i])
  q2.append(i-a[i])

c2=Counter(q2)

ans=0
for i in q1:
  ans+=c2[i]

print(ans)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
L = [A[i]+i for i in range(N)]
R = [i-A[i] for i in range(N)]
c = Counter(R)

ans = 0
for l in L:
    ans += c[l]

print(ans)

Solution (Python):
from collections import Counter

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

q1=[]
q2=[]

for i in range(n):
  q1.append(i+a[i])
  q2.append(i-a[i])

c1=Counter(q1)
c2=Counter(q2)

ans=0
for i in c1.keys():
  num1=c1[i]
  num2=c2[i]
  ans+=num1*num2

print(ans)

Solution (Python):
import collections as col

n = int(input())
a = [0] + list(map(int,input().split()))

wa = [i + a[i] for i in range(1,n+1)]
sa = [i - a[i] for i in range(1,n+1)]
cnt = col.Counter(sa)

ans = 0
for num in wa: ans += cnt[num]
print(ans)

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


























L={}
for i in range(N):
    s = i+1 + Agents[i] 
    if s in L:
        L[s]+=1
    else:
        L[s]=1

R={}
for j in range(N):
    s= j+1 - Agents[j]
    if s in R:
        R[s]+=1
    else:
        R[s]=1
cnt=0

for key,value in L.items():
    cnt += R.get(key,0) * value






print(cnt)

Solution (Python):
from collections import Counter

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

L = []
R = []
for i, a in enumerate(A_list, start=1):
    L.append(i+a)
    R.append(i-a)

c_R = Counter(R)
c_L = Counter(L)

ans = 0
for r, cnt in c_R.items():
    ans += cnt * c_L.get(r, 0)

print(ans)

Solution (Python):

import collections


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

    dictionary = collections.defaultdict(int)
    result = 0
    for idx, a in enumerate(A):
        current = idx - a
        result += dictionary[current]
        dictionary[idx + a] += 1
    print(result)
    pass

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
l1=[0]*n
l2=[0]*n
k=0
ans=0
for i in a:
  if k+i<n:
    l1[k+i]+=1
  if k-i>=0:
    l2[k-i]+=1
  k+=1
for i in range(n):
  ans+=l1[i]*l2[i]
print(ans)

Solution (Python):
from collections import defaultdict
n=int(input())
A=list(map(int,input().split()))
ans=0
d=defaultdict(int)

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

print(ans)

Solution (Python):
import bisect
N=int(input())
A=list(map(int,input().split()))
L=[]
R=[]
ans=0
for i in range(N):
    L.append(i+1+A[i])
    R.append(i+1-A[i])
L.sort()
R.sort()
for i in L:
    l=bisect.bisect_left(R,i)
    r=bisect.bisect_right(R,i)
    ans=ans+r-l
print(ans)

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

ans = 0
for i in range(N):
  t = A[i]+i
  
  if t in R.keys():
    ans += R[t]
    
print(ans)

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

Solution (Python):
N = int(input())

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

L = {}
R = {}

for i in range(N):
  t = A[i]+i
  if t in L:
    L[t] += 1
  else:
    L[t] = 1
  s = i - A[i]
  if s in R:
    R[s] += 1
  else:
    R[s] = 1  

ans = 0
for i in range(N):
  t = A[i]+i
  
  if t in R.keys():
    ans += R[t]
    
print(ans)

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

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

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

Solution (Python):
n=int(input())
li=list(map(int,input().split()))
r={0:0}
l={0:0}
ans=0
for i in range(len(li)):
    if l.get(i+li[i])!=None:
        ans+=l[i+li[i]]
    if r.get(i-li[i])!=None:
        ans+=r[i-li[i]]
    if r.get(i+li[i])!=None:
        r[i+li[i]]+=1
    else:
        r[i+li[i]]=1
    if l.get(i+li[i])!=None:
        l[i-li[i]]+=1
    else:
        l[i-li[i]]=1
print(ans)

Solution (Python):
from collections import Counter

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

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

print(ans)

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline


N = int(input().strip())


all_list = list(map(int, input().strip().split()))


li = []

for i, item in enumerate(all_list):
    li.append(i + all_list[i])



ri = []

for j, item in enumerate(all_list):
    ri.append(j - all_list[j])




li_hash = {}
for i, item in enumerate(li):
    if item not in li_hash:
        li_hash[item] = 1
        continue
    elif item in li_hash:
        li_hash[item] += 1



res = 0
for item in ri:
    if item in li_hash:
        res += li_hash[item]

print(res)

Solution (Python):
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    from collections import defaultdict
    count = defaultdict(int)
    ans = 0
    for i, a in enumerate(A):
        if i - a in count:
            ans += count[i-a]
        count[i+a] += 1
    print(ans)
 
 
if __name__ == '__main__':
    main()

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

Solution (Python):




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

    L = []
    R = []

    for i, a in enumerate(A):
        L.append(i+a)
        R.append(i-a)
    
    Ldic = {}
    for l in L:
        if l in Ldic:
            Ldic[l] += 1
        else:
            Ldic[l] = 1
    
    Rdic = {}
    for r in R:
        if r in Rdic:
            Rdic[r] += 1
        else:
            Rdic[r] = 1
    
    total = 0

    for lkey in Ldic:
        if lkey in Rdic:
            total += Ldic[lkey] * Rdic[lkey]
    
    print(total)



if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    from collections import defaultdict
    count = defaultdict(int)
    ans = 0
    for i, a in enumerate(A):
        if i - a in count:
            ans += count[i-a]
        count[i+a] += 1
    print(ans)


if __name__ == '__main__':
    main()