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:
N = int(input())
A = list(map(int, input().split()))
ans = 0
B = []
C = []
maA = max(A) + N -1
for k in range(N):
  if A[k] + k <= maA:
    B.append(A[k]+k)
  if -A[k] + k >0:
    C.append(-A[k]+k)
B.sort()
C.sort()

c = 0
b = 0
while b < len(B) and c < len(C):
  if B[b] == C[c]:
    s = 0
    t = 0
    j = float('inf')
    k = float('inf')
    for j in range(b, len(B)):
      if B[b] == B[j]:
        s += 1
      else:
        break
    for k in range(c, len(C)):
      if C[c] == C[k]:
        t +=1
      else:
        break
    ans += s*t
    b = j
    c = k
    continue
  elif B[b] > C[c]:
    c += 1
  else:
    b += 1
#print(B)
#print(C)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
B = []
C = []
maA = max(A) + N -1
for k in range(N):
  if A[k] + k <= maA:
    B.append(A[k]+k)
  if -A[k] + k >0:
    C.append(-A[k]+k)
B.sort()
C.sort()

c = 0
b = 0
while b < len(B) and c < len(C):
  if B[b] == C[c]:
    s = 0
    t = 0
    j = float('inf')
    k = float('inf')
    for j in range(b, len(B)):
      if B[b] == B[j]:
        s += 1
      else:
        break
    for k in range(c, len(C)):
      if C[c] == C[k]:
        t +=1
      else:
        break
    ans += s*t
    b = j
    c = k
    continue
  elif B[b] > C[c]:
    c += 1
  else:
    b += 1
#print(B)
#print(C)
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#import numpy as np
#import math
#from decimal import *
#from numba import njit
from collections import Counter

#@njit
def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = 0
    r = []
    l = []
    for i in range(N):
        r += i - A[i],
        l += i + A[i],

    c = Counter(l)
    for i in r:
        count += c[i]

    print(count)
main()

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
b=[]
c=[]
ans=0
for i,j in enumerate(a):
    b.append(i+j)
    c.append(i-j)
d=Counter(c)
for i in b:
    ans+=d.get(i,0)

print(ans)

Submission status: Accepted

Solution code:
def getInts():
    return [ int(x) for x in input().split() ]

[N] = getInts()
As = getInts()
A = {}
for i in range(1, N+1):
    A[i] = As[i-1]

L = {}
R = {}
for i in range(1, N+1):
    if i - A[i] not in L:
        L[i-A[i]] = 1
    else:
        L[i-A[i]] += 1
    if i + A[i] not in R:
        R[i+A[i]] = 1
    else:
        R[i+A[i]] += 1
count = 0
for k in R.keys():
    if k in L:
        count += R[k]*L[k]
print(count)

Submission status: Accepted

Solution code:
import collections
import bisect
n = int(input())
cnt = collections.defaultdict(list)
dat = list(map(int, input().split()))
buf = []
for i in range(n):
    cnt[i - dat[i]].append(i)
#print(buf)
res = 0
for i in range(n):
    x = i + dat[i]
    if x in cnt:
        res += len(cnt[x]) - bisect.bisect_left(cnt[x], x)
print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(lambda: 0)
    ans = 0
    for i, ai in enumerate(a, 1):
        d[str(i+ai)] += 1
        ans += d[str(i-ai)]
    print(ans)
        

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
def main():
    n = int(input())
    A = list(map(int, input().split()))
    L = Counter([i + a for i, a in enumerate(A)])
    R = Counter([i - a for i, a in enumerate(A)])
    ans = 0
    for key in R.keys():
        ans += L[key] * R[key]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
List = list(map(int,input().split()))
dicL = defaultdict(int)
dicR = defaultdict(int)
for i in range(N):
  dicL[List[i]+i] += 1
  dicR[i - List[i]] += 1
counter = 0
for item in dicL:
  if item in dicR:
    counter += dicL[item]*dicR[item]
print(counter)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    h = [int(i) for i in input().split()]

    d1 = { }
    d2 = { }
    for j in range(n):
        dif = j - h[j]
        tot = j + h[j]
        if dif not in d1:
            d1[dif] = 0
        if tot not in d2:
            d2[tot] = 0

        d1[dif] += 1
        d2[tot] += 1

    npairs = 0
    # print(d1)
    # print(d2)
    for i in range(n):
        dif = i - h[i]
        tot = i + h[i]
        if tot in d1:
            npairs += d1[tot]
        if dif in d2:
            npairs += d2[dif]

    print(npairs // 2)

main()

Submission status: Accepted

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()

a = LIST()

first = a[:]

dd = defaultdict(int)

for i in range(n):
    first[i] = first[i]+(i+1)
    dd[first[i]]+=1

ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())
A = [0]
A.extend(list(map(int,input().split())))
B = {}
pair = 0

