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:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode

INF = float("inf")

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

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

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

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

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

def RS():
    return list(input().split())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input().split()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

n = I()
a = RI()

I = []
for i in range(n):
    I.append( a[i] + (i+1) )

J = []
for j in range(n):
    J.append( (j+1) - a[j] )
J.sort()

c = 0
for i in range(n):
    b = I[i]
    c += mycount(J,b)

print(c)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
import math

# import copy
from bisect import bisect_left, bisect_right
# import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7

def divide(k):
    return pow(k, MOD-2, MOD)


def make_nck(n):
    ret = [1]
    for i in range(1, n+1):
        ret.append((ret[-1] * (n + 1 - i) * divide(i)) % MOD)

    # print(ret)
    return ret

def make_nigojyo(n):
    ret = [1]

    for i in range(1, n+1):
        ret.append((ret[-1] * 25) % MOD)

    # print(ret)
    return ret


def solve():
    n = getN()
    nums = getList()

    agentss = [num - i for i, num in enumerate(nums)]
    agents = [-num - i for i, num in enumerate(nums)]
    agents.sort()
    agentss.sort()
    ans = 0
    for ag in agentss:
        ans += bisect_right(agents, ag) - bisect_left(agents,ag)
        # print(ag, ans)
        # bisect_left()
    print(ans)
    # print(agents)
    # print(agentss)


def main():
    n = getN()
    for _ in range(n):
        solve()


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = []
    c = {}
    answer = 0
    for i in range(n):
        b.append(a[i] + i + 1)
        tmp = -a[i] + i + 1
        if tmp in c:
            c[tmp] += 1
        else:
            c[tmp] = 1
    for i in range(n):
        if b[i] in c:
            answer += c[b[i]]
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ja1 = []
ja2 = []
for i in range(1,n+1):
  ja1.append(i-a[i-1])
  ja2.append(i+a[i-1])

s = Counter(ja1)
t = Counter(ja2)

su = 0
for i in s.keys():
  su += s[i]*t[i]
print(su)

Submission status: Accepted

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

from collections import Counter

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

C1 = Counter(L1)
C2 = Counter(L2)
C = Counter(L1+L2)

ans = 0
for k in C.keys():
    ans += C1[k]*C2[k]
print(ans)

Submission status: Accepted

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

N = INT()
A = LIST()

A1 = [A[i]+i+1 for i in range(N)]
A2 = [i+1-A[i] for i in range(N)]

C1 = Counter(A1)
C2 = Counter(A2)

ans = 0
for x in C1.keys():
	ans += C1[x]*C2[x]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
a = list(map(int, input().split()))
llst = [0] * N
rlst = [0] * N

for i in range(N - 1):
    llst[i] = (i + 1) + a[i]
    rlst[-i - 1] = (N - i) - a[-i - 1]

d = defaultdict(int)
for i in range(1, N):
    d[rlst[i]] += 1

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

print(cnt)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

ctr1 = defaultdict(int)
ctr2 = defaultdict(int)
for i in range(N):
    ctr2[i + A[i]] += 1
    if i - A[i] > 0:
        ctr1[i - A[i]] += 1

ans = 0
for v in ctr1:
    ans += ctr1[v] * ctr2[v]
print(ans)

Submission status: Accepted

Solution code:
# i>j とする.
# |i-j|=Ai+Aj
#  i-j = Ai+Aj

N=int(input())
A=list(map(int,input().split()))
cnt=[0]*300000
ans=0
for i in range(N):
    if i-A[i]>=0:
        ans=ans+cnt[i-A[i]]
    if i+A[i]<len(cnt):
        cnt[i+A[i]]=cnt[i+A[i]]+1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
A1 = []
A2 = []

for i in range(1,N+1):
   A1.append(i + A[i-1])
   A2.append(i - A[i-1])
  
c1 = Counter(A1)
c2 = Counter(A2)

result = 0
for k in set(c1).intersection(c2):
    result += c1[k] * c2[k]
print(result)

