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())
l = list(map(int,input().split()))

a=[]
b={}
ans=0

for i in range(n):
    a.append(i+l[i])
    b[i-l[i]] = b.get(i-l[i],0) + 1

for av in a:
    ans = ans + b.get(av,0)

print(ans)

Submission status: Accepted

Solution code:



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


"""
indexのi,jについて、 j - i = A[i] + A[j] になる組の数を数え上げる。
愚直にやると、i,jを全ペアためす。N(O^2)で間に合わない

二つの変数が出てきて、ある式で関係性が表せる場合は、iの式 = jの式 みたいにしてやるとうまくいきことが多い
j - i = A[i] + A[j] 
-> A[i] + i = j - A[j]

なので、各参加者について、iとして使う時とjとして使うときで分けて数えていって、最後に組の数を数え上げる
A[i], i >=1 なので、２以上
1 <= j <= N, A[j] >= 1 なので、N未満
の範囲を調べればよい
"""
from collections import defaultdict
I = defaultdict(int)
J = defaultdict(int)

for i in range(N):
    I[A[i] + i+1] += 1
    J[i+1 - A[i]] += 1

ans = 0
for i in range(2, N):
    ans += I[i] * J[i]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
b = [a[i] + i for i in range(n)]
c = [j - a[j] for j in range(n)]
d = dict()
ans = 0
for i in range(n):
  if d.get(b[i]) is None:
    d.setdefault(b[i],1)
  else:
    d[b[i]] += 1
  if d.get(c[i]) is not None:
    ans += d[c[i]]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
P = {}
Q = {}
X = {}
for i in range(N):
  p = i+1+A[i]
  q = i+1-A[i]
  if str(p) in P:
    P[str(p)] += 1
  else:
    P[str(p)] = 1
  if str(q) in Q:
    Q[str(q)] += 1
  else:
    Q[str(q)] = 1
  X.setdefault(str(p),i+1+A[i])
  X.setdefault(str(q),i+1-A[i])

#J[a]とI[b]が等しいのが条件
#とりうる値 < 10**9を全て試す
ans = 0

for key in X.keys():
  if (key in P) and (key in Q):
  	ans += P[key] * Q[key]

print(ans)

Submission status: Accepted

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

L={}
R={}
ans=0
for i in range(N):
    if i+A[i] in L:
        L[i+A[i]]+=1
    else:
        L[i+A[i]]=1
    if A[i]<=i:
        if i-A[i] in R:
            R[i-A[i]]+=1
        else:
            R[i-A[i]]=1
for r in R:
    if r in L:
        ans+=L[r]*R[r]
print(ans)

Submission status: Accepted

Solution code:
# unihernandez22
# https://atcoder.jp/contests/abc166/tasks/abc166_e
# hashing, math, implementation
n = int(input())
a = list(map(int, input().split()))

x = []
y = {}

for i, elem in enumerate(a):
    x.append(i+a[i])
    y[i-a[i]] = y.get(i-a[i], 0) + 1

ans = 0

for i in x:
    ans += y.get(i, 0)

print(ans)

Submission status: Accepted

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

x = []
y = {}

for i, elem in enumerate(a):
    x.append(i+a[i])
    y[i-a[i]] = y.get(i-a[i], 0) + 1

ans = 0

for i in x:
    ans += y.get(i, 0)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from collections import defaultdict

def main():
  n = int(input())
  la = list(map(int, input().split()))
  cum = defaultdict(int)
  for i, a in enumerate(la, 1):
    cum[i - a] += 1
    
  cnt = 0
  for i, a in enumerate(la, 1):
    cnt += cum[a + i]
  print(cnt)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
J = [0 for i in range(int(2e5+3))]