for i in range(1,n+1):
  if i+A[i] in B:
    B[i+A[i]] += 1
  else:
    B[i+A[i]] = 1
  
for j in range(2,n+1):
  if j-A[j] in B:
    pair += B[j-A[j]]

print(pair)

Submission status: Accepted

Solution code:
import collections

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

b = [0] * n
c = [0] * n
for i in range(n):
  b[i] = a[i] + (i + 1)
  c[i] = (i + 1) - a[i]

ans = 0
cCc = collections.Counter(c)
# print(cCc)
for k, v in collections.Counter(b).items():
  if v != 0 and k in cCc:
    # print(k,v,cCc[k])
    ans += v * cCc[k]

print(ans)

Submission status: Accepted

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

B = [i-A[i] for i in range(N)]
C = [i+A[i] for i in range(N)]

B.sort()
C.sort()

j = 0
ct = 0
cashe = 0
for i in range(N):
  if i >= 1 and B[i-1] == B[i]:
    ct += cashe
  else:
    cashe = 0
    while C[j] <= B[i]:
      if C[j] == B[i]:
        ct += 1
        cashe += 1
      j += 1
      
print(ct)

Submission status: Accepted

Solution code:
#(身長)ー（番号）＝ー（身長）ー（番号）はありえない、身長=0にならないから
N = int(input())
A = list(map(int,input().split()))
from collections import defaultdict
AB = defaultdict(int)
_AB = defaultdict(int)
for i,a in enumerate(A):
    AB[i+1+A[i]]+=1
    _AB[-A[i]+i+1]+=1
ans = 0
for key,val in AB.items():
    ans += val*_AB[key]
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc166/tasks/abc166_e

"""
変数分離すれば互いに独立なので、
連想配列でO(N)になる。
"""


import sys
input = sys.stdin.readline
from collections import Counter

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

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

dic = Counter(P)

res = 0
for m in M:
    res += dic.get(m,0)

print(res)

Submission status: Accepted

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

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

Ld = {}
Rd = {}

for i in range(N):
    if R[i] in Rd.keys():
        Rd[R[i]] += 1
    else:
        Rd[R[i]] = 1

ans = 0
for i in range(N):
    if L[i] in Rd.keys():
        ans += Rd[L[i]]
    if L[i] == R[i]:
        ans -= 1
        
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
A = list(map(int,input().split()))
d = {}
ans = 0
for i in range(N):
    sa = i-A[i]
    if sa in d:
        ans += d[sa]
    wa = i+A[i]
    if wa in d:
        d[wa] += 1
    else:
        d[wa] = 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
t = list(map(int, input().split()))
r = {}
ans = 0
for i in range(len(t)):
  try:
    r[i+1 + t[i]] += 1
  except KeyError:
    r[i+1 + t[i]] = 1
for i in range(len(t)):
  if((i+1 - t[i]) >= 1):
    try:
      ans += r[i+1-t[i]]
    except KeyError:
      continue
print(ans)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = [0]+list(map(int,input().split()))

d = {}

ans = 0
for i in range(1,n+1):
    if i+A[i] not in d:
        d[i+A[i]] = 1
    else:
        d[i+A[i]] += 1
    if i-A[i] in d:
        ans += d[i-A[i]]
        
print(ans)

Submission status: Accepted

Solution code:
import sys
def Ii():return int(sys.stdin.buffer.readline())
def Mi():return map(int,sys.stdin.buffer.readline().split())
def Li():return list(map(int,sys.stdin.buffer.readline().split()))

n = Ii()
a = Li()
x = [0]*n
ans = 0
for i in range(n): 
  if a[i]+i < n:
    x[a[i]+i] += 1
  if i-a[i] >= 0:
    ans += x[i-a[i]]
      
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
A = [int(i) for i in input().split()]

ans = 0
d = defaultdict(int)
cur = N
for a in A[::-1]:
    ans += d[cur + a]
    if cur - a > 0:
        d[cur - a] += 1
    cur -= 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
L = Counter(list(i + 1 + A[i] for i in range(N)))
R = Counter(list(j + 1 - A[j] for j in range(N)))
# print(L, R)
ans = 0
for k, v in R.items():
    ans += L[k] *  v