Submission status: Accepted

Solution code:
from collections import defaultdict
iim = lambda: map(int, input().rstrip().split())
 
def resolve():
    N = int(input())
    A = list(iim())
    ans = 0
    dp = [0]*N
    for i, ai in enumerate(A):
        x = i + ai
        if x < N:
            dp[x] += 1
        x = i - ai
        if x >= 0:
            ans += dp[x]
    print(ans)
resolve()

Submission status: Accepted

Solution code:
def main():
  ## IMPORT MODULE
  #import sys

  #sys.setrecursionlimit(100000)
  #input=lambda :sys.stdin.readline().rstrip()
  from collections import defaultdict

  #f_inf=float("inf")
  #MOD=10**9+7
  
  if 'get_ipython' in globals(): 
    ## SAMPLE INPUT
    n = 6
    A = [2, 3, 3, 1, 3, 1]

  else:
    ##INPUT 
    n = int(input())
    A = list(map(int, input().split()))

  ## SUBMITION CODES HERE
  d = defaultdict(int)
  ans = 0
  for j in range(n):
    Dif = j - A[j]
    ans += d[Dif]
    Sum = A[j] + j
    d[Sum] += 1
  print(ans)
  
main()

Submission status: Accepted

Solution code:
'''
i<j としたとき求めたいのは
i + Ai = j - Aj の組み合わせの数
そこで各々の Li = i + Ai, Ri = i - Aiを計算し辞書型で保持しておく
'''
from collections import defaultdict

N =int(input())
A = list(map(int,input().split()))
Li = defaultdict(int)
Ri = defaultdict(int)
for i,a in enumerate(A,start=1):
    Li[i+a] += 1
    Ri[i-a] += 1

ans = 0
for k,v in Li.items():
    if k in Ri:
        ans += Li[k]*Ri[k]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

dd = defaultdict(int)

ans = 0
for i in range(n):
    diff = i - A[i]
    ans += dd[diff]
    summ = A[i] + i
    dd[summ] += 1

print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
aL = list(map(int, input().split(" ")))

d1 = {}
d2 = {}
for i in range(n):
    d1i = i - aL[i]
    d2i = i + aL[i]

    if d1i in d1:
        d1[d1i] += 1
    else:
        d1[d1i] = 1

    if d2i in d2:
        d2[d2i] += 1
    else:
        d2[d2i] = 1

ans = 0
for k, v in d2.items():
    if k in d1:
        ans += v * d1[k]

print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

Aminus = [a - i - 1 for i, a in enumerate(A)]

from collections import defaultdict
Aplus = defaultdict(int)
for i, a in enumerate(A):
    Aplus[i + 1 + a] += 1


ans = 0
for i in range(len(A)):
    ans += Aplus[-(Aminus[i])]
    if -(A[i] + i + 1) == A[i] - i - 1:
        ans -= 1
print(ans)

Submission status: Accepted

Solution code:

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

ans = 0
i_plus_h_dic = {}

for i, a in enumerate(al):
    input_val = a+i
    i_plus_h_dic.setdefault(input_val,0)
    i_plus_h_dic[input_val] += 1

    comp_val = i - a
    if comp_val in i_plus_h_dic:
        ans += i_plus_h_dic[comp_val]


print(ans)

Submission status: Accepted

Solution code:
"""
i - j = A[i] + A[j]
> i - A[i] = j + A[j]
i > j
"""
from collections import defaultdict

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

ans = 0
calc = defaultdict(int)
for i in range(1, N+1):
  ans += calc[i - high[i-1]]
  calc[i + high[i-1]] += 1

print(ans)

Submission status: Accepted

Solution code:
"""
i - j = A[i] + A[j]
> i - A[i] = j + A[j]
i > j
"""
N = int(input())
high = list(map(int, input().split()))

ans = 0
calc = {}
for i in range(1, N+1):
  try:
    ans += calc[i - high[i-1]]
  except KeyError:
    pass
  if (i + high[i-1]) in calc.keys():
    calc[i + high[i-1]] += 1
  else:
    calc[i + high[i-1]] = 1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