def main():
    n = int(input())
    a = list(map(lambda x: int(x)-1, input().split()))

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

    cnt = 0
    for i in range(n):
        tmp = a[i]+i+2
        if tmp < len(J)-1:
            cnt += J[tmp]

    print(cnt)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# N人の参加者, 参加者iの身長はAi, 背番号の差の絶対値が身長の和に等しい組
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
memo = defaultdict(int)
ans = 0
for i, x in enumerate(A, 1):
    ans += memo[i-A[i-1]]
    memo[i+A[i-1]] += 1
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)
for i in range(n):
    ans += d[i+1-a[i]]
    d[i+1+a[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc166/submissions/12754173

def abc166_e():
    _ = int(input())
    A = list(map(int, input().split()))
    ans = 0
    from collections import defaultdict
    d = defaultdict(int)
    for i, x in enumerate(A, 1):
        ans += d[i - x]
        d[x + i] += 1
    print(ans)

abc166_e()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
L,R={},{}
for i,a in enumerate(A):
  l,r = i-a,i+a
  if l not in L: L[l]=0
  if r not in R: R[r]=0
  L[l]+=1
  R[r]+=1

count = 0
for x in L:
  if x not in R: continue
  count += L[x]*R[x]
print(count)

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

Submission status: Accepted

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

ans = 0
mp = {}

for i in range(n):
    sa = i - a[i]
    if sa in mp:
        ans += mp[sa]
    wa = i + a[i]
    if wa in mp:
        mp[wa] += 1
    else:
        mp[wa] = 1

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
N = int(input())

A = list(map(int,input().split()))
L = []
R = []
for i in range(N):
  L.append(A[i]+i+1)
  R.append(i+1 - A[i])
L.sort()
R.sort()
R = Counter(R)

cnt = 0
for i in range(N):
  cnt += R[L[i]]

print(cnt)

Submission status: Accepted

Solution code:
import bisect


def main():
    N = int(input())
    A = list(map(int, input().split(' ')))
    pos_scores = [i + 1 + a for i, a in enumerate(A)]
    neg_scores = [i + 1 - a for i, a in enumerate(A)]
    neg_scores.sort()
    cnt = 0
    for ps in pos_scores:
        left = bisect.bisect_left(neg_scores, ps)
        right = bisect.bisect_right(neg_scores, ps)
        cnt += right - left
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

dict1 = {}
dict2 = {}
for i in add1:
    if i in dict1:
        dict1[i] += 1
    else:
        dict1[i] = 1
for i in sub1:
    if i in dict2:
        dict2[i] += 1
    else:
        dict2[i] = 1

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

print(ans)

Submission status: Accepted

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

tmp_i = [a_input[i]-i for i in range(n)]
tmp_j = [-a_input[i]-i for i in range(n)]

count_i = Counter(tmp_i)
count_j = Counter(tmp_j)

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

Submission status: Accepted

Solution code:
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:
import sys
import bisect
from collections import deque
import copy
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    plus_list = []
    minus_list = []
    for i in range(N):
        plus_list.append((i+1) + A[i])
        minus_list.append((i+1) - A[i])
    plus_list.sort()
    minus_list.sort()
    count = 0
    for i in range(N):
        count += bisect.bisect_right(minus_list, plus_list[i]) - bisect.bisect_left(minus_list, plus_list[i])
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=lambda :sys.stdin.readline().rstrip()
from collections import defaultdict

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

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)

Submission status: Accepted

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

J=[a[i-1]+i for i in range(1,n+1)]
I=[i-a[i-1] for i in range(1,n+1)]
dj=Counter(J)
di=Counter(I)
ans=0
for i in dj:
  if i in di:
    ans+=(dj[i]*di[i])
print(ans)

Submission status: Accepted

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

C = {}
s = 0
for i in range(len(A), 0, -1):
    t = i - A[i-1]
    if not(t in C):
        C[t] = 0
    C[t] += 1

    k = i + A[i-1]
    if k in C:
        s += C[k]

print(s)

Submission status: Accepted

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

B = Counter(i + a for i, a in enumerate(A))
C = Counter(j - a for j, a in enumerate(A))
#print(B)
#print(C)
for k, v in B.items():
    cnt += C[k] * v
print(cnt)

Submission status: Accepted

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

l = Counter()
r = Counter()

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

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

print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = [0] + list(map(int, input().split()))
d = defaultdict(int)

ans = 0
for i in range(1, N + 1):
    d[A[i] + i] += 1  # Ai + iを数える
    ans += d[i - A[i]]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
p_map = {}
q_map = {}
for i, A in enumerate(a):
    p = i + A
    q = i - A
    if p not in p_map:
        p_map[p] = 1
    else:
        p_map[p] += 1
    if q not in q_map:
        q_map[q] = 1
    else:
        q_map[q] += 1
ans = 0
for i, v in p_map.items():
    if i in q_map:
       ans += v*q_map[i]
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(N):
    if i+1-A[i] in p:
        ans += p[i+1-A[i]]
    if A[i] + (i+1) in p:
        p[i+1+A[i]] += 1
    else:
        p[i+1+A[i]] = 1
    
#print(p)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: 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 printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

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

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
dic1,dic2,freq1,freq2=[0]*(n+1),[0]*(n+1),{},{}
for i,a in enumerate(a,1):
  dic1[i]=i+a
  dic2[i]=i-a
#print(dic1,dic2)
for i in range(n+1):
  if freq1.get(dic1[i],0)==0:
    freq1[dic1[i]]=1
  else:
    freq1[dic1[i]]+=1
    
for i in range(n+1):
  if freq2.get(dic2[i],0)==0:
    freq2[dic2[i]]=1
  else:
    freq2[dic2[i]]+=1
#print(freq1,freq2)
ans=0
for i in range(1,n+1):
    x=dic1[i]
    if freq2.get(x,0)!=0:
        ans+=freq2[x]
        #print(i,x)
        
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
lst = list(map(int, input().split()))
lst1 = []
lst2 = []
for i in range(n):
  lst1.append(i+lst[i])
  lst2.append(i-lst[i])
lst1 = Counter(lst1)
lst2 = Counter(lst2)
count = lst1.most_common()
ans = 0
for i in range(len(count)):
  a = lst1[count[i][0]]
  b = lst2[count[i][0]]
  ans += a*b
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):
    ima = i-a
    if ima in CNT:
        ans += CNT[ima]

    CNT[i+a] += 1

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===
def main():
    n = ni()
    a = na()

    larger = [i - a[i] for i in range(n)]
    larger_cnt = collections.Counter(larger)

    ans = 0
    for j in range(n):
        smaller = a[j] + j
        larger_cnt[j - a[j]] -= 1
        if smaller in larger_cnt.keys():
            if larger_cnt[smaller] != 0:
                ans += larger_cnt[smaller]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===