print(ans)

Submission status: Accepted

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

L = [A[i]+i+1 for i in range(N)]
R = [i + 1 - A[i] for i in range(N)]
from collections import defaultdict
l = defaultdict(int)
for i in L:
  l[i] += 1
ans = 0
for r in R:
  ans += l.get(r, 0)
print(ans)

Submission status: Accepted

Solution code:
import collections

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

# N = 32
# A = [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]

# N = 6
# A = [2, 3, 3, 1, 3, 1]

i = [k for k in range(1,N+1)]
j = [k for k in range(1,N+1)]

Li = [i[k]+A[k] for k in range(N)]
Rj = [j[k]-A[k] for k in range(N)]

Li = collections.Counter(Li)

counter = 0
for r in Rj:
    counter += Li[r]
print(counter)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

for i in range(N):
  B[A[i]+i+1] += 1
  C[-A[i]+i+1] += 1

ans = 0

for x in B.keys():
  ans += B[x] * C[x]

print(ans)

Submission status: Accepted

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

#index,h
pp =[]
pm = []
all_set = set()
for i in range(N):

    pp.append(i + A[i])
    pm.append( i - A[i])

count = 0
c = collections.Counter(pp)
#print(c)
for i in range(N):
    count += c[pm[i]] 
    #print(c[pm[i]])

    #count += pp.count(pm[i])
    #count += pm.count(pp[i])

print(count)

Submission status: Accepted

Solution code:
import collections
n = int(input())
a_list = list(map(int,input().split()))
a_list2 = [i-a for i,a in enumerate(a_list)]
a_list_dic = collections.Counter(a_list2)
count = 0
for i,a in enumerate(a_list):
    a_list_dic[i-a] -= 1
    count += a_list_dic[i+a]

print(count)

Submission status: Accepted

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

cnt={}
ans = 0
for i in range(1,n+1):
    if a[i] +i not in cnt:
        cnt[a[i] +i] = 0
    cnt[a[i] + i] += 1
for i in range(1, n+1):
    if i-a[i] in cnt:
        ans+= cnt[i-a[i]]
print(ans)

Submission status: Accepted

Solution code:
import collections

cnt = collections.defaultdict(int)
N = int(input())
ans = 0
height = list(map(int, input().split()))
for n in range(1, N+1):
    ans += cnt[n - height[n-1]]
    cnt[n+height[n-1]] += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
L = []
for i in range(N):
    L.append(i-A[i])
R = []
for j in range(N):
    R.append(j+A[j])
from collections import Counter
LL = Counter(L)
RR = Counter(R)
ans = 0
for k,v in RR.items():
    ans += v*LL[k]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())

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

cnt = defaultdict(int)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import collections
cc = collections.Counter()
count = 0
n = raw_input()
for i,e in enumerate(map(int, raw_input().split())):
	count += cc[e - i]
	cc[-e-  i] +=1
print count

Submission status: Accepted

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

    dict1 = {}
    dict2 = {}

    for i in range(n):
        a1 = a[i] + i + 1
        a2 = i + 1 - a[i]
        if a1 in dict1:
            dict1[a1] += 1
        else:
            dict1[a1] = 1
        if a2 in dict2:
            dict2[a2] += 1
        else:
            dict2[a2] = 1
    ans = 0
    for i in dict1:
        if i in dict2:
            ans += dict1[i]*dict2[i]
    print(ans)
            
resolve()

Submission status: Accepted

Solution code:
from collections import Counter


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

X = Counter([i - a for i, a in enumerate(A)])

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

print(ans)

Submission status: Accepted

Solution code:
N = 6
ARR = [2, 3, 3, 1, 3, 1]

# N = 6
# ARR = [5, 2, 4, 2, 8, 8]

N = 32
ARR = [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]

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


def calculate(n, arr):
    A = {}
    B = {}

    for i in range(1, n + 1):
        Ai = arr[i - 1]
        Aj = arr[i - 1]

        a = Ai + i
        b = (i - Aj)

        if A.get(a) == None:
            A.__setitem__(a, [i])
        else:
            tmp = A.__getitem__(a)
            tmp.append(i)
            A.__setitem__(a, tmp)

        if B.get(b) == None:
            B.__setitem__(b, [i])
        else:
            tmp = B.__getitem__(b)
            tmp.append(i)
            B.__setitem__(b, tmp)

    result = 0
    for key in A.keys():
        l1 = len(A.get(key))

        if B.get(key) != None:
            l2 = len(B.get(key))
            result += l1 * l2

    print(result)


