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()))
res = 0
dict = {}
dict[a[0]+1] = 1
for i in range(1,n):
  res += dict.get(i+1-a[i],0)
  dict[a[i]+i+1] = dict.get(a[i]+i+1,0) + 1
print(res)

Submission status: Accepted

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


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


def S(): return input()


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


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


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


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


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


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


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


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


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

show_flg = False
# show_flg = True


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

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

    ans = 0

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

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

    print(ans // 2)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from bisect import bisect, bisect_left

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

Submission status: Accepted

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

N = INT()
A = LIST()

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

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

Submission status: Accepted

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

#Ai+i = j-Aj(j > i)

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

Submission status: Accepted

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

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

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

P.sort() 
M.sort()

P_ = []
M_ = []

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

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

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

R = {}

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
X = dict()
ans = 0
for i in range(n):
    R = i + a[i]
    if not R in X:
        X[R]=1
    else: X[R]+=1

for i in range(n):
    L = i - a[i]
    if L in X:
        ans+=X[L]
print(ans)

Submission status: Accepted

Solution code:
# 32:35
n = int(input())

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

values = dict()

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

count = 0
for j, h in enumerate(heights):
    if (j + h) in values:
        count += len(values[j + h])
        # for i, ai in values[j + h]:
        #     if i > 0 and i > j and not (j, i) in count:
        #         count[(j, i)] = True


print(count)

Submission status: Accepted

Solution code:
from collections import Counter
#K = int(input())
#N,K = map(int,input().split())
N = int(input())
A = list(map(int,input().split()))
c=0
l=[]
r=[]
for i in range(N):
    l.append(A[i]+i+1)
    r.append(i+1-A[i])

C=Counter(r)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

c = collections.Counter(ru)
#print(c)

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

print(ans)

Submission status: Accepted

Solution code:
# E - This Message Will Self-Destruct in 5s
from collections import defaultdict

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
from bisect import bisect_left
from collections import defaultdict


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

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

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

    return str(ret)


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

Submission status: Accepted

Solution code:
import sys

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


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

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

    from collections import Counter
    c = Counter(y)

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# E

import collections

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

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

Submission status: Accepted

Solution code:
# 二分探索モジュール bisect
# 添字はiとjの分で相殺(本当の添字は+1が必要だが[(i+1)-(j+1)]なのでいらない(以下は式変形))
import bisect
N = int(input())
A = list(map(int, input().split()))
X = [A[i]+i for i in range(N)]
Y = [j - A[j] for j in range(N)]
ans = 0
Y.sort()
for i in range(N):
    ans += bisect.bisect_right(Y,X[i]) - bisect.bisect_left(Y,X[i])
print(ans)


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

Submission status: Accepted

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

m = {}

count = 0

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

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

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

plus = {}
minus = {}

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

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

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

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#from numpy import arange as ar
#from numpy import count_nonzero as ct 
N=int(input())
A=list(map(int,input().split()))
L= [i+A[i] for i in range(N)]
R= [i-A[i] for i in range(N)]
L_dict={}
R_dict={}
count = 0
for i in range(N):
  if L[i] in L_dict:
    L_dict[L[i]] += 1
  else:
    L_dict[L[i]]  = 1
  if R[i] in R_dict:
    R_dict[R[i]] += 1
  else:
    R_dict[R[i]]  = 1
for i in L_dict.keys():
  if i in R_dict:
    count += L_dict[i] * R_dict[i]
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

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

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

L = defaultdict(int)
R = defaultdict(int)
for i, x in enumerate(A, 1):
    l = i + x
    r = i - x
    L[l] += 1
    R[r] += 1
# print('L', L)
# print('R', R)
ans = 0
for key, l_value in L.items():
    r_value = R[key]
    ans += l_value * r_value
print(ans)
# memo = defaultdict(int)
# answer = 0
# for i, x in enumerate(A, 1):
#     answer += memo[i - x]
#     memo[x + i] += 1

# print('memo', memo)
# print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

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

m1 = dict()
for i, v in enumerate(a):
    t = (i+1) + v
    if t not in m1:
        m1[t] = set()
    m1[t].add(i+1)

m2 = dict()
for i, v in enumerate(a):
    t = (i+1) - v
    if t <= 1:
        continue
    if t not in m1:
        continue
    if t not in m2:
        m2[t] = set()
    m2[t].add(i+1)

ans = 0
for k in m2:
    ans += len(m1[k]) * len(m2[k])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
add_dic = {}
sub_dic = {}

for i in range(n):
    add_tmp = i + a[i]
    sub_tmp = i - a[i]
    if not add_tmp in add_dic.keys():
        add_dic[add_tmp] = 1
    else:
        add_dic[add_tmp] += 1
    if not sub_tmp in sub_dic.keys():
        sub_dic[sub_tmp] = 1
    else:
        sub_dic[sub_tmp] += 1

ans = 0
for k, v in add_dic.items():
    if k in sub_dic.keys():
        ans += v * sub_dic[k]
print(ans)

Submission status: Accepted

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

r1 = [(i+1)+A[i] for i in range(n)]
r2 = [(i+1)-A[i] for i in range(n)]
co1 = collections.Counter(r1)
co2 = collections.Counter(r2)
ans = 0
for num,c1 in co1.items():
    c2 = co2[num]
    ans+=c1*c2
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

n=int(input())
a=list(map(int,input().split()))
ans=0
l=[]
r=[]
for i,j in enumerate(a):
    l.append(i+j)
    r.append(i-j)

#ref = dict(Counter(r))
#ans = 0
#for i in l:
#    ans += ref.get(i, 0)
#print(ans)
b=Counter(r)
for i in l:
    ans+=b.get(i,0)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import collections
def main():
    n = int(input())
    a = list(map(int,input().split()))
    ii = collections.defaultdict(int)
    jj = collections.defaultdict(int)

    for i, aa in enumerate(a):
        ii[i+aa] += 1
        jj[i-aa] += 1

    ans = 0


    ans = 0
    for k in ii:
        ans += ii[k]*jj[k]
    print(ans)
if __name__=='__main__':
    main()

Submission status: Accepted

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

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

ans=0
for j in range(N):
    if str(j+A[j]) in d:
        ans+=d[str(j+A[j])]
print(ans)

Submission status: Accepted

Solution code:
 
N=int(input())
*A,=map(int,input().split())
ref,count={},0

for i,a in enumerate(A):
  if i-a in ref:
     count += ref[i-a]
  if i+a in ref:
     ref[i+a]+=1
  else:
     ref[i+a]=1

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

m = defaultdict(int)
ans = 0
for i in range(N):
    ans += m[i-A[i]]
    m[i+A[i]] += 1

print(ans)

Submission status: Accepted

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

X = dict()
ans = 0
for i in range(N):
    L = i + A[i]
    if L not in X:
        X[L] = 1
    else:
        X[L] += 1

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

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right

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

diff_AB = [(i+1-A[i]) for i in range(N)]
diff_AB.sort()

ans = 0
for i in range(N):
    ans += (bisect_right(diff_AB,A[i]+i+1)- bisect_left(diff_AB,A[i]+i+1))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N= int(input())
A = list(map(int,input().split()))
dict_L = {}
for i in range(N):
    if(i+1+A[i] in dict_L.keys()):
        dict_L[i+1+A[i]] += 1
    else:
        dict_L[i+1+A[i]] = 1
cnt = 0
for j in range(N):
    R = j+1-A[j]
    if(R in dict_L.keys()):
        cnt += dict_L[R]
print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    l1 = []
    for ea in a:
        l1.append(cnt - ea)
        cnt += 1
    l1c = Counter(l1)
    r = 0
    for i2 in range(n):
        r += l1c[i2 + a[i2]]
    print(int(r))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = tuple(map(int, input().split()))
AI = tuple(i + a for i, a in enumerate(A))
AJ = tuple(i - a for i, a in enumerate(A))

cai = {}
caj = {}
vali = set()
valj = set()
for ai, aj in zip(AI, AJ):
    if not ai in cai:
        cai[ai] = 0
    if not aj in caj:
        caj[aj] = 0

    cai[ai] += 1
    vali.add(ai)

    caj[aj] += 1
    valj.add(aj)

count = 0
for val in vali & valj:
    count += cai[val] * caj[val]

print(count)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
x=[]
y=[]
for i in range(n):
    x.append(i+a[i])
    y.append(i-a[i])
x=Counter(x)    
y=Counter(y)
s=set(x.keys()).union(set(y.keys()))

ans=0
for k in s:
    ans += x[k]*y[k]
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

for i in range(N):
    l = i + A[i]
    r = i - A[i]
    if r in L.keys():
        count += L[r]
    if l in L.keys():
        L[l] += 1
    else:
        L[l] = 1
print(count)

Submission status: Accepted

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

X = dict()
ans = 0
for i in range(N):
    L = i + A[i]
    if L not in X:
        X[L] = 1
    else:
        X[L] += 1

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

Submission status: Accepted

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

dic_l={}
dic_r={}
for i in range(n):
    a = a_inputs[i]+(i+1)
    b = (i+1)-a_inputs[i]
    dic_l[a]=dic_l.get(a,0)+1
    dic_r[b]=dic_r.get(b,0)+1

ans=0
for key in dic_l.keys():
    ans+=dic_l[key]*dic_r.get(key,0)
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ap=[int(x) for x in input().split()]
L={}
R={}
for i,a in enumerate(ap):
  if i+a in L:
    L[i+a]+=1
  else:
    L[i+a]=1
  if i-a in R:
    R[i-a]+=1
  else:
    R[i-a]=1
ans=0
for i in range(n):
  if i in L and i in R:
    ans += L[i]*R[i]
print(ans)

Submission status: Accepted

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

L_dict = {}
R_dict = {}

for i, v in enumerate(A_list):
    l = i + v
    r = i - v
    if not l in L_dict.keys():
        L_dict[l] = [i]
    else:
        L_dict[l].append(i)
    
    if not r in R_dict.keys():
        R_dict[r] = [i]
    else:
        R_dict[r].append(i)

res = 0
for r in R_dict.keys():
    if r in L_dict.keys():
        res += len(R_dict[r]) * len(L_dict[r])
print(res)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect, insort

def main():
    n = int(input())
    a = list(map(int, input().split()))
    h_plus_i = [ai+i for i, ai in enumerate(a, 1)]
    h_minus_i = [ai-i for i, ai in enumerate(a, 1)]
    s_h_plus_i = list(sorted(h_plus_i))
    s_h_minus_i = list(sorted(h_minus_i))
    ans = 0
    for hpi, hmi in zip(h_plus_i, h_minus_i):
        i1 = bisect_left(s_h_minus_i, -hpi)
        i2 = bisect_left(s_h_plus_i, -hmi)
        if (i1 < n and s_h_minus_i[i1] == -hpi):
            ans += bisect_left(s_h_minus_i, -hpi+1)-i1
        if (i2 < n and s_h_plus_i[i2] == -hmi):
            ans += bisect_left(s_h_plus_i, -hmi+1)-i2
            
    print(ans//2)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N =int(input())
A = list(map(int,input().split()))
d ={}
r ={}
ans =0
for x in range(N):
    d[x] =0
    r[x] =0
for x in range(N):
    dt = A[x]+x
    rt = x-A[x]
    if dt<N:
        d[dt] +=1
    if rt >-1:
        r[rt] +=1
for x in range(N):
    if d[x] !=0 and r[x]!=0:
        ans = ans + d[x]*r[x]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list((i, j) for i, j in enumerate(map(int, input().split())))
dictL = {}
dictR = {}

ans = 0
for i in range(N):
    l, r = A[i][1] + A[i][0], A[i][0] - A[i][1]
    if l not in dictL:
        dictL[l] = 1
    else:
        dictL[l] += 1
    if r not in dictR:
        dictR[r] = 1
    else:
        dictR[r] += 1

for i in dictL.keys():
    if i in dictR.keys():
        ans += dictL[i] * dictR[i]
        
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
C=[]
for i in range(n):
    C.append([i+A[i],0])
    C.append([i-A[i],1])
C.sort()
now=C[0][0]
c0=0
c1=0
ans=0
for c in C:
    if now==c[0]:
        if c[1]==0:
            c0+=1
        else:
            c1+=1
    else:
        ans+=c0*c1
        c0=0
        c1=0
        if c[1]==0:
            c0+=1
        else:
            c1+=1
        now=c[0]
ans+=c0*c1
print(ans)

Submission status: Accepted

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

L={}
R={}
for i in range(1,len(A)+1):
  L[str(i+A[i-1])] = L.get(str(i+A[i-1]),0)+1
  R[str(i-A[i-1])] = R.get(str(i-A[i-1]),0)+1

ans = 0
for i in range(1,N+1):
  if str(i) in L and str(i) in R:
    ans += L[str(i)] * R[str(i)]

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
from collections import Counter

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

sum = np.arange(n) + a
diff = np.arange(n) - a
sum_c = Counter(sum)
diff_c = Counter(diff)

res = 0
for k, v in sum_c.items():
    res += diff_c[k] * v
print(res)

Submission status: Accepted

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

x_occurance = {}
minus = []
for i in range(N):
	p = A[i] + i
	m = i - A[i]
	x_occurance[p] = x_occurance.get(p, 0) + 1
	minus.append(m)

count = 0
for i in range(N):
	count += x_occurance.get(minus[i], 0) 

print count

Submission status: Accepted

Solution code:

from collections import defaultdict


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





ls = defaultdict(int)
rs = defaultdict(int)
for i, a in enumerate(A):
    ls[a+i+1] += 1
    rs[i+1-a] += 1


ans = 0

for i in range(2, N):
    ans += ls[i] * rs[i] 

print(ans)

Submission status: Accepted

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

c = 0
for i in range(2, n):
    if i in l and i in r:
        c += l[i]*r[i]
print(c)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
L = [i + ai for i, ai in enumerate(a)]
R = [i - ai for i, ai in enumerate(a)]
t = collections.Counter(L)
u = collections.Counter(R)
ans = 0
for i in t.keys():
    ans += t[i]*u[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from pprint import pprint
from collections import deque, defaultdict
import math
import itertools
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.buffer.readline
INF = float("inf")

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

min_diff = 1
max_diff = N - 1

dl = defaultdict(list)
dr = defaultdict(list)
for i in range(1, N + 1):
    key = i + A[i-1]
    dl[key].append(i)
    key = i - A[i-1]
    if key > 0:
        dr[key].append(i)
# pprint(dict(dl))
# pprint(dict(dr))

count = 0
for diff in range(min_diff, max_diff + 1):
    count += len(dl[diff]) * len(dr[diff])
print(count)

Submission status: Accepted

Solution code:
from collections import Counter
inputs = open(0).readlines()
n = int(inputs[0])
*A, = (map(int, inputs[1].split()))
B = Counter([a+i for i, a in enumerate(A, 1)])
C = Counter([i-a for i, a in enumerate(A, 1)])
ans = 0
for k, v in B.items():
    if k in C:
        ans += C[k]*v
print(ans)

Submission status: Accepted

Solution code:
import math
import fractions
import collections
import itertools
import pprint
N=int(input())
A=list(map(int,input().split()))
plus=[]
minus=[]
cnt=0
for i in range(N):
    plus.append(A[i]+(i+1))
    minus.append(A[i]-(i+1))
c1=collections.Counter(plus)
c2=collections.Counter(minus)
l2=[]

values=list(c2.values())	#aのCollectionのvalue値のリスト(n_1こ、n_2こ…)
key=list(c2.keys())	#先のvalue値に相当する要素のリスト(要素1,要素2,…)
for i in range(len(key)):
    l2.append([key[i],values[i]])#lは[要素i,n_i]の情報を詰めたmatrix
#print(l2)
for i in range(len(l2)):
    cnt=cnt+l2[i][1]*c1[-l2[i][0]]
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
lst1 = {}
lst2 = {}
ans = 0
for i in range(len(a)):
  val1 = str(i + a[i])
  val2 = str(i - a[i])
  if val2 in lst1.keys():
    ans += lst1[val2]
  if val1 in lst1.keys():
    lst1[val1] += 1
  else:
    lst1[val1] = 1
print(ans)