def main():
    n = ni()
    a = na()

    larger = [(i + 1) - a[i] for i in range(n)]
    larger_cnt = collections.Counter(larger)

    ans = 0
    for j in range(n):
        smaller = a[j] + (j + 1)
        larger_cnt[(j + 1) - a[j]] -= 1
        if smaller in larger_cnt.keys():
            if larger_cnt[smaller] != 0:
                ans += larger_cnt[smaller]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = {}
r = {}
xc = 0
for x in input().split():
    xi = int(x)
    lx = xc + xi
    if lx in l:
        l[lx] += 1
    else:
        l[lx] = 1
    rx = xc - xi
    if rx in r:
        r[rx] += 1
    else:
        r[rx] = 1
    xc += 1
c = 0
for x in l:
    if x in r:
        c += l[x] * r[x]
print(c)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
i_Ai = {}
cnt = 0

for i, h in enumerate(A):
	imh = i - h
	if imh in i_Ai:
		cnt += i_Ai[imh]
	iph = i + h
	if iph in i_Ai:
		i_Ai[iph] += 1
	else:
		i_Ai[iph] = 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
d_l = {}
d_r = {}
ans = 0
for i, x in enumerate(a):
    l = i + x
    r = i - x
    if i == 0:
        d_l[l] = 1
    elif i == n - 1:
        if r in d_r:
            d_r[r] += 1
        else:
            d_r[r] = 1
    else:
        if l in d_l:
            d_l[l] += 1
        else:
            d_l[l] = 1
        if r in d_r:
            d_r[r] += 1
        else:
            d_r[r] = 1