l = [i + a[i] for i in range(n)]
r = [i - a[i] for i in range(n)]
countl = Counter(l)
countr = Counter(r)
ans = sum([countl[i]*countr[i] for i in countl.keys()])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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



li = [[0]*N,[0]*N ]

for index,a in enumerate(A):
    li[0][index] = a- index
    li[1][index]= -a - index


from collections import Counter


cou_one = Counter(li[0])
cou_two = Counter(li[1])




counter = 0
for key,value in cou_one.items():
    counter += cou_two[key]*value


print(counter)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
 
ans = 0
l = [0]*N
u = [0]*N

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

for i in range(N):
  ans += l[i]*u[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
import decimal


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True

    @staticmethod
    def fact(N):
        res = {}
        tmp = N
        for i in range(2, int(N ** 0.5 + 1) + 1):
            cnt = 0
            while tmp % i == 0:
                cnt += 1
                tmp //= i
            if cnt > 0:
                res[i] = cnt
        if tmp != 1:
            res[tmp] = 1
        if res == {}:
            res[N] = 1
        return res


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N = Scanner.int()
    A = Scanner.map_int()
    memo = defaultdict(int)

    ans = 0
    for i in range(N):
        memo[A[i] + i + 1] += 1
    for i in range(N):
        ans += memo[-A[i] + (i + 1)]
    print(ans)


def main():
    # sys.stdin = open("sample.txt")
    # T = Scanner.int()
    # for _ in range(T):
    #     solve()
    # print('YNeos'[not solve()::2])
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

n=int(input())
arr=list(map(int,input().split()))
ans=0
cnt=collections.defaultdict(int)
for i in range(1,n+1):
	ans+=cnt[i-arr[i-1]] #すでに見た要素について、i-Aiと等しいものの個数を数える
	cnt[i+arr[i-1]]+=1 #i+Aiを付け加える
print(ans)

Submission status: Accepted

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

s = defaultdict(lambda :0)
b = defaultdict(lambda :0)
for i,a in enumerate(A):
    s[i-a] = s[i-a] + 1
    b[i+a] = b[i+a] + 1

c = 0
for k in s:
    if k in b:
        c += b[k]*s[k]

print(c)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
minus_a_ct = Counter([-a-i for i,a in enumerate(a_ls)])
a_ct = Counter([a-i for i,a in enumerate(a_ls)])
_sum = 0
for k, v in a_ct.items():
    _sum += v * minus_a_ct[k]
print(_sum)

Submission status: Accepted

Solution code:
import bisect


def main():
    n = int(input())
    a = [int(v) for v in input().split()]
    count = {}
    for i, v in enumerate(a):
        v = i - v
        if count.get(v) is None:
            count[v] = [i]
        else:
            count[v].append(i)

    ans = 0
    for i, v in enumerate(a):
        t = count.get(v+i)
        if t is None:
            continue
        idx = bisect.bisect_right(t, i)
        ans += len(t) - idx

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

sum_dic = {}

for i in range(N-1):
    s = i + A_array[i]
    if s in sum_dic:
        sum_dic[s] += 1
    else:
        sum_dic[s] = 1
    t = A_array[i+1] - (i+1)
    if -t in sum_dic:
        ans += sum_dic[-t]


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = 0
for ll, hh in zip(l, h):
    ans += ll * hh

print(ans)

Submission status: Accepted

Solution code:
def resolve():
    from collections import defaultdict
    N = int(input())
    A = [int(i) for i in input().split()]
    L = defaultdict(int)
    ans = 0
    for i in range(N):
        L[A[i] + i] += 1
        ans += L[i - A[i]]
    print(ans)


resolve()

Submission status: Accepted

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

l = {}
r = {}

for i, j in enumerate(a):
	if i - j in l:
		l[i-j] += 1
	else:
		l[i-j] = 1
	if i + j in r:
		r[i+j] += 1
	else:
		r[i+j] = 1
ans = 0
for i, j in l.items():
	if i in r:
		ans += r[i] * j

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=map(int,input().split())
pulasu_i=dict()
mainasu_i=dict()
for i,num in enumerate(a):
    x=i+1
    if num+x not in pulasu_i:
        pulasu_i[num+x]=1
    else:
        pulasu_i[num+x]+=1
    if num-x not in mainasu_i:
        mainasu_i[num-x]=1
    else:
        mainasu_i[num-x]+=1
#print(pulasu_i,mainasu_i)
ans=0
for num,cnt in pulasu_i.items():
    if -num not in mainasu_i:
        continue
    else:
        ans+=cnt*mainasu_i[-num]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
def fact(n):
    ans = 1
    for i in range(2, n+1):
        ans*= i
    return ans
def comb(n, c):
    return fact(n)//(fact(n-c)*c)

n = int(input())
nums = list(map(int, input().split()))
ans = 0
l = {}
r = []
for i in range(1,n+1):
    if(i + nums[i-1] not in l):
        l[i+nums[i-1]] =0
    l[i+nums[i-1]]+=1
    if((-1*nums[i-1])+i in l):
        ans+=l[(-1*nums[i-1])+i]
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, a in enumerate(A):
    ans += cnt[i+1 - a]
    cnt[i+1 + a] += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
dic = defaultdict(int)

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

j_arr =[]

ans = 0
for i in range(N):
    dic[i+arr[i]] += 1
    j_arr.append(-1*arr[i]+i)
    ans += dic[-1*arr[i]+i]

print(ans)

Submission status: Accepted

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

dic = {}
j_arr =[]

ans = 0
for i in range(N):
    if i+arr[i] not in dic:
        dic[i+arr[i]] = 0
    dic[i+arr[i]] += 1
    j_arr.append(-1*arr[i]+i)
    if -1*arr[i]+i in dic:
        ans += dic[-1*arr[i]+i]

print(ans)

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,a in enumerate(A):
    ans += memo[i-a]
    memo[i+a] += 1
print(ans)

Submission status: Accepted

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

S = {-A[0] : 1}
ans = 0
for i in range(1,N):
    ans += S.get(A[i]-i, 0)
    S[-A[i]-i] = S.get(-A[i]-i, 0) + 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    plus = [None]*n
    minus = [None]*n
    for i, ai in enumerate(a, 1):
        plus[i-1] = i+ai
        minus[i-1] = i-ai
    plus.sort()
    minus.sort()
    ans = 0
    j = 0
    i = 0
    while i < n:
        pi = plus[i]
        p_num = 1
        m_num = 1
        while j < n-1 and minus[j] < pi:
            j += 1
        mi = minus[j]
        if mi == pi:
            while j < n-1 and mi == minus[j+1]:
                m_num += 1
                j += 1
            while i < n-1 and pi == plus[i+1]:
                p_num += 1
                i += 1
            ans += p_num*m_num
        i += 1
        
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
ans = 0
A = list(map(int, input().split()))
for i in range(N):
    dic[i] = 0

for i in range(N):
    b = A[i]+N-i-1
    if A[i] < N-i:
        a = N-i-A[i]-1
        dic[a] += 1
    if b < N:
        ans += dic[b]
print(ans)

Submission status: Accepted

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


def main():
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    ans = 0
    c = collections.Counter()

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
plus = dict()
minus = dict()
for i in range(n):
  num = i+1
  p = a[i] + num
  m = num - a[i]
  if p in plus:
    plus[p] += 1
  else:
    plus[p] = 1
  if (m) in minus:
    minus[m] += 1
  else:
    minus[m] = 1
c = 0
for v in plus.keys():
  if v in minus:
    c += plus[v] * minus[v]
print(c)

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  # j-i=Ai+Aj
  # Ai+i=j-Aj

  a=[]
  b={}
  for i in range(n):
    x=l[i]
    a.append(x+i)
    y=i-x
    if y in b:
      b[y]+=1
    else:
      b[y]=1

  ans=0
  for x in a:
    if x in b:
      ans+=b[x]

  return ans

# main()
print(main())

Submission status: Accepted

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

# 線形で計算できる
# j > iとしてA[j]+A[i] = j-iよりA[i]+i=j-A[j]
# iとjを分離できたのでjを前に進めていって、計算結果を貯めていけばいい
# 

counter = 0
dic = {}
for j in range(N):
    if j-A[j] in dic:
        counter += dic[j - A[j]]
    if j + A[j] in dic:
        dic[j+A[j]] += 1
    else:
        dic[j+A[j]] = 1
print(counter)

Submission status: Accepted

Solution code:
import collections

n = int(input())
arr = list(map(int,input().split()))
ans = 0
cnt = collections.defaultdict(int)

for i in range(1, n+1):
  ans += cnt[i - arr[i-1]]
  cnt[i + arr[i-1]]+=1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

#cnt = 0
#for i in range(N):
#  for j in range(i, N):
#    if j - i == A[i] + A[j]:
#      cnt += 1

Ap = [A[i]+i for i in range(len(A))]
Am = [i-A[i] for i in range(len(A))]

S = set(Ap) & set(Am)

Cp = Counter(Ap)
Cm = Counter(Am)

cnt = 0
for i in S:
  cnt += Cp[i]*Cm[i]
  
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
tempi=[0]*n
tempj=[0]*n
for i in range(n):
  tempi[i]=a[i]+i+1
for i in range(n):
  tempj[i]=-a[i]+i+1
import collections
tempid=collections.Counter(tempi)
tempjd=collections.Counter(tempj)
for k in tempid.keys():
  a=tempid[k]
  b=tempjd[k]
  ans=ans+a*b
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
l =[]
r =[]
for id,h in enumerate(A,start=1):
    r.append(id -h)
    l.append(id +h)

r_cou =Counter(r)
l_cou =Counter(l)
ans =[]
for i in l_cou.keys():
    ans.append(l_cou[i] *r_cou.get(i,0))

print(sum(ans))

Submission status: Accepted

Solution code:
from collections import *

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
import bisect
def f():
    return []

d=defaultdict(f)
n=int(input())
a=list(map(int,input().split()))
x=[i+1+a[i] for i in range(n)]
y=[i+1-a[i] for i in range(n)]
for i in range(n):
    d[y[i]].append(i)

ans=0
for i in range(n):
    ans+=len(d[x[i]])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
dd = defaultdict(int)
ans = 0
for i, num in enumerate(a, start = 1):
    dd[i + num] += 1
for i, num in enumerate(a, start = 1):
    ans += dd[i - num]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
L = [-1 for _ in range(N)]
R = [-1 for _ in range(N)]

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

# L = -R となる物のうち、i>jになりゃいいんだけど、最後にいくつかで破れば良い気がす
# i > 0 なので、自分自身を２回選んでL＝Rみたいにはならない・・・？

LC = Counter(L)
RC = Counter(R)

ans = 0
for k,v in LC.items():
  if (-1) * k in RC:
    ans += v * RC[(-1)*k]
    #print(k,v,(-1)*k,RC[(-1)*k], ans)
print(ans)

Submission status: Accepted

Solution code:
def solve(n, a):
    S, T = {}, {}
    for i in range(n):
        b = a[i] + i
        if not b in S:
            S[b] = 0
        S[b] += 1

        c = -a[i] + i
        if not c in T:
            T[c] = 0
        T[c] += 1
    ans = 0
    for x, num in S.items():
        if x in T:
            ans += S[x] * T[x]
    return ans

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

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache
from collections import defaultdict
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
x=read()
a=list(reads())
dic=[]
dic2=defaultdict(int)
for i in range(x):
  dic.append(i+a[i])
  dic2[i-a[i]]+=1
ans=0
#print(dic,dic2)
for i in dic:
  ans+=dic2[i]
print(ans)