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 Counter

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

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

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

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

print(ans)

Submission status: Accepted

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

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    # Failed to predict input format
    N = int(input())
    A = list(map(int, input().split()))

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

for i,a in enumerate(A):
    ans+=d[i-a] #jから先
    d[i+a]+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

L = {}
R = {}

for i in range(N):
  t = A[i]+i
  if t in L:
    L[t] += 1
  else:
    L[t] = 1
  s = i - A[i]
  if s in R:
    R[s] += 1
  else:
    R[s] = 1  
#print(L,R)
ans = 0
for i in range(N):
  t = A[i]+i
  #ans += R[t]
  if t in R.keys():
    ans += R[t]
    #print(t,R[t])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

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

print(ans)

Submission status: Accepted

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


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


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


li = []

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

#print(li)

ri = []

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

#print(ri)

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

#print(li_hash)

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

    L = []
    R = []

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

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



if __name__ == '__main__':
    main()

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
An = list(map(int,input().split()))
countN = 0
sum = [0]*N
for i, name in enumerate(An):
    if i + name < N:
        sum[i+name] = sum[i+name] + 1
    if i - name >= 0:
        countN = countN + sum[i-name]
print(countN)

Submission status: Accepted

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

lst = [0] * N

ans = 0
for i in range(N):
    if i + 1 + A[i] < N: #iは0から始まるので +1 してあげる
        lst[i + A[i]] += 1
    if i + 1 - A[i] >= 1:
        ans += lst[i - A[i]]

print(ans)

Submission status: Accepted

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

Aii = [i+A[i] for i in range(N)]
Ajj = [j-A[j] for j in range(N)]
import bisect

Ajj.sort()
ans = 0
for a in Aii:
    right = bisect.bisect_right(Ajj, a)
    left = bisect.bisect_left(Ajj, a)
    ans += right-left
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
A=list(map(int,input().split()))
B = [ i-A[i]    for i in range(n)]
C = [ i+A[i]    for i in range(n)]
b = collections.Counter(B)
c = collections.Counter(C)
ans =0
cnt = 0
 
for i in b.keys():
  ans +=c[i]*b[i]
     
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

N = int(input())
A = list(map(int, input().split()))
d1 = defaultdict(int)
d2 = defaultdict(int)
for i,a in enumerate(A):
    d1[i+a] += 1
    d2[i-a] += 1
ans = 0
for k,v in d1.items():
    ans += v*d2[k]
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

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

d=defaultdict(int)
ans=0
for i in range(n):
    d[l[i]]+=1
    ans+=d[r[i]]
print(ans)

Submission status: Accepted

Solution code:
import collections
n=int(input())
a = list(map(int, input().split()))
an=0
ii=[]
jj=[]
for i in range(n):
    ii.append(i+a[i])
    jj.append(i-a[i])
jjj=collections.Counter(jj)
for i in range(n):
    an+=jjj[ii[i]]
print(an)

Submission status: Accepted

Solution code:
n = int(input())
heights = [int(ch) for ch in input().split()]
ans = 0
dict = {}
for i in range(len(heights)):
    key = i+heights[i]
    if key in dict.keys():
        dict[key] += 1
    else:
        dict[key] = 1
for i in range(len(heights)):
    key = i-heights[i]
    if key in dict.keys():
        ans+=dict[key]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May  3 21:27:48 2020