for i, j in d_l.items():
    if i in d_r.keys():
        ans += j * d_r[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

a_l = defaultdict(int)
a_r = defaultdict(int)

for i in range(n):
    a_l[i+a[i]] += 1
    a_r[i-a[i]] += 1

cnt = 0
for k in set(a_l.keys()) & set(a_r.keys()):
    cnt += a_l[k] * a_r[k]

print(cnt)

Submission status: Accepted

Solution code:
from collections import Counter

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

B = Counter(i - A[i] for i in range(N))
C = Counter(i + A[i] for i in range(N))
ans = 0
for v, num in B.items():
    ans += C[v] * num
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
B = []
C = []
for i, a in enumerate(A):
  B.append(i+1+a)
  C.append(i+1-a)
cb = Counter(B)
cc = Counter(C)
ans = 0
for k, v in cb.items():
  ans += v*cc[k]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
a_list = list(map(int, input().split()))
l_dict = {}
ans = 0
for i in range(0, n):
    a = a_list[i]
    ri = i - a
    if ri in l_dict:
        ans += l_dict[ri]
    li = i + a
    if li in l_dict:
        l_dict[li] += 1
    else:
        l_dict[li] = 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

plusc = defaultdict(list)
for i in range(N):
    plusc[plus[i]].append(i)
ans = 0
for i in range(1,N):
    ans += bisect.bisect_left(plusc[diff[i]],i)
print(ans)

Submission status: Accepted

Solution code:
import sys
l = sys.stdin.readline().strip()
N = int(l)
l = sys.stdin.readline().strip() 
a = list(map(int, l.split()))
count = 0
# j - i = Ai + Aj -> Ai +i = j - Aj
# Li = Ai + i
# Ri = j - Aj
L = []
R = []
for i, one in enumerate(a):
    L.append(one + i+1)
    if (i+1 - one) > 0:
        R.append(i+1-one)
L = sorted(L)
R_count = {}
for one in R:
    if one in R_count:
        R_count[one] += 1
    else:
        R_count[one] = 1
count = 0
for one in L:
    if one in R_count:
        count += R_count[one]

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict

n=int(input())
d=defaultdict(int)

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

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


ans=0

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

Submission status: Accepted

Solution code:
from sys import stdin, stdout
import math
import bisect
import queue
from collections import defaultdict

n = int(stdin.readline().strip())
d1 = defaultdict(int)
d2 = defaultdict(int)
arr = list(map(int,stdin.readline().strip().split()))

cur = 1
ans = 0

for each in arr:
    d1[each + cur] += 1
    d2[cur - each] += 1
    cur += 1

for key, value in d1.items():
    ans += d2[key]*value

stdout.writelines(str(ans) + '\n')

Submission status: Accepted

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

Submission status: Accepted

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

def main():
  N = int(readline())
  line = readline().split()
  R = {}
  L = {}
  for i in range(N):
    r = int(line[i]) + i + 1
    l = i + 1 - int(line[i])
    if r in R:
      R[r] += 1
    else:
      R[r] = 1
    if l in L:
      L[l] += 1
    else:
      L[l] = 1
  cnt = 0
  for i in R:
    if i in L:
      cnt += R[i] * L[i]
  print(cnt)
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
ans = 0
d1 = defaultdict(int)
d2 = defaultdict(int)
for i, j in enumerate(map(int, input().split())):
    d1[i-j]+=1
    d2[i+j]+=1
for i in d1.keys():
    ans += d1[i]*d2[i]
print(ans)

Submission status: Accepted

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

    y=i+1-a[i]
    if y in r:
        r[y]+=1
    else:
        r[y]=1

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

Submission status: Accepted

Solution code:
from math import sqrt
n = int(input())
arr = list(map(int,input().split()))
x = dict()
y = dict()
for i in range(n):
    key = i+arr[i]
    if(x.get(key,0) == 0):
        x[key] = [i]
    else:
        x[key].append(i)
    key = i - arr[i]
    if (y.get(key, 0) == 0):
        y[key] = [i]
    else:
        y[key].append(i)
ans = 0
for key in x:
    if(y.get(key,0)!=0):
        ans += len(y[key])*len(x[key])
print(ans)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
*A, = map(int, input().split())
B = [A[i] + i for i in range(n)]
C = [i - A[i] for i in range(n)]
C.sort()
ans = 0
for b in B:
    ans += bisect.bisect_right(C, b) - bisect.bisect_left(C, b)
print(ans)

Submission status: Accepted

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

L = {}
R = {}
for i in range(N+1):
    L[i] = []  
    R[i] = []

for i in range(N):
    key = i+1+A[i]
    if key<N+1: L[key].append(i+1)
   
for i in range(N):
    key = i+1-A[i]
    if (key<N+1)and(0<=key): R[key].append(i+1)
    

ans=0
for x in range(N+1):
    ans+=len(L[x])*len(R[x])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
 
N = int(input())	# 使わない
 
# j - i = A[i] + A[j] から、
# i + A[i] = j - A[j] となり、iとjが独立するため、同時並行で処理
L = defaultdict(lambda: 0)
R = defaultdict(lambda: 0)
for i, val in enumerate(map(int, input().split())):
  L[i - val] += 1	# i - val <= j - A[j]
  R[i + val] += 1	# i + val <= i + A[i]
  
cnt = 0
for key, val in L.items():
  if key > 0:
    cnt += L[key] * R[key]
    
print(cnt)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n=int(input())
    A=list(map(int,input().split()))
    X,Y=[0]*n,[0]*n
    for i in range(n):
        X[i]=i+A[i]
        Y[i]=i-A[i]
    set_x=set(X)
    yc=Counter(Y)
    xc=Counter(X)
    ans=0
    for x in set_x:
        ans+=xc[x]*yc[x]
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(N, A):
    ret = 0
    ref = {}
    for idx, a in enumerate(A):
        ith_val = idx - a
        ret += ref[ith_val] if ith_val in ref else 0
        ref[idx + a] = ref.get(idx + a, 0) + 1
    return ret

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    print(solve(N, A))

Submission status: Accepted

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

for j in range(N):
  if j-A[j] in D:
    ans += D[j-A[j]]
  if j+A[j] in D:
    D[j+A[j]] += 1
  else:
    D[j+A[j]] = 1
    
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import defaultdict
n = int(input())
A = list(map(int, input().split()))
D = defaultdict(int)
ans = 0
for i, a in enumerate(A):
    ans += D[i-a]
    D[i+a] += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input().strip())