calculate(N, ARR)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = [0] * n
r = [0] * n

for i, h in enumerate(a):
    if 0 <= i + h < n:
        l[i + h] += 1
    if 0 <= i - h < n:
        r[i - h] += 1

ans = 0
for i in range(n):
    ans += l[i] * r[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
dp = dict()

for i in range(n):
    a_p = a[i] + i+1
    a_m = i+1 -a[i]
    if a_p in dp:
        dp[a_p] = dp[a_p] + 1
    else:
        dp[a_p] = 1
    if a_m in dp:
        ans = ans + dp[a_m]


print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# 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 MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): 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, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

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

    memo = {}
    ans = 0
    for i, a in enumerate(A):
        i = i + 1
        # print(i, a - i, i - a)
        ans = ans + memo.get(i - a, 0)
        memo[a + i] = memo.get(a + i, 0) + 1
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n = int(input())

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

ap = {}
am = {}


for i in range(1,n+1):
    j = i - 1

    if (i + a[j]) in ap:
        ap[i+a[j]] = ap[i+a[j]] + 1
    else:
        ap[i+a[j]] = 1
    
    if (i - a[j]) in am:
        am[i-a[j]] = am[i-a[j]] + 1
    else:
        am[i-a[j]] = 1

ans = 0
for i in ap.keys():
    if i in am:
        ans += am[i] * ap[i]
print(ans)

Submission status: Accepted

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

I = [i+a for i,a in enumerate(A, start=1)]
J = [i-a for i,a in enumerate(A, start=1)]
#print(I, J)

dic = {}
ans = 0

for i in range(N):
    if I[i] in dic:
        dic[I[i]] += 1
    else:
        dic[I[i]] = 1
    if J[i] in dic:
        ans += dic[J[i]]


#print(dic)
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
n = int(readline())
a = list(map(int, readline().split()))
b = defaultdict(list)
c = defaultdict(list)

for i, v in enumerate(a, 1):
  b[i + v].append(i)
  if 2 <= i - v <= 100000001:
    c[i - v].append(i)
c = dict(c)

ans = 0
for v, L in b.items():
  for j in L:
    if v in c:
      ans += len(c[v])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

def rl():
    return list(map(int, input().split()))


def solve():
    n = ri()
    A = rl()
    types = dd(int)
    ans = 0
    for i, h in enumerate(A):
        j = i + 1
        ans += types[j - h]
        types[j + h] += 1
    print (ans)






mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()

Submission status: Accepted

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

plus = {}
minus = {}
for i in range(n):
    p = i + 1 + a[i]
    m = i + 1 - a[i]
    if p not in plus:
        plus[p] = 0
    if m not in minus:
        minus[m] = 0
    plus[p] += 1
    minus[m] += 1

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

print(cnt)

Submission status: Accepted

Solution code:
import sys
import numpy as np
from collections import defaultdict



input = sys.stdin.readline

def main():

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


    wa = defaultdict(int)
    sa = defaultdict(int)

    for i in range(1,N+1):
        wa[i+A[i-1]] +=1
        sa[i-A[i-1]]+=1

    ans = 0
    for j in range(N):
        ans += wa[j] *sa[j]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0

dic = {}
for i in range(N):
    temp = i + A[i]
    if temp in dic:
        dic[temp] += 1
    else:
        dic[temp] = 1
        
for i in range(N):
    temp = i - A[i]
    if temp in dic:
        ans += dic[temp]

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n = II()
    a = LI()
    d = defaultdict(int)
    ans = 0
    for i in range(n):
        x = i + 1 - a[i]
        ans += d[x]
        d[a[i] + i + 1] += 1
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

ans = 0
cnt = defaultdict(int)
for i in range(n):
	ans += cnt[i - a[i]]
	cnt[i + a[i]] += 1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
a=list(map(int,input().split()))

ld=defaultdict(int)
rd=defaultdict(int)
for i in range(n):
    l=i+a[i]
    r=i-a[i]
    ld[l]+=1
    rd[r]+=1
ans=0
for i,j in ld.items():
    ans+=j*rd[i]

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

arr1 = []
arr2 = []
for i in range(1, n + 1):
    arr1.append(i - a[i - 1])
    arr2.append(i + a[i - 1])
    
dis1 = Counter(arr1)
dis2 = Counter(arr2)

ans = 0
for i in dis1.keys():
    ans += dis1[i] * dis2[i]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(lambda : 0)
    ans = 0
    for i, ai in enumerate(a):
        sa = i - ai
        ans += d[sa]
        wa = i + ai
        d[wa] += 1
    print(ans)
        

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    bigger = [None]*n
    smaller = [None]*n
    for i, ai in enumerate(a):
        bigger[i] = i - ai
        smaller[i] = i + ai
    b = sorted(Counter(bigger).items())
    s = sorted(Counter(smaller).items())
    nb = len(b)
    ns = len(s)
    b.append((10**10, 0))
    s.append((10**11, 0))
    i, j = 0, 0
    ans = 0
    while i < nb or j < ns:
        bi, nbi = b[i]
        si, nsi = s[j]
        if bi == si:
            ans += nbi*nsi
            i += 1
            j += 1
        elif bi > si:
            j += 1
        else:
            i += 1
    print(ans)
        

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
import collections
B=[]
C=[]
for i,a in enumerate(A):
  B.append(a-i)
  C.append(-a-i)
C=collections.Counter(C)
ans=0
for b in B:    
  ans+=C[b]
print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

count_l = collections.Counter(l)
count_r = collections.Counter(r)

ans = 0
for i in count_l:
  ans += count_r.get(i,0) * count_l[i]
      
print(ans)

Submission status: Accepted

Solution code:
import collections

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

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

count = collections.Counter(r)
ans = 0
for i in l:
  ans += count.get(i,0)
      
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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:
import collections

def list_count(l):
    return collections.Counter(l)

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

l=[i+a[i] for i in range(len(a))]
m=[i-a[i] for i in range(len(a))]

mlist=list_count(m)

mlist0=mlist[m[0]]
mlist[m[0]]=mlist0-1

ans=0
for i in range(len(l)-1):
    ansi=mlist.get(l[i], 0)
    ans+=ansi

    mlist[m[i]]=mlist[m[i]]-1

print(ans)

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

dic={}
ans=0
for i in range(n):
    if i-a[i] in dic:
        ans+=dic[i-a[i]]
    if a[i]+i in dic:
        dic[a[i]+i]+=1
    else:
        dic[a[i]+i]=1
print(ans)

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

dic=defaultdict(int)
ans=0
for i in range(n):
    ans+=dic[i-a[i]]
    dic[a[i]+i]+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d = defaultdict(int)
ans = 0
for i in range(n):
    ans += d[i - a[i]]
    d[a[i] + i] += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
A = l()
L = []
L2 = [0]*(10**6)
for i in range(N):
    if i+A[i]+1 < 10**6:
        L.append(i+A[i]+1)
    if i-A[i]+1 >= 0 and i-A[i]+1 < 10**6:
        L2[i-A[i]+1] += 1
ans = 0
for l in L:
    ans += L2[l]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

def CHK():
    N = int(input())
    A = list(map(int,input().split()))
    L=[]
    R=[]
    cnt = 0
    for i in range(N):
        L.append(i + A[i])
    L_Cnt=Counter(L)

    for i in range(N):
        chk = i -A[i]
        cnt += L_Cnt[chk]
    print(cnt)

CHK()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
f={}
f2={}
for i in range(n):
    z=i+a[i]
    z1=i-a[i]
    #print(z,z1)
    try:
        f[z]+=1
    except:
        f[z]=1
    try:
        f2[z1]+=1
    except:
        f2[z1]=1
s=0
for i in f.keys():
    if(i in f2):
        s+=f[i]*f2[i]
print(s)

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)]

Q = Counter(Q)

cnt = 0
for p in P:
    cnt += Q[p]

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
d = defaultdict(int)
for i,x in enumerate(a):
    d[i+1-x] += 1
res = 0
for i,x in enumerate(a):
    res += d[i+1+x]
print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
l,r = {},{}
for i in range(n):
  xl = i+a[i]
  if xl in l:
    l[xl] += 1
  else:
    l[xl] = 1
  
  xr = i-a[i]
  if xr in r:
    r[xr] += 1
  else:
    r[xr] = 1

ans = 0
for x in l:
  if x in r:
    ans += l[x] * r[x]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = []
c = {}

ans = 0
for i in range(n):
    b.append(a[i]+i+1)
    x = -a[i]+i+1
    if x in c:
        c[x] += 1
    else:
        c[x] = 1
for i in range(n):
    if b[i] in c:
        ans += c[b[i]]
print(ans)