"""

import sys
import numpy as np
import itertools

sys.setrecursionlimit(10 ** 9)

N = int(input())
A = np.array(list(map(int,input().split())))
B = np.arange(N)
x = B - A
y = B + A

x = x[x>0]
y = y[y<N]
x_count = np.bincount(x, minlength = N)
y_count = np.bincount(y, minlength = N)

ans = np.sum(x_count * y_count)

print(ans)

Submission status: Accepted

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

R=[[] for _ in range(N)]
L=[[] for _ in range(N)]
for i in range(N):
    L[i].append(i+A[i])
    R[i].append(i-A[i])
L.sort()
R.sort()

i,j=0,0
out=0
while i<N and j<N:
    if L[i]>R[j]:
        j+=1
        continue
    elif L[i]<R[j]:
        i+=1
        continue
    if L[i] == R[j]:
        k=0
        while L[i+k]==R[j]:
            k+=1
            if i+k==N:
                break
        l=0
        while L[i]==R[j+l]:
            l+=1
            if j+l==N:
                break
        out+=k*l
        i,j=i+k,j+l


print(out)

Submission status: Accepted

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

B = []
for j in range(n):
    B.append(j - A[j])

C = []
for i in range(n):
    C.append(i + A[i])

d = defaultdict(int)

for j in C:
    d[j] += 1

count = 0
for k in B:
    count += d[k]

print(count)

# 時間超過
# count = 0
# for i in range(n):
#     b = i + A[i]
#     count += B[(i+1):].count(b)
# 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, item in enumerate(a):
    d[i+item]+=1
for i, item in enumerate(a):
    ans+=d[i-item]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

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

d=defaultdict(int)
ans=0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = input().split()
pl = []
sb = []
for i in range(len(a)):
	pl.append(i+int(a[i])+1)
	if i - int(a[i]) > 0:
		sb.append(i-int(a[i])+1)


if len(sb) == 0:
	print('0')
else:

# spl = sorted(pl)
	isb = [int(i) for i in sb]
	ipl = [int(i) for i in pl]
	ssb = sorted(isb)
	t = min(ssb)
	# tl = [0] * max(ipl)
	tl = [0] * 2000000	
	for i in ssb:
		if int(i)-1 < len(tl):
			tl[int(i)-1] += 1
	# print(pl)
	# print(sb)
	# print(tl)
	cnt = 0
	for i in pl:
		if int(i)-1 < len(tl):
			cnt += tl[int(i)-1]

	print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
sin = list(map(int,input().split()))
wa = []
sa = []
for i in range(n):
    wa.append(i+1+sin[i])
from collections import Counter
wa = Counter(wa)
ans = 0
for i in range(n):
    sa = (i+1)-sin[i]
    ans += wa[sa]
print(ans)

Submission status: Accepted

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

b = a[:]
c = a[:]
for i in range(N):
    b[i] += i
    c[i] -= i

cnt1 = Counter(b)
cnt2 = Counter(c)

lst = list(cnt1.keys())
lst.sort()

ans = 0

for i in range(len(lst)):
    ans += cnt1[lst[i]] * cnt2[-lst[i]]

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N = int(input())
values = list(map(int, input().split()))
keys = list(range(1, N + 1))

memo = defaultdict(int)
count = 0
for i, x in enumerate(values):
    count += memo[i - x]
    memo[i + x] += 1
print(count)

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect

class Counter(dict):

    def __init__(self, l=None):
        if l is None:
            return
        for e in l:
            self[e] += 1

    def __missing__(self, key):
        self[key] = 0
        return self[key]

def make_i_list(height_list):
    i_list = [0] * len(height_list) # [0] is dummy
    for i, h in enumerate(height_list):
        i_list[i] = h + i
    i_list.pop(0) # delete dummy
    return i_list

def make_j_list(height_list):
    j_list = [0] * len(height_list) # [0] is dummy
    for j, h in enumerate(height_list):
        j_list[j] = - h + j
    j_list.pop(0) # delete dummy
    return j_list

def make_same_ij_person_dict(i_list, j_list, same_ij_person_dict):
    for key in range(1, len(i_list)): # skip 0 because dummy
        if i_list[key] == j_list[key]:
            value = i_list[key]
            same_ij_person_dict[value] += 1


if __name__ == '__main__':
    num_person = int(input())
    height_list = [0] + list(map(int, input().split())) # [0] is dummy
    i_list = make_i_list(height_list)
    j_list = make_j_list(height_list)
    i_dict = Counter(i_list) # var[value] = count
    j_dict = Counter(j_list) # var[value] = count
    # there is no person that i_list[key] == j_list[key]
    #print('i_list') # debug
    #print(i_list) # debug
    #print(i_dict) # debug
    #print('j_list') # debug
    #print(j_list) # debug
    #print(j_dict) # debug
    ans_count = 0
    for value in i_dict.keys():
        if value in j_dict.keys():
            ans_count += i_dict[value] * j_dict[value]
    print(ans_count)


    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


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

    X = [0]*N
    Y = [0]*N
    for i in range(N):
        X[i] = i + A[i]
        Y[i] = i - A[i]
    Y.sort()

    res = 0
    for x in X:
        res += bisect.bisect_right(Y, x) - bisect.bisect_left(Y, x)
    
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt_dic = {}

for i, a in enumerate(A, start=1):
    t = i + a

    if t in cnt_dic:
        cnt_dic[t] += 1
    else:
        cnt_dic[t] = 1


cnt = 0
for i, a in enumerate(A, start=1):
    if i - a <= 0:
        continue

    if i - a in cnt_dic:
        cnt += cnt_dic[i - a]

print(cnt)

Submission status: Accepted

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    A1 = [i + a for i, a in enumerate(A)]
    A2 = [i - a for i, a in enumerate(A)]
    c1 = Counter(A1)
    c2 = Counter(A2)
    c2_keys = set(c2.keys())
    ans = 0
    for key, val in c1.items():
        if key in c2_keys:
            ans += val * c2[key]

    print(ans)

if __name__ == '__main__':
    main()

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 += v*R[k]
print(ans)

Submission status: Accepted

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    A1 = [i + a for i, a in enumerate(A)]
    A2 = [i - a for i, a in enumerate(A)]
    c1 = Counter(A1)
    c2 = Counter(A2)
    c2_keys = c2.keys()
    ans = 0
    for key, val in c1.items():
        if key in c2_keys:
            ans += val * c2[key]

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = [0] * n
    ans = 0
    for i in range(n):
        ai = a[i]
        l, r = i + ai, i - ai
        if 0 <= l < n:
            d[l] += 1
        if 0 <= r < n:
            ans += d[r]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

    cnt = 0
    l = Counter(l)
    for r in r:
        cnt += l.get(r, 0)

    
    return cnt




print(solve())

Submission status: Accepted

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
dicA={}
dicB={}
for i in range(N):
  a=i+ls[i]
  if a in dicA:
    dicA[a]+=1
  else:
    dicA[a]=1
  b=i-ls[i]
  if b in dicB:
    dicB[b]+=1
  else:
    dicB[b]=1
ans=0
for a,na in dicA.items():
  if a in dicB:
    ans+=na*dicB[a]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

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

ans = 0

c = Counter()

for idx, a in enumerate(A):
    idx += 1
    
    ans += c[idx-a]
    c[idx+a] += 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)]
ans=0
Lc=Counter(L)
Rc=Counter(R)
for key,value in Lc.items():
    ans+=value*Rc.get(key,0)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
il=[i+a[i] for i in range(n)]
jl=[j-a[j] for j in range(n)]
from collections import Counter as c
d=c(jl)
x=0
for i in il:
  x+=d[i]
print(x)

Submission status: Accepted

Solution code:
#import sys
import numpy as np

#input = sys.stdin.readline
MOD = 1000000007


N = int(input())

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

M = {}
res = 0
for i in range(len(A)):
    if (A[i]+i) not in M:
        M[A[i]+i] = 1
    else:
        M[A[i]+i] +=1
    
    if (i - A[i]) in M:
        res+=M[i-A[i]]

print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
a_ls = list(map(int, input().split()))
L_dict = defaultdict(int)
for i in range(n):
    Li = a_ls[i] + i + 1
    L_dict[Li] += 1

ans = 0
for j in range(n):
    Rj = j + 1 - a_ls[j]
    ans += L_dict[Rj]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array([int(i) for i in input().split()])

H = [0]*(5*(10**5))
L = [0]*(5*(10**5))

for i in range(N):
    if A[i] <= 2*(10**5):
        H[A[i]+i] += 1
    if i-A[i] >= 0:
        L[i-A[i]] += 1

ans = 0

for i in range(5*(10**5)):
    ans += H[i]*L[i]

print(ans)

Submission status: Accepted

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

a = list(map(int, input().split()))
ri = defaultdict(lambda: 0)
lj = defaultdict(lambda: 0)

ret = 0
for i in range(n):
	ri[a[i]+i]+=1
	lj[i-a[i]]+=1

for k in ri:
	if(lj[k] >= 1):
		ret+=ri[k]*lj[k]
	
#print(ri)
#print(lj)
print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import*

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

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

c = 0
l = Counter(l)
for r in r:
    c += l.get(r, 0)
print(c)

Submission status: Accepted

Solution code:
# なんか身長順でソートすればいいんじゃない？
# 身長差でありえるのは1〜Nなので，身長差が1になるエージェントの組み合わせをだして，それぞれの身長差について調べる
# でも結局全探索になるか？やっぱり配列を作ると同時に答えが独立してうまく求まるようなのがいいよね
# j-i = Aj+Ai となるのが求めるもの，これを変換してiとjの式のみに分離すると，うまく配列を作るだけでうまくいくようになる
# j-Aj = Ai+i となる．こうすると独立しているので,X=i-Ai,Y=Ai+iを作り，setもしくはdictで探索すればすぐです！
# i==jのときはi-i == Ai+Ai が絶対に成立しないので問題ない

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)]
#Y=set(Y) # set使うと同じ要素が減るのでだめ！！！
dicY=dict()
for y in Y:
    if not y in dicY:
        dicY[y]=1
    else:
        dicY[y]+=1
count=0
for x in X:
    if x in dicY:
        count+=dicY[x]
print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

values = dict()

count = 0
for i, h in enumerate(heights):
    left = i + h
    if left in values:
        values[left].append((i, h))
    else:
        values[left] = [(i, h)]

    r = i - h
    if r in values:
        count += len(values[r])


print(count)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
*A, = map(int, input().split())
counter = Counter()
ans = 0
for i, a in enumerate(A, 1):
    temp = a - i
    ans += counter[temp]
    counter[-a-i] += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

N=int(input())
L,R=[0]*N,[0]*N
for i,a in enumerate(input().split()):
  L[i],R[i]=i+int(a)+1,i+1-int(a)

l=collections.Counter(L)
r=collections.Counter(R)
t=0
for i in l.keys():
  t+=l[i]*r[i]

print(t)

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:
import numpy as np

n = int(input())

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

x = {}
y = {}

ans = 0

for i in range(n):
  x.setdefault((i+a[i]), []).append(i) 
  y.setdefault((i-a[i]), []).append(i)

z = list(set(x.keys()) & set(y.keys()))

# print(x)
# print(y)
# print(z)

for i in z:
  if x[i][len(x[i])-1] < y[i][0]:
    n = len(y[i]) * len(x[i])
    ans += n
    # print(x[i])
    # print(y[i])
    # print(n)
    # print(ans)

  else:
    for j in range(len(x[i])):
      # print(x[i])
      # print(x[i][j])
      # print(y[i])
      ans += sum(1 for k in y[i] if x[i][j] < k)

print(ans)

Submission status: Accepted

Solution code:
def solve():
    n=int(input())
    A=list(map(int,input().split()))
    l=[]
    r=[]
    d={}
    x=[]
    for i in range(n):
        l.append(i+A[i])
        r.append(i-A[i])
        if(i+A[i] not in d):
            d[i+A[i]]=[0,0]
            x.append(i+A[i])
        if(i-A[i] not in d):
            d[i-A[i]]=[0,0]
            x.append(i-A[i])
        d[i+A[i]][0]+=1
        d[i-A[i]][1]+=1
    ans=0
    for i in x:
        ans+=d[i][0]*d[i][1]
    print(ans)
solve()

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
count = 0
l_array = []
r_array = []
for i, X in enumerate(A):
    l_array.append(i + X)
    r_array.append(i - X)

counter = Counter(r_array)

for x in l_array:
    count += counter.get(x, 0)
print(count)

Submission status: Accepted

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

for i,var in enumerate(a,1):
    li.append(i+var)

li=collections.Counter(li)

for j,var in enumerate(a,1):
    ans+=li[j-var]

print(ans)

Submission status: Accepted

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

from collections import defaultdict
d = defaultdict(int)

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
L1 = list()
D2 = dict()
ret = 0
for i in range(len(A)):
    num1 = i + 1 - A[i]
    num2 = i + 1 + A[i]
    L1.append(num1)
    if num2 not in D2:
        D2[num2] = 1
    else:
        D2[num2] += 1
ret = 0
retlist = dict()
for l in L1:
    if l in D2.keys():
        ret += D2[l]
print(ret)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
i_plus_A = Counter([i + A[i] for i in range(N)])
i_minus_A = [i - A[i] for i in range(N)]
i_minus_A = [x for x in i_minus_A if x > 0]
print(sum(i_plus_A[x] for x in i_minus_A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
i_plus_A = Counter([i + A[i] for i in range(N)])
i_minus_A = [i - A[i] for i in range(N)]
print(sum(i_plus_A[x] for x in i_minus_A))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
i_plus_A = Counter([i + A[i] for i in range(N)])
i_minus_A = [i - A[i] for i in range(N)]
ans = 0
for x in i_minus_A:
    ans += i_plus_A[x]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

if __name__ == '__main__':
    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:
import collections
N = int(input())
A = list(map(int, input().split()))

L = []
R = []

ans = 0

for i in range(N):
    L.append(i + A[i])
    R.append(i - A[i])

r = collections.Counter(R)

for l in L:
    ans += r[l]

print(ans)

Submission status: Accepted

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

from collections import defaultdict
l = defaultdict(int)
r = defaultdict(int)

for i in range(n):
    l[i - a[i]] += 1
    r[i + a[i]] += 1
ans = 0
for li in l:
    ans += l[li] * r[li]

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(n):
    a1.append(i+a[i])
    a2.append(i-a[i])
a1,a2 = Counter(a1),Counter(a2)
rng = max(a2)
ans = 0
for i in range(1,rng+1):
    if i in a1 and i in a2:
        ans += a1[i]*a2[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import gcd
import time
import sys
from collections import Counter
import itertools
import math
# read = sys.stdin.buffer.read
# readline = sys.stdin.buffer.readline
# readlines = sys.stdin.buffer.readlines
#input = sys.stdin.readline

start = time.time()
N = int(input())
A = list(map(int, input().split(' ')))
Asum = [i+A[i] for i in range(N)]
Adif = [i-A[i] for i in range(N)]

ans = 0
d = defaultdict(int)
for i in range(N):
    ans += d[Adif[i]]
    d[Asum[i]] += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
"""
A[i] + A[j] = j - i
<=> A[i] + i = j - A[j]
"""
L_i = [A[i] + i for i in range(N)]
L_j = [j - A[j] for j in range(N)]
# L_j[x] < x <= 2 * (10**5)
ans_lis = [0] * (2*(10**5))
for x in L_i:
    if x < 2*(10**5):
        ans_lis[x-1] += 1
for y in L_j:
    if y > 0:
        ans += ans_lis[y-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import gcd
import time
import sys
from collections import Counter
import itertools
import math
# read = sys.stdin.buffer.read
# readline = sys.stdin.buffer.readline
# readlines = sys.stdin.buffer.readlines
input = sys.stdin.readline

start = time.time()
N = int(input())
A = list(map(int, input().split(' ')))
Asum = [i+A[i] for i in range(N)]
Adif = [i-A[i] for i in range(N)]

ans = 0
d = defaultdict(int)
for i in range(N):
    ans += d[Adif[i]]
    d[Asum[i]] += 1

print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

from collections import defaultdict

n = ni()
a = na()

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[0]*200100
c=[0]*200100
for i in range(n):
    if a[i]+i<n:
        c[a[i]+i]+=1
    if i-a[i]>=0:
        b[i-a[i]]+=1
ans=0
for i in range(n):
    ans+=b[i]*c[i]
print(ans)

Submission status: Accepted

Solution code:




def solve(arr,n):
    hashmap=dict()
    for index in range(1,n+1):
        key=index+arr[index-1]
        if hashmap.get(key)!=None:
            hashmap[key]+=1
        else:
            hashmap[key]=1    
    count=0
    for index in range(1,n+1):
        key=index-arr[index-1]
        if hashmap.get(key)!=None:
            count+=hashmap[key]
    return count        






n=int(input())
arr=[int(i) for i in input().split()]
print(solve(arr,n))