l = [int(x.strip()) for x in input().split()]
d = {}

count = 0
for i in range(N):
    hi = l[i]
    val_i = i - hi
    
    if (val_i in d):
        count += d[val_i]
    
    val_j = i + hi
    if (val_j in d):
        d[val_j] = d[val_j] + 1
    else:
        d[val_j] = 1

print(count)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=0
d=dict()
for i in range(n):
    tar=i+1-l[i]
    if tar in d:
        ans+=d[tar]
    tar1=i+1+l[i]
    if tar1 in d:
        d[tar1]+=1
    else:
        d.update({tar1:1})
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
L = defaultdict(lambda: 0)
R = defaultdict(lambda: 0)
for i, val in enumerate(map(int, input().split())):
  L[i - val] += 1
  R[i + val] += 1
  
cnt = 0
for key, val in L.items():
  if key > 0:
    cnt += L[key] * R[key]
print(cnt)

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input().strip())
h_list = [int(x.strip()) for x in input().split()]
v_dict = {}

count = 0
for i in range(N):
    # i - j == h_list[i+1] + h_list[j+1]
    # -> i - h_list[i+1] == j + h_list[j+1]
    hi = h_list[i]
    val_i = i - hi
    
    if (val_i in v_dict):
        count += v_dict[val_i]
    
    val_j = i + hi
    if (val_j in v_dict):
        v_dict[val_j] = v_dict[val_j] + 1
    else:
        v_dict[val_j] = 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
INF = 2 * 10 ** 5
dp = [0] * INF
ans = 0
for i in range(N):
    i_i_ = i + 1 - A[i]
    if 0 < i_i_ < INF:
        ans += dp[i_i_]
    i_ = i + 1 + A[i]
    if 0 < i_ < INF:
        dp[i_] += 1

print(ans)

Submission status: Accepted

Solution code:
import logging
import collections

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)

def main():
    N = int(input())
    A_i = list(map(int, input().split()))
    
    Ai_plu_i = collections.Counter([i + 1 + A_i[i] for i in range(N)])
    ans = sum([Ai_plu_i.get(j + 1 - A_i[j], 0) for j in range(N)])
    
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# Problem E - This Message Will Self-Destruct in 5s

# input
N = int(input())
a_nums = list(map(int, input().split()))

# initialization
tmp_1 = {}
ans = 0

# count
for i in range(1, N+1):
    if i+a_nums[i-1] in tmp_1:
        tmp_1[i + a_nums[i-1]] += 1
    else:
        tmp_1[i + a_nums[i-1]] = 1

# count 2
for j in range(1, N+1):
    if j-a_nums[j-1] in tmp_1:
        ans += tmp_1[j-a_nums[j-1]]

# output
print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8
N = int(input())
A = list(map(int, input().split()))
pl = {}
mi = {}
for i in range(1, N+1, 1):
    if i+A[i-1] not in pl.keys():
        pl[i+A[i-1]] = 1
    else:
        pl[i+A[i-1]] += 1
    if i-A[i-1] not in mi.keys():
        mi[i-A[i-1]] = 1
    else:
        mi[i-A[i-1]] += 1
ans = 0
for k in pl.keys():
    if k in mi.keys():
        ans += pl[k] * mi[k]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    input()  # n is never used
    dct = defaultdict(int)
    ans = 0
    it = (int(x) for x in input().split())
    for i, height in enumerate(it):
        dct[i + height] += 1
        ans += dct[i - height]
    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split()))
cnt = defaultdict(int)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
import itertools
sys.setrecursionlimit(10**7)

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
a = na()
b = a.copy()
for i in range(n):
    a[i] += i+1
    b[i] -= i+1
ans = 0
b.sort()
for i in range(n):
    ans += bisect.bisect_right(b, -a[i]) - bisect.bisect_left(b, -a[i])
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    N = int(input())
    A = list(map(int, input().split()))
    cnt = defaultdict(int)
    ans = 0
    for i in range(N):
        ans += cnt[i - A[i]]
        cnt[A[i] + i] += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


ans = 0

d1 = {}
d2 = {}
for i in range(N):
    if i+1 + A[i] in d1:
        d1[i+1 + A[i]] += 1
    else:
        d1[i+1 + A[i]] = 1
    if i+1 - A[i] in d2:
        d2[i+1 - A[i]] += 1
    else:
        d2[i+1 - A[i]] = 1


for a in d1.keys():
    if a in d2:
        # print(d2[a])
        ans += d2[a]*d1[a]


# print(d1)
# print(d2)
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())

height = input()
height = height.split()
height = [int(s) for s in height]

count = 